Skip to content

Commit c9fa3db

Browse files
committed
feat(core): add validation for combo security schemes #1416
This commit introduces validation for combo security schemes to ensure that a combo scheme contains either 'allOf' or 'oneOf', but not both. - Throws an error if a combo scheme is invalid. - Adds unit tests to verify the new validation logic.
1 parent 0bc5c88 commit c9fa3db

2 files changed

Lines changed: 54 additions & 7 deletions

File tree

packages/core/src/consumed-thing.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -451,23 +451,25 @@ export default class ConsumedThing extends Thing implements IConsumedThing {
451451

452452
const visitSchemes = (security: Array<string>) => {
453453
const resolveComboScheme = (
454-
combo: ComboSecurityScheme
454+
combo: ComboSecurityScheme,
455+
name: string
455456
): AllOfSecurityScheme | OneOfSecurityScheme | undefined => {
456-
if (combo.allOf instanceof Array) {
457+
if (combo.allOf instanceof Array && combo.oneOf === undefined) {
457458
const allOf = visitSchemes(combo.allOf as string[]);
458459
return <AllOfSecurityScheme>{
459460
scheme: "combo",
460461
allOf,
461462
};
462-
}
463-
if (combo.oneOf instanceof Array) {
463+
} else if (combo.oneOf instanceof Array && combo.allOf === undefined) {
464464
const oneOf = visitSchemes(combo.oneOf as string[]);
465465
return <OneOfSecurityScheme>{
466466
scheme: "combo",
467467
oneOf,
468468
};
469+
} else {
470+
// invalid combination that should be spotted by the TD schema verificator
471+
throw new Error(`Combo SecurityScheme '${name}' is invalid`);
469472
}
470-
return undefined; // not supported , but handled gracefully
471473
};
472474
const scs: SecurityScheme[] = [];
473475
for (const s of security) {
@@ -480,7 +482,7 @@ export default class ConsumedThing extends Thing implements IConsumedThing {
480482
let ws: SecurityScheme | undefined = this.securityDefinitions[s];
481483
// also push nosec in case of proxy
482484
if (ws?.scheme === "combo") {
483-
ws = resolveComboScheme(ws as ComboSecurityScheme);
485+
ws = resolveComboScheme(ws as ComboSecurityScheme, s);
484486
}
485487
if (ws != null) {
486488
scs.push(ws);

packages/core/test/ClientTest.ts

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
*/
2222

2323
import { suite, test } from "@testdeck/mocha";
24-
import { expect, should, use as chaiUse } from "chai";
24+
import { expect, should, use as chaiUse, assert } from "chai";
2525

2626
import { Subscription } from "rxjs/Subscription";
2727

@@ -1000,4 +1000,49 @@ class WoTClientTest {
10001000
const a2 = second.allOf[0];
10011001
expect(a1).equals(a2);
10021002
}
1003+
1004+
@test "invalid combo with allOf AND onOf should be detected and throw"() {
1005+
const ct = new ConsumedThing(WoTClientTest.servient);
1006+
ct.securityDefinitions = {
1007+
// a badly designed combo has allOf and oneOf
1008+
a: {
1009+
scheme: "a",
1010+
},
1011+
b: {
1012+
scheme: "b",
1013+
},
1014+
combo_oneOf_and_allof: {
1015+
scheme: "combo",
1016+
allOf: ["a", "b"],
1017+
oneOf: ["a", "b"],
1018+
},
1019+
};
1020+
ct.security = ["combo_oneOf_and_allof"];
1021+
const pc = new TestProtocolClient();
1022+
const form: Form = {
1023+
href: "https://example.com/",
1024+
};
1025+
assert.throws(() => {
1026+
ct.ensureClientSecurity(pc, form);
1027+
}, /Combo SecurityScheme 'combo_oneOf_and_allof' is invalid/);
1028+
}
1029+
1030+
@test "invalid combo with missing allOf and oneOf should be detected and throw"() {
1031+
const ct = new ConsumedThing(WoTClientTest.servient);
1032+
ct.securityDefinitions = {
1033+
// a badly designed combo has NO allOf and NO oneOf
1034+
1035+
combo_without_oneOf_and_without_allof: {
1036+
scheme: "combo",
1037+
},
1038+
};
1039+
ct.security = ["combo_without_oneOf_and_without_allof"];
1040+
const pc = new TestProtocolClient();
1041+
const form: Form = {
1042+
href: "https://example.com/",
1043+
};
1044+
assert.throws(() => {
1045+
ct.ensureClientSecurity(pc, form);
1046+
}, /Combo SecurityScheme 'combo_without_oneOf_and_without_allof' is invalid/);
1047+
}
10031048
}

0 commit comments

Comments
 (0)