fix(i18n): harden locale validation and fix missing translation tags
This commit is contained in:
@@ -2,11 +2,18 @@ import { getRequestConfig } from 'next-intl/server';
|
||||
import * as Sentry from '@sentry/nextjs';
|
||||
|
||||
export default getRequestConfig(async ({ requestLocale }) => {
|
||||
let locale = await requestLocale;
|
||||
// Hardened locale validation: only allow 'en' or 'de'
|
||||
// Use a temporary variable to validate before assigning to locale
|
||||
const rawLocale = await requestLocale;
|
||||
const supportedLocales = ['en', 'de'];
|
||||
const locale =
|
||||
typeof rawLocale === 'string' && supportedLocales.includes(rawLocale) ? rawLocale : 'en';
|
||||
|
||||
// Ensure that a valid locale is used
|
||||
if (!locale || !['en', 'de'].includes(locale)) {
|
||||
locale = 'en';
|
||||
// Log to Sentry if we had to fallback, as it might indicate a routing issue
|
||||
if (!rawLocale || !supportedLocales.includes(rawLocale as string)) {
|
||||
console.warn(
|
||||
`[i18n] Invalid or missing requestLocale received: "${rawLocale}". Falling back to "en".`,
|
||||
);
|
||||
}
|
||||
|
||||
return {
|
||||
@@ -26,6 +33,6 @@ export default getRequestConfig(async ({ requestLocale }) => {
|
||||
return path;
|
||||
}
|
||||
return 'fallback';
|
||||
}
|
||||
},
|
||||
} as any;
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user