File tree 3 files changed +44
-0
lines changed
3 files changed +44
-0
lines changed Original file line number Diff line number Diff line change @@ -908,6 +908,14 @@ export default abstract class Server<ServerOptions extends Options = Options> {
908
908
req . headers [ 'x-forwarded-proto' ] ??= isHttps ? 'https' : 'http'
909
909
req . headers [ 'x-forwarded-for' ] ??= originalRequest . socket ?. remoteAddress
910
910
911
+ // Validate that if i18n isn't configured or the passed parameters are not
912
+ // valid it should be removed from the query.
913
+ if ( ! this . i18nProvider ?. validateQuery ( parsedUrl . query ) ) {
914
+ delete parsedUrl . query . __nextLocale
915
+ delete parsedUrl . query . __nextDefaultLocale
916
+ delete parsedUrl . query . __nextInferredLocaleFromDefault
917
+ }
918
+
911
919
// This should be done before any normalization of the pathname happens as
912
920
// it captures the initial URL.
913
921
this . attachRequestMeta ( req , parsedUrl )
Original file line number Diff line number Diff line change @@ -134,6 +134,37 @@ export class I18NProvider {
134
134
}
135
135
}
136
136
137
+ /**
138
+ * Validates that the locale is valid.
139
+ *
140
+ * @param locale The locale to validate.
141
+ * @returns `true` if the locale is valid, `false` otherwise.
142
+ */
143
+ private validate ( locale : string ) : boolean {
144
+ return this . lowerCaseLocales . includes ( locale . toLowerCase ( ) )
145
+ }
146
+
147
+ /**
148
+ * Validates that the locales in the query object are valid.
149
+ *
150
+ * @param query The query object to validate.
151
+ * @returns `true` if the locale is valid, `false` otherwise.
152
+ */
153
+ public validateQuery ( query : NextParsedUrlQuery ) {
154
+ if ( query . __nextLocale && ! this . validate ( query . __nextLocale ) ) {
155
+ return false
156
+ }
157
+
158
+ if (
159
+ query . __nextDefaultLocale &&
160
+ ! this . validate ( query . __nextDefaultLocale )
161
+ ) {
162
+ return false
163
+ }
164
+
165
+ return true
166
+ }
167
+
137
168
/**
138
169
* Analyzes the pathname for a locale and returns the pathname without it.
139
170
*
Original file line number Diff line number Diff line change @@ -214,6 +214,11 @@ export function getResolveRoutes(
214
214
parsedUrl . pathname = maybeAddTrailingSlash ( parsedUrl . pathname )
215
215
}
216
216
}
217
+ } else {
218
+ // As i18n isn't configured we remove the locale related query params.
219
+ delete parsedUrl . query . __nextLocale
220
+ delete parsedUrl . query . __nextDefaultLocale
221
+ delete parsedUrl . query . __nextInferredLocaleFromDefault
217
222
}
218
223
219
224
const checkLocaleApi = ( pathname : string ) => {
You can’t perform that action at this time.
0 commit comments