diff --git a/packages/vite/src/node/server/middlewares/transform.ts b/packages/vite/src/node/server/middlewares/transform.ts index 8515c88db31a73..01f4937759fce5 100644 --- a/packages/vite/src/node/server/middlewares/transform.ts +++ b/packages/vite/src/node/server/middlewares/transform.ts @@ -12,10 +12,8 @@ import { isJSRequest, normalizePath, prettifyUrl, - rawRE, removeImportQuery, removeTimestampQuery, - urlRE, } from '../../utils' import { send } from '../send' import { ERR_LOAD_URL, transformRequest } from '../transformRequest' @@ -45,6 +43,11 @@ const debugCache = createDebugger('vite:cache') const knownIgnoreList = new Set(['/', '/favicon.ico']) const trailingQuerySeparatorsRE = /[?&]+$/ +// TODO: consolidate this regex pattern with the url, raw, and inline checks in plugins +const urlRE = /[?&]url\b/ +const rawRE = /[?&]raw\b/ +const inlineRE = /[?&]inline\b/ + /** * A middleware that short-circuits the middleware chain to serve cached transformed modules */ @@ -176,7 +179,8 @@ export function transformMiddleware( ) if ( (rawRE.test(urlWithoutTrailingQuerySeparators) || - urlRE.test(urlWithoutTrailingQuerySeparators)) && + urlRE.test(urlWithoutTrailingQuerySeparators) || + inlineRE.test(urlWithoutTrailingQuerySeparators)) && !ensureServingAccess( urlWithoutTrailingQuerySeparators, server, diff --git a/playground/fs-serve/__tests__/fs-serve.spec.ts b/playground/fs-serve/__tests__/fs-serve.spec.ts index 4f55df0faa3b42..795c70e5ae201a 100644 --- a/playground/fs-serve/__tests__/fs-serve.spec.ts +++ b/playground/fs-serve/__tests__/fs-serve.spec.ts @@ -67,6 +67,18 @@ describe.runIf(isServe)('main', () => { expect(await page.textContent('.unsafe-fetch-8498-2-status')).toBe('404') }) + test('unsafe fetch import inline', async () => { + expect(await page.textContent('.unsafe-fetch-import-inline-status')).toBe( + '403', + ) + }) + + test('unsafe fetch raw query import', async () => { + expect( + await page.textContent('.unsafe-fetch-raw-query-import-status'), + ).toBe('403') + }) + test('safe fs fetch', async () => { expect(await page.textContent('.safe-fs-fetch')).toBe(stringified) expect(await page.textContent('.safe-fs-fetch-status')).toBe('200') @@ -120,6 +132,18 @@ describe.runIf(isServe)('main', () => { expect(await page.textContent('.unsafe-fs-fetch-8498-2-status')).toBe('404') }) + test('unsafe fs fetch import inline', async () => { + expect( + await page.textContent('.unsafe-fs-fetch-import-inline-status'), + ).toBe('403') + }) + + test('unsafe fs fetch import inline wasm init', async () => { + expect( + await page.textContent('.unsafe-fs-fetch-import-inline-wasm-init-status'), + ).toBe('403') + }) + test('nested entry', async () => { expect(await page.textContent('.nested-entry')).toBe('foobar') }) diff --git a/playground/fs-serve/root/src/index.html b/playground/fs-serve/root/src/index.html index 26375949c217fe..931ac38d5e749e 100644 --- a/playground/fs-serve/root/src/index.html +++ b/playground/fs-serve/root/src/index.html @@ -23,6 +23,8 @@