Skip to content

Commit

Permalink
fix: port #16250 to v3 (#16253)
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red authored Mar 24, 2024
1 parent 8352b75 commit 89c7c64
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 5 deletions.
17 changes: 13 additions & 4 deletions packages/vite/src/node/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
17 changes: 17 additions & 0 deletions playground/fs-serve/__tests__/deny/fs-serve-deny.spec.ts
Original file line number Diff line number Diff line change
@@ -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)
})
})
1 change: 1 addition & 0 deletions playground/fs-serve/__tests__/deny/vite.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('../../root/vite.config-deny')
5 changes: 4 additions & 1 deletion playground/fs-serve/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
1 change: 1 addition & 0 deletions playground/fs-serve/root/src/deny/.deny
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.deny
1 change: 1 addition & 0 deletions playground/fs-serve/root/src/deny/deny.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
deny
22 changes: 22 additions & 0 deletions playground/fs-serve/root/vite.config-deny.js
Original file line number Diff line number Diff line change
@@ -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, '/'))
}
})

0 comments on commit 89c7c64

Please sign in to comment.