Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit 16abc11

Browse files
authored
Ensure API routes are not available under the locale (vercel#26629)
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: vercel#25790 ## Bug - [x] Related issues linked using `fixes #number` - [x] Integration tests added
1 parent dd12f04 commit 16abc11

File tree

2 files changed

+36
-19
lines changed

2 files changed

+36
-19
lines changed

packages/next/next-server/server/next-server.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,13 @@ export default class Server {
465465
pathname: localePathResult.pathname,
466466
})
467467
;(req as any).__nextStrippedLocale = true
468+
469+
if (
470+
localePathResult.pathname === '/api' ||
471+
localePathResult.pathname.startsWith('/api/')
472+
) {
473+
return this.render404(req, res, parsedUrl)
474+
}
468475
}
469476

470477
// If a detected locale is a domain specific locale and we aren't already

test/integration/i18n-support/test/shared.js

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ async function addDefaultLocaleCookie(browser) {
3737
}
3838

3939
export function runTests(ctx) {
40-
it.only('should have domainLocales available on useRouter', async () => {
40+
it('should have domainLocales available on useRouter', async () => {
4141
const browser = await webdriver(ctx.appPort, `${ctx.basePath || '/'}`)
4242
expect(
4343
JSON.parse(await browser.elementByCss('#router-domain-locales').text())
@@ -1146,44 +1146,54 @@ export function runTests(ctx) {
11461146
for (const locale of locales) {
11471147
const res = await fetchViaHTTP(
11481148
ctx.appPort,
1149-
`${ctx.basePath || ''}${
1150-
locale === 'en-US' ? '' : `/${locale}`
1151-
}/api/hello`,
1149+
`${ctx.basePath || ''}/${locale}/api/hello`,
11521150
undefined,
11531151
{
11541152
redirect: 'manual',
11551153
}
11561154
)
11571155

1158-
const data = await res.json()
1159-
expect(data).toEqual({
1160-
hello: true,
1161-
query: {},
1162-
})
1156+
expect(res.status).toBe(404)
11631157
}
1158+
1159+
const res = await fetchViaHTTP(
1160+
ctx.appPort,
1161+
`${ctx.basePath || ''}/api/hello`
1162+
)
1163+
1164+
const data = await res.json()
1165+
expect(data).toEqual({
1166+
hello: true,
1167+
query: {},
1168+
})
11641169
})
11651170

11661171
it('should visit dynamic API route directly correctly', async () => {
11671172
for (const locale of locales) {
11681173
const res = await fetchViaHTTP(
11691174
ctx.appPort,
1170-
`${ctx.basePath || ''}${
1171-
locale === 'en-US' ? '' : `/${locale}`
1172-
}/api/post/first`,
1175+
`${ctx.basePath || ''}/${locale}/api/post/first`,
11731176
undefined,
11741177
{
11751178
redirect: 'manual',
11761179
}
11771180
)
11781181

1179-
const data = await res.json()
1180-
expect(data).toEqual({
1181-
post: true,
1182-
query: {
1183-
slug: 'first',
1184-
},
1185-
})
1182+
expect(res.status).toBe(404)
11861183
}
1184+
1185+
const res = await fetchViaHTTP(
1186+
ctx.appPort,
1187+
`${ctx.basePath || ''}/api/post/first`
1188+
)
1189+
1190+
const data = await res.json()
1191+
expect(data).toEqual({
1192+
post: true,
1193+
query: {
1194+
slug: 'first',
1195+
},
1196+
})
11871197
})
11881198

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

0 commit comments

Comments
 (0)