From 5551dff351f77d6ba670c77b707446840067fa1c Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Sat, 26 Jun 2021 13:19:52 +0800 Subject: [PATCH] refactor!: rename `server.fsServe` to `server.fs` (#3965) --- docs/config/index.md | 8 ++++---- packages/playground/fs-serve/root/vite.config.js | 2 +- packages/vite/src/node/server/index.ts | 14 +++++++------- .../vite/src/node/server/middlewares/static.ts | 14 +++++++------- scripts/jestPerTestSetup.ts | 2 +- 5 files changed, 20 insertions(+), 20 deletions(-) diff --git a/docs/config/index.md b/docs/config/index.md index b9e57636e2f462..b287ab9eb88f9c 100644 --- a/docs/config/index.md +++ b/docs/config/index.md @@ -488,7 +488,7 @@ async function createServer() { createServer() ``` -### server.fsServe.strict +### server.fs.strict - **Experimental** - **Type:** `boolean` @@ -496,12 +496,12 @@ createServer() Restrict serving files outside of workspace root. -### server.fsServe.allow +### server.fs.allow - **Experimental** - **Type:** `string[]` - Restrict files that could be served via `/@fs/`. When `server.fsServe.strict` is set to `true`, accessing files outside this directory list will result in a 403. + Restrict files that could be served via `/@fs/`. When `server.fs.strict` is set to `true`, accessing files outside this directory list will result in a 403. Vite will search for the root of the potential workspace and use it as default. A valid workspace met the following conditions, otherwise will fallback to the [project root](/guide/#index-html-and-project-root). @@ -514,7 +514,7 @@ createServer() ```js export default { server: { - fsServe: { + fs: { // Allow serving files from one level up to the project root allow: [ '..' diff --git a/packages/playground/fs-serve/root/vite.config.js b/packages/playground/fs-serve/root/vite.config.js index 70356a8d82b5fb..5da35ef3d354c1 100644 --- a/packages/playground/fs-serve/root/vite.config.js +++ b/packages/playground/fs-serve/root/vite.config.js @@ -5,7 +5,7 @@ const path = require('path') */ module.exports = { server: { - fsServe: { + fs: { root: __dirname, strict: true }, diff --git a/packages/vite/src/node/server/index.ts b/packages/vite/src/node/server/index.ts index ceefe975959f89..dd85a102535523 100644 --- a/packages/vite/src/node/server/index.ts +++ b/packages/vite/src/node/server/index.ts @@ -126,11 +126,11 @@ export interface ServerOptions { /** * Options for files served via '/\@fs/'. */ - fsServe?: FileSystemServeOptions + fs?: FileSystemServeOptions } export interface ResolvedServerOptions extends ServerOptions { - fsServe: Required> + fs: Required> } export interface FileSystemServeOptions { @@ -148,7 +148,7 @@ export interface FileSystemServeOptions { /** * * @expiremental - * @deprecated use `fsServe.allow` instead + * @deprecated use `fs.allow` instead */ root?: string @@ -717,10 +717,10 @@ export function resolveServerOptions( raw?: ServerOptions ): ResolvedServerOptions { const server = raw || {} - let allowDirs = server.fsServe?.allow + let allowDirs = server.fs?.allow if (!allowDirs) { - allowDirs = [server.fsServe?.root || searchForWorkspaceRoot(root)] + allowDirs = [server.fs?.root || searchForWorkspaceRoot(root)] } allowDirs = allowDirs.map((i) => resolvedAllowDir(root, i)) @@ -730,9 +730,9 @@ export function resolveServerOptions( allowDirs.push(resolvedClientDir) } - server.fsServe = { + server.fs = { // TODO: make strict by default - strict: server.fsServe?.strict, + strict: server.fs?.strict, allow: allowDirs } return server as ResolvedServerOptions diff --git a/packages/vite/src/node/server/middlewares/static.ts b/packages/vite/src/node/server/middlewares/static.ts index f386edc581a15b..3ee25b0a997ddf 100644 --- a/packages/vite/src/node/server/middlewares/static.ts +++ b/packages/vite/src/node/server/middlewares/static.ts @@ -94,7 +94,7 @@ export function serveRawFsMiddleware( // the paths are rewritten to `/@fs/` prefixed paths and must be served by // searching based from fs root. if (url.startsWith(FS_PREFIX)) { - // restrict files outside of `fsServe.root` + // restrict files outside of `fs.root` ensureServingAccess(slash(path.resolve(fsPathFromId(url))), server) url = url.slice(FS_PREFIX.length) if (isWindows) url = url.replace(/^[A-Z]:/i, '') @@ -112,21 +112,21 @@ export function isFileServingAllowed( server: ViteDevServer ): boolean { // explicitly disabled - if (server.config.server.fsServe.strict === false) return true + if (server.config.server.fs.strict === false) return true const file = ensureLeadingSlash(normalizePath(cleanUrl(url))) if (server.moduleGraph.safeModulesPath.has(file)) return true - if (server.config.server.fsServe.allow.some((i) => file.startsWith(i + '/'))) + if (server.config.server.fs.allow.some((i) => file.startsWith(i + '/'))) return true - if (!server.config.server.fsServe.strict) { + if (!server.config.server.fs.strict) { server.config.logger.warnOnce(`Unrestricted file system access to "${url}"`) server.config.logger.warnOnce( `For security concerns, accessing files outside of serving allow list will ` + `be restricted by default in the future version of Vite. ` + - `Refer to https://vitejs.dev/config/#server-fsserve-allow for more details.` + `Refer to https://vitejs.dev/config/#server-fs-allow for more details.` ) return true } @@ -136,13 +136,13 @@ export function isFileServingAllowed( export function ensureServingAccess(url: string, server: ViteDevServer): void { if (!isFileServingAllowed(url, server)) { - const allow = server.config.server.fsServe.allow + const allow = server.config.server.fs.allow throw new AccessRestrictedError( `The request url "${url}" is outside of Vite serving allow list: ${allow.map((i) => `- ${i}`).join('\n')} -Refer to docs https://vitejs.dev/config/#server-fsserve-allow for configurations and more details.` +Refer to docs https://vitejs.dev/config/#server-fs-allow for configurations and more details.` ) } } diff --git a/scripts/jestPerTestSetup.ts b/scripts/jestPerTestSetup.ts index 620309694f7893..206f5a4970091c 100644 --- a/scripts/jestPerTestSetup.ts +++ b/scripts/jestPerTestSetup.ts @@ -94,7 +94,7 @@ beforeAll(async () => { interval: 100 }, host: true, - fsServe: { + fs: { strict: !isBuildTest } },