Skip to content
Closed
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
23 changes: 20 additions & 3 deletions packages/core/auth-js/src/lib/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,10 @@ export class Deferred<T = any> {
}
}

function isJSONObject(value: unknown): value is Record<string, unknown> {
return typeof value === 'object' && value !== null && !Array.isArray(value)
}

export function decodeJWT(token: string): {
header: JwtHeader
payload: JwtPayload
Expand All @@ -203,10 +207,23 @@ export function decodeJWT(token: string): {
throw new AuthInvalidJwtError('JWT not in base64url format')
}
}
const data = {
let header: unknown
let payload: unknown
try {
// using base64url lib
header: JSON.parse(stringFromBase64URL(parts[0])),
payload: JSON.parse(stringFromBase64URL(parts[1])),
header = JSON.parse(stringFromBase64URL(parts[0]))
payload = JSON.parse(stringFromBase64URL(parts[1]))
} catch {
throw new AuthInvalidJwtError('JWT header or payload is not valid JSON')
}

if (!isJSONObject(header) || !isJSONObject(payload)) {
throw new AuthInvalidJwtError('JWT header or payload is not a JSON object')
}

const data = {
header: header as JwtHeader,
payload: payload as JwtPayload,
signature: base64UrlToUint8Array(parts[2]),
raw: {
header: parts[0],
Expand Down
41 changes: 41 additions & 0 deletions packages/core/auth-js/test/GoTrueClient.getClaims.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import GoTrueClient from '../src/GoTrueClient'
import { memoryLocalStorageAdapter } from '../src/lib/local-storage'

const b64 = (value: string) => Buffer.from(value).toString('base64url')

describe('GoTrueClient getClaims with a malformed JWT', () => {
test.each([
[
'a header that is not JSON',
`${b64('not json')}.${b64('{}')}.c2ln`,
'JWT header or payload is not valid JSON',
],
[
'a payload that is not JSON',
`${b64('{"alg":"HS256"}')}.${b64('{bad')}.c2ln`,
'JWT header or payload is not valid JSON',
],
[
'a null payload',
`${b64('{"alg":"HS256"}')}.${b64('null')}.c2ln`,
'JWT header or payload is not a JSON object',
],
])('returns AuthInvalidJwtError instead of throwing for %s', async (_case, token, message) => {
const mockFetch = jest.fn()
const client = new GoTrueClient({
url: 'http://localhost:9999',
storage: memoryLocalStorageAdapter({}),
fetch: mockFetch,
autoRefreshToken: false,
persistSession: false,
})

const { data, error } = await client.getClaims(token)

expect(data).toBeNull()
expect(error?.name).toBe('AuthInvalidJwtError')
expect(error?.code).toBe('invalid_jwt')
expect(error?.message).toBe(message)
expect(mockFetch).not.toHaveBeenCalled()
})
})
26 changes: 26 additions & 0 deletions packages/core/auth-js/test/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,32 @@ describe('decodeJWT', () => {
)
})

it('should reject a header or payload that is not valid JSON', () => {
const b64 = (value: string) => Buffer.from(value).toString('base64url')

expect(() => decodeJWT(`${b64('not json')}.${b64('{}')}.c2ln`)).toThrow(
new AuthInvalidJwtError('JWT header or payload is not valid JSON')
)
expect(() => decodeJWT(`${b64('{"alg":"HS256"}')}.${b64('{bad')}.c2ln`)).toThrow(
new AuthInvalidJwtError('JWT header or payload is not valid JSON')
)
})

it('should reject a header or payload that is not a JSON object', () => {
const b64 = (value: string) => Buffer.from(value).toString('base64url')

for (const token of [
`${b64('null')}.${b64('{}')}.c2ln`,
`${b64('{"alg":"HS256"}')}.${b64('null')}.c2ln`,
`${b64('{"alg":"HS256"}')}.${b64('[]')}.c2ln`,
`${b64('{"alg":"HS256"}')}.${b64('42')}.c2ln`,
]) {
expect(() => decodeJWT(token)).toThrow(
new AuthInvalidJwtError('JWT header or payload is not a JSON object')
)
}
})

it('should decode JWT successfully', () => {
expect(
decodeJWT(
Expand Down