Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion packages/vite/src/node/server/middlewares/static.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,12 @@ export function isFileLoadingAllowed(

if (!fs.strict) return true

if (config.fsDenyGlob(filePath)) return false
// NOTE: `fs.readFile('/foo.png/')` tries to load `'/foo.png'`
// so we should check the path without trailing slash
const filePathWithoutTrailingSlash = filePath.endsWith('/')
? filePath.slice(0, -1)
: filePath
if (config.fsDenyGlob(filePathWithoutTrailingSlash)) return false

if (config.safeModulePaths.has(filePath)) return true

Expand Down
26 changes: 25 additions & 1 deletion playground/fs-serve/__tests__/fs-serve.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,14 @@ import {
import type { Page } from 'playwright-chromium'
import WebSocket from 'ws'
import testJSON from '../safe.json'
import { browser, isServe, page, viteServer, viteTestUrl } from '~utils'
import {
browser,
isServe,
isWindows,
page,
viteServer,
viteTestUrl,
} from '~utils'

const __dirname = path.dirname(fileURLToPath(import.meta.url))

Expand Down Expand Up @@ -538,6 +545,23 @@ describe.runIf(isServe)('invalid request', () => {
expect(response).toContain('HTTP/1.1 403 Forbidden')
})

test('should deny request to denied file when a request ends with \\', async () => {
const response = await sendRawRequest(viteTestUrl, '/src/.env\\')
expect(response).toContain(
isWindows ? 'HTTP/1.1 403 Forbidden' : 'HTTP/1.1 404 Not Found',
)
})

test('should deny request to denied file when a request ends with \\ with /@fs/', async () => {
const response = await sendRawRequest(
viteTestUrl,
path.posix.join('/@fs/', root, 'root/src/.env') + '\\',
)
expect(response).toContain(
isWindows ? 'HTTP/1.1 403 Forbidden' : 'HTTP/1.1 404 Not Found',
)
})

test('should deny request with /@fs/ to denied file when a request has /.', async () => {
const response = await sendRawRequest(
viteTestUrl,
Expand Down