From 89c7c645f09d16a38f146ef4a1528f218e844d67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BF=A0=20/=20green?= Date: Sun, 24 Mar 2024 23:41:19 +0900 Subject: [PATCH] fix: port #16250 to v3 (#16253) --- packages/vite/src/node/server/index.ts | 17 ++++++++++---- .../__tests__/deny/fs-serve-deny.spec.ts | 17 ++++++++++++++ .../fs-serve/__tests__/deny/vite.config.js | 1 + playground/fs-serve/package.json | 5 ++++- playground/fs-serve/root/src/deny/.deny | 1 + playground/fs-serve/root/src/deny/deny.txt | 1 + playground/fs-serve/root/vite.config-deny.js | 22 +++++++++++++++++++ 7 files changed, 59 insertions(+), 5 deletions(-) create mode 100644 playground/fs-serve/__tests__/deny/fs-serve-deny.spec.ts create mode 100644 playground/fs-serve/__tests__/deny/vite.config.js create mode 100644 playground/fs-serve/root/src/deny/.deny create mode 100644 playground/fs-serve/root/src/deny/deny.txt create mode 100644 playground/fs-serve/root/vite.config-deny.js diff --git a/packages/vite/src/node/server/index.ts b/packages/vite/src/node/server/index.ts index a5429b9dc32ec7..941daf5833f031 100644 --- a/packages/vite/src/node/server/index.ts +++ b/packages/vite/src/node/server/index.ts @@ -452,10 +452,19 @@ export async function createServer( _importGlobMap: new Map(), _forceOptimizeOnRestart: false, _pendingRequests: new Map(), - _fsDenyGlob: picomatch(config.server.fs.deny, { - matchBase: true, - nocase: true - }) + _fsDenyGlob: picomatch( + // matchBase: true does not work as it's documented + // https://github.com/micromatch/picomatch/issues/89 + // convert patterns without `/` on our side for now + config.server.fs.deny.map((pattern) => + pattern.includes('/') ? pattern : `**/${pattern}` + ), + { + matchBase: false, + nocase: true, + dot: true + } + ) } server.transformIndexHtml = createDevHtmlTransformFn(server) diff --git a/playground/fs-serve/__tests__/deny/fs-serve-deny.spec.ts b/playground/fs-serve/__tests__/deny/fs-serve-deny.spec.ts new file mode 100644 index 00000000000000..a4e7c29224c144 --- /dev/null +++ b/playground/fs-serve/__tests__/deny/fs-serve-deny.spec.ts @@ -0,0 +1,17 @@ +import { describe, expect, test } from 'vitest' +import { isServe, page, viteTestUrl } from '~utils' + +describe.runIf(isServe)('main', () => { + test('**/deny/** should deny src/deny/deny.txt', async () => { + const res = await page.request.fetch( + new URL('/src/deny/deny.txt', viteTestUrl).href + ) + expect(res.status()).toBe(403) + }) + test('**/deny/** should deny src/deny/.deny', async () => { + const res = await page.request.fetch( + new URL('/src/deny/.deny', viteTestUrl).href + ) + expect(res.status()).toBe(403) + }) +}) diff --git a/playground/fs-serve/__tests__/deny/vite.config.js b/playground/fs-serve/__tests__/deny/vite.config.js new file mode 100644 index 00000000000000..8c7726526c2f8b --- /dev/null +++ b/playground/fs-serve/__tests__/deny/vite.config.js @@ -0,0 +1 @@ +module.exports = require('../../root/vite.config-deny') diff --git a/playground/fs-serve/package.json b/playground/fs-serve/package.json index 6a5d18e2303c53..b3cb7746544934 100644 --- a/playground/fs-serve/package.json +++ b/playground/fs-serve/package.json @@ -6,6 +6,9 @@ "dev": "vite root", "build": "vite build root", "debug": "node --inspect-brk ../../packages/vite/bin/vite", - "preview": "vite preview root" + "preview": "vite preview root", + "dev:deny": "vite root --config ./root/vite.config-deny.js", + "build:deny": "vite build root --config ./root/vite.config-deny.js", + "preview:deny": "vite preview root --config ./root/vite.config-deny.js" } } diff --git a/playground/fs-serve/root/src/deny/.deny b/playground/fs-serve/root/src/deny/.deny new file mode 100644 index 00000000000000..73bd3960853c61 --- /dev/null +++ b/playground/fs-serve/root/src/deny/.deny @@ -0,0 +1 @@ +.deny diff --git a/playground/fs-serve/root/src/deny/deny.txt b/playground/fs-serve/root/src/deny/deny.txt new file mode 100644 index 00000000000000..f9df83416f8a72 --- /dev/null +++ b/playground/fs-serve/root/src/deny/deny.txt @@ -0,0 +1 @@ +deny diff --git a/playground/fs-serve/root/vite.config-deny.js b/playground/fs-serve/root/vite.config-deny.js new file mode 100644 index 00000000000000..dd6bc2ab734acf --- /dev/null +++ b/playground/fs-serve/root/vite.config-deny.js @@ -0,0 +1,22 @@ +import path from 'node:path' +import { defineConfig } from 'vite' + +export default defineConfig({ + build: { + rollupOptions: { + input: { + main: path.resolve(__dirname, 'src/index.html') + } + } + }, + server: { + fs: { + strict: true, + allow: [path.resolve(__dirname, 'src')], + deny: ['**/deny/**'] + } + }, + define: { + ROOT: JSON.stringify(path.dirname(__dirname).replace(/\\/g, '/')) + } +})