Unverified Commit 92304404 authored by Jan Potoms's avatar Jan Potoms Committed by GitHub

Disallow basePath: false for internal routes (#15837)

parent 35e4a370
# Invalid External Rewrite
#### Why This Error Occurred
A rewrite was defined with both `basePath: false` and an internal `destination`. Rewrites that capture urls outside of the `basePath` must route externally, as they are intended for proxying in the case of incremental adoption of Next.js in a project.
#### Possible Ways to Fix It
Look for any rewrite where `basePath` is `false` and make sure its `destination` starts with `http://` or `https://`.
### Useful Links
- [Rewrites section in Documentation](https://nextjs.org/docs/api-reference/next.config.js/rewrites)
...@@ -170,6 +170,23 @@ function checkCustomRoutes( ...@@ -170,6 +170,23 @@ function checkCustomRoutes(
continue continue
} }
if (
type === 'rewrite' &&
(route as Rewrite).basePath === false &&
!(
(route as Rewrite).destination.startsWith('http://') ||
(route as Rewrite).destination.startsWith('https://')
)
) {
console.error(
`The route ${
(route as Rewrite).source
} rewrites urls outside of the basePath. Please use a destination that starts with \`http://\` or \`https://\` https://err.sh/vercel/next.js/invalid-external-rewrite.md`
)
numInvalidRoutes++
continue
}
const keys = Object.keys(route) const keys = Object.keys(route)
const invalidKeys = keys.filter((key) => !allowedKeys.has(key)) const invalidKeys = keys.filter((key) => !allowedKeys.has(key))
const invalidParts: string[] = [] const invalidParts: string[] = []
......
<html>
<body>
hello from external
</body>
</html>
...@@ -13,7 +13,7 @@ module.exports = { ...@@ -13,7 +13,7 @@ module.exports = {
}, },
{ {
source: '/rewrite-no-basepath', source: '/rewrite-no-basepath',
destination: '/gssp', destination: 'http://localhost:__EXTERNAL_APP_PORT__/page.html',
basePath: false, basePath: false,
}, },
{ {
......
/* eslint-env jest */ /* eslint-env jest */
import webdriver from 'next-webdriver' import webdriver from 'next-webdriver'
import { join } from 'path' import { join, resolve } from 'path'
import url from 'url' import url from 'url'
import { import {
nextServer, nextServer,
...@@ -21,6 +21,7 @@ import { ...@@ -21,6 +21,7 @@ import {
getRedboxSource, getRedboxSource,
hasRedbox, hasRedbox,
fetchViaHTTP, fetchViaHTTP,
startStaticServer,
} from 'next-test-utils' } from 'next-test-utils'
import fs, { import fs, {
readFileSync, readFileSync,
...@@ -34,6 +35,23 @@ jest.setTimeout(1000 * 60 * 2) ...@@ -34,6 +35,23 @@ jest.setTimeout(1000 * 60 * 2)
const appDir = join(__dirname, '..') const appDir = join(__dirname, '..')
let externalApp
const nextConfig = new File(resolve(appDir, 'next.config.js'))
beforeAll(async () => {
externalApp = await startStaticServer(resolve(__dirname, '../external'))
nextConfig.replace(
'__EXTERNAL_APP_PORT__',
String(externalApp.address().port)
)
})
afterAll(async () => {
nextConfig.restore()
externalApp.close()
})
const runTests = (context, dev = false) => { const runTests = (context, dev = false) => {
if (dev) { if (dev) {
it('should render error in dev overlay correctly', async () => { it('should render error in dev overlay correctly', async () => {
...@@ -138,39 +156,7 @@ const runTests = (context, dev = false) => { ...@@ -138,39 +156,7 @@ const runTests = (context, dev = false) => {
it('should rewrite without basePath when set to false', async () => { it('should rewrite without basePath when set to false', async () => {
const html = await renderViaHTTP(context.appPort, '/rewrite-no-basePath') const html = await renderViaHTTP(context.appPort, '/rewrite-no-basePath')
expect(html).toContain('getServerSideProps') expect(html).toContain('hello from external')
})
it('should have correct asPath for rewrite without basePath', async () => {
const browser = await webdriver(context.appPort, '/rewrite-no-basePath')
expect(await browser.eval(() => window.location.pathname)).toBe(
'/rewrite-no-basePath'
)
expect(await browser.eval(() => window.next.router.asPath)).toBe(
'/rewrite-no-basePath'
)
expect(await browser.eval(() => window.next.router.pathname)).toBe('/gssp')
})
it('should have correct asPath for rewrite without basePath on back()', async () => {
const browser = await webdriver(context.appPort, '/rewrite-no-basePath')
await browser.eval(() => (window.navigationMarker = true))
await browser.eval(() => window.next.router.push('/hello'))
await check(
() => browser.eval(() => window.location.pathname),
'/docs/hello'
)
await browser.back()
await check(
() => browser.eval(() => window.location.pathname),
'/rewrite-no-basePath'
)
await check(
() => browser.eval(() => window.next.router.asPath),
'/rewrite-no-basePath'
)
expect(await browser.eval(() => window.next.router.pathname)).toBe('/gssp')
expect(await browser.eval(() => window.navigationMarker)).toBe(true)
}) })
it('should redirect with basePath by default', async () => { it('should redirect with basePath by default', async () => {
...@@ -258,8 +244,9 @@ const runTests = (context, dev = false) => { ...@@ -258,8 +244,9 @@ const runTests = (context, dev = false) => {
) )
}) })
it('should navigating back to a non-basepath 404 that starts with basepath', async () => { it('should navigate back to a non-basepath 404 that starts with basepath', async () => {
const browser = await webdriver(context.appPort, '/docshello') const browser = await webdriver(context.appPort, '/docshello')
await browser.eval(() => (window.navigationMarker = true))
await browser.eval(() => window.next.router.push('/hello')) await browser.eval(() => window.next.router.push('/hello'))
await browser.waitForElementByCss('#pathname') await browser.waitForElementByCss('#pathname')
await browser.back() await browser.back()
...@@ -267,6 +254,7 @@ const runTests = (context, dev = false) => { ...@@ -267,6 +254,7 @@ const runTests = (context, dev = false) => {
expect(await browser.eval(() => window.next.router.asPath)).toBe( expect(await browser.eval(() => window.next.router.asPath)).toBe(
'/docshello' '/docshello'
) )
expect(await browser.eval(() => window.navigationMarker)).toBe(true)
}) })
it('should update dynamic params after mount correctly', async () => { it('should update dynamic params after mount correctly', async () => {
...@@ -1008,7 +996,11 @@ describe('basePath production', () => { ...@@ -1008,7 +996,11 @@ describe('basePath production', () => {
let app let app
beforeAll(async () => { beforeAll(async () => {
await nextBuild(appDir) await nextBuild(appDir, [], {
env: {
EXTERNAL_APP: `http://localhost:${externalApp.address().port}`,
},
})
app = nextServer({ app = nextServer({
dir: join(__dirname, '../'), dir: join(__dirname, '../'),
dev: false, dev: false,
...@@ -1028,8 +1020,6 @@ describe('basePath serverless', () => { ...@@ -1028,8 +1020,6 @@ describe('basePath serverless', () => {
let context = {} let context = {}
let app let app
const nextConfig = new File(join(appDir, 'next.config.js'))
beforeAll(async () => { beforeAll(async () => {
await nextConfig.replace( await nextConfig.replace(
'// replace me', '// replace me',
......
...@@ -155,6 +155,12 @@ const runTests = () => { ...@@ -155,6 +155,12 @@ const runTests = () => {
source: '/hello/world/(.*)', source: '/hello/world/(.*)',
destination: '/:0', destination: '/:0',
}, },
{
// basePath with relative destination
source: '/hello',
destination: '/world',
basePath: false,
},
// invalid objects // invalid objects
null, null,
'string', 'string',
...@@ -205,6 +211,11 @@ const runTests = () => { ...@@ -205,6 +211,11 @@ const runTests = () => {
expect(stderr).not.toContain( expect(stderr).not.toContain(
'Valid redirect statusCode values are 301, 302, 303, 307, 308' 'Valid redirect statusCode values are 301, 302, 303, 307, 308'
) )
expect(stderr).toContain(
`The route /hello rewrites urls outside of the basePath. Please use a destination that starts with \`http://\` or \`https://\` https://err.sh/vercel/next.js/invalid-external-rewrite.md`
)
expect(stderr).toContain('Invalid rewrites found') expect(stderr).toContain('Invalid rewrites found')
}) })
......
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