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: don't resolve URL starting with double slash #19059

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
11 changes: 7 additions & 4 deletions packages/vite/src/node/server/middlewares/static.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,15 @@ export function serveStaticMiddleware(
if (
cleanedUrl[cleanedUrl.length - 1] === '/' ||
path.extname(cleanedUrl) === '.html' ||
isInternalRequest(req.url!)
isInternalRequest(req.url!) ||
// skip url starting with // as these will be interpreted as
// scheme relative URLs by new URL() and will not be a valid file path
req.url?.startsWith('//')
) {
return next()
}

const url = new URL(req.url!.replace(/^\/{2,}/, '/'), 'http://example.com')
const url = new URL(req.url!, 'http://example.com')
const pathname = decodeURI(url.pathname)

// apply aliases to static requests as well
Expand Down Expand Up @@ -177,12 +180,12 @@ export function serveRawFsMiddleware(

// Keep the named function. The name is visible in debug logs via `DEBUG=connect:dispatcher ...`
return function viteServeRawFsMiddleware(req, res, next) {
const url = new URL(req.url!.replace(/^\/{2,}/, '/'), 'http://example.com')
// In some cases (e.g. linked monorepos) files outside of root will
// reference assets that are also out of served root. In such cases
// the paths are rewritten to `/@fs/` prefixed paths and must be served by
// searching based from fs root.
if (url.pathname.startsWith(FS_PREFIX)) {
if (req.url!.startsWith(FS_PREFIX)) {
const url = new URL(req.url!, 'http://example.com')
const pathname = decodeURI(url.pathname)
// restrict files outside of `fs.allow`
if (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ if (!isBuild) {

test.runIf(!isBuild)('denied .env', async () => {
expect(await page.textContent('.unsafe-dotenv')).toBe('403')
expect(await page.textContent('.unsafe-dotenv-double-slash')).toBe('403')
expect(await page.textContent('.unsafe-dotenv-double-slash')).toBe('200') // SPA fallback
})
Loading