Skip to content

Commit

Permalink
fix: decode url before serving static files (#2201)
Browse files Browse the repository at this point in the history
close #2195
  • Loading branch information
CHOYSEN committed Feb 26, 2021
1 parent ea323cc commit 1342108
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 8 deletions.
3 changes: 3 additions & 0 deletions packages/playground/assets/index.html
Expand Up @@ -13,6 +13,9 @@ <h2>Raw References from publicDir</h2>
<li class="raw-css">
Raw CSS from publicDir should load (this should be red)
</li>
<li>
<img src="/white space.png" />
</li>
</ul>

<h2>Asset Imports from JS</h2>
Expand Down
Binary file added packages/playground/assets/static/white space.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions packages/vite/src/node/server/index.ts
Expand Up @@ -25,6 +25,7 @@ import {
indexHtmlMiddleware
} from './middlewares/indexHtml'
import history from 'connect-history-api-fallback'
import { decodeURIMiddleware } from './middlewares/decodeURI'
import {
serveRawFsMiddleware,
servePublicMiddleware,
Expand Down Expand Up @@ -416,6 +417,9 @@ export async function createServer(
// hmr reconnect ping
middlewares.use('/__vite_ping', (_, res) => res.end('pong'))

//decode request url
middlewares.use(decodeURIMiddleware())

// serve static files under /public
// this applies before the transform middleware so that these files are served
// as-is without transforms.
Expand Down
18 changes: 18 additions & 0 deletions packages/vite/src/node/server/middlewares/decodeURI.ts
@@ -0,0 +1,18 @@
import { Connect } from 'types/connect'

export function decodeURIMiddleware(): Connect.NextHandleFunction {
return (req, _, next) => {
// #2195
req.url = decodeURI(req.url!)

// `sirv` middleware uses the req._parsedUrl values to find the file,
// so decode it all together.
// @ts-ignore
const parsedUrl = req._parsedUrl
for (const key of Object.keys(parsedUrl)) {
const val = parsedUrl[key]
if (val) parsedUrl[key] = decodeURI(val)
}
next()
}
}
4 changes: 0 additions & 4 deletions packages/vite/src/node/server/middlewares/static.ts
Expand Up @@ -36,9 +36,6 @@ export function serveStaticMiddleware(
return next()
}

// #1426
url = req.url = decodeURI(url)

// apply aliases to static requests as well
let redirected: string | undefined
for (const { find, replacement } of config.resolve.alias) {
Expand Down Expand Up @@ -74,7 +71,6 @@ export function serveRawFsMiddleware(): Connect.NextHandleFunction {
if (url.startsWith(FS_PREFIX)) {
url = url.slice(FS_PREFIX.length)
if (isWin) url = url.replace(/^[A-Z]:/i, '')
req.url = decodeURI(url)
serveFromRoot(req, res, next)
} else {
next()
Expand Down
5 changes: 1 addition & 4 deletions packages/vite/src/node/server/middlewares/transform.ts
Expand Up @@ -54,10 +54,7 @@ export function transformMiddleware(

let url
try {
url = decodeURI(removeTimestampQuery(req.url!)).replace(
NULL_BYTE_PLACEHOLDER,
'\0'
)
url = removeTimestampQuery(req.url!).replace(NULL_BYTE_PLACEHOLDER, '\0')
} catch (err) {
// if it starts with %PUBLIC%, someone's migrating from something
// like create-react-app
Expand Down

0 comments on commit 1342108

Please sign in to comment.