Skip to content

Commit

Permalink
fix: port #16250 to v2 (#16254)
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red authored Mar 24, 2024
1 parent bfc5649 commit 011bbca
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 4 deletions.
22 changes: 22 additions & 0 deletions packages/playground/fs-serve/__tests__/deny/fs-serve-deny.spec.ts
Original file line number Diff line number Diff line change
@@ -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.
})
}
})
1 change: 1 addition & 0 deletions packages/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 packages/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 ../../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"
}
}
1 change: 1 addition & 0 deletions packages/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 packages/playground/fs-serve/root/src/deny/deny.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
deny
15 changes: 15 additions & 0 deletions packages/playground/fs-serve/root/vite.config-deny.js
Original file line number Diff line number Diff line change
@@ -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, '/'))
}
})
12 changes: 9 additions & 3 deletions packages/vite/src/node/server/middlewares/static.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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

Expand Down

0 comments on commit 011bbca

Please sign in to comment.