Skip to content

Commit

Permalink
chore: change options to fsServe object
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed May 7, 2021
1 parent 099ce97 commit f771868
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
2 changes: 1 addition & 1 deletion docs/config/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ export default async ({ command, mode }) => {

File system watcher options to pass on to [chokidar](https://github.com/paulmillr/chokidar#api).

### server.fsServeRoot
### server.fsServe.root

- **Type:** `string`

Expand Down
12 changes: 9 additions & 3 deletions packages/vite/src/node/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,19 @@ export interface ServerOptions {
*/
base?: string
/**
* Restrict files that could be served via '/\@fs/'.
* Accessing files outside this directory will result in a 403.
* Options for files served via '/\@fs/'.
*/
fsServe?: FileSystemServeOptions
}

export interface FileSystemServeOptions {
/**
* Restrict accessing files outside this directory will result in a 403.
*
* Accepts absolute path or a path relative to project root.
* Will try to search up for workspace root by default.
*/
fsServeRoot?: string
root?: string
}

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/vite/src/node/server/middlewares/static.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export function serveRawFsMiddleware(
const serveFromRoot = sirv('/', sirvOptions)
const serveRoot = path.resolve(
config.root,
config.server?.fsServeRoot || searchForWorkspaceRoot(config.root)
config.server?.fsServe?.root || searchForWorkspaceRoot(config.root)
)

return (req, res, next) => {
Expand All @@ -90,7 +90,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 `fsServeRoot`
// restrict files outside of `fsServe.root`
if (!path.resolve(fsPathFromId(url)).startsWith(serveRoot + path.sep)) {
res.statusCode = 403
res.write(renderFsRestrictedHTML(serveRoot))
Expand Down

0 comments on commit f771868

Please sign in to comment.