Skip to content

Commit

Permalink
refactor!: rename server.fsServe to server.fs (#3965)
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu authored Jun 26, 2021
1 parent 480ee13 commit 5551dff
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 20 deletions.
8 changes: 4 additions & 4 deletions docs/config/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -488,20 +488,20 @@ async function createServer() {
createServer()
```

### server.fsServe.strict
### server.fs.strict

- **Experimental**
- **Type:** `boolean`
- **Default:** `false` (will change to `true` in future versions)

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).

Expand All @@ -514,7 +514,7 @@ createServer()
```js
export default {
server: {
fsServe: {
fs: {
// Allow serving files from one level up to the project root
allow: [
'..'
Expand Down
2 changes: 1 addition & 1 deletion packages/playground/fs-serve/root/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const path = require('path')
*/
module.exports = {
server: {
fsServe: {
fs: {
root: __dirname,
strict: true
},
Expand Down
14 changes: 7 additions & 7 deletions packages/vite/src/node/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,11 @@ export interface ServerOptions {
/**
* Options for files served via '/\@fs/'.
*/
fsServe?: FileSystemServeOptions
fs?: FileSystemServeOptions
}

export interface ResolvedServerOptions extends ServerOptions {
fsServe: Required<Omit<FileSystemServeOptions, 'root'>>
fs: Required<Omit<FileSystemServeOptions, 'root'>>
}

export interface FileSystemServeOptions {
Expand All @@ -148,7 +148,7 @@ export interface FileSystemServeOptions {
/**
*
* @expiremental
* @deprecated use `fsServe.allow` instead
* @deprecated use `fs.allow` instead
*/
root?: string

Expand Down Expand Up @@ -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))

Expand All @@ -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
Expand Down
14 changes: 7 additions & 7 deletions packages/vite/src/node/server/middlewares/static.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, '')
Expand All @@ -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
}
Expand All @@ -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.`
)
}
}
2 changes: 1 addition & 1 deletion scripts/jestPerTestSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ beforeAll(async () => {
interval: 100
},
host: true,
fsServe: {
fs: {
strict: !isBuildTest
}
},
Expand Down

0 comments on commit 5551dff

Please sign in to comment.