Commit 3e923d05 authored by JJ Kasper's avatar JJ Kasper Committed by Joe Haddad

Fix `public/` file name encoding (#10022)

* Update serving of files from public folder to handle special chars

* Update tests and match handling in dev mode

* Fix windows public file handling

* Remove colon test to make git on windows happy

* Remove un-used files

* Add missing await
parent b90289fc
...@@ -647,30 +647,35 @@ export default class Server { ...@@ -647,30 +647,35 @@ export default class Server {
} }
protected generatePublicRoutes(): Route[] { protected generatePublicRoutes(): Route[] {
const routes: Route[] = [] const publicFiles = new Set(
const publicFiles = recursiveReadDirSync(this.publicDir) recursiveReadDirSync(this.publicDir).map(p => p.replace(/\\/g, '/'))
)
publicFiles.forEach(path => {
const unixPath = path.replace(/\\/g, '/') return [
// Only include public files that will not replace a page path {
// this should not occur now that we check this during build match: route('/:path*'),
if (!this.pagesManifest![unixPath]) { name: 'public folder catchall',
routes.push({ fn: async (req, res, params, parsedUrl) => {
match: route(unixPath), const path = `/${(params.path || []).join('/')}`
type: 'route',
name: 'public catchall', if (publicFiles.has(path)) {
fn: async (req, res, _params, parsedUrl) => { await this.serveStatic(
const p = join(this.publicDir, unixPath) req,
await this.serveStatic(req, res, p, parsedUrl) res,
// we need to re-encode it since send decodes it
join(this.dir, 'public', encodeURIComponent(path)),
parsedUrl
)
return { return {
finished: true, finished: true,
} }
}, }
}) return {
} finished: false,
}) }
},
return routes } as Route,
]
} }
protected getDynamicRoutes() { protected getDynamicRoutes() {
......
...@@ -258,14 +258,14 @@ export default class DevServer extends Server { ...@@ -258,14 +258,14 @@ export default class DevServer extends Server {
protected async _beforeCatchAllRender( protected async _beforeCatchAllRender(
req: IncomingMessage, req: IncomingMessage,
res: ServerResponse, res: ServerResponse,
_params: Params, params: Params,
parsedUrl: UrlWithParsedQuery parsedUrl: UrlWithParsedQuery
) { ) {
const { pathname } = parsedUrl const { pathname } = parsedUrl
const path = `/${(params.path || []).join('/')}`
// check for a public file, throwing error if there's a // check for a public file, throwing error if there's a
// conflicting page // conflicting page
if (await this.hasPublicFile(pathname!)) { if (await this.hasPublicFile(path)) {
if (await this.hasPage(pathname!)) { if (await this.hasPage(pathname!)) {
const err = new Error( const err = new Error(
`A conflicting public file and page file was found for path ${pathname} https://err.sh/zeit/next.js/conflicting-public-file-page` `A conflicting public file and page file was found for path ${pathname} https://err.sh/zeit/next.js/conflicting-public-file-page`
...@@ -274,7 +274,7 @@ export default class DevServer extends Server { ...@@ -274,7 +274,7 @@ export default class DevServer extends Server {
await this.renderError(err, req, res, pathname!, {}) await this.renderError(err, req, res, pathname!, {})
return true return true
} }
await this.servePublic(req, res, pathname!) await this.servePublic(req, res, path)
return true return true
} }
...@@ -484,7 +484,8 @@ export default class DevServer extends Server { ...@@ -484,7 +484,8 @@ export default class DevServer extends Server {
} }
servePublic(req: IncomingMessage, res: ServerResponse, path: string) { servePublic(req: IncomingMessage, res: ServerResponse, path: string) {
const p = join(this.publicDir, path) const p = join(this.publicDir, encodeURIComponent(path))
// we need to re-encode it since send decodes it
return this.serveStatic(req, res, p) return this.serveStatic(req, res, p)
} }
......
hello world copy
\ No newline at end of file
hello world %20
\ No newline at end of file
hello world +
\ No newline at end of file
...@@ -350,6 +350,34 @@ function runTests(dev) { ...@@ -350,6 +350,34 @@ function runTests(dev) {
expect(data).toMatch(/hello world/) expect(data).toMatch(/hello world/)
}) })
it('should serve file with space from public folder', async () => {
const res = await fetchViaHTTP(appPort, '/hello copy.txt')
const text = (await res.text()).trim()
expect(text).toBe('hello world copy')
expect(res.status).toBe(200)
})
it('should serve file with plus from public folder', async () => {
const res = await fetchViaHTTP(appPort, '/hello+copy.txt')
const text = (await res.text()).trim()
expect(text).toBe('hello world +')
expect(res.status).toBe(200)
})
it('should serve file from public folder encoded', async () => {
const res = await fetchViaHTTP(appPort, '/hello%20copy.txt')
const text = (await res.text()).trim()
expect(text).toBe('hello world copy')
expect(res.status).toBe(200)
})
it('should serve file with %20 from public folder', async () => {
const res = await fetchViaHTTP(appPort, '/hello%2520copy.txt')
const text = (await res.text()).trim()
expect(text).toBe('hello world %20')
expect(res.status).toBe(200)
})
if (dev) { if (dev) {
it('should work with HMR correctly', async () => { it('should work with HMR correctly', async () => {
const browser = await webdriver(appPort, '/post-1/comments') const browser = await webdriver(appPort, '/post-1/comments')
...@@ -389,6 +417,10 @@ function runTests(dev) { ...@@ -389,6 +417,10 @@ function runTests(dev) {
join(appDir, '.next/routes-manifest.json') join(appDir, '.next/routes-manifest.json')
) )
for (const route of manifest.dynamicRoutes) {
route.regex = normalizeRegEx(route.regex)
}
expect(manifest).toEqual({ expect(manifest).toEqual({
version: 1, version: 1,
basePath: '', basePath: '',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment