Skip to content

Commit

Permalink
fix: symbolic links in public dir (#15264)
Browse files Browse the repository at this point in the history
  • Loading branch information
patak-dev committed Dec 6, 2023
1 parent 8ad81b4 commit ef2a024
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
13 changes: 11 additions & 2 deletions packages/vite/src/node/publicDir.ts
Expand Up @@ -2,6 +2,7 @@ import fs from 'node:fs'
import path from 'node:path'
import type { ResolvedConfig } from './config'
import {
ERR_SYMLINK_IN_RECURSIVE_READDIR,
cleanUrl,
normalizePath,
recursiveReaddir,
Expand All @@ -12,8 +13,16 @@ const publicFilesMap = new WeakMap<ResolvedConfig, Set<string>>()

export async function initPublicFiles(
config: ResolvedConfig,
): Promise<Set<string>> {
const fileNames = await recursiveReaddir(config.publicDir)
): Promise<Set<string> | undefined> {
let fileNames: string[]
try {
fileNames = await recursiveReaddir(config.publicDir)
} catch (e) {
if (e.code === ERR_SYMLINK_IN_RECURSIVE_READDIR) {
return
}
throw e
}
const publicFiles = new Set(
fileNames.map((fileName) => fileName.slice(config.publicDir.length)),
)
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/server/index.ts
Expand Up @@ -645,7 +645,7 @@ export async function _createServer(
file = normalizePath(file)
await container.watchChange(file, { event: isUnlink ? 'delete' : 'create' })

if (config.publicDir && file.startsWith(config.publicDir)) {
if (publicFiles && config.publicDir && file.startsWith(config.publicDir)) {
publicFiles[isUnlink ? 'delete' : 'add'](
file.slice(config.publicDir.length),
)
Expand Down
4 changes: 2 additions & 2 deletions packages/vite/src/node/server/middlewares/static.ts
Expand Up @@ -54,7 +54,7 @@ const sirvOptions = ({

export function servePublicMiddleware(
server: ViteDevServer,
publicFiles: Set<string>,
publicFiles?: Set<string>,
): Connect.NextHandleFunction {
const dir = server.config.publicDir
const serve = sirv(
Expand Down Expand Up @@ -82,7 +82,7 @@ export function servePublicMiddleware(
// in-memory set of known public files. This set is updated on restarts.
// also skip import request and internal requests `/@fs/ /@vite-client` etc...
if (
!publicFiles.has(toFilePath(req.url!)) ||
(publicFiles && !publicFiles.has(toFilePath(req.url!))) ||
isImportRequest(req.url!) ||
isInternalRequest(req.url!)
) {
Expand Down
9 changes: 9 additions & 0 deletions packages/vite/src/node/utils.ts
Expand Up @@ -623,6 +623,8 @@ export function copyDir(srcDir: string, destDir: string): void {
}
}

export const ERR_SYMLINK_IN_RECURSIVE_READDIR =
'ERR_SYMLINK_IN_RECURSIVE_READDIR'
export async function recursiveReaddir(dir: string): Promise<string[]> {
if (!fs.existsSync(dir)) {
return []
Expand All @@ -637,6 +639,13 @@ export async function recursiveReaddir(dir: string): Promise<string[]> {
}
throw e
}
if (dirents.some((dirent) => dirent.isSymbolicLink())) {
const err: any = new Error(
'Symbolic links are not supported in recursiveReaddir',
)
err.code = ERR_SYMLINK_IN_RECURSIVE_READDIR
throw err
}
const files = await Promise.all(
dirents.map((dirent) => {
const res = path.resolve(dir, dirent.name)
Expand Down

0 comments on commit ef2a024

Please sign in to comment.