Skip to content
Open
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
39 changes: 39 additions & 0 deletions packages/vite/src/node/server/middlewares/__tests__/static.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import picomatch from 'picomatch'
import { describe, expect, test } from 'vitest'
import { isWindows } from '../../../../shared/utils'
import type {
ResolvedConfig,
ResolvedEnvironmentOptions,
Expand Down Expand Up @@ -175,3 +177,40 @@ describe('isFileLoadingAllowed', () => {
)
})
})

describe('isFileLoadingAllowed with `:` in the path', () => {
const root = isWindows ? 'C:/project' : '/project'
// mirrors how `fsDenyGlob` is built from the default `server.fs.deny`
const environmentConfig = {
server: { fs: { strict: true, allow: [root] } },
fsDenyGlob: picomatch(
['**/.env', '**/.env.*', '**/*.{crt,pem}', '**/.git/**'],
{
matchBase: false,
nocase: true,
dot: true,
},
),
safeModulePaths: new Set<string>(),
} as unknown as ResolvedConfig & ResolvedEnvironmentOptions
const allowed = (filePath: string) =>
isFileLoadingAllowed(environmentConfig, `${root}${filePath}`)

test.skipIf(isWindows)('allows `:` in file and directory names', () => {
expect(allowed('/src/routes/user:show.js')).toBe(true)
expect(allowed('/src/routes/company:logo/index.ts')).toBe(true)
})

test.skipIf(isWindows)('denies streams of denied files', () => {
expect(allowed('/.env::$DATA')).toBe(false)
expect(allowed('/.env:stream')).toBe(false)
expect(allowed('/certs/server.pem::$DATA')).toBe(false)
expect(allowed('/.git::$INDEX_ALLOCATION/config')).toBe(false)
})

test.runIf(isWindows)('denies any `:` after the drive letter', () => {
expect(allowed('/src/routes/user:show.js')).toBe(false)
expect(allowed('/src/index.js:stream')).toBe(false)
expect(allowed('/.env::$DATA')).toBe(false)
})
})
18 changes: 16 additions & 2 deletions packages/vite/src/node/server/middlewares/static.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,8 +338,9 @@ export function isFileLoadingAllowed(

const hasDriveLetter = isWindows && windowsDriveRE.test(filePath)
const hasColon = (hasDriveLetter ? filePath.slice(2) : filePath).includes(':')
if (hasColon) {
// the `:` is included in the path which may be used for NTFS ADS
if (isWindows && hasColon) {
// `:` can't be part of a file name on Windows, so it's an NTFS alternate
// data stream (e.g. `.env::$DATA`)
return false
}

Expand All @@ -350,6 +351,19 @@ export function isFileLoadingAllowed(
: filePath
if (environmentConfig.fsDenyGlob(filePathWithoutTrailingSlash)) return false

// `:` is a valid file name character on other platforms, but an NTFS volume
// mounted there can still expose alternate data streams. Check the path with
// the stream part of each segment removed, so `.env::$DATA` is denied like
// `.env`.
if (
hasColon &&
environmentConfig.fsDenyGlob(
filePathWithoutTrailingSlash.replace(/:[^/]*/g, ''),
)
) {
return false
}

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

if (fs.allow.some((uri) => isFileInTargetPath(uri, filePath))) return true
Expand Down
19 changes: 19 additions & 0 deletions playground/fs-serve/__tests__/fs-serve.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { mkdir, rm, writeFile } from 'node:fs/promises'
import net from 'node:net'
import path from 'node:path'
import { describe, expect, test } from 'vitest'
Expand Down Expand Up @@ -147,3 +148,21 @@ describe.runIf(isServe)('invalid request', () => {
})
}
})

// `:` is a valid file name character outside Windows
describe.runIf(isServe && !isBundledDev && !isWindows)('`:` in path', () => {
test('serves a file in a directory whose name contains `:`', async () => {
const dir = path.resolve(playgroundTempDir, 'fs-serve/root/src/route:name')
await mkdir(dir, { recursive: true })
await writeFile(path.join(dir, 'index.js'), 'export default "colon"')
try {
const response = await fetch(
`${viteTestUrl.replace(/\/$/, '')}/src/route:name/index.js`,
)
expect(response.status).toBe(200)
expect(await response.text()).toContain('colon')
} finally {
await rm(dir, { recursive: true, force: true })
}
})
})
Loading