Unverified Commit fb5fb7f2 authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

fix: ignore invalid accept-language header (#26476)



Fixes #22329

## Bug

- [x] Related issues linked using fixes #22329
- [x] Integration tests added

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.

## Documentation / Examples

- [ ] Make sure the linting passes
parent fc67fc73
...@@ -351,10 +351,15 @@ export function getUtils({ ...@@ -351,10 +351,15 @@ export function getUtils({
let defaultLocale = i18n.defaultLocale let defaultLocale = i18n.defaultLocale
let detectedLocale = detectLocaleCookie(req, i18n.locales) let detectedLocale = detectLocaleCookie(req, i18n.locales)
let acceptPreferredLocale = let acceptPreferredLocale
i18n.localeDetection !== false try {
? accept.language(req.headers['accept-language'], i18n.locales) acceptPreferredLocale =
: detectedLocale i18n.localeDetection !== false
? accept.language(req.headers['accept-language'], i18n.locales)
: detectedLocale
} catch (_) {
acceptPreferredLocale = detectedLocale
}
const { host } = req.headers || {} const { host } = req.headers || {}
// remove port from host and remove port if present // remove port from host and remove port if present
......
...@@ -427,11 +427,15 @@ export default class Server { ...@@ -427,11 +427,15 @@ export default class Server {
let defaultLocale = i18n.defaultLocale let defaultLocale = i18n.defaultLocale
let detectedLocale = detectLocaleCookie(req, i18n.locales) let detectedLocale = detectLocaleCookie(req, i18n.locales)
let acceptPreferredLocale = let acceptPreferredLocale
i18n.localeDetection !== false try {
? accept.language(req.headers['accept-language'], i18n.locales) acceptPreferredLocale =
: detectedLocale i18n.localeDetection !== false
? accept.language(req.headers['accept-language'], i18n.locales)
: detectedLocale
} catch (_) {
acceptPreferredLocale = detectedLocale
}
const { host } = req?.headers || {} const { host } = req?.headers || {}
// remove port from host if present // remove port from host if present
const hostname = host?.split(':')[0].toLowerCase() const hostname = host?.split(':')[0].toLowerCase()
......
...@@ -267,6 +267,28 @@ describe('i18n Support', () => { ...@@ -267,6 +267,28 @@ describe('i18n Support', () => {
expect($('#router-as-path').text()).toBe('/') expect($('#router-as-path').text()).toBe('/')
}) })
it('should ignore the invalid accept-language header', async () => {
nextConfig.replace('localeDetection: false', 'localeDetection: true')
const res = await fetchViaHTTP(
ctx.appPort,
'/',
{},
{
headers: {
'accept-language': 'ldfir;',
},
}
)
expect(res.status).toBe(200)
const $ = cheerio.load(await res.text())
expect($('html').attr('lang')).toBe('en-US')
expect($('#router-locale').text()).toBe('en-US')
expect(JSON.parse($('#router-locales').text())).toEqual(locales)
expect($('#router-pathname').text()).toBe('/')
expect($('#router-as-path').text()).toBe('/')
})
it('should set locale from detected path', async () => { it('should set locale from detected path', async () => {
for (const locale of nonDomainLocales) { for (const locale of nonDomainLocales) {
const res = await fetchViaHTTP( const res = await fetchViaHTTP(
......
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