Skip to content

Commit 96b0c10

Browse files
authored
fix: backport #22572, reject windows alternate paths (#22576)
1 parent 8fed5cf commit 96b0c10

6 files changed

Lines changed: 90 additions & 0 deletions

File tree

‎packages/vite/src/node/server/middlewares/static.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,8 @@ export function isUriInFilePath(uri: string, filePath: string): boolean {
266266
return isSameFileUri(uri, filePath) || isParentDirectory(uri, filePath)
267267
}
268268

269+
const windowsDriveRE = /^[A-Z]:/i
270+
269271
export function isFileLoadingAllowed(
270272
config: ResolvedConfig,
271273
filePath: string,
@@ -274,6 +276,19 @@ export function isFileLoadingAllowed(
274276

275277
if (!fs.strict) return true
276278

279+
if (isWindows && filePath.includes('~')) {
280+
// `~` is used for Windows 8.3 short names, which can be used to bypass the check.
281+
// While is it valid to have files with `~` in the path, we disallow it to be safe.
282+
return false
283+
}
284+
285+
const hasDriveLetter = isWindows && windowsDriveRE.test(filePath)
286+
const hasColon = (hasDriveLetter ? filePath.slice(2) : filePath).includes(':')
287+
if (hasColon) {
288+
// the `:` is included in the path which may be used for NTFS ADS
289+
return false
290+
}
291+
277292
// NOTE: `fs.readFile('/foo.png/')` tries to load `'/foo.png'`
278293
// so we should check the path without trailing slash
279294
const filePathWithoutTrailingSlash = filePath.endsWith('/')

‎playground/fs-serve/__tests__/commonTests.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
import type { Page } from 'playwright-chromium'
1414
import WebSocket from 'ws'
1515
import testJSON from '../safe.json'
16+
import { getWindows83ShortNameForDotEnv as getWindows83ShortNameForDotEnv } from '../root/windows83Filename'
1617
import { browser, isServe, page, viteServer, viteTestUrl } from '~utils'
1718

1819
const getViteTestIndexHtmlUrl = () => {
@@ -234,6 +235,23 @@ describe.runIf(isServe)('main', () => {
234235
.poll(() => page.textContent('.unsafe-dotenv-query-dot-svg-wasm-init'))
235236
.toBe('403')
236237
})
238+
239+
test('denied .env with NTFS ADS suffix', async () => {
240+
// It is 403 on NTFS, 404 on others
241+
await expect
242+
.poll(() => page.textContent('.unsafe-dotenv-ntfs-ads'))
243+
.toStrictEqual(expect.toBeOneOf(['403', '404']))
244+
})
245+
246+
const dotEnvWindows83ShortName = getWindows83ShortNameForDotEnv()
247+
test.skipIf(dotEnvWindows83ShortName === undefined)(
248+
'denied .env with 8.3 short name',
249+
async () => {
250+
await expect
251+
.poll(() => page.textContent('.unsafe-dotenv-83-short-name'))
252+
.toBe('403')
253+
},
254+
)
237255
})
238256

239257
describe('fetch', () => {

‎playground/fs-serve/root/src/index.html‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ <h2>Denied</h2>
6464
<pre class="unsafe-dotenv"></pre>
6565
<pre class="unsafe-dotEnV-casing"></pre>
6666
<pre class="unsafe-dotenv-query-dot-svg-wasm-init"></pre>
67+
<pre class="unsafe-dotenv-ntfs-ads"></pre>
68+
<pre class="unsafe-dotenv-83-short-name"></pre>
6769

6870
<script type="module">
6971
import '../../entry'
@@ -87,6 +89,7 @@ <h2>Denied</h2>
8789
text('.named', msg)
8890

8991
const base = typeof BASE !== 'undefined' ? BASE : ''
92+
const dotEnvWindows83ShortName = DOTENV83SHORTNAME
9093

9194
// inside allowed dir, safe fetch
9295
fetch(joinUrlSegments(base, '/src/safe.txt'))
@@ -449,6 +452,33 @@ <h2>Denied</h2>
449452
console.error(e)
450453
})
451454

455+
fetch(
456+
joinUrlSegments(
457+
base,
458+
joinUrlSegments('/@fs/', ROOT) + '/root/src/.env::$DATA',
459+
),
460+
)
461+
.then((r) => {
462+
text('.unsafe-dotenv-ntfs-ads', r.status)
463+
})
464+
.catch((e) => {
465+
console.error(e)
466+
})
467+
468+
fetch(
469+
joinUrlSegments(
470+
base,
471+
joinUrlSegments('/@fs/', ROOT) +
472+
`/root/src/${dotEnvWindows83ShortName}.json`,
473+
),
474+
)
475+
.then((r) => {
476+
text('.unsafe-dotenv-83-short-name', r.status)
477+
})
478+
.catch((e) => {
479+
console.error(e)
480+
})
481+
452482
function text(sel, text) {
453483
document.querySelector(sel).textContent = text
454484
}

‎playground/fs-serve/root/vite.config-base.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import path from 'node:path'
22
import { defineConfig } from 'vite'
3+
import { getWindows83ShortNameForDotEnv } from './windows83Filename'
34

45
const BASE = '/base/'
56

@@ -33,5 +34,6 @@ export default defineConfig({
3334
define: {
3435
ROOT: JSON.stringify(path.dirname(__dirname).replace(/\\/g, '/')),
3536
BASE: JSON.stringify(BASE),
37+
DOTENV83SHORTNAME: JSON.stringify(getWindows83ShortNameForDotEnv()),
3638
},
3739
})

‎playground/fs-serve/root/vite.config.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import path from 'node:path'
22
import { defineConfig } from 'vite'
3+
import { getWindows83ShortNameForDotEnv } from './windows83Filename'
34

45
export default defineConfig({
56
build: {
@@ -29,5 +30,6 @@ export default defineConfig({
2930
},
3031
define: {
3132
ROOT: JSON.stringify(path.dirname(__dirname).replace(/\\/g, '/')),
33+
DOTENV83SHORTNAME: JSON.stringify(getWindows83ShortNameForDotEnv()),
3234
},
3335
})
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { execSync } from 'node:child_process'
2+
import path from 'node:path'
3+
4+
function getWindows83ShortName(inputPath: string): string | undefined {
5+
try {
6+
const result = execSync(
7+
`powershell -Command "(New-Object -ComObject Scripting.FileSystemObject).GetFile('${inputPath}').ShortPath"`,
8+
{ encoding: 'utf-8' },
9+
).trim()
10+
return result !== inputPath && result.includes('~') ? result : undefined
11+
} catch {
12+
return undefined
13+
}
14+
}
15+
16+
export function getWindows83ShortNameForDotEnv(): string | undefined {
17+
const dotEnvPath = path.resolve(__dirname, '../root/src/.env')
18+
const dotEnvWindows83ShortName = getWindows83ShortName(dotEnvPath)
19+
if (dotEnvWindows83ShortName === undefined) {
20+
return undefined
21+
}
22+
return path.basename(dotEnvWindows83ShortName)
23+
}

0 commit comments

Comments
 (0)