Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

Regex audience matching without anchors is a security footgun

Đang mở
#1,019 1 bình luận 1 reaction 0 người được giao Xem trên GitHub

@Hashim1999164 đang làm issue này rồi.

Từ ngày 22/7/2026.

  • #1040 của @Hashim1999164 — đang mở

Đá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
52/100
Loại issue
Tài liệu
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Ít trao đổi
Công nghệ
javascript, node.js
Lĩnh vực
authentication, security

Hướng nghiên cứu

Bắt đầu với phần kiểm tra audience trong verify.js và phần audience trong README. Xem lại cách các audience dạng string và RegExp hiện được mô tả, sau đó xác nhận hành vi được đề xuất nào mà các maintainer mong muốn. Phần hoàn tất cần nêu rõ các tác động bảo mật của audience regex không được neo, đồng thời cập nhật phạm vi kiểm thử liên quan nếu cần.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

Summary

When options.audience contains a RegExp, verify() calls audience.test(targetAudience) without enforcing that the regex is anchored. This means a developer who writes:

jwt.verify(token, secret, { audience: /api\.myapp\.com/ });

will inadvertently accept tokens with audiences like evil-api.myapp.com.attacker.com — the . matches any character and there are no ^/$ anchors.

The string comparison path (audience === targetAudience) is strict. The regex path silently shifts the security burden to the caller.

Reproduction

const jwt = require('jsonwebtoken');

const secret = 'test-secret';
const token = jwt.sign({ aud: 'evil-api.myapp.com.attacker.com' }, secret);

// Developer intends to only accept "api.myapp.com"
jwt.verify(token, secret, { audience: /api\.myapp\.com/ }, (err, decoded) => {
  console.log(err);     // null — no error!
  console.log(decoded); // token accepted despite malicious audience
});

Impact

This is not a vulnerability in the library itself — the regex works as designed. But it's a footgun: developers who mix string and regex audience checks may not realize the security model differs between the two paths. The string path is exact-match. The regex path accepts partial matches unless the developer manually adds ^ and $.

Given that audience validation is a security-critical check, the gap between "looks like it works" and "actually secure" is a concern.

Suggestions (pick any)

  1. Document it prominently — Add a note to the README's audience section warning that regex audiences must be anchored to avoid partial matches.
  2. Warn on unanchored regexes — If the regex source doesn't start with ^ or end with $, emit a console warning or throw.
  3. Auto-anchor — Wrap unanchored regexes in ^(?:...)$ before testing. This would be a breaking change for anyone relying on partial matches intentionally.

Option 1 is the lowest-friction fix. Option 2 provides defense-in-depth without breaking existing behavior.

Relevant code

verify.js audience check:

return audiences.some(function (audience) {
  return audience instanceof RegExp ? audience.test(targetAudience) : audience === targetAudience;
});
Ngôn ngữ chính
JavaScript
Star
18.2k
Fork
1.3k
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. 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.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của auth0/node-jsonwebtoken

Tất cả issue của auth0/node-jsonwebtoken

Issue tương tự

Thêm issue về JavaScript

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.