Skip to content

Commit

Permalink
feat: check access for secret values on runtime configuration when dev
Browse files Browse the repository at this point in the history
  • Loading branch information
nopeless committed Mar 22, 2024
1 parent 70a619f commit ed14aea
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
7 changes: 1 addition & 6 deletions packages/nuxt/src/app/nuxt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,12 +315,7 @@ export function createNuxtApp (options: CreateOptions) {
Object.assign(nuxtApp.payload, nuxtApp.ssrContext!.payload)
}
nuxtApp.ssrContext!.payload = nuxtApp.payload

// Expose client runtime-config to the payload
nuxtApp.ssrContext!.config = {
public: options.ssrContext!.runtimeConfig.public,
app: options.ssrContext!.runtimeConfig.app
}
nuxtApp.ssrContext!.config = options.ssrContext!.runtimeConfig
}

// Listen to chunk load errors
Expand Down
25 changes: 24 additions & 1 deletion packages/nuxt/src/core/runtime/nitro/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,17 @@ const getSPARenderer = lazyCachedFunction(async () => {
state: {},
once: new Set<string>()
}
ssrContext.config = {

// client side config
const csc = ssrContext.config = {
public: config.public,
app: config.app
}

if (import.meta.dev) {
configureErrorGetter(config, csc)
}

return Promise.resolve(result)
}

Expand All @@ -183,6 +190,22 @@ const getSPARenderer = lazyCachedFunction(async () => {
}
})

/**
* Configure error getter on runtime secret property access that doesn't exist in SPA
*/
const configureErrorGetter = (serverSideConfig: Record<string, unknown>, clientSideConfig: Record<string, unknown>) => {
for (const key in serverSideConfig) {
if (key in clientSideConfig) { continue }

Object.defineProperty(clientSideConfig, key, {
get () {
throw new Error(`Runtime config key ${key} is not available on the client side. See useRuntimeConfig for more information`)
},
enumerable: true
})
}
}

const payloadCache = import.meta.prerender ? useStorage('internal:nuxt:prerender:payload') : null
const islandCache = import.meta.prerender ? useStorage('internal:nuxt:prerender:island') : null
const islandPropCache = import.meta.prerender ? useStorage('internal:nuxt:prerender:island-props') : null
Expand Down

0 comments on commit ed14aea

Please sign in to comment.