`parseAcceptLanguage` does not trim whitespace or respect `q` weights (`getHeaderLocales` throws on `"en, fr"`)
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 2/5
- 見積もり時間
- 1〜3時間
- 初心者へのやさしさ
- 74/100
- issue の種類
- バグ
- 明瞭さ
- 明確に書かれている
- 活発さ
- 活発
- 技術スタック
- typescript
調査の方向性
パース処理は src/shared.ts の parseAcceptLanguage にあり、カンマとセミコロンで分割しますが、空白のトリミングや q 重みの読み取りは行っていません。呼び出し元である getHeaderLocales、tryHeaderLocales、getHeaderLanguage、getHeaderLocale に症状が現れています。issue に記載された3つの期待される出力が成立すれば完了です。空白がトリミングされ、エントリが 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:
- When a header has spaces after commas (such as
"da, en-gb;q=0.8, en;q=0.7"or headers combined viaHeaders.prototype.append), every tag after the first keeps a leading space (" en-gb")," *"bypasses the'*'filter,getHeaderLocalesthrowsRangeError: Incorrect locale information provided, andtryHeaderLocalesreturnsnull. - When
qvalues are not pre-sorted ("fr;q=0.8,en-US,en;q=0.9"), header order is returned instead of descendingqorder, sogetHeaderLanguage/getHeaderLocalepick"fr"instead of"en-US". - Entries with
q=0("en;q=0,fr;q=0.8") are kept instead of excluded, sogetHeaderLanguage/getHeaderLocalereturn"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"], andgetHeaderLocales/tryHeaderLocalesreturn[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"], andgetHeaderLanguagereturns"en-US".parseAcceptLanguage("en;q=0,fr;q=0.8")returns["fr"], andgetHeaderLocalereturnsIntl.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分
- マージ済み PR(30日)
- 51
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
intlify/utils のほかの issue
-
feature status: proposal
難易度 3/5 1〜2日 初心者へのやさしさ 38/100
メンテナーはふだん 1 日以内に返信
-
Dependency Dashboardオープン
難易度 5/5 1週間以上 初心者へのやさしさ 20/100
メンテナーはふだん 1 日以内に返信
似ている issue
-
check:failed streams:edit
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
iptv-org/iptv#60585 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 90/100
anthropics/claude-code#112972 ·
-
Type: Improvement
難易度 2/5 1〜3時間 初心者へのやさしさ 85/100
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
ehmpathy/rhachet-roles-bhrain#878 ·
メンテナーはふだん 1 日以内に返信
-
Chat UI: Message actions dropdown is cut off beneath composer when message is near bottom of chat対応中かも @koeria が今日担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
milind-soni/OpenMausBot#8023 ·
メンテナーはふだん 1 日以内に返信