Skip to content

fix: 支持小写bearer和Bearer后带多个空格 && 修复 WSS预扣费错误提取key的问题 - #2513

Merged
Calcium-Ion merged 1 commit into
QuantumNous:mainfrom
seefs001:fix/token-auth-bearer
Dec 26, 2025
Merged

Calcium-Ion merged 1 commit into
QuantumNous:mainfrom
seefs001:fix/token-auth-bearer

Conversation

@seefs001

@seefs001 seefs001 commented Dec 24, 2025 •

Copy link
Copy Markdown
Collaborator

Summary by CodeRabbit

  • Bug Fixes
    • Corrected token key normalization in quota management to use precise "sk-" prefix handling instead of trimming individual characters, improving token lookup accuracy
    • Enhanced authentication flow to accept both "Bearer" and "bearer" prefixes (case-insensitive) in Authorization and API secret headers for better client compatibility

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai

coderabbitai Bot commented Dec 24, 2025 •

Copy link
Copy Markdown
Contributor

Walkthrough

Two files modified to improve token and header parsing: middleware/auth.go now handles Bearer prefixes case-insensitively, and service/quota.go replaces character-based prefix trimming with exact prefix removal for token key normalization.

Changes

Cohort / File(s) Summary
Header Prefix Handling
middleware/auth.go
TokenAuth flow updated to detect and trim both "Bearer " and "bearer " prefixes (case-insensitive) in Authorization and mj-api-secret headers, replacing exact "Bearer " removal
Token Key Normalization
service/quota.go
PreWssConsumeQuota now uses strings.TrimPrefix() instead of strings.TrimLeft() for "sk-" token prefix removal, ensuring exact prefix matching rather than character-set trimming

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Poem

🐰 Bearer or bearer, both now pass with grace,
And token keys trim with precision—not space.
Prefixes behave as they truly should be,
Case-insensitive parsing, clean as can see!

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the two main changes: support for lowercase 'bearer' prefix and handling multiple spaces after 'Bearer', plus fixing incorrect token key extraction in WSS pre-deduction.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

📜 Recent review details

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 12555a3 and 14c58ae.

📒 Files selected for processing (2)
  • middleware/auth.go
  • service/quota.go
🧰 Additional context used
🧬 Code graph analysis (1)
service/quota.go (1)
model/token.go (1)
  • GetTokenByKey (150-172)
🔇 Additional comments (3)
middleware/auth.go (2)

221-223: LGTM! Case-insensitive Bearer handling implemented correctly.

The implementation properly handles both "Bearer " and "bearer " prefixes (case-insensitive) and correctly trims multiple spaces after the prefix. The approach is safe because the HasPrefix check ensures the string is long enough before slicing at index 7.


226-228: LGTM! Consistent handling across headers.

The same case-insensitive Bearer prefix handling is correctly applied to the mj-api-secret header, maintaining consistency with the Authorization header processing.

service/quota.go (1)

98-98: Critical bug fix: Correct use of TrimPrefix instead of TrimLeft.

This is an important correctness fix. The difference in behavior:

  • TrimLeft("sk-sk-abc", "sk-") removes all leading 's', 'k', and '-' characters → "abc" ❌
  • TrimPrefix("sk-sk-abc", "sk-") removes the exact prefix once → "sk-abc" ✅

The old implementation would incorrectly strip valid token key characters if the key portion after "sk-" happened to start with 's', 'k', or '-', causing token lookup failures. This change is consistent with the same pattern already used in middleware/auth.go (lines 229, 233).


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@Calcium-Ion
Calcium-Ion merged commit 88e394a into QuantumNous:main Dec 26, 2025
ennnnny pushed a commit to ennnnny/new-api that referenced this pull request Mar 17, 2026
fix: 支持小写bearer和Bearer后带多个空格 && 修复 WSS预扣费错误提取key的问题
salem-2007 added a commit to salem-2007/new-api that referenced this pull request Sep 10, 2026
fix: 支持小写bearer和Bearer后带多个空格 && 修复 WSS预扣费错误提取key的问题
junjundesk pushed a commit to junjundesk/new-api that referenced this pull request Oct 3, 2026
fix: 支持小写bearer和Bearer后带多个空格 && 修复 WSS预扣费错误提取key的问题
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants