`parseAcceptLanguage` does not trim whitespace or respect `q` weights (`getHeaderLocales` throws on `"en, fr"`)
Maintainer thường phản hồi trong vòng 1 ngày
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 2/5
- Thời gian dự kiến
- 1-3 giờ
- Mức phù hợp với người mới
- 74/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức độ hoạt động
- Sôi nổi
- Công nghệ
- typescript
- Lĩnh vực
- internationalization
Hướng nghiên cứu
Logic phân tích nằm trong parseAcceptLanguage ở src/shared.ts, hàm này tách theo dấu phẩy và dấu chấm phẩy mà không cắt khoảng trắng hay đọc trọng số q. Các hàm gọi nó là getHeaderLocales, tryHeaderLocales, getHeaderLanguage và getHeaderLocale đang cho thấy triệu chứng. Hoàn thành khi ba kết quả mong đợi trong issue đúng: khoảng trắng được cắt bỏ, các mục được sắp xếp theo q giảm dần, và các mục q=0 bị loại bỏ. Chạy script tái hiện trong issue để xác nhận.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
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/
- Ngôn ngữ chính
- TypeScript
- Star
- 17
- Fork
- 1
- Merge trung bình
- 4 giờ 4 phút
- Pull request đã merge (30 ngày)
- 51
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của intlify/utils
-
feature status: proposal
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 38/100
intlify/utils#30 · 3 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Dependency DashboardĐang mở
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 20/100
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của intlify/utils
Issue tương tự
-
check:failed streams:edit
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
iptv-org/iptv#60585 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 90/100
anthropics/claude-code#112972 ·
-
Type: Improvement
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 85/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
ehmpathy/rhachet-roles-bhrain#878 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Chat UI: Message actions dropdown is cut off beneath composer when message is near bottom of chatCó thể đã có người làm @koeria đã nhận hôm nay. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
milind-soni/OpenMausBot#8023 ·
Maintainer thường phản hồi trong vòng 1 ngày