Skip to content

Commit 0c227a1

Browse files
fix(auth): return null from getItemAsync on JSON parse failure (#2336)
Co-authored-by: Katerina Skroumpelou <mandarini@users.noreply.github.com>
1 parent e5ea1ea commit 0c227a1

2 files changed

Lines changed: 53 additions & 1 deletion

File tree

‎packages/core/auth-js/src/lib/helpers.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,12 @@ export const getItemAsync = async (storage: SupportedStorage, key: string): Prom
139139
try {
140140
return JSON.parse(value)
141141
} catch {
142-
return value
142+
// Storage values are always written as JSON via setItemAsync. A non-JSON
143+
// value means the entry is corrupted (e.g. mismatched chunked cookies in
144+
// SSR contexts). Treat as absent so callers do not mutate or re-save the
145+
// garbage, which would otherwise trigger a TypeError downstream and
146+
// leak the raw value into error logs.
147+
return null
143148
}
144149
}
145150

‎packages/core/auth-js/test/helpers.test.ts‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
decodeJWT,
44
generateCallbackId,
55
getAlgorithm,
6+
getItemAsync,
67
parseParametersFromURL,
78
parseResponseAPIVersion,
89
getCodeChallengeAndMethod,
@@ -340,3 +341,49 @@ describe('validateUUID', () => {
340341
}
341342
})
342343
})
344+
345+
describe('getItemAsync', () => {
346+
const makeStorage = (initial: { [key: string]: string | null }) => {
347+
const data: { [key: string]: string | null } = { ...initial }
348+
return {
349+
getItem: jest.fn(async (key: string) => data[key] ?? null),
350+
setItem: jest.fn(async (key: string, value: string) => {
351+
data[key] = value
352+
}),
353+
removeItem: jest.fn(async (key: string) => {
354+
delete data[key]
355+
}),
356+
}
357+
}
358+
359+
it('returns null when the storage value is missing', async () => {
360+
const storage = makeStorage({})
361+
expect(await getItemAsync(storage, 'session')).toBeNull()
362+
})
363+
364+
it('returns null when the storage value is empty string', async () => {
365+
const storage = makeStorage({ session: '' })
366+
expect(await getItemAsync(storage, 'session')).toBeNull()
367+
})
368+
369+
it('returns the parsed object for valid JSON', async () => {
370+
const session = { access_token: 'a', refresh_token: 'b', expires_at: 1 }
371+
const storage = makeStorage({ session: JSON.stringify(session) })
372+
expect(await getItemAsync(storage, 'session')).toEqual(session)
373+
})
374+
375+
it('returns null when the storage value is not valid JSON', async () => {
376+
// Simulates corrupted chunked cookies: combined+decoded payload that is
377+
// not parseable. Returning the raw string would cause _recoverAndRefresh
378+
// to throw `TypeError: Cannot create property 'user' on string ...`.
379+
const storage = makeStorage({ session: '{"access_token":"abc' })
380+
expect(await getItemAsync(storage, 'session')).toBeNull()
381+
})
382+
383+
it('returns null for a JSON-encoded primitive that auth callers do not expect', async () => {
384+
// JSON.parse('"hello"') succeeds and returns the string "hello", which is
385+
// valid behavior. We are only guarding against parse failures here.
386+
const storage = makeStorage({ session: '"hello"' })
387+
expect(await getItemAsync(storage, 'session')).toEqual('hello')
388+
})
389+
})

0 commit comments

Comments
 (0)