Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: symbolic links in public dir #15264

Merged
merged 1 commit into from Dec 6, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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