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

Conversation

antfu
Copy link
Member

@antfu antfu commented Apr 4, 2021

Description

fix #2820

  • new options serve.fsServeRoot to prevent serving entire fs
  • auto detect workspace root as fsServeRoot
  • add tests
  • test on Windows
  • update docs

Additional context


What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

Before submitting the PR, please make sure you do the following

  • Read the Contributing Guidelines.
  • Read the Pull Request Guidelines and follow the Commit Convention.
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • Ideally, include relevant tests that fail without this PR but pass with it.

@antfu antfu changed the title fix: new options fsServeRoot to prevent serving unrestricted files (f… fix: new options fsServeRoot to prevent serving unrestricted files (f… Apr 4, 2021
@antfu antfu added p5-urgent Fix build-breaking bugs affecting most users, should be released ASAP (priority) security labels Apr 4, 2021
@antfu antfu changed the title fix: new options fsServeRoot to prevent serving unrestricted files (f… fix!: prevent serving unrestricted files (fix #2820) Apr 4, 2021
antfu and others added 2 commits April 4, 2021 18:35
@antfu antfu marked this pull request as ready for review April 4, 2021 12:08
@patak-dev
Copy link
Member

Thanks for pushing for this change @antfu. Some initial thoughts.

@marvinhagemeister said they are planning to support an array of folders, instead of only one fsServeRoot. #2820 (comment). I don't know exactly what would be the use case for this? Maybe in a monorepo, you only would like to allow for a few internal packages and not all of them?

I think that it would be good to have an option to support common monorepos, maybe behind an option. But this to me should be pushed to another PR, as it is not related to the security issue at hand.

docs/config/index.md Outdated Show resolved Hide resolved
packages/vite/src/node/server/index.ts Outdated Show resolved Hide resolved
Shinigami92
Shinigami92 previously approved these changes Apr 4, 2021
@patak-dev
Copy link
Member

About support for an array of serve roots, that could be added later if needed and we can merge this PR without it. We could think if fsServeRoot is a good name for string | string[] or if we should name it fsServeRoots to match other options like entries (that can also be string | string[]). I don't have a strong position on this one.

Co-authored-by: patak <matias.capeletto@gmail.com>
@patak-dev
Copy link
Member

@antfu tests are failing because of missing imports from utils normalizePath and fsPathFromId, GitHub didn't allow me to add them in the suggestion

@antfu
Copy link
Member Author

antfu commented Apr 5, 2021

Added

patak-dev
patak-dev previously approved these changes Apr 5, 2021
Shinigami92
Shinigami92 previously approved these changes Apr 5, 2021
@antfu antfu marked this pull request as draft April 10, 2021 01:45
@antfu
Copy link
Member Author

antfu commented Apr 10, 2021

Converting to draft for now as we need to have some rework on this to provide better DX for users to migrate this change.

@antfu antfu added p4-important Violate documented behavior or significantly improves performance (priority) and removed p5-urgent Fix build-breaking bugs affecting most users, should be released ASAP (priority) labels Apr 10, 2021
@patak-dev
Copy link
Member

Tested locally on Windows and is working as expected 👍🏼

Co-authored-by: patak <matias.capeletto@gmail.com>
antfu and others added 2 commits May 6, 2021 02:48
patak-dev
patak-dev previously approved these changes May 7, 2021
Shinigami92
Shinigami92 previously approved these changes May 7, 2021
Copy link
Member

@Shinigami92 Shinigami92 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thoughts (non-blocking): Okay, so if understand everything correct, we are currently provide a new option to set the root and no upper files are allowed to be accessed.

In Mono-Repos we have some strategies to find the root automatically, but for non-Mono-Repos we don't have such.

But the fsServeRoot is optional and therefore this fix doesn't apply at all for non-Mono-Repos, does it?
If so, I would highly suggest to search for just e.g. package.json or .git and let the user/developer define its own fsServeRoot to override it and give the responsibility to them (the user).
So we are strict, but it's possible to relax the strictness.

What are your thoughts about this?

@antfu
Copy link
Member Author

antfu commented May 7, 2021

@Shinigami92 Sorry wdym? For non-monorepos, it's limited to the project root.

@Shinigami92
Copy link
Member

@Shinigami92 Sorry wdym? For non-monorepos, it's limited to the project root.

Ah, so this is this: [...] otherwise will fallback to the [project root](/guide/#index-html-and-project-root)

Okay 🙂

@antfu antfu dismissed stale reviews from Shinigami92 and patak-dev via f771868 May 7, 2021 08:15
* Accepts absolute path or a path relative to project root.
* Will try to search up for workspace root by default.
*/
root?: string
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussion with @patak-js, we are likely to add more options to configure /@fs/ to enhance further security in the future. For example, disabling dotfiles (.env) by default (e.g. sirv). Changed this to an object for further extensions.

@antfu antfu changed the title fix!: prevent serving unrestricted files (fix #2820) fix: prevent serving unrestricted files (fix #2820) May 7, 2021
@antfu antfu merged commit 792a6e1 into vitejs:main May 7, 2021
@antfu antfu deleted the fix/fs-root branch May 7, 2021 10:36
ElMassimo added a commit to ElMassimo/vite_ruby that referenced this pull request May 10, 2021
…ng Vite (> 2.2.4)

Although it shouldn't cause problems for most Vite Ruby projects, after
vitejs/vite#2850 files outside the Vite root
would not be served by the dev server middleware.

By explicitly setting `server.fsServe.root`, we ensure that every file
in the project can be served raw if needed.
fi3ework pushed a commit to fi3ework/vite that referenced this pull request May 22, 2021
Co-authored-by: Shinigami <chrissi92@hotmail.de>
Co-authored-by: patak <matias.capeletto@gmail.com>
@@ -4,7 +4,7 @@ import { join } from 'path'

// https://github.com/vitejs/vite/issues/2820#issuecomment-812495079
const ROOT_FILES = [
'.git',
// '.git',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the explanation for this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p4-important Violate documented behavior or significantly improves performance (priority) security
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unrestricted directory traversal with @fs
7 participants