Skip to content

[i18n] Routing fix #70761

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/next/src/server/base-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1017,6 +1017,14 @@ export default abstract class Server<
req.headers['x-forwarded-proto'] ??= isHttps ? 'https' : 'http'
req.headers['x-forwarded-for'] ??= originalRequest?.socket?.remoteAddress

// Validate that if i18n isn't configured or the passed parameters are not
// valid it should be removed from the query.
if (!this.i18nProvider?.validateQuery(parsedUrl.query)) {
delete parsedUrl.query.__nextLocale
delete parsedUrl.query.__nextDefaultLocale
delete parsedUrl.query.__nextInferredLocaleFromDefault
}

// This should be done before any normalization of the pathname happens as
// it captures the initial URL.
this.attachRequestMeta(req, parsedUrl)
Expand Down
31 changes: 31 additions & 0 deletions packages/next/src/server/lib/i18n-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,37 @@ export class I18NProvider {
}
}

/**
* Validates that the locale is valid.
*
* @param locale The locale to validate.
* @returns `true` if the locale is valid, `false` otherwise.
*/
private validate(locale: string): boolean {
return this.lowerCaseLocales.includes(locale.toLowerCase())
}

/**
* Validates that the locales in the query object are valid.
*
* @param query The query object to validate.
* @returns `true` if the locale is valid, `false` otherwise.
*/
public validateQuery(query: NextParsedUrlQuery) {
if (query.__nextLocale && !this.validate(query.__nextLocale)) {
return false
}

if (
query.__nextDefaultLocale &&
!this.validate(query.__nextDefaultLocale)
) {
return false
}

return true
}

/**
* Analyzes the pathname for a locale and returns the pathname without it.
*
Expand Down
5 changes: 5 additions & 0 deletions packages/next/src/server/lib/router-utils/resolve-routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,11 @@ export function getResolveRoutes(
parsedUrl.pathname = maybeAddTrailingSlash(parsedUrl.pathname)
}
}
} else {
// As i18n isn't configured we remove the locale related query params.
delete parsedUrl.query.__nextLocale
delete parsedUrl.query.__nextDefaultLocale
delete parsedUrl.query.__nextInferredLocaleFromDefault
}

const checkLocaleApi = (pathname: string) => {
Expand Down
Loading