Skip to content

fix: accept tokens containing hyphens in the Authorization header - #1221

Open
Chandra-Prakash-TS wants to merge 1 commit into
cnpm:masterfrom
Chandra-Prakash-TS:fix/bearer-token-hyphen
Open

Chandra-Prakash-TS wants to merge 1 commit into
cnpm:masterfrom
Chandra-Prakash-TS:fix/bearer-token-hyphen

Conversation

@Chandra-Prakash-TS

@Chandra-Prakash-TS Chandra-Prakash-TS commented Dec 1, 2026 •

Copy link
Copy Markdown

Fixes #1144

Problem

Tokens are created by randomToken(this.config.cnpmcore.name) as <name>_<random>_<crc>. When the registry name contains a hyphen (for example local-demo), the token contains one too. TokenService.getUserAndToken() only accepts ^Bearer ([\w.]+?)$, so such a token is rejected before it is looked up, and every authenticated request (for example GET /-/whoami) fails with 401 Invalid token.

Fix

Allow - in BEARER_TOKEN_RE. Tokens are still looked up by their sha512 hash, so an unknown token is still rejected with Invalid token (the existing Bearer foo-token assertion in whoami.test.ts still passes).

Tests

Added a case to test/port/controller/UserController/whoami.test.ts that sets config.cnpmcore.name to local-demo, creates a user and calls /-/whoami with the returned token.

  • Without the fix: expected 200 "OK", got 401 "Unauthorized"
  • With the fix: all 4 tests in the file pass

Also ran test/port/middleware/AlwaysAuth.test.ts and test/port/controller/TokenController/createToken.test.ts (MySQL), plus vp lint, vp fmt --check and tsc --noEmit.

Summary by CodeRabbit

  • Bug Fixes
    • Bearer tokens containing hyphens are now accepted, so tokens with hyphenated names can authenticate successfully. The current-user endpoint also correctly returns the username associated with the authenticated token, including when the configured name contains a hyphen. This improves compatibility for these tokens without changing the expected authentication experience for tokens that do not contain hyphens.

Tokens are created as `${config.cnpmcore.name}_<random>`, so a registry
whose name contains a hyphen (e.g. `local-demo`) issues tokens that the
Bearer token pattern `[\w.]+` never matches, and every request with such
a token fails with "Invalid token". Allow `-` in the token pattern.

Fixes cnpm#1144
@coderabbitai

coderabbitai Bot commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The bearer-token validation regex now accepts hyphens. A whoami test checks that a token with the prefix local-demo authenticates and returns the created user’s username.

Changes: Bearer token validation

Layer / File(s) Summary
Accept hyphenated bearer tokens
app/core/service/TokenService.ts, test/port/controller/UserController/whoami.test.ts
The validation regex accepts hyphens in bearer-token values. The test checks that a token with the prefix local-demo authenticates and returns the created username.

Estimated code review effort: 2 (Simple) | ~5 minutes

Change: Bug fix · Severity of issue fixed: Medium

Suggested reviewers: fengmk2

Merge Risk: Minimal · up to 4e14e

Tokens from registries with hyphens in their names will now authenticate. No actionable merge risk remains.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title states the main change: the Authorization header now accepts tokens that contain hyphens. The raw summary says BEARER_TOKEN_RE now accepts hyphens, and the objectives say the PR fixes authen…
Linked Issues check Passed [#1144] The issue requires that Bearer tokens containing - pass validation. At the reviewed head, app/core/service/TokenService.ts sets BEARER_TOKEN_RE = /^Bearer ([\\w.-]+?)$/, which matches the…
Out of Scope Changes check Passed The change set has two files. TokenService.ts changes only the bearer-token regex that the linked issue names. whoami.test.ts adds a regression test for the same hyphenated-token authentication pa…
Full details: Title check

Explanation

The title states the main change: the Authorization header now accepts tokens that contain hyphens. The raw summary says BEARER_TOKEN_RE now accepts hyphens, and the objectives say the PR fixes authentication for registry names that produce hyphenated tokens. The title is specific and matches the changeset.

Full details: Linked Issues check

Explanation

[#1144] The issue requires that Bearer tokens containing - pass validation. At the reviewed head, app/core/service/TokenService.ts sets BEARER_TOKEN_RE = /^Bearer ([\w.-]+?)$/, which matches the issue's proposed character class. getUserAndToken() still hashes the captured value with sha512 and looks it up, so unknown tokens remain rejected with Invalid token. The PR adds a local-demo case to test/port/controller/UserController/whoami.test.ts that creates a user and calls /-/whoami with the returned token. The test file was confirmed only from the change summary; its content was not read directly because the changed blobs were unavailable in the checkout. The source fix was read directly at head.

Full details: Out of Scope Changes check

Explanation

The change set has two files. TokenService.ts changes only the bearer-token regex that the linked issue names. whoami.test.ts adds a regression test for the same hyphenated-token authentication path. Both changes relate directly to #1144. The PR description also lists test runs (AlwaysAuth.test.ts, createToken.test.ts, lint, format, and type checks). These runs support the change and add no unrelated work. No unrelated change was found.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: ab59bc08-23af-4ed0-994c-ef5a89313c41
Commits

Reviewing files that changed from the base of the PR and between e0344ac and 4e14e8b.

Files selected for processing: 2

  • app/core/service/TokenService.ts
  • test/port/controller/UserController/whoami.test.ts

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review.

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.

[Bug]: Token with hyphen - in token_mark fails authentication with "Invalid token"

1 participant