Skip to content

Commit 0383cd7

Browse files
authored
fix(devtools): require the dev auth token for server config and storage mounts (#1124)
* fix(devtools): resume pending actions once a token is entered in the auth prompt * fix(devtools): require the dev auth token for server config and storage mounts
1 parent be9a4cd commit 0383cd7

10 files changed

Lines changed: 97 additions & 29 deletions

File tree

‎docs/content/2.module/1.utils-kit.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ import { onDevtoolsClientConnected } from '@nuxt/devtools-kit/iframe-client'
145145
onDevtoolsClientConnected(async (client) => {
146146
// client is NuxtDevtoolsIframeClient
147147

148-
const config = client.devtools.rpc.getServerConfig()
148+
const components = await client.devtools.rpc.getComponents()
149149
// ...
150150
})
151151
```

‎packages/devtools-kit/src/_types/rpc.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import type { GetWizardArgs, WizardActions } from './wizard'
1212

1313
export interface ServerFunctions {
1414
// Static RPCs (can be provide on production build in the future)
15-
getServerConfig: () => NuxtOptions
15+
getServerConfig: (token: string) => Promise<NuxtOptions>
1616
getServerDebugContext: () => Promise<ServerDebugContext | undefined>
1717
getServerData: (token: string) => Promise<NuxtServerData>
1818
getServerRuntimeConfig: (token: string) => Promise<Record<string, any>>
@@ -45,8 +45,8 @@ export interface ServerFunctions {
4545
runTerminalAction: (token: string, id: string, action: TerminalAction) => Promise<boolean>
4646

4747
// Storage
48-
getStorageMounts: () => Promise<StorageMounts>
49-
getStorageKeys: (base?: string) => Promise<string[]>
48+
getStorageMounts: (token: string) => Promise<StorageMounts>
49+
getStorageKeys: (token: string, base?: string) => Promise<string[]>
5050
getStorageItem: (token: string, key: string) => Promise<StorageValue>
5151
setStorageItem: (token: string, key: string, value: StorageValue) => Promise<void>
5252
removeStorageItem: (token: string, key: string) => Promise<void>

‎packages/devtools/client/components/StorageDetails.vue‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ const currentStorage = useSessionState<string>('storage:current', '')
1919
const currentItem = ref()
2020
const fileKey = useSessionState<string>('storage:file:state', '')
2121
22-
const { data: storageMounts } = await useAsyncData('storageMounts', () => rpc.getStorageMounts())
22+
const { data: storageMounts } = await useAsyncData('storageMounts', async () => rpc.getStorageMounts(await ensureDevAuthToken()))
2323
const { data: storageKeys, refresh: refreshStorageKeys } = await useAsyncData('storageKeys', async () => {
2424
if (currentStorage.value)
25-
return await rpc.getStorageKeys(currentStorage.value)
25+
return await rpc.getStorageKeys(await ensureDevAuthToken(), currentStorage.value)
2626
return []
2727
})
2828

‎packages/devtools/client/composables/dev-auth.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ async function authConfirmAction() {
7373

7474
const result = await Promise.race([
7575
AuthConfirm.start(),
76-
until(devAuthToken.value).toBeTruthy(),
76+
until(devAuthToken).toBeTruthy(),
7777
])
7878

7979
if (result === false) {

‎packages/devtools/client/composables/state.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { objectPick } from '@antfu/utils'
44
import { computed } from 'vue'
55
import { useFetch } from '#app/composables/fetch'
66
import { useClientRouter } from './client'
7-
import { ensureDevAuthToken } from './dev-auth'
7+
import { ensureDevAuthToken, isDevAuthed } from './dev-auth'
88
import { rpc } from './rpc'
99
import { useAsyncState } from './utils'
1010

@@ -37,7 +37,8 @@ export function useStaticAssets() {
3737
}
3838

3939
export function useServerConfig() {
40-
return useAsyncState('getServerConfig', () => rpc.getServerConfig())
40+
// Refetch once authorized, e.g. after the initial prompt was dismissed.
41+
return useAsyncState('getServerConfig', async () => rpc.getServerConfig(await ensureDevAuthToken()), { watch: [isDevAuthed] })
4142
}
4243

4344
export function useServerDebugContext() {

‎packages/devtools/client/pages/modules/overview.vue‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,12 @@ function authorize() {
4444

4545
<template>
4646
<NPanelGrids h-screen w-full flex>
47-
<div v-if="!config">
47+
<button v-if="!config && !isDevAuthed" title="Authorize" @click="authorize">
48+
<NTip n="orange5" icon="i-carbon-locked" justify-center>
49+
Project data requires authorization. Click to authorize this browser.
50+
</NTip>
51+
</button>
52+
<div v-else-if="!config">
4853
Loading...
4954
</div>
5055
<div v-else flex="~ col gap2" ma h-full max-w-300 w-full p5 px5 md:px20>

‎packages/devtools/src/server-rpc/general.ts‎

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -86,20 +86,13 @@ export function setupGeneralRPC({
8686
})
8787

8888
return {
89-
getServerConfig(): NuxtOptions {
90-
// This method is intentionally reachable without a dev auth token (the
91-
// client reads it eagerly across many views), so strip the private
92-
// `runtimeConfig` values — they can hold secrets — from the payload.
93-
// The Runtime Config tab reads them separately via the token-gated
94-
// `getServerRuntimeConfig`. `public`/`app` are non-secret and kept.
95-
const { runtimeConfig, ...rest } = nuxt.options
96-
return {
97-
...rest,
98-
runtimeConfig: {
99-
app: runtimeConfig?.app,
100-
public: runtimeConfig?.public,
101-
},
102-
} as unknown as NuxtOptions
89+
async getServerConfig(token: string): Promise<NuxtOptions> {
90+
// Secrets live all over the resolved options (runtimeConfig and its
91+
// `nitro` copy, `_layers[].config`, module options, storage drivers), so
92+
// the whole object is gated rather than redacted.
93+
await ensureDevAuthToken(token)
94+
// `@nuxt/schema` and `nuxt/schema` resolve to copies with different module augmentations
95+
return nuxt.options as unknown as NuxtOptions
10396
},
10497
async getServerDebugContext() {
10598
if (!nuxt._debug)

‎packages/devtools/src/server-rpc/server-data.ts‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,6 @@ export function setupServerDataRPC({
5858
})
5959

6060
return {
61-
getServerConfig() {
62-
return nuxt.options
63-
},
6461
async getServerData(token: string) {
6562
await ensureDevAuthToken(token)
6663
return {

‎packages/devtools/src/server-rpc/storage.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,13 @@ export function setupStorageRPC({
5555
})
5656

5757
return {
58-
async getStorageMounts() {
58+
async getStorageMounts(token: string) {
59+
// Mount options carry driver credentials (passwords, access keys).
60+
await ensureDevAuthToken(token)
5961
return storageMounts
6062
},
61-
async getStorageKeys(base?: string) {
63+
async getStorageKeys(token: string, base?: string) {
64+
await ensureDevAuthToken(token)
6265
if (!storage)
6366
return []
6467
try {
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import { mkdtemp } from 'node:fs/promises'
2+
import { tmpdir } from 'node:os'
3+
import { join } from 'node:path'
4+
import { runWithNuxtContext } from '@nuxt/kit'
5+
import { createHooks } from 'hookable'
6+
import { beforeAll, describe, expect, it } from 'vitest'
7+
import { defaultOptions } from '../src/constant'
8+
import { getDevAuthToken } from '../src/dev-auth'
9+
import { setupRPC } from '../src/server-rpc'
10+
11+
const SECRET = 'synthetic-secret'
12+
13+
// Exercise the composed RPC table the HMR socket dispatches to, so a later
14+
// module re-registering a method without the token guard is caught.
15+
async function setupServer() {
16+
const hooks = createHooks()
17+
const rootDir = await mkdtemp(join(tmpdir(), 'nuxt-devtools-test-'))
18+
const nuxt = {
19+
hooks,
20+
hook: hooks.hook.bind(hooks),
21+
callHook: hooks.callHook.bind(hooks),
22+
options: {
23+
rootDir,
24+
srcDir: rootDir,
25+
dir: { public: 'public' },
26+
app: { baseURL: '/' },
27+
runtimeConfig: { secret: SECRET, public: {} },
28+
nitro: { runtimeConfig: { secret: SECRET } },
29+
_layers: [{ config: { runtimeConfig: { secret: SECRET } } }],
30+
},
31+
} as any
32+
const { rpc } = runWithNuxtContext(nuxt, () => setupRPC(nuxt, defaultOptions))
33+
await nuxt.callHook('nitro:init', {
34+
storage: {},
35+
options: { storage: { db: { driver: 'redis', password: SECRET } }, devStorage: {} },
36+
})
37+
return { nuxt, functions: rpc.functions }
38+
}
39+
40+
describe('private server data rpc', () => {
41+
let server: Awaited<ReturnType<typeof setupServer>>
42+
43+
beforeAll(async () => {
44+
// sandbox where the dev auth token is persisted (see getHomeDir)
45+
process.env.XDG_CONFIG_HOME = await mkdtemp(join(tmpdir(), 'nuxt-devtools-home-'))
46+
server = await setupServer()
47+
})
48+
49+
it('getServerConfig rejects a missing or invalid token', async () => {
50+
// @ts-expect-error simulating a tokenless client
51+
await expect(server.functions.getServerConfig()).rejects.toThrow(/invalid dev auth token/i)
52+
await expect(server.functions.getServerConfig('invalid')).rejects.toThrow(/invalid dev auth token/i)
53+
})
54+
55+
it('getServerConfig returns the options with a valid token', async () => {
56+
await expect(server.functions.getServerConfig(await getDevAuthToken())).resolves.toBe(server.nuxt.options)
57+
})
58+
59+
it('getStorageMounts and getStorageKeys reject an invalid token', async () => {
60+
await expect(server.functions.getStorageMounts('invalid')).rejects.toThrow(/invalid dev auth token/i)
61+
await expect(server.functions.getStorageKeys('invalid', 'db')).rejects.toThrow(/invalid dev auth token/i)
62+
})
63+
64+
it('getStorageMounts returns mounts with a valid token', async () => {
65+
await expect(server.functions.getStorageMounts(await getDevAuthToken()))
66+
.resolves
67+
.toEqual({ db: { driver: 'redis', password: SECRET } })
68+
})
69+
})

0 commit comments

Comments
 (0)