mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-12-03 21:18:19 +07:00
[GH-ISSUE #6255] [Enhancement]: An invalid Bearer token gets 404, indistinguishable from a wrong base path #4248
Labels
No labels
backlog
bug
clarification needed
documentation
duplicate
enhancement
github_actions
good first issue
invalid
javascript
New Feature
PR welcomed
pull-request
question
wontfix
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
MHSanaei/3x-ui#4248
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @rokokol on GitHub (Aug 18, 2026).
Original GitHub issue: https://github.com/MHSanaei/3x-ui/issues/6255
Describe the bug
checkAPIAuthaborts with404 Not Foundwhenever authentication fails and the request is notX-Requested-With: XMLHttpRequest. Hiding the API surface from unauthenticated scanners is reasonable, but the same 404 is returned when the caller did presentAuthorization: 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:Since
Createreturns the plaintext only once and rows storeHashTokenSHA256, 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 namedcli-fallback-*and all of them irrecoverable — a pile of live admin credentials nobody can identify or verify.How to reproduce the problem
curl -sk -o /dev/null -w '%{http_code}\n' -H 'Authorization: Bearer <valid>' https://host:PORT/BASE/panel/api/inbounds/list→200.Bearer definitely-not-a-token→404.Authorizationheader at all →404.404.Expected behavior
A request that carries an
Authorization: Bearerheader is not an anonymous scan, so answering it honestly costs nothing:401 Unauthorizedwhen 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
404with 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
@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:checkAPIAuthtries the bearer branch atinternal/web/controller/api.go:50-62. WhenMatchTokenreturns false, control falls through to the reject path atapi.go:63-69, which consults onlysession.IsLogin(c)andX-Requested-With. The failed token is not considered, so a wrong token and a missing header take the identical exit.internal/web/controller/api_auth_test.go:232-233("xhr gets 401"/"non-xhr gets 404").checkAPIAuthnorApiTokenService.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:200— token valid and permitted for this route.401— base path is correct, the credential is wrong, disabled or expired (api.go:64-65).403with a JSON body — token is valid but scopedmonitorornode-syncand this route is not on its allowlist (api.go:129-134).404— the request never reachedcheckAPIAuth, 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 byinternal/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:
crypto.HashTokenSHA256(plaintext)is stored (api_token.go:107), and the list view never carries the value (api_token.go:39-51).GET /panel/api/setting/apiTokensandPOST /panel/api/setting/apiTokens/delete/:id(internal/web/controller/setting.go:74,76), with.../apiTokens/setEnabled/:idto disable instead of delete.nodes.api_tokencolumn (internal/database/model/model.go:784), so that one must be rotated on both sides together.cli-fallback, with no suffix (main.go:36), andx-ui setting -getApiTokencallsRecreateByName, 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 namedcli-fallback-<suffix>, they were created by something else. Could you paste thenamevalues as text fromGET /panel/api/setting/apiTokens, so it is clear which path produced them?@MHSanaei the request here is to return 401 when an
Authorization: Bearerheader is present but does not match, while keeping 404 for requests with no header, which is a one-branch change atinternal/web/controller/api.go:63-69plus an update toTestCheckAPIAuth_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.
@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 literallycli-fallback-<unix>:cli-fallback-1784221992,cli-fallback-1784222060, … throughcli-fallback-1786989581, twenty of them between 2026-07-16 and 2026-08-17, alongsideinstalland one hand-made token.The suffix comes from the released code, not from some other path. At
v3.6.0,main.go:465readsso every
x-ui setting -getApiTokeninserts a new row. The single-name behaviour you describe (cliFallbackTokenName+RecreateByName) exists only onmainand 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_tokensatv3.6.0has columnsid, name, token, enabled, created_at— noscope. 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.