Skip to content

Commit

Permalink
Ensure API routes are not available under the locale (#26629)
Browse files Browse the repository at this point in the history
This ensures API routes are not available under the locale path since API routes don't need to be localized and we don't provide the locale to the API in any way currently so the user wouldn't be aware if the localized API route was visited instead of the non-localized. 

Fixes: #25790

## Bug

- [x] Related issues linked using `fixes #number`
- [x] Integration tests added
  • Loading branch information
ijjk authored Jun 28, 2021
1 parent c5751fa commit 5b2c845
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 19 deletions.
7 changes: 7 additions & 0 deletions packages/next/next-server/server/next-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,13 @@ export default class Server {
pathname: localePathResult.pathname,
})
;(req as any).__nextStrippedLocale = true

if (
localePathResult.pathname === '/api' ||
localePathResult.pathname.startsWith('/api/')
) {
return this.render404(req, res, parsedUrl)
}
}

// If a detected locale is a domain specific locale and we aren't already
Expand Down
48 changes: 29 additions & 19 deletions test/integration/i18n-support/test/shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ async function addDefaultLocaleCookie(browser) {
}

export function runTests(ctx) {
it.only('should have domainLocales available on useRouter', async () => {
it('should have domainLocales available on useRouter', async () => {
const browser = await webdriver(ctx.appPort, `${ctx.basePath || '/'}`)
expect(
JSON.parse(await browser.elementByCss('#router-domain-locales').text())
Expand Down Expand Up @@ -1146,44 +1146,54 @@ export function runTests(ctx) {
for (const locale of locales) {
const res = await fetchViaHTTP(
ctx.appPort,
`${ctx.basePath || ''}${
locale === 'en-US' ? '' : `/${locale}`
}/api/hello`,
`${ctx.basePath || ''}/${locale}/api/hello`,
undefined,
{
redirect: 'manual',
}
)

const data = await res.json()
expect(data).toEqual({
hello: true,
query: {},
})
expect(res.status).toBe(404)
}

const res = await fetchViaHTTP(
ctx.appPort,
`${ctx.basePath || ''}/api/hello`
)

const data = await res.json()
expect(data).toEqual({
hello: true,
query: {},
})
})

it('should visit dynamic API route directly correctly', async () => {
for (const locale of locales) {
const res = await fetchViaHTTP(
ctx.appPort,
`${ctx.basePath || ''}${
locale === 'en-US' ? '' : `/${locale}`
}/api/post/first`,
`${ctx.basePath || ''}/${locale}/api/post/first`,
undefined,
{
redirect: 'manual',
}
)

const data = await res.json()
expect(data).toEqual({
post: true,
query: {
slug: 'first',
},
})
expect(res.status).toBe(404)
}

const res = await fetchViaHTTP(
ctx.appPort,
`${ctx.basePath || ''}/api/post/first`
)

const data = await res.json()
expect(data).toEqual({
post: true,
query: {
slug: 'first',
},
})
})

it('should rewrite to API route correctly', async () => {
Expand Down

0 comments on commit 5b2c845

Please sign in to comment.