Skip to content

Commit

Permalink
fix: isObject helper in different vm contexts or jest re-assigned glo…
Browse files Browse the repository at this point in the history
…bals

closes #178
  • Loading branch information
panva committed Apr 13, 2021
1 parent 1477592 commit 7819df7
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 13 deletions.
5 changes: 3 additions & 2 deletions src/jwks/remote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
JWKSMultipleMatchingKeys,
} from '../util/errors.js'
import fetchJson from '../runtime/fetch.js'
import isObject from '../lib/is_object.js'

function getKtyFromAlg(alg: string) {
switch (alg.substr(0, 2)) {
Expand Down Expand Up @@ -55,8 +56,8 @@ export interface RemoteJWKSetOptions {
agent?: https.Agent | http.Agent
}

function isJWKLike(key: object): key is JWK {
return key && typeof key === 'object'
function isJWKLike(key: unknown): key is JWK {
return isObject(key)
}

class RemoteJWKSet {
Expand Down
16 changes: 15 additions & 1 deletion src/lib/is_object.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
function isObjectLike(value: unknown) {
return typeof value === 'object' && value !== null
}

export default function isObject(input: unknown): boolean {
return typeof input === 'object' && !!input && input.constructor === Object
if (!isObjectLike(input) || Object.prototype.toString.call(input) !== '[object Object]') {
return false
}
if (Object.getPrototypeOf(input) === null) {
return true
}
let proto = input
while (Object.getPrototypeOf(proto) !== null) {
proto = Object.getPrototypeOf(proto)
}
return Object.getPrototypeOf(input) === proto
}
3 changes: 2 additions & 1 deletion src/lib/jwt_claims_set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { JWTClaimValidationFailed, JWTExpired, JWTInvalid } from '../util/errors
import { decoder } from './buffer_utils.js'
import epoch from './epoch.js'
import secs from './secs.js'
import isObject from './is_object.js'

const normalizeTyp = (value: string) => value.toLowerCase().replace(/^application\//, '')

Expand Down Expand Up @@ -46,7 +47,7 @@ export default (
//
}

if (typeof payload !== 'object' || !payload || Array.isArray(payload)) {
if (!isObject(payload)) {
throw new JWTInvalid('JWT Claims Set must be a top-level JSON object')
}

Expand Down
12 changes: 7 additions & 5 deletions test/jwk/jwk2key.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@ Promise.all([import(`${keyRoot}/jwk/parse`), import(`${keyRoot}/jwk/from_key_lik
instanceOf: TypeError,
message: 'JWK must be an object',
});
await t.throwsAsync(parseJwk(Object.create(null)), {
instanceOf: TypeError,
message: 'JWK must be an object',
});
const nullPrototype = Object.create(null);
nullPrototype.crv = 'P-256';
nullPrototype.kty = 'EC';
nullPrototype.x = 'q3zAwR_kUwtdLEwtB2oVfucXiLHmEhu9bJUFYjJxYGs';
nullPrototype.y = '8h0D-ONoU-iZqrq28TyUxEULxuGwJZGMJYTMbeMshvI';
await t.notThrowsAsync(parseJwk(nullPrototype, 'ES256'));
});

test('JWK kty must be recognized', async (t) => {
Expand Down Expand Up @@ -150,7 +152,7 @@ Promise.all([import(`${keyRoot}/jwk/parse`), import(`${keyRoot}/jwk/from_key_lik
await t.notThrowsAsync(async () => {
let key = await parseJwk({ ...jwk, ext: true });
t.is(key.type, 'private');
const exportedJwk = await fromKeyLike(key)
const exportedJwk = await fromKeyLike(key);
key = await parseJwk(exportedJwk, jwk.alg);
t.is(key.type, 'private');
});
Expand Down
10 changes: 6 additions & 4 deletions test/jwk/thumbprint.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,12 @@ import(`${keyRoot}/jwk/thumbprint`).then(
instanceOf: TypeError,
message: 'JWK must be an object',
});
await t.throwsAsync(thumbprint(Object.create(null)), {
instanceOf: TypeError,
message: 'JWK must be an object',
});
const nullPrototype = Object.create(null);
nullPrototype.crv = 'P-256';
nullPrototype.kty = 'EC';
nullPrototype.x = 'q3zAwR_kUwtdLEwtB2oVfucXiLHmEhu9bJUFYjJxYGs';
nullPrototype.y = '8h0D-ONoU-iZqrq28TyUxEULxuGwJZGMJYTMbeMshvI';
await t.notThrowsAsync(thumbprint(nullPrototype));
});

test('JWK kty must be recognized', async (t) => {
Expand Down

0 comments on commit 7819df7

Please sign in to comment.