Skip to content

Commit

Permalink
fix: record sessionStorage when authenticating
Browse files Browse the repository at this point in the history
  • Loading branch information
harlan-zw committed May 22, 2024
1 parent 383f246 commit 9fec05f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
11 changes: 11 additions & 0 deletions packages/core/src/puppeteer/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ export async function setupPage(page: Page) {
resolvedConfig.localStorage,
)
}
// set session storage
if (resolvedConfig.sessionStorage) {
await page.evaluateOnNewDocument(
(data) => {
sessionStorage.clear()
for (const key in data)
sessionStorage.setItem(key, data[key])
},
resolvedConfig.sessionStorage,
)
}
if (resolvedConfig.cookies) {
await page.setCookie(...resolvedConfig.cookies.map(cookie => ({ domain: resolvedConfig.site, ...cookie })))
.catch(softErrorHandler('Failed to set cookies'))
Expand Down
6 changes: 6 additions & 0 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,12 @@ export interface ResolvedUserConfig {
* @default {}
*/
localStorage: Record<string, any>
/**
* Session storage to add to the browser context.
*
* @default {}
*/
sessionStorage: Record<string, any>
/**
* Extra headers to provide for any HTTP requests.
*
Expand Down
19 changes: 19 additions & 0 deletions packages/core/src/unlighthouse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ export async function createUnlighthouse(userConfig: UserConfig, provider?: Prov
if (resolvedConfig.hooks?.authenticate) {
// do an authentication step
await worker.cluster.execute({}, async (taskCtx) => {
logger.debug('Running authentication hook')
await taskCtx.page.setBypassCSP(true)

await hooks.callHook('authenticate', taskCtx.page)
// collect page authentication, either cookie or localStorage tokens
const localStorageData = await taskCtx.page.evaluate(() => {
Expand All @@ -155,12 +158,28 @@ export async function createUnlighthouse(userConfig: UserConfig, provider?: Prov
json[key] = localStorage.getItem(key)
}
return json
}).catch((e) => {
logger.warn('Failed to collect authentication localStorage.\n', e)
return {}
})
const sessionStorageData = await taskCtx.page.evaluate(() => {
const json: Record<string, any> = {}
for (let i = 0; i < sessionStorage.length; i++) {
const key = sessionStorage.key(i)
if (key)
json[key] = sessionStorage.getItem(key)
}
return json
}).catch((e) => {
logger.warn('Failed to collect authentication sessionStorage.\n', e)
return {}
})
const cookies = await taskCtx.page.cookies()
// merge this into the config
// @ts-expect-error untyped
ctx.resolvedConfig.cookies = [...(ctx.resolvedConfig.cookies || []), ...cookies as any as ResolvedUserConfig['cookies']]
ctx.resolvedConfig.localStorage = { ...ctx.resolvedConfig.localStorage, ...localStorageData }
ctx.resolvedConfig.sessionStorage = { ...ctx.resolvedConfig.sessionStorage, ...sessionStorageData }
})
}

Expand Down

0 comments on commit 9fec05f

Please sign in to comment.