Skip to content

Commit

Permalink
Validate i18n locale domain (#50220)
Browse files Browse the repository at this point in the history
The `domain` value is meant to be a hostname and not include `http` or
port so this ensures we validate it doesn't contain `:` unexpectedly
causing invalid behavior.

Closes: #49656
x-ref:
#24991 (comment)

---------

Co-authored-by: Jiachi Liu <inbox@huozhi.im>
  • Loading branch information
ijjk and huozhi committed Jun 14, 2023
1 parent 616ae10 commit 916d2aa
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
7 changes: 7 additions & 0 deletions packages/next/src/server/config.ts
Expand Up @@ -537,6 +537,13 @@ function assignDefaults(
if (!item.defaultLocale) return true
if (!item.domain || typeof item.domain !== 'string') return true

if (item.domain.includes(':')) {
console.warn(
`i18n domain: "${item.domain}" is invalid it should be a valid domain without protocol (https://) or port (:3000) e.g. example.vercel.sh`
)
return true
}

const defaultLocaleDuplicate = i18n.domains?.find(
(altItem) =>
altItem.defaultLocale === item.defaultLocale &&
Expand Down
26 changes: 26 additions & 0 deletions test/integration/i18n-support/test/index.test.js
Expand Up @@ -550,4 +550,30 @@ describe('i18n Support', () => {
)
expect(stderr).toContain(`eN, fr`)
})

it('should show proper error for invalid locale domain', async () => {
nextConfig.write(`
module.exports = {
i18n: {
locales: ['en', 'fr', 'nl', 'eN', 'fr'],
domains: [
{
domain: 'hello:3000',
defaultLocale: 'en',
}
],
defaultLocale: 'en',
}
}
`)

const { code, stderr } = await nextBuild(appDir, undefined, {
stderr: true,
})
nextConfig.restore()
expect(code).toBe(1)
expect(stderr).toContain(
`i18n domain: "hello:3000" is invalid it should be a valid domain without protocol (https://) or port (:3000) e.g. example.vercel.sh`
)
})
})

0 comments on commit 916d2aa

Please sign in to comment.