Commit e8f4e7a1 authored by Wyatt Johnson's avatar Wyatt Johnson Committed by Zack Tanner

[i18n] Routing fix (#70761)

This fixes a bug where an incorrectly sanitized query parameter would
cause an invalid routing condition resulting in the wrong route being
served to users. This currently requires some specific edge conditions
in order to trigger such as missing i18n configuration, self-hosted,
specifically crafted query parameter, and a path-based middleware for
authorization.
parent 681d59f7
...@@ -835,6 +835,14 @@ export default abstract class Server<ServerOptions extends Options = Options> { ...@@ -835,6 +835,14 @@ export default abstract class Server<ServerOptions extends Options = Options> {
this.hasAppDir this.hasAppDir
) )
// 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.attachRequestMeta(req, parsedUrl) this.attachRequestMeta(req, parsedUrl)
const domainLocale = this.i18nProvider?.detectDomainLocale( const domainLocale = this.i18nProvider?.detectDomainLocale(
......
...@@ -134,6 +134,37 @@ export class I18NProvider { ...@@ -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. * Analyzes the pathname for a locale and returns the pathname without it.
* *
......
...@@ -213,6 +213,11 @@ export function getResolveRoutes( ...@@ -213,6 +213,11 @@ export function getResolveRoutes(
parsedUrl.pathname = maybeAddTrailingSlash(parsedUrl.pathname) 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) => { const checkLocaleApi = (pathname: string) => {
......
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