Skip to content
This repository has been archived by the owner on Jun 21, 2023. It is now read-only.

Commit

Permalink
fix: ignore invalid accept-language header (vercel#26476)
Browse files Browse the repository at this point in the history
Fixes vercel#22329

## Bug

- [x] Related issues linked using fixes vercel#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
  • Loading branch information
huozhi committed Jun 22, 2021
1 parent 498afc0 commit f5b7065
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -351,10 +351,15 @@ export function getUtils({

let defaultLocale = i18n.defaultLocale
let detectedLocale = detectLocaleCookie(req, i18n.locales)
let acceptPreferredLocale =
i18n.localeDetection !== false
? accept.language(req.headers['accept-language'], i18n.locales)
: detectedLocale
let acceptPreferredLocale
try {
acceptPreferredLocale =
i18n.localeDetection !== false
? accept.language(req.headers['accept-language'], i18n.locales)
: detectedLocale
} catch (_) {
acceptPreferredLocale = detectedLocale
}

const { host } = req.headers || {}
// remove port from host and remove port if present
Expand Down
14 changes: 9 additions & 5 deletions packages/next/next-server/server/next-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -427,11 +427,15 @@ export default class Server {

let defaultLocale = i18n.defaultLocale
let detectedLocale = detectLocaleCookie(req, i18n.locales)
let acceptPreferredLocale =
i18n.localeDetection !== false
? accept.language(req.headers['accept-language'], i18n.locales)
: detectedLocale

let acceptPreferredLocale
try {
acceptPreferredLocale =
i18n.localeDetection !== false
? accept.language(req.headers['accept-language'], i18n.locales)
: detectedLocale
} catch (_) {
acceptPreferredLocale = detectedLocale
}
const { host } = req?.headers || {}
// remove port from host if present
const hostname = host?.split(':')[0].toLowerCase()
Expand Down
22 changes: 22 additions & 0 deletions test/integration/i18n-support/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,28 @@ describe('i18n Support', () => {
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 () => {
for (const locale of nonDomainLocales) {
const res = await fetchViaHTTP(
Expand Down

0 comments on commit f5b7065

Please sign in to comment.