Skip to content

Commit

Permalink
fix(nuxt): restrict access to single renderer outside of test/rootDir (
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Apr 27, 2023
1 parent 48c034c commit 65a8f4e
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/nuxt/src/app/components/nuxt-root.vue
Expand Up @@ -23,7 +23,7 @@ const nuxtApp = useNuxtApp()
const onResolve = nuxtApp.deferHydration()
const url = process.server ? nuxtApp.ssrContext.url : window.location.pathname
const SingleRenderer = process.dev && process.server && url.startsWith('/__nuxt_component_test__/') && defineAsyncComponent(() => import('#build/test-component-wrapper.mjs')
const SingleRenderer = process.test && process.dev && process.server && url.startsWith('/__nuxt_component_test__/') && /* #__PURE__ */ defineAsyncComponent(() => import('#build/test-component-wrapper.mjs')
.then(r => r.default(process.server ? url : window.location.href)))
// Inject default route (outside of pages) as active route
Expand Down
9 changes: 8 additions & 1 deletion packages/nuxt/src/app/components/test-component-wrapper.ts
@@ -1,13 +1,20 @@
import { parseURL } from 'ufo'
import { defineComponent, h } from 'vue'
import { parseQuery } from 'vue-router'
import { resolve } from 'pathe'
// @ts-expect-error virtual file
import { devRootDir } from '#build/nuxt.config.mjs'

export default (url:string) => defineComponent({
export default (url: string) => defineComponent({
name: 'NuxtTestComponentWrapper',

async setup (props, { attrs }) {
const query = parseQuery(parseURL(url).search)
const urlProps = query.props ? JSON.parse(query.props as string) : {}
const path = resolve(query.path as string)
if (!path.startsWith(devRootDir)) {
throw new Error(`[nuxt] Cannot access path outside of project root directory: \`${path}\`.`)
}
const comp = await import(/* @vite-ignore */ query.path as string).then(r => r.default)
return () => [
h('div', 'Component Test Wrapper for ' + query.path),
Expand Down
3 changes: 2 additions & 1 deletion packages/nuxt/src/core/templates.ts
Expand Up @@ -299,7 +299,8 @@ export const nuxtConfigTemplate = {
return [
...Object.entries(ctx.nuxt.options.app).map(([k, v]) => `export const ${camelCase('app-' + k)} = ${JSON.stringify(v)}`),
`export const renderJsonPayloads = ${!!ctx.nuxt.options.experimental.renderJsonPayloads}`,
`export const devPagesDir = ${ctx.nuxt.options.dev ? JSON.stringify(ctx.nuxt.options.dir.pages) : 'null'}`
`export const devPagesDir = ${ctx.nuxt.options.dev ? JSON.stringify(ctx.nuxt.options.dir.pages) : 'null'}`,
`export const devRootDir = ${ctx.nuxt.options.dev ? JSON.stringify(ctx.nuxt.options.rootDir) : 'null'}`
].join('\n\n')
}
}
Expand Down
2 changes: 2 additions & 0 deletions packages/schema/src/config/vite.ts
@@ -1,4 +1,5 @@
import { resolve } from 'pathe'
import { isTest } from 'std-env'
import { withoutLeadingSlash } from 'ufo'
import { defineUntypedSchema } from 'untyped'

Expand All @@ -21,6 +22,7 @@ export default defineUntypedSchema({
define: {
$resolve: async (val, get) => ({
'process.dev': await get('dev'),
'process.test': isTest,
...val || {}
})
},
Expand Down
1 change: 1 addition & 0 deletions packages/webpack/package.json
Expand Up @@ -45,6 +45,7 @@
"postcss-import": "^15.1.0",
"postcss-loader": "^7.2.4",
"postcss-url": "^10.1.3",
"std-env": "^3.3.2",
"time-fix-plugin": "^2.0.7",
"ufo": "^1.1.1",
"unplugin": "^1.3.1",
Expand Down
2 changes: 2 additions & 0 deletions packages/webpack/src/presets/base.ts
Expand Up @@ -10,6 +10,7 @@ import FriendlyErrorsWebpackPlugin from '@nuxt/friendly-errors-webpack-plugin'
import escapeRegExp from 'escape-string-regexp'
import { joinURL } from 'ufo'
import type { NuxtOptions } from '@nuxt/schema'
import { isTest } from 'std-env'
import type { WarningFilter } from '../plugins/warning-ignore'
import WarningIgnorePlugin from '../plugins/warning-ignore'
import type { WebpackConfigContext } from '../utils/config'
Expand Down Expand Up @@ -233,6 +234,7 @@ function getEnv (ctx: WebpackConfigContext) {
'process.env.NODE_ENV': JSON.stringify(ctx.config.mode),
'process.mode': JSON.stringify(ctx.config.mode),
'process.dev': options.dev,
'process.test': isTest,
__NUXT_VERSION__: JSON.stringify(ctx.nuxt._version),
'process.env.VUE_ENV': JSON.stringify(ctx.name),
'process.browser': ctx.isClient,
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 65a8f4e

Please sign in to comment.