Skip to content

Commit 6f2cb0d

Browse files
committed
fix(client): avoid exposing sensitive config when using --build-static
Fixes #119
1 parent 578acac commit 6f2cb0d

File tree

3 files changed

+39
-7
lines changed

3 files changed

+39
-7
lines changed

packages/client/types.d.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { ResolvedUserConfig, RuntimeSettings } from '@unlighthouse/core'
1+
import type { ClientOptionsPayload, ScanMeta } from '@unlighthouse/core'
22
import type { UnlighthouseRouteReport } from '@unlighthouse/core'
33

44
declare global {
@@ -10,6 +10,6 @@ declare global {
1010
/**
1111
* Data provided for offline / demo mode.
1212
*/
13-
__unlighthouse_payload: { options: ResolvedUserConfig & RuntimeSettings; scanMeta: ScanMeta; reports: UnlighthouseRouteReport[] }
13+
__unlighthouse_payload: { options: ClientOptionsPayload; scanMeta: ScanMeta; reports: UnlighthouseRouteReport[] }
1414
}
1515
}

packages/core/src/build.ts

+34-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
import { dirname, join, resolve } from 'node:path'
22
import fs from 'fs-extra'
33
import { withLeadingSlash, withTrailingSlash } from 'ufo'
4+
import { pick } from 'lodash-es'
45
import { useLogger, useUnlighthouse } from './unlighthouse'
5-
import type { GenerateClientOptions, ResolvedUserConfig, RuntimeSettings, ScanMeta, UnlighthouseContext, UnlighthouseRouteReport } from './types'
6+
import type {
7+
ClientOptionsPayload,
8+
GenerateClientOptions,
9+
ScanMeta,
10+
UnlighthouseContext,
11+
UnlighthouseRouteReport,
12+
} from './types'
613
import { createScanMeta } from './data'
714

815
/**
@@ -32,13 +39,35 @@ export async function generateClient(options: GenerateClientOptions = {}, unligh
3239
.replace(/(href|src)="\/assets\/(.*?)"/gm, `$1="${prefix}assets/$2"`)
3340
await fs.writeFile(resolve(runtimeSettings.generatedClientPath, 'index.html'), indexHTML, 'utf-8')
3441

35-
const staticData: { options: ResolvedUserConfig & RuntimeSettings; scanMeta: ScanMeta; reports: UnlighthouseRouteReport[] } = {
42+
const staticData: { options: ClientOptionsPayload; scanMeta: ScanMeta; reports: UnlighthouseRouteReport[] } = {
3643
reports: [],
3744
scanMeta: createScanMeta(),
38-
options: { ...runtimeSettings, ...resolvedConfig },
45+
// need to be selective about what options we put here to avoid exposing anything sensitive
46+
options: pick({
47+
...runtimeSettings,
48+
...resolvedConfig,
49+
}, [
50+
'client',
51+
'site',
52+
'websocketUrl',
53+
'lighthouseOptions',
54+
'scanner',
55+
'routerPrefix',
56+
'websocketUrl',
57+
'apiUrl',
58+
]),
59+
}
60+
// avoid exposing sensitive cookie / header options
61+
staticData.options.lighthouseOptions = { onlyCategories: resolvedConfig.lighthouseOptions.onlyCategories }
62+
if (options.static) {
63+
staticData.reports = worker.reports().map((r) => {
64+
return {
65+
...r,
66+
// avoid exposing user paths
67+
artifactPath: '',
68+
}
69+
})
3970
}
40-
if (options.static)
41-
staticData.reports = worker.reports()
4271

4372
await fs.writeFile(
4473
join(runtimeSettings.generatedClientPath, 'assets', 'payload.js'),

packages/core/src/types.ts

+3
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,9 @@ export interface ResolvedUserConfig {
488488
}
489489
}
490490

491+
export type ClientOptionsPayload = Pick<ResolvedUserConfig, 'client' | 'site' | 'lighthouseOptions' | 'scanner' | 'routerPrefix'>
492+
& Pick<RuntimeSettings, 'websocketUrl' | 'apiUrl'>
493+
491494
export type DeepPartial<T> = T extends Function ? T : (T extends object ? { [P in keyof T]?: DeepPartial<T[P]>; } : T)
492495
export type UserConfig = DeepPartial<ResolvedUserConfig>
493496

0 commit comments

Comments
 (0)