Merge commit from fork
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Yusuke Wada · committed by GitHub · parent 58d3d3a · Browse files · 2 files·+178 added−6 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+178 added−6 removed
- tests:+166 added−0 removed
- other:+12 added−6 removed
Provenance
- Commit
a5bd9eb- Tree
cfe4d52- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
58d3d3a
2 files changed+178 added−6 removed
modified:src/utils/jwt/jwt.test.ts+166 added
106 unmodified lines107 expect(err).toEqual(new JwtTokenNotBefore(tok))108 expect(authorized).toBeUndefined()109 })110 111+ describe('JwtTokenNotBefore with malformed nbf claim', () => {112+ it('rejects token with nbf as a non-numeric string', async () => {113+ const secret = 'a-secret'114+ const tok = await JWT.sign(115+ // @ts-expect-error - testing malformed payload (nbf must be number)116+ { message: 'hello', nbf: 'tomorrow' },117+ secret,118+ AlgorithmTypes.HS256119+ )120+121+ let err122+ let authorized123+ try {124+ authorized = await JWT.verify(tok, secret, AlgorithmTypes.HS256)125+ } catch (e) {126+ err = e127+ }128+ expect(err).toEqual(new JwtTokenNotBefore(tok))129+ expect(authorized).toBeUndefined()130+ })131+132+ it('rejects token with nbf = Infinity (parsed from 1e400 in JSON)', async () => {133+ // JSON.stringify converts Infinity to null, so hand-craft the payload.134+ const secret = 'a-secret'135+ const encode = (s: string) => encodeBase64Url(utf8Encoder.encode(s).buffer).replace(/=/g, '')136+ const encodedHeader = encode('{"alg":"HS256","typ":"JWT"}')137+ const encodedPayload = encode('{"message":"hello","nbf":1e400}')138+ const signingInput = `${encodedHeader}.${encodedPayload}`139+ const signatureBuffer = await signing(140+ secret,141+ AlgorithmTypes.HS256,142+ utf8Encoder.encode(signingInput)143+ )144+ const tok = `${signingInput}.${encodeBase64Url(signatureBuffer).replace(/=/g, '')}`145+146+ let err147+ let authorized148+ try {149+ authorized = await JWT.verify(tok, secret, AlgorithmTypes.HS256)150+ } catch (e) {151+ err = e152+ }153+ expect(err).toEqual(new JwtTokenNotBefore(tok))154+ expect(authorized).toBeUndefined()155+ })156+ })157+158 it('JwtTokenExpired', async () => {159 const tok =160 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE2MzMwNDYxMDAsImV4cCI6MTYzMzA0NjQwMH0.H-OI1TWAbmK8RonvcpPaQcNvOKS9sxinEOsgKwjoiVo'161 const secret = 'a-secret'29 unmodified lines191 192 vi.useRealTimers()193 })194 195+ describe('JwtTokenExpired with malformed exp claim', () => {196+ it('rejects token with exp = 0 (epoch zero)', async () => {197+ const secret = 'a-secret'198+ const tok = await JWT.sign({ message: 'hello', exp: 0 }, secret, AlgorithmTypes.HS256)199+200+ let err201+ let authorized202+ try {203+ authorized = await JWT.verify(tok, secret, AlgorithmTypes.HS256)204+ } catch (e) {205+ err = e206+ }207+ expect(err).toEqual(new JwtTokenExpired(tok))208+ expect(authorized).toBeUndefined()209+ })210+211+ it('rejects token with exp as a non-numeric string', async () => {212+ const secret = 'a-secret'213+ const tok = await JWT.sign(214+ // @ts-expect-error - testing malformed payload (exp must be number)215+ { message: 'hello', exp: 'tomorrow' },216+ secret,217+ AlgorithmTypes.HS256218+ )219+220+ let err221+ let authorized222+ try {223+ authorized = await JWT.verify(tok, secret, AlgorithmTypes.HS256)224+ } catch (e) {225+ err = e226+ }227+ expect(err).toEqual(new JwtTokenExpired(tok))228+ expect(authorized).toBeUndefined()229+ })230+231+ it('rejects token with exp = Infinity (parsed from 1e400 in JSON)', async () => {232+ // JSON.stringify converts Infinity to null, so hand-craft the payload.233+ const secret = 'a-secret'234+ const encode = (s: string) => encodeBase64Url(utf8Encoder.encode(s).buffer).replace(/=/g, '')235+ const encodedHeader = encode('{"alg":"HS256","typ":"JWT"}')236+ const encodedPayload = encode('{"message":"hello","exp":1e400}')237+ const signingInput = `${encodedHeader}.${encodedPayload}`238+ const signatureBuffer = await signing(239+ secret,240+ AlgorithmTypes.HS256,241+ utf8Encoder.encode(signingInput)242+ )243+ const tok = `${signingInput}.${encodeBase64Url(signatureBuffer).replace(/=/g, '')}`244+245+ let err246+ let authorized247+ try {248+ authorized = await JWT.verify(tok, secret, AlgorithmTypes.HS256)249+ } catch (e) {250+ err = e251+ }252+ expect(err).toEqual(new JwtTokenExpired(tok))253+ expect(authorized).toBeUndefined()254+ })255+ })256+257 it('JwtTokenIssuedAt', async () => {258 const now = 1633046400259 vi.useFakeTimers().setSystemTime(new Date().setTime(now * 1000))260 12 unmodified lines273 expect(err).toEqual(new JwtTokenIssuedAt(now, iat))274 expect(authorized).toBeUndefined()275 })276 277+ describe('JwtTokenIssuedAt with malformed iat claim', () => {278+ it('rejects token with iat as a non-numeric string', async () => {279+ const now = 1633046400280+ vi.useFakeTimers().setSystemTime(new Date(now * 1000))281+282+ const secret = 'a-secret'283+ const tok = await JWT.sign(284+ // @ts-expect-error - testing malformed payload (iat must be number)285+ { message: 'hello', iat: 'tomorrow' },286+ secret,287+ AlgorithmTypes.HS256288+ )289+290+ let err291+ let authorized292+ try {293+ authorized = await JWT.verify(tok, secret, AlgorithmTypes.HS256)294+ } catch (e) {295+ err = e296+ }297+ expect(err).toEqual(new JwtTokenIssuedAt(now, 'tomorrow' as unknown as number))298+ expect(authorized).toBeUndefined()299+300+ vi.useRealTimers()301+ })302+303+ it('rejects token with iat = Infinity (parsed from 1e400 in JSON)', async () => {304+ // JSON.stringify converts Infinity to null, so hand-craft the payload.305+ const now = 1633046400306+ vi.useFakeTimers().setSystemTime(new Date(now * 1000))307+308+ const secret = 'a-secret'309+ const encode = (s: string) => encodeBase64Url(utf8Encoder.encode(s).buffer).replace(/=/g, '')310+ const encodedHeader = encode('{"alg":"HS256","typ":"JWT"}')311+ const encodedPayload = encode('{"message":"hello","iat":1e400}')312+ const signingInput = `${encodedHeader}.${encodedPayload}`313+ const signatureBuffer = await signing(314+ secret,315+ AlgorithmTypes.HS256,316+ utf8Encoder.encode(signingInput)317+ )318+ const tok = `${signingInput}.${encodeBase64Url(signatureBuffer).replace(/=/g, '')}`319+320+ let err321+ let authorized322+ try {323+ authorized = await JWT.verify(tok, secret, AlgorithmTypes.HS256)324+ } catch (e) {325+ err = e326+ }327+ expect(err).toEqual(new JwtTokenIssuedAt(now, Infinity))328+ expect(authorized).toBeUndefined()329+330+ vi.useRealTimers()331+ })332+ })333+334 it('JwtTokenIssuer (none)', async () => {335 const tok =336 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE2MzMwNDY0MDB9.Ha3tPZzmnLGyFfZYd7GSV0iCn2F9kbZffFVZcTe5kJo'337 const secret = 'a-secret'1437 unmodified linesmodified:src/utils/jwt/jwt.ts+12 added-6 removed
126 unmodified lines127 if (header.alg !== alg) {128 throw new JwtAlgorithmMismatch(alg, header.alg)129 }130 const now = Math.floor(Date.now() / 1000)131- if (nbf && payload.nbf && payload.nbf > now) {132- throw new JwtTokenNotBefore(token)131+ if (nbf && payload.nbf !== undefined) {132+ if (typeof payload.nbf !== 'number' || !Number.isFinite(payload.nbf) || payload.nbf > now) {133+ throw new JwtTokenNotBefore(token)134+ }135 }134- if (exp && payload.exp && payload.exp <= now) {135- throw new JwtTokenExpired(token)136+ if (exp && payload.exp !== undefined) {137+ if (typeof payload.exp !== 'number' || !Number.isFinite(payload.exp) || payload.exp <= now) {138+ throw new JwtTokenExpired(token)139+ }140 }137- if (iat && payload.iat && now < payload.iat) {138- throw new JwtTokenIssuedAt(now, payload.iat)141+ if (iat && payload.iat !== undefined) {142+ if (typeof payload.iat !== 'number' || !Number.isFinite(payload.iat) || now < payload.iat) {143+ throw new JwtTokenIssuedAt(now, payload.iat)144+ }145 }146 if (iss) {147 if (!payload.iss) {148 throw new JwtTokenIssuer(iss, null)143 unmodified lines