Skip to content

Commit 7b5d718

Browse files
committed
fix: fixed a bug where the locale was incorrectly parsed
1 parent 52a167a commit 7b5d718

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

packages/next/src/server/base-server.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,6 +1017,14 @@ export default abstract class Server<
10171017
req.headers['x-forwarded-proto'] ??= isHttps ? 'https' : 'http'
10181018
req.headers['x-forwarded-for'] ??= originalRequest?.socket?.remoteAddress
10191019

1020+
// Validate that if i18n isn't configured or the passed parameters are not
1021+
// valid it should be removed from the query.
1022+
if (!this.i18nProvider?.validateQuery(parsedUrl.query)) {
1023+
delete parsedUrl.query.__nextLocale
1024+
delete parsedUrl.query.__nextDefaultLocale
1025+
delete parsedUrl.query.__nextInferredLocaleFromDefault
1026+
}
1027+
10201028
// This should be done before any normalization of the pathname happens as
10211029
// it captures the initial URL.
10221030
this.attachRequestMeta(req, parsedUrl)

packages/next/src/server/lib/i18n-provider.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,25 @@ export class I18NProvider {
134134
}
135135
}
136136

137+
private validate(locale: string): boolean {
138+
return this.lowerCaseLocales.includes(locale.toLowerCase())
139+
}
140+
141+
public validateQuery(query: NextParsedUrlQuery) {
142+
if (query.__nextLocale && !this.validate(query.__nextLocale)) {
143+
return false
144+
}
145+
146+
if (
147+
query.__nextDefaultLocale &&
148+
!this.validate(query.__nextDefaultLocale)
149+
) {
150+
return false
151+
}
152+
153+
return true
154+
}
155+
137156
/**
138157
* Analyzes the pathname for a locale and returns the pathname without it.
139158
*

packages/next/src/server/lib/router-utils/resolve-routes.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,10 @@ export function getResolveRoutes(
218218
parsedUrl.pathname = maybeAddTrailingSlash(parsedUrl.pathname)
219219
}
220220
}
221+
} else {
222+
delete parsedUrl.query.__nextLocale
223+
delete parsedUrl.query.__nextDefaultLocale
224+
delete parsedUrl.query.__nextInferredLocaleFromDefault
221225
}
222226

223227
const checkLocaleApi = (pathname: string) => {

0 commit comments

Comments
 (0)