fix: port #16250 to v2 (#16254)

This commit is contained in:
翠 / green 2024-03-25 00:00:45 +09:00 committed by GitHub
parent bfc5649b8d
commit 011bbca350
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 53 additions and 4 deletions

View File

@ -0,0 +1,22 @@
import { isBuild } from '../../../testUtils'
describe('main', () => {
if (!isBuild) {
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)
})
} else {
test('dummy test to make jest happy', async () => {
// Your test suite must contain at least one test.
})
}
})

View File

@ -0,0 +1 @@
module.exports = require('../../root/vite.config-deny')

View File

@ -6,6 +6,9 @@
"dev": "vite root",
"build": "vite build root",
"debug": "node --inspect-brk ../../vite/bin/vite",
"preview": "vite preview"
"preview": "vite preview",
"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"
}
}

View File

@ -0,0 +1 @@
.deny

View File

@ -0,0 +1 @@
deny

View File

@ -0,0 +1,15 @@
const path = require('path')
const { defineConfig } = require('vite')
module.exports = defineConfig({
server: {
fs: {
strict: true,
allow: [path.resolve(__dirname, 'src')],
deny: ['**/deny/**']
}
},
define: {
ROOT: JSON.stringify(path.dirname(__dirname).replace(/\\/g, '/'))
}
})

View File

@ -156,7 +156,11 @@ export function serveRawFsMiddleware(
}
}
const _matchOptions = { matchBase: true, nocase: true }
const _matchOptions = {
matchBase: false,
nocase: true,
dot: true
}
export function isFileServingAllowed(
url: string,
@ -166,8 +170,10 @@ export function isFileServingAllowed(
const file = fsPathFromUrl(url)
if (server.config.server.fs.deny.some((i) => isMatch(file, i, _matchOptions)))
return false
const deny = server.config.server.fs.deny.map((pattern) =>
pattern.includes('/') ? pattern : `**/${pattern}`
)
if (deny.some((i) => isMatch(file, i, _matchOptions))) return false
if (server.moduleGraph.safeModulesPath.has(file)) return true