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: prevent serving unrestricted files (fix #2820) #2850

Merged
merged 24 commits into from
May 7, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
chore: update docs
  • Loading branch information
antfu committed Apr 4, 2021
commit f47345b21a86f45defd84ecfe2214d759e0b8774
8 changes: 8 additions & 0 deletions docs/config/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,14 @@ export default async ({ command, mode }) => {

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

### server.fsServeRoot

- **Type:** `string`

Restrict files that could be served via `/@fs/`. Acessing files outside this directory will result a 403.
Shinigami92 marked this conversation as resolved.
Show resolved Hide resolved

Default to [project root](/guide/#index-html-and-project-root).

## Build Options

### build.target
Expand Down
4 changes: 3 additions & 1 deletion packages/vite/src/node/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,10 @@ export interface ServerOptions {
base?: string
/**
* Restrict files that could be served via '/\@fs/'.
* Acessing files outside this directory will result a 403.
Shinigami92 marked this conversation as resolved.
Show resolved Hide resolved
*
* Default to the project root (`config.root`).
* Accepts absolute path or a path relative to project root.
* Default to the project root.
*/
fsServeRoot?: string
antfu marked this conversation as resolved.
Show resolved Hide resolved
}
Expand Down
5 changes: 4 additions & 1 deletion packages/vite/src/node/server/middlewares/static.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,10 @@ export function serveRawFsMiddleware(
): Connect.NextHandleFunction {
const isWin = os.platform() === 'win32'
const serveFromRoot = sirv('/', sirvOptions)
const root = config.server?.fsServeRoot || config.root
const root = path.resolve(
config.root,
config.server?.fsServeRoot || config.root
)

return (req, res, next) => {
let url = req.url!
Expand Down