Skip to content

Commit 011bbca

Browse files
authored
fix: port #16250 to v2 (#16254)
1 parent bfc5649 commit 011bbca

File tree

7 files changed

+53
-4
lines changed

7 files changed

+53
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { isBuild } from '../../../testUtils'
2+
3+
describe('main', () => {
4+
if (!isBuild) {
5+
test('**/deny/** should deny src/deny/deny.txt', async () => {
6+
const res = await page.request.fetch(
7+
new URL('/src/deny/deny.txt', viteTestUrl).href
8+
)
9+
expect(res.status()).toBe(403)
10+
})
11+
test('**/deny/** should deny src/deny/.deny', async () => {
12+
const res = await page.request.fetch(
13+
new URL('/src/deny/.deny', viteTestUrl).href
14+
)
15+
expect(res.status()).toBe(403)
16+
})
17+
} else {
18+
test('dummy test to make jest happy', async () => {
19+
// Your test suite must contain at least one test.
20+
})
21+
}
22+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('../../root/vite.config-deny')

packages/playground/fs-serve/package.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
"dev": "vite root",
77
"build": "vite build root",
88
"debug": "node --inspect-brk ../../vite/bin/vite",
9-
"preview": "vite preview"
9+
"preview": "vite preview",
10+
"dev:deny": "vite root --config ./root/vite.config-deny.js",
11+
"build:deny": "vite build root --config ./root/vite.config-deny.js",
12+
"preview:deny": "vite preview root --config ./root/vite.config-deny.js"
1013
}
1114
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.deny
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
deny
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const path = require('path')
2+
const { defineConfig } = require('vite')
3+
4+
module.exports = defineConfig({
5+
server: {
6+
fs: {
7+
strict: true,
8+
allow: [path.resolve(__dirname, 'src')],
9+
deny: ['**/deny/**']
10+
}
11+
},
12+
define: {
13+
ROOT: JSON.stringify(path.dirname(__dirname).replace(/\\/g, '/'))
14+
}
15+
})

packages/vite/src/node/server/middlewares/static.ts

+9-3
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,11 @@ export function serveRawFsMiddleware(
156156
}
157157
}
158158

159-
const _matchOptions = { matchBase: true, nocase: true }
159+
const _matchOptions = {
160+
matchBase: false,
161+
nocase: true,
162+
dot: true
163+
}
160164

161165
export function isFileServingAllowed(
162166
url: string,
@@ -166,8 +170,10 @@ export function isFileServingAllowed(
166170

167171
const file = fsPathFromUrl(url)
168172

169-
if (server.config.server.fs.deny.some((i) => isMatch(file, i, _matchOptions)))
170-
return false
173+
const deny = server.config.server.fs.deny.map((pattern) =>
174+
pattern.includes('/') ? pattern : `**/${pattern}`
175+
)
176+
if (deny.some((i) => isMatch(file, i, _matchOptions))) return false
171177

172178
if (server.moduleGraph.safeModulesPath.has(file)) return true
173179

0 commit comments

Comments
 (0)