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 5 commits
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
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/`. Accessing files outside this directory will result in a 403.

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

## Build Options

### build.target
Expand Down
10 changes: 9 additions & 1 deletion packages/vite/src/node/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,14 @@ export interface ServerOptions {
* Should start and end with the `/` character
*/
base?: string
/**
* Restrict files that could be served via '/\@fs/'.
* Accessing files outside this directory will result in a 403.
*
* 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 Expand Up @@ -438,7 +446,7 @@ export async function createServer(
middlewares.use(transformMiddleware(server))

// serve static files
middlewares.use(serveRawFsMiddleware())
middlewares.use(serveRawFsMiddleware(config))
middlewares.use(serveStaticMiddleware(root, config))

// spa fallback
Expand Down
20 changes: 19 additions & 1 deletion packages/vite/src/node/server/middlewares/static.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,15 @@ export function serveStaticMiddleware(
}
}

export function serveRawFsMiddleware(): Connect.NextHandleFunction {
export function serveRawFsMiddleware(
config: ResolvedConfig
): Connect.NextHandleFunction {
const isWin = os.platform() === 'win32'
const serveFromRoot = sirv('/', sirvOptions)
const root = path.resolve(
config.root,
config.server?.fsServeRoot || config.root
)

return (req, res, next) => {
let url = req.url!
Expand All @@ -84,6 +90,18 @@ export function serveRawFsMiddleware(): Connect.NextHandleFunction {
// searching based from fs root.
if (url.startsWith(FS_PREFIX)) {
url = url.slice(FS_PREFIX.length)

// restrict files outside of `fsServeRoot`
if (
path
.relative(root, path.isAbsolute(url) ? url : `/${url}`)
.startsWith('../')
) {
res.statusCode = 403
res.end()
return
}

antfu marked this conversation as resolved.
Show resolved Hide resolved
if (isWin) url = url.replace(/^[A-Z]:/i, '')

req.url = url
Expand Down