Skip to content

Commit ba5269c

Browse files
authored
fix: fs.deny with globs with directories (#16250)
1 parent 7a2791c commit ba5269c

File tree

6 files changed

+58
-5
lines changed

6 files changed

+58
-5
lines changed

packages/vite/src/node/server/index.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -685,10 +685,19 @@ export async function _createServer(
685685
_importGlobMap: new Map(),
686686
_forceOptimizeOnRestart: false,
687687
_pendingRequests: new Map(),
688-
_fsDenyGlob: picomatch(config.server.fs.deny, {
689-
matchBase: true,
690-
nocase: true,
691-
}),
688+
_fsDenyGlob: picomatch(
689+
// matchBase: true does not work as it's documented
690+
// https://github.com/micromatch/picomatch/issues/89
691+
// convert patterns without `/` on our side for now
692+
config.server.fs.deny.map((pattern) =>
693+
pattern.includes('/') ? pattern : `**/${pattern}`,
694+
),
695+
{
696+
matchBase: false,
697+
nocase: true,
698+
dot: true,
699+
},
700+
),
692701
_shortcutsOptions: undefined,
693702
}
694703

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { describe, expect, test } from 'vitest'
2+
import { isServe, page, viteTestUrl } from '~utils'
3+
4+
describe.runIf(isServe)('main', () => {
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+
})

playground/fs-serve/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
"preview": "vite preview root",
1111
"dev:base": "vite root --config ./root/vite.config-base.js",
1212
"build:base": "vite build root --config ./root/vite.config-base.js",
13-
"preview:base": "vite preview root --config ./root/vite.config-base.js"
13+
"preview:base": "vite preview root --config ./root/vite.config-base.js",
14+
"dev:deny": "vite root --config ./root/vite.config-deny.js",
15+
"build:deny": "vite build root --config ./root/vite.config-deny.js",
16+
"preview:deny": "vite preview root --config ./root/vite.config-deny.js"
1417
}
1518
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.deny
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
deny
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import path from 'node:path'
2+
import { defineConfig } from 'vite'
3+
4+
export default defineConfig({
5+
build: {
6+
rollupOptions: {
7+
input: {
8+
main: path.resolve(__dirname, 'src/index.html'),
9+
},
10+
},
11+
},
12+
server: {
13+
fs: {
14+
strict: true,
15+
allow: [path.resolve(__dirname, 'src')],
16+
deny: ['**/deny/**'],
17+
},
18+
},
19+
define: {
20+
ROOT: JSON.stringify(path.dirname(__dirname).replace(/\\/g, '/')),
21+
},
22+
})

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy