Skip to content

Commit

Permalink
fix: safeguard TokenSet prototype methods
Browse files Browse the repository at this point in the history
closes #511
  • Loading branch information
panva committed Aug 23, 2022
1 parent 2e02d5b commit 7468674
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/token_set.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ const now = require('./helpers/unix_timestamp');
class TokenSet {
constructor(values) {
Object.assign(this, values);
const { constructor, ...properties } = Object.getOwnPropertyDescriptors(
this.constructor.prototype,
);

Object.defineProperties(this, properties);
}

set expires_in(value) {
Expand Down
26 changes: 26 additions & 0 deletions test/tokenset/tokenset.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,30 @@ describe('TokenSet', function () {

expect(JSON.parse(JSON.stringify(ts))).to.eql(ts);
});

it('cannot have its prototype methods overloaded', function () {
let ts = new TokenSet({
claims: null,
id_token:
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ',
});

expect(ts.claims).to.be.a('function');
expect(ts.claims()).to.eql({ admin: true, name: 'John Doe', sub: '1234567890' });

ts = new TokenSet({ expires_in: 'foo' });
ts.expires_in = 200;
expect(ts.expires_in).to.be.a('number');
expect(ts.expired()).to.eql(false);

const e = new Error();
class CustomTokenSet extends TokenSet {
expired() {
throw e;
}
}

ts = new CustomTokenSet({});
expect(() => ts.expired()).to.throw(e);
});
});

1 comment on commit 7468674

@fredericoo
Copy link

Choose a reason for hiding this comment

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

way better implementation than what I first suggested!

Please sign in to comment.