Skip to content

Commit

Permalink
fix: validator.isValid is not a function for certain objects
Browse files Browse the repository at this point in the history
Objects with keys like `valueOf`, `toString`, and `__proto__` cause a
`TypeError` to be raised when calling `jwt.sign`. This is because the
key technically does exist on the `schema` param of `validate`, when
checked with `if (schema[key]) {}`. Using
`Object.prototype.hasOwnProperty` solves the issue.
  • Loading branch information
ej-shafran committed Nov 3, 2023
1 parent bc28861 commit fe2805f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
4 changes: 2 additions & 2 deletions sign.js
Expand Up @@ -45,13 +45,13 @@ function validate(schema, allowUnknown, object, parameterName) {
}
Object.keys(object)
.forEach(function(key) {
const validator = schema[key];
if (!validator) {
if (!Object.prototype.hasOwnProperty.call(schema, key)) {
if (!allowUnknown) {
throw new Error('"' + key + '" is not allowed in "' + parameterName + '"');
}
return;
}
const validator = schema[key];
if (!validator.isValid(object[key])) {
throw new Error(validator.message);
}
Expand Down
12 changes: 12 additions & 0 deletions test/issue_945.tests.js
@@ -0,0 +1,12 @@
const jwt = require("..");

const KEY = "any_key";

describe("issue 945 - validator.isValid is not a function", () => {
it("should work", () => {
jwt.sign({ hasOwnProperty: null }, KEY);
jwt.sign({ valueOf: null }, KEY);
jwt.sign({ toString: null }, KEY);
jwt.sign({ __proto__: null }, KEY);
});
});

0 comments on commit fe2805f

Please sign in to comment.