`parseAcceptLanguage` does not trim whitespace or respect `q` weights (`getHeaderLocales` throws on `"en, fr"`)

未关闭 适合新手
#164 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
74/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
typescript

调研方向

解析逻辑位于 src/shared.ts 中的 parseAcceptLanguage,它按逗号和分号切分,但不去除空白,也不读取 q 权重。其调用方 getHeaderLocales、tryHeaderLocales、getHeaderLanguage 和 getHeaderLocale 表现出了这些问题。当 issue 中的三个预期输出都成立时即完成:空白被去除,条目按 q 降序排列,q=0 的条目被丢弃。运行 issue 中的复现脚本进行确认。

由索引模型根据 Issue 内容生成。

描述

Describe the bug

In src/shared.ts, parseAcceptLanguage splits on , and ; without trimming optional whitespace or parsing quality weights (value.split(',').map(tag => tag.split(';')[0]).filter(tag => !(tag === '*' || tag === ''))). Per RFC 9110 §12.5.4 (and §5.6.1 / §12.4.2), list items allow optional whitespace around , and ;, entries are ordered by q weight (default 1), and q=0 marks a language as not acceptable:

  1. When a header has spaces after commas (such as "da, en-gb;q=0.8, en;q=0.7" or headers combined via Headers.prototype.append), every tag after the first keeps a leading space (" en-gb"), " *" bypasses the '*' filter, getHeaderLocales throws RangeError: Incorrect locale information provided, and tryHeaderLocales returns null.
  2. When q values are not pre-sorted ("fr;q=0.8,en-US,en;q=0.9"), header order is returned instead of descending q order, so getHeaderLanguage / getHeaderLocale pick "fr" instead of "en-US".
  3. Entries with q=0 ("en;q=0,fr;q=0.8") are kept instead of excluded, so getHeaderLanguage / getHeaderLocale return "en".
// npm install @intlify/[email protected] && node intlify-utils-tslib.repro.mjs
import {
  parseAcceptLanguage,
  getHeaderLanguage,
  getHeaderLocale,
  getHeaderLocales,
  tryHeaderLocales
} from "@intlify/utils"

const req = (h) => new Request("https://example.com/", { headers: { "accept-language": h } })

// 1. Optional whitespace around commas (e.g. "da, en-gb;q=0.8, en;q=0.7" or Headers.append)
console.log("whitespace:", JSON.stringify(parseAcceptLanguage("da, en-gb;q=0.8, en;q=0.7, *;q=0.5")))
console.log("tryHeaderLocales:", tryHeaderLocales(req("da, en-gb;q=0.8, en;q=0.7")))
try {
  getHeaderLocales(req("da, en-gb;q=0.8, en;q=0.7"))
} catch (err) {
  console.log("getHeaderLocales:", `${err.name}: ${err.message}`)
}

// 2. Unsorted quality weights (default q=1)
console.log("unsorted q:", JSON.stringify(parseAcceptLanguage("fr;q=0.8,en-US,en;q=0.9")))
console.log("getHeaderLanguage:", JSON.stringify(getHeaderLanguage(req("fr;q=0.8,en-US,en;q=0.9"))))

// 3. Excluded language (q=0)
console.log("q=0:", JSON.stringify(parseAcceptLanguage("en;q=0,fr;q=0.8")))
console.log("getHeaderLocale q=0:", getHeaderLocale(req("en;q=0,fr;q=0.8")).toString())

Actual output:

whitespace: ["da"," en-gb"," en"," *"]
tryHeaderLocales: null
getHeaderLocales: RangeError: Incorrect locale information provided
unsorted q: ["fr","en-US","en"]
getHeaderLanguage: "fr"
q=0: ["en","fr"]
getHeaderLocale q=0: en

Expected output:

  • parseAcceptLanguage("da, en-gb;q=0.8, en;q=0.7, *;q=0.5") returns ["da", "en-gb", "en"], and getHeaderLocales / tryHeaderLocales return [Intl.Locale("da"), Intl.Locale("en-GB"), Intl.Locale("en")] without throwing.
  • parseAcceptLanguage("fr;q=0.8,en-US,en;q=0.9") returns ["en-US", "en", "fr"], and getHeaderLanguage returns "en-US".
  • parseAcceptLanguage("en;q=0,fr;q=0.8") returns ["fr"], and getHeaderLocale returns Intl.Locale("fr").
Reproduction

https://gist.github.com/trackoor/3ab40c075bd1ad3f55ac8d8dfa82dea1

System Info
System:
    OS: Linux 6.1 Debian GNU/Linux 12 (bookworm) 12 (bookworm)
    CPU: (64) x64 Intel(R) Xeon(R) CPU @ 2.80GHz
    Memory: 217.08 GB / 251.88 GB
    Container: Yes
    Shell: 5.2.15 - /bin/bash
  Binaries:
    Node: 26.13.0 - /home/hanzhi1/eval-tools/node-v26.13.0-linux-x64/bin/node
    npm: 11.20.0 - /home/hanzhi1/eval-tools/node-v26.13.0-linux-x64/bin/npm
    pnpm: 12.18.0 - /home/hanzhi1/eval-tools/node-v26.13.0-linux-x64/bin/pnpm
  npmPackages:
    @intlify/utils: ^1.2.0 => 1.2.0
Used Package Manager

npm

Additional context

Also reproduces on @intlify/[email protected] and main (8e7f733), and affects @intlify/utils/node as well since both entrypoints use parseAcceptLanguage.

Validations
  • Follow our Code of Conduct
  • Read the Contributing Guide.
  • Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
  • Check that this is a concrete bug. For Q&A, please open a GitHub Discussion instead.
  • The provided reproduction is a minimal reproducible of the bug.

Credit: https://ucsbplse.github.io/

主要语言
TypeScript
星标
17
派生
1
平均合并
4 小时 4 分钟
30 天内合并 PR
51

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

intlify/utils 的其他 Issue

查看 intlify/utils 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。