Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/vite/src/node/server/middlewares/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ export function transformMiddleware(
'',
)
if (
!url.startsWith('/@id/\0') &&
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At this point, we have replaced the NULL_BYTE_PLACEHOLDER but didn't yet unwrapped the id. We could actually remove the whole check now that we are doing it before load but leaving that to be done in a separate PR.

deniedServingAccessForTransform(
urlWithoutTrailingQuerySeparators,
server,
Expand Down Expand Up @@ -264,7 +265,10 @@ export function transformMiddleware(
const result = await transformRequest(environment, url, {
html: req.headers.accept?.includes('text/html'),
allowId(id) {
return !deniedServingAccessForTransform(id, server, res, next)
return (
id.startsWith('\0') ||
!deniedServingAccessForTransform(id, server, res, next)
)
Comment on lines +268 to +271
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test will pass without this added check because of the fallback logic added to deniedServingAccessForTransform, but if we know the id is a virtual module, then we can skip the check directly.

},
})
if (result) {
Expand Down
4 changes: 4 additions & 0 deletions playground/fs-serve/__tests__/fs-serve.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ describe.runIf(isServe)('main', () => {
expect(await page.textContent('.named')).toBe(testJSON.msg)
})

test('virtual svg module', async () => {
expect(await page.textContent('.virtual-svg')).toMatch('<svg')
})

test('safe fetch', async () => {
expect(await page.textContent('.safe-fetch')).toMatch('KEY=safe')
expect(await page.textContent('.safe-fetch-status')).toBe('200')
Expand Down
7 changes: 7 additions & 0 deletions playground/fs-serve/root/src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,17 @@ <h2>Denied</h2>
<pre class="unsafe-dotEnV-casing"></pre>
<pre class="unsafe-dotenv-query-dot-svg-wasm-init"></pre>

<d2>Virtual SVG module</d2>
<pre class="virtual-svg"></pre>

<script type="module">
import '../../entry'
import json, { msg } from '../../safe.json'
import './code.js'

// Check virtual svg module still works
import fooSvg from 'virtual:foo.svg'

function joinUrlSegments(a, b) {
if (!a || !b) {
return a || b || ''
Expand All @@ -81,6 +87,7 @@ <h2>Denied</h2>

text('.full', JSON.stringify(json))
text('.named', msg)
text('.virtual-svg', fooSvg)

const base = typeof BASE !== 'undefined' ? BASE : ''

Expand Down
20 changes: 20 additions & 0 deletions playground/fs-serve/root/svgVirtualModulePlugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import type { Plugin } from 'vite'
const svgVirtualModuleId = 'virtual:foo.svg'
const resolvedSvgVirtualModuleId = '\0' + svgVirtualModuleId

export default function svgVirtualModulePlugin(): Plugin {
return {
name: 'svg-virtual-module',
resolveId(id) {
if (id === svgVirtualModuleId) {
return resolvedSvgVirtualModuleId
}
},
async load(id, _options) {
if (id === resolvedSvgVirtualModuleId) {
return `export default '<svg><rect width="100" height="100"></svg>'`
}
},
enforce: 'pre',
}
}
2 changes: 2 additions & 0 deletions playground/fs-serve/root/vite.config-base.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import path from 'node:path'
import { defineConfig } from 'vite'
import svgVirtualModulePlugin from './svgVirtualModulePlugin'

const BASE = '/base/'

Expand Down Expand Up @@ -33,4 +34,5 @@ export default defineConfig({
ROOT: JSON.stringify(path.dirname(__dirname).replace(/\\/g, '/')),
BASE: JSON.stringify(BASE),
},
plugins: [svgVirtualModulePlugin()],
})
2 changes: 2 additions & 0 deletions playground/fs-serve/root/vite.config-deny.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import path from 'node:path'
import { defineConfig } from 'vite'
import svgVirtualModulePlugin from './svgVirtualModulePlugin'

export default defineConfig({
build: {
Expand All @@ -19,4 +20,5 @@ export default defineConfig({
define: {
ROOT: JSON.stringify(path.dirname(__dirname).replace(/\\/g, '/')),
},
plugins: [svgVirtualModulePlugin()],
})
2 changes: 2 additions & 0 deletions playground/fs-serve/root/vite.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import path from 'node:path'
import { defineConfig } from 'vite'
import svgVirtualModulePlugin from './svgVirtualModulePlugin'

export default defineConfig({
build: {
Expand Down Expand Up @@ -29,4 +30,5 @@ export default defineConfig({
define: {
ROOT: JSON.stringify(path.dirname(__dirname).replace(/\\/g, '/')),
},
plugins: [svgVirtualModulePlugin()],
})