Skip to content

Commit 26395fd

Browse files
CelineSebexfournet
andauthored
[backend] fix conversion error during local strategy conversion(#14756)
Co-authored-by: Xavier Fournet <461943+xfournet@users.noreply.github.com>
1 parent dd15daf commit 26395fd

File tree

2 files changed

+73
-10
lines changed

2 files changed

+73
-10
lines changed

opencti-platform/opencti-graphql/src/modules/authenticationProvider/authenticationProvider-migration-converter.ts

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -104,17 +104,26 @@ export class ConfigExtractor {
104104
/** Read a key and mark it as consumed. Also checks the camelCase/snake_case alias. */
105105
get<T = any>(key: string, defaultValue?: T): T {
106106
this.consumed.add(key);
107+
let value: any;
107108
if (key in this.config) {
108-
return this.config[key] as T;
109-
}
110-
const alias = SNAKE_TO_CAMEL[key] ?? CAMEL_TO_SNAKE[key];
111-
if (alias) {
112-
this.consumed.add(alias);
113-
if (alias in this.config) {
114-
return this.config[alias] as T;
109+
value = this.config[key];
110+
} else {
111+
const alias = SNAKE_TO_CAMEL[key] ?? CAMEL_TO_SNAKE[key];
112+
if (alias) {
113+
this.consumed.add(alias);
114+
if (alias in this.config) {
115+
value = this.config[alias];
116+
}
115117
}
116118
}
117-
return defaultValue as T;
119+
if (value === undefined) {
120+
return defaultValue as T;
121+
}
122+
// Auto-convert string → boolean when the expected type is boolean
123+
if (typeof defaultValue === 'boolean' && typeof value === 'string') {
124+
return (value.toLowerCase() === 'true') as unknown as T;
125+
}
126+
return value as T;
118127
}
119128

120129
/** Check if a key exists (also checks alias). */
@@ -636,7 +645,7 @@ export const convertLdapEnvConfig = (envKey: string, entry: EnvProviderEntry): C
636645
const searchFilter = ext.get<string>('search_filter', '(uid={{username}})');
637646
const groupSearchBase = ext.get<string>('group_search_base', '');
638647
const groupSearchFilter = ext.get<string>('group_search_filter', '');
639-
const allowSelfSigned = ext.get<any>('allow_self_signed', false);
648+
const allowSelfSigned = ext.get<boolean>('allow_self_signed', false);
640649

641650
// Promoted fields (previously in extra_conf, now first-class)
642651
const searchAttributes = ext.get<string[] | null>('search_attributes', null);
@@ -689,7 +698,7 @@ export const convertLdapEnvConfig = (envKey: string, entry: EnvProviderEntry): C
689698
search_filter: searchFilter,
690699
group_base: groupSearchBase,
691700
group_filter: groupSearchFilter,
692-
allow_self_signed: allowSelfSigned === true || allowSelfSigned === 'true',
701+
allow_self_signed: allowSelfSigned,
693702
search_attributes: searchAttributes,
694703
username_field: usernameField,
695704
password_field: passwordField,

opencti-platform/opencti-graphql/tests/01-unit/modules/authenticationProvider/authenticationProvider-converter-test.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,60 @@ describe('ConfigExtractor', () => {
6969
const ext = new ConfigExtractor({ a: 1, b: 2 });
7070
expect(ext.getUnconsumedEntries()).toStrictEqual([['a', 1], ['b', 2]]);
7171
});
72+
73+
// --- String → boolean auto-conversion ---
74+
75+
it('should convert string "true" to boolean true when defaultValue is boolean', () => {
76+
const ext = new ConfigExtractor({ flag: 'true' });
77+
expect(ext.get<boolean>('flag', false)).toBe(true);
78+
});
79+
80+
it('should convert string "True" to boolean true (case-insensitive)', () => {
81+
const ext = new ConfigExtractor({ flag: 'True' });
82+
expect(ext.get<boolean>('flag', false)).toBe(true);
83+
});
84+
85+
it('should convert string "TRUE" to boolean true (case-insensitive)', () => {
86+
const ext = new ConfigExtractor({ flag: 'TRUE' });
87+
expect(ext.get<boolean>('flag', false)).toBe(true);
88+
});
89+
90+
it('should convert string "false" to boolean false when defaultValue is boolean', () => {
91+
const ext = new ConfigExtractor({ flag: 'false' });
92+
expect(ext.get<boolean>('flag', true)).toBe(false);
93+
});
94+
95+
it('should convert string "False" to boolean false (case-insensitive)', () => {
96+
const ext = new ConfigExtractor({ flag: 'False' });
97+
expect(ext.get<boolean>('flag', true)).toBe(false);
98+
});
99+
100+
it('should convert string "FALSE" to boolean false (case-insensitive)', () => {
101+
const ext = new ConfigExtractor({ flag: 'FALSE' });
102+
expect(ext.get<boolean>('flag', true)).toBe(false);
103+
});
104+
105+
it('should convert any non-"true" string to false when defaultValue is boolean', () => {
106+
const ext = new ConfigExtractor({ flag: 'yes' });
107+
expect(ext.get<boolean>('flag', true)).toBe(false);
108+
});
109+
110+
it('should NOT convert string when defaultValue is not boolean', () => {
111+
const ext = new ConfigExtractor({ val: 'true' });
112+
expect(ext.get<string>('val', 'fallback')).toBe('true');
113+
});
114+
115+
it('should NOT convert string when no defaultValue is provided', () => {
116+
const ext = new ConfigExtractor({ val: 'true' });
117+
expect(ext.get('val')).toBe('true');
118+
});
119+
120+
it('should return boolean value as-is when value is already boolean', () => {
121+
const ext = new ConfigExtractor({ flag: true });
122+
expect(ext.get<boolean>('flag', false)).toBe(true);
123+
const ext2 = new ConfigExtractor({ flag: false });
124+
expect(ext2.get<boolean>('flag', true)).toBe(false);
125+
});
72126
});
73127

74128
// ==========================================================================

0 commit comments

Comments
 (0)