Skip to content

Commit 20cd2d2

Browse files
fix(auth): malformed SSO cache didn't prompt reauth
Problem: When we loaded sso cache from disk, we would only invalidate (leading to a reauth prompt) if the cache file was missing. But if the cache file was present, though its content was malformed, we would incorrectly treat it as recoverable by throwing instead of returning undefined. Solution: If we detect a SyntaxError treat it as non-recoverable, meaning it will trigger a reauth. Also added some code to validate the content of the SSO cache we loaded from disk to ensure it is what we expected. Signed-off-by: nkomonen-amazon <[email protected]>
1 parent 83aac85 commit 20cd2d2

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

packages/core/src/auth/sso/cache.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ import { getLogger } from '../../shared/logger/logger'
1010
import fs from '../../shared/fs/fs'
1111
import { createDiskCache, KeyedCache, mapCache } from '../../shared/utilities/cacheUtils'
1212
import { stripUndefined } from '../../shared/utilities/collectionUtils'
13-
import { hasProps, selectFrom } from '../../shared/utilities/tsUtils'
13+
import { getMissingProps, hasProps, selectFrom } from '../../shared/utilities/tsUtils'
1414
import { SsoToken, ClientRegistration } from './model'
1515
import { DevSettings } from '../../shared/settings'
1616
import { onceChanged } from '../../shared/utilities/functionUtils'
1717
import globals from '../../shared/extensionGlobals'
18+
import { ToolkitError } from '../../shared'
1819

1920
interface RegistrationKey {
2021
readonly startUrl: string
@@ -92,6 +93,12 @@ export function getTokenCache(directory = getCacheDir()): KeyedCache<SsoAccess>
9293

9394
stripUndefined(token)
9495

96+
// Validate data is not missing.
97+
const missingProps = getMissingProps(token, 'accessToken', 'refreshToken')
98+
if (missingProps.length > 0) {
99+
throw new ToolkitError(`SSO cache data unexpectedly missing props: ${JSON.stringify(missingProps)}`)
100+
}
101+
95102
return {
96103
token,
97104
registration,

packages/core/src/shared/utilities/cacheUtils.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,23 @@ export function createDiskCache<V, K>(
116116
log('loaded', key)
117117
return result
118118
} catch (error) {
119+
// Non-recoverable errors mean there is no usable data.
120+
// Recoverable errors mean we can possibly use the data for something like
121+
// an SSO token refresh, or to just retry.
122+
// Returning undefined implies non-recoverable.
123+
124+
// -- Non-recoverable Errors --
119125
if (isFileNotFoundError(error)) {
120126
log('read failed (file not found)', key)
121127
return
122128
}
129+
if (error instanceof SyntaxError) {
130+
// file content was malformed or empty
131+
log(`read failed (invalid JSON)`, key)
132+
return
133+
}
134+
135+
// -- Recoverable Errors --
123136
log(`read failed ${error}`, key)
124137
throw createDiskCacheError(error, 'LOAD', target, key)
125138
}

0 commit comments

Comments
 (0)