[GH-ISSUE #6255] [Enhancement]: An invalid Bearer token gets 404, indistinguishable from a wrong base path #4248

Closed
opened 2026-10-31 20:38:16 +07:00 by bsakaguchi · 2 comments
Owner

Originally created by @rokokol on GitHub (Aug 18, 2026).
Original GitHub issue: https://github.com/MHSanaei/3x-ui/issues/6255

Describe the bug

checkAPIAuth aborts with 404 Not Found whenever authentication fails and the request is not X-Requested-With: XMLHttpRequest. Hiding the API surface from unauthenticated scanners is reasonable, but the same 404 is returned when the caller did present Authorization: Bearer <token> and it simply did not match.

For a script author the two failures are indistinguishable, and neither is distinguishable from a mistyped webBasePath:

  • wrong base path → 404
  • correct path, no token → 404
  • correct path, wrong or disabled token → 404

Since Create returns the plaintext only once and rows store HashTokenSHA256, an existing token cannot be re-read to check it. The practical result is that a failing script gets retried against every path variant, and then a fresh token is minted "just in case". Our panel accumulated 21 enabled tokens this way, 20 of them named cli-fallback-* and all of them irrecoverable — a pile of live admin credentials nobody can identify or verify.

How to reproduce the problem

  1. Settings → Security → API Token: create a token and keep it.
  2. curl -sk -o /dev/null -w '%{http_code}\n' -H 'Authorization: Bearer <valid>' https://host:PORT/BASE/panel/api/inbounds/list → 200.
  3. Repeat with Bearer definitely-not-a-token → 404.
  4. Repeat with no Authorization header at all → 404.
  5. Repeat with a wrong base path → 404.

Expected behavior

A request that carries an Authorization: Bearer header is not an anonymous scan, so answering it honestly costs nothing: 401 Unauthorized when the token does not match or is disabled. Requests without the header can keep returning 404, which preserves the reason the masking exists.

Actual behavior

Every one of the five cases above returns 404 with an empty body.

Relevant logs

Nothing is logged for a rejected token, so the server side gives no way to tell the cases apart either.

3x-ui version

3.6.0

Xray-core version

26.7.28

How did you install 3x-ui?

Install script

Operating system

Ubuntu 24.04

Originally created by @rokokol on GitHub (Aug 18, 2026). Original GitHub issue: https://github.com/MHSanaei/3x-ui/issues/6255 ### Describe the bug `checkAPIAuth` aborts with `404 Not Found` whenever authentication fails and the request is not `X-Requested-With: XMLHttpRequest`. Hiding the API surface from unauthenticated scanners is reasonable, but the same 404 is returned when the caller *did* present `Authorization: Bearer <token>` and it simply did not match. For a script author the two failures are indistinguishable, and neither is distinguishable from a mistyped `webBasePath`: - wrong base path → 404 - correct path, no token → 404 - correct path, wrong or disabled token → 404 Since `Create` returns the plaintext only once and rows store `HashTokenSHA256`, an existing token cannot be re-read to check it. The practical result is that a failing script gets retried against every path variant, and then a fresh token is minted "just in case". Our panel accumulated 21 enabled tokens this way, 20 of them named `cli-fallback-*` and all of them irrecoverable — a pile of live admin credentials nobody can identify or verify. ### How to reproduce the problem 1. Settings → Security → API Token: create a token and keep it. 2. `curl -sk -o /dev/null -w '%{http_code}\n' -H 'Authorization: Bearer <valid>' https://host:PORT/BASE/panel/api/inbounds/list` → `200`. 3. Repeat with `Bearer definitely-not-a-token` → `404`. 4. Repeat with no `Authorization` header at all → `404`. 5. Repeat with a wrong base path → `404`. ### Expected behavior A request that carries an `Authorization: Bearer` header is not an anonymous scan, so answering it honestly costs nothing: `401 Unauthorized` when the token does not match or is disabled. Requests without the header can keep returning 404, which preserves the reason the masking exists. ### Actual behavior Every one of the five cases above returns `404` with an empty body. ### Relevant logs Nothing is logged for a rejected token, so the server side gives no way to tell the cases apart either. ### 3x-ui version 3.6.0 ### Xray-core version 26.7.28 ### How did you install 3x-ui? Install script ### Operating system Ubuntu 24.04
bsakaguchi 2026-10-31 20:38:16 +07:00
Author
Owner

@github-actions[bot] commented on GitHub (Aug 18, 2026):

Confirmed, and the 404 is deliberate masking rather than a routing fault — but the specific gap you describe is real: a presented Bearer token is rejected exactly like no credential at all. Because the current behavior is intentional in the source, I have relabelled this as an enhancement and changed the title prefix from [Bug]: to [Enhancement]:; the wording is otherwise unchanged (original title: "[Bug]: An invalid Bearer token gets 404, indistinguishable from a wrong base path").

What the code does, on the current main:

  • checkAPIAuth tries the bearer branch at internal/web/controller/api.go:50-62. When MatchToken returns false, control falls through to the reject path at api.go:63-69, which consults only session.IsLogin(c) and X-Requested-With. The failed token is not considered, so a wrong token and a missing header take the identical exit.
  • That exit is pinned by a characterization test: internal/web/controller/api_auth_test.go:232-233 ("xhr gets 401" / "non-xhr gets 404").
  • Your logging observation also checks out: neither checkAPIAuth nor ApiTokenService.MatchToken (internal/web/service/panel/api_token.go:227-253) emits a log line on rejection.

A workaround that separates the cases today is to send X-Requested-With: XMLHttpRequest:

curl -sk -o /dev/null -w '%{http_code}\n' \
  -H 'X-Requested-With: XMLHttpRequest' \
  -H 'Authorization: Bearer <token>' \
  https://host:PORT/BASE/panel/api/inbounds/list
  • 200 — token valid and permitted for this route.
  • 401 — base path is correct, the credential is wrong, disabled or expired (api.go:64-65).
  • 403 with a JSON body — token is valid but scoped monitor or node-sync and this route is not on its allowlist (api.go:129-134).
  • 404 — the request never reached checkAPIAuth, so the base path is wrong. engine.NoRoute (internal/web/web.go:273-278) returns a hard 404, and the SPA fallback applies only to HTML GETs under the real base path (internal/web/controller/spa.go:86-99); pinned by internal/web/controller/spa_test.go:141-144.

That still does not separate "no header" from "wrong token", but it does separate path from credential, which is what drives the retry-against-every-path-variant loop.

On the accumulated tokens:

  • You are right that they cannot be read back: only crypto.HashTokenSHA256(plaintext) is stored (api_token.go:107), and the list view never carries the value (api_token.go:39-51).
  • They are still enumerable and removable by id. Settings → Security → API Token shows id, name, scope, enabled state and creation time, backed by GET /panel/api/setting/apiTokens and POST /panel/api/setting/apiTokens/delete/:id (internal/web/controller/setting.go:74,76), with .../apiTokens/setEnabled/:id to disable instead of delete.
  • One caution before deleting in bulk: if this panel is a sub-node, the central panel authenticates to it with a token held in the master's nodes.api_token column (internal/database/model/model.go:784), so that one must be rotated on both sides together.
  • One detail does not match. The CLI mints exactly one row named cli-fallback, with no suffix (main.go:36), and x-ui setting -getApiToken calls RecreateByName, which deletes any same-named row inside the transaction before inserting (main.go:517, api_token.go:120-140) — so on this path repeated CLI calls cannot pile up. If your rows are literally named cli-fallback-<suffix>, they were created by something else. Could you paste the name values as text from GET /panel/api/setting/apiTokens, so it is clear which path produced them?

@MHSanaei the request here is to return 401 when an Authorization: Bearer header is present but does not match, while keeping 404 for requests with no header, which is a one-branch change at internal/web/controller/api.go:63-69 plus an update to TestCheckAPIAuth_RejectsUnauthenticated. #5622 (closed) is the same ambiguity hitting a user in practice. Your call on whether the masking should be relaxed.

This reply was generated automatically; a maintainer may follow up.

<!-- gh-comment-id:5329981123 --> @github-actions[bot] commented on GitHub (Aug 18, 2026): Confirmed, and the 404 is deliberate masking rather than a routing fault — but the specific gap you describe is real: a *presented* Bearer token is rejected exactly like no credential at all. Because the current behavior is intentional in the source, I have relabelled this as an enhancement and changed the title prefix from `[Bug]:` to `[Enhancement]:`; the wording is otherwise unchanged (original title: "[Bug]: An invalid Bearer token gets 404, indistinguishable from a wrong base path"). What the code does, on the current `main`: - `checkAPIAuth` tries the bearer branch at `internal/web/controller/api.go:50-62`. When `MatchToken` returns false, control falls through to the reject path at `api.go:63-69`, which consults only `session.IsLogin(c)` and `X-Requested-With`. The failed token is not considered, so a wrong token and a missing header take the identical exit. - That exit is pinned by a characterization test: `internal/web/controller/api_auth_test.go:232-233` (`"xhr gets 401"` / `"non-xhr gets 404"`). - Your logging observation also checks out: neither `checkAPIAuth` nor `ApiTokenService.MatchToken` (`internal/web/service/panel/api_token.go:227-253`) emits a log line on rejection. A workaround that separates the cases today is to send `X-Requested-With: XMLHttpRequest`: ```bash curl -sk -o /dev/null -w '%{http_code}\n' \ -H 'X-Requested-With: XMLHttpRequest' \ -H 'Authorization: Bearer <token>' \ https://host:PORT/BASE/panel/api/inbounds/list ``` - `200` — token valid and permitted for this route. - `401` — base path is correct, the credential is wrong, disabled or expired (`api.go:64-65`). - `403` with a JSON body — token is valid but scoped `monitor` or `node-sync` and this route is not on its allowlist (`api.go:129-134`). - `404` — the request never reached `checkAPIAuth`, so the base path is wrong. `engine.NoRoute` (`internal/web/web.go:273-278`) returns a hard 404, and the SPA fallback applies only to HTML GETs under the real base path (`internal/web/controller/spa.go:86-99`); pinned by `internal/web/controller/spa_test.go:141-144`. That still does not separate "no header" from "wrong token", but it does separate path from credential, which is what drives the retry-against-every-path-variant loop. On the accumulated tokens: - You are right that they cannot be read back: only `crypto.HashTokenSHA256(plaintext)` is stored (`api_token.go:107`), and the list view never carries the value (`api_token.go:39-51`). - They are still enumerable and removable by id. Settings → Security → API Token shows id, name, scope, enabled state and creation time, backed by `GET /panel/api/setting/apiTokens` and `POST /panel/api/setting/apiTokens/delete/:id` (`internal/web/controller/setting.go:74,76`), with `.../apiTokens/setEnabled/:id` to disable instead of delete. - One caution before deleting in bulk: if this panel is a sub-node, the central panel authenticates to it with a token held in the master's `nodes.api_token` column (`internal/database/model/model.go:784`), so that one must be rotated on both sides together. - One detail does not match. The CLI mints exactly one row named `cli-fallback`, with no suffix (`main.go:36`), and `x-ui setting -getApiToken` calls `RecreateByName`, which deletes any same-named row inside the transaction before inserting (`main.go:517`, `api_token.go:120-140`) — so on this path repeated CLI calls cannot pile up. If your rows are literally named `cli-fallback-<suffix>`, they were created by something else. Could you paste the `name` values as text from `GET /panel/api/setting/apiTokens`, so it is clear which path produced them? @MHSanaei the request here is to return 401 when an `Authorization: Bearer` header is present but does not match, while keeping 404 for requests with no header, which is a one-branch change at `internal/web/controller/api.go:63-69` plus an update to `TestCheckAPIAuth_RejectsUnauthenticated`. #5622 (closed) is the same ambiguity hitting a user in practice. Your call on whether the masking should be relaxed. *This reply was generated automatically; a maintainer may follow up.*
Author
Owner

@rokokol commented on GitHub (Aug 19, 2026):

On the token names, as asked. This panel is v3.6.0, the current release, and the rows are literally cli-fallback-<unix>: cli-fallback-1784221992, cli-fallback-1784222060, … through cli-fallback-1786989581, twenty of them between 2026-07-16 and 2026-08-17, alongside install and one hand-made token.

The suffix comes from the released code, not from some other path. At v3.6.0, main.go:465 reads

fallbackName := fmt.Sprintf("cli-fallback-%d", time.Now().Unix())
created, err := apiTokenService.Create(fallbackName)

so every x-ui setting -getApiToken inserts a new row. The single-name behaviour you describe (cliFallbackTokenName + RecreateByName) exists only on main and has not shipped in a release yet — v3.6.0 (2026-07-30) is the latest. So the pile-up is expected for anyone on a release and is already fixed for the next one; no action needed on that half.

One correction to the reply: api_tokens at v3.6.0 has columns id, name, token, enabled, created_at — no scope. The 403 scope branch does not exist in the release, so on a released panel the outcomes are only 200 / 401 / 404, which makes the 404 ambiguity that much more load-bearing.

<!-- gh-comment-id:5341732803 --> @rokokol commented on GitHub (Aug 19, 2026): On the token names, as asked. This panel is `v3.6.0`, the current release, and the rows are literally `cli-fallback-<unix>`: `cli-fallback-1784221992`, `cli-fallback-1784222060`, … through `cli-fallback-1786989581`, twenty of them between 2026-07-16 and 2026-08-17, alongside `install` and one hand-made token. The suffix comes from the released code, not from some other path. At `v3.6.0`, `main.go:465` reads ```go fallbackName := fmt.Sprintf("cli-fallback-%d", time.Now().Unix()) created, err := apiTokenService.Create(fallbackName) ``` so every `x-ui setting -getApiToken` inserts a new row. The single-name behaviour you describe (`cliFallbackTokenName` + `RecreateByName`) exists only on `main` and has not shipped in a release yet — `v3.6.0` (2026-07-30) is the latest. So the pile-up is expected for anyone on a release and is already fixed for the next one; no action needed on that half. One correction to the reply: `api_tokens` at `v3.6.0` has columns `id, name, token, enabled, created_at` — no `scope`. The 403 scope branch does not exist in the release, so on a released panel the outcomes are only 200 / 401 / 404, which makes the 404 ambiguity that much more load-bearing.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
MHSanaei/3x-ui#4248
No description provided.