Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: validator.isValid is not a function for certain objects #946

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions sign.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const jwt = require("..");

const KEY = "any_key";

describe("issue 945 - validator.isValid is not a function", () => {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As the issue has been detected via property based testing mechanisms, I wonder if at some point having such tests to cover this project could be a thing. It seems that it could be useful in addition to these tests to avoid regressions or even more nasty bugs such as things related to nested objects...

it("should work", () => {
jwt.sign({ hasOwnProperty: null }, KEY);
jwt.sign({ valueOf: null }, KEY);
jwt.sign({ toString: null }, KEY);
jwt.sign({ __proto__: null }, KEY);
});
});