fix: validate languages (#3638)

This commit is contained in:
YidaHu 2024-04-20 10:50:10 +08:00 committed by GitHub
parent 73e9f35ab1
commit 7251748d59
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -46,6 +46,12 @@ export const getLocaleOnServer = (): Locale => {
languages = new Negotiator({ headers: negotiatorHeaders }).languages()
}
// Validate languages
if (!Array.isArray(languages) || languages.length === 0 || !languages.every(lang => typeof lang === 'string' && /^[\w-]+$/.test(lang))) {
console.error(`Invalid languages: ${languages}`)
languages = [i18n.defaultLocale]
}
// match locale
const matchedLocale = match(languages, locales, i18n.defaultLocale) as Locale
return matchedLocale