Skip to content

Commit a1bb7ec

Browse files
committed
Inline worker dispatch validation to satisfy CodeQL dynamic-call check
- worker.jsCryptoRunner performs the dynamic lookup, own-property checks (hasOwnProperty for both operationType and algorithm name), and the function-type check all locally, matching the pattern CodeQL's unvalidated-dynamic-method-call query recognizes as safe. Excludes inherited Object.prototype members and non-function values. - Remove now-unused operations.get; operations.exists retained for subtleInterface validation - Rebuild dist bundles
1 parent e7d2262 commit a1bb7ec

4 files changed

Lines changed: 32 additions & 20 deletions

File tree

dist/msrcrypto.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,6 @@ operations.exists = function(operationType, algorithmName) {
6767
typeof op[algorithmName] === "function";
6868
};
6969

70-
operations.get = function(operationType, algorithmName) {
71-
return operations.exists(operationType, algorithmName)
72-
? operations[operationType][algorithmName]
73-
: null;
74-
};
75-
7670
var scriptUrl = (function() {
7771

7872
if (typeof document !== "undefined") {
@@ -864,7 +858,18 @@ var msrcryptoWorker = (function() {
864858
result,
865859
p = e.data;
866860

867-
var func = operations.get(operation, algorithmName);
861+
if (!Object.prototype.hasOwnProperty.call(operations, operation)) {
862+
throw new Error("unregistered algorithm.");
863+
}
864+
865+
var algorithmMap = operations[operation];
866+
867+
if (typeof algorithmMap !== "object" || algorithmMap === null ||
868+
!Object.prototype.hasOwnProperty.call(algorithmMap, algorithmName)) {
869+
throw new Error("unregistered algorithm.");
870+
}
871+
872+
var func = algorithmMap[algorithmName];
868873

869874
if (typeof func !== "function") {
870875
throw new Error("unregistered algorithm.");

dist/msrcrypto.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/operations.js

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,3 @@ operations.exists = function(operationType, algorithmName) {
4444
return Object.prototype.hasOwnProperty.call(op, algorithmName) &&
4545
typeof op[algorithmName] === "function";
4646
};
47-
48-
// Safe accessor: returns the registered function or null.
49-
// Never resolves to an inherited Object.prototype member.
50-
operations.get = function(operationType, algorithmName) {
51-
return operations.exists(operationType, algorithmName)
52-
? operations[operationType][algorithmName]
53-
: null;
54-
};

src/worker.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,26 @@ var msrcryptoWorker = (function() {
4646
result,
4747
p = e.data;
4848

49-
var func = operations.get(operation, algorithmName);
49+
// Resolve the registered handler with the dynamic lookup, the
50+
// own-property checks, and the function-type check all performed
51+
// locally. This matches the pattern CodeQL's "unvalidated dynamic
52+
// method call" query recognizes as safe, and ensures a
53+
// user-controlled operation/algorithm name can never dispatch to an
54+
// inherited Object.prototype member (valueOf, hasOwnProperty, ...)
55+
// or to a non-function value.
56+
if (!Object.prototype.hasOwnProperty.call(operations, operation)) {
57+
throw new Error("unregistered algorithm.");
58+
}
59+
60+
var algorithmMap = operations[operation];
61+
62+
if (typeof algorithmMap !== "object" || algorithmMap === null ||
63+
!Object.prototype.hasOwnProperty.call(algorithmMap, algorithmName)) {
64+
throw new Error("unregistered algorithm.");
65+
}
66+
67+
var func = algorithmMap[algorithmName];
5068

51-
// Explicit own-property + function-type validation at the call site
52-
// so the dynamically resolved handler can never dispatch to an
53-
// inherited Object.prototype member or a non-function value.
5469
if (typeof func !== "function") {
5570
throw new Error("unregistered algorithm.");
5671
}

0 commit comments

Comments
 (0)