Skip to content

Commit ef2a024

Browse files
authored
fix: symbolic links in public dir (#15264)
1 parent 8ad81b4 commit ef2a024

File tree

4 files changed

+23
-5
lines changed

4 files changed

+23
-5
lines changed

packages/vite/src/node/publicDir.ts

+11-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import fs from 'node:fs'
22
import path from 'node:path'
33
import type { ResolvedConfig } from './config'
44
import {
5+
ERR_SYMLINK_IN_RECURSIVE_READDIR,
56
cleanUrl,
67
normalizePath,
78
recursiveReaddir,
@@ -12,8 +13,16 @@ const publicFilesMap = new WeakMap<ResolvedConfig, Set<string>>()
1213

1314
export async function initPublicFiles(
1415
config: ResolvedConfig,
15-
): Promise<Set<string>> {
16-
const fileNames = await recursiveReaddir(config.publicDir)
16+
): Promise<Set<string> | undefined> {
17+
let fileNames: string[]
18+
try {
19+
fileNames = await recursiveReaddir(config.publicDir)
20+
} catch (e) {
21+
if (e.code === ERR_SYMLINK_IN_RECURSIVE_READDIR) {
22+
return
23+
}
24+
throw e
25+
}
1726
const publicFiles = new Set(
1827
fileNames.map((fileName) => fileName.slice(config.publicDir.length)),
1928
)

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,7 @@ export async function _createServer(
645645
file = normalizePath(file)
646646
await container.watchChange(file, { event: isUnlink ? 'delete' : 'create' })
647647

648-
if (config.publicDir && file.startsWith(config.publicDir)) {
648+
if (publicFiles && config.publicDir && file.startsWith(config.publicDir)) {
649649
publicFiles[isUnlink ? 'delete' : 'add'](
650650
file.slice(config.publicDir.length),
651651
)

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ const sirvOptions = ({
5454

5555
export function servePublicMiddleware(
5656
server: ViteDevServer,
57-
publicFiles: Set<string>,
57+
publicFiles?: Set<string>,
5858
): Connect.NextHandleFunction {
5959
const dir = server.config.publicDir
6060
const serve = sirv(
@@ -82,7 +82,7 @@ export function servePublicMiddleware(
8282
// in-memory set of known public files. This set is updated on restarts.
8383
// also skip import request and internal requests `/@fs/ /@vite-client` etc...
8484
if (
85-
!publicFiles.has(toFilePath(req.url!)) ||
85+
(publicFiles && !publicFiles.has(toFilePath(req.url!))) ||
8686
isImportRequest(req.url!) ||
8787
isInternalRequest(req.url!)
8888
) {

packages/vite/src/node/utils.ts

+9
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,8 @@ export function copyDir(srcDir: string, destDir: string): void {
623623
}
624624
}
625625

626+
export const ERR_SYMLINK_IN_RECURSIVE_READDIR =
627+
'ERR_SYMLINK_IN_RECURSIVE_READDIR'
626628
export async function recursiveReaddir(dir: string): Promise<string[]> {
627629
if (!fs.existsSync(dir)) {
628630
return []
@@ -637,6 +639,13 @@ export async function recursiveReaddir(dir: string): Promise<string[]> {
637639
}
638640
throw e
639641
}
642+
if (dirents.some((dirent) => dirent.isSymbolicLink())) {
643+
const err: any = new Error(
644+
'Symbolic links are not supported in recursiveReaddir',
645+
)
646+
err.code = ERR_SYMLINK_IN_RECURSIVE_READDIR
647+
throw err
648+
}
640649
const files = await Promise.all(
641650
dirents.map((dirent) => {
642651
const res = path.resolve(dir, dirent.name)

0 commit comments

Comments
 (0)