Repository navigation
Description
Goal
Reject an Authorization header whose value is not readable text (not visible ASCII), consistently with other invalid headers, and protect that behavior with a test.
Background
The REST API authentication middleware (packages/axum-rest-api-server/src/v1/middlewares/auth.rs) takes the token from the Authorization: Bearer <token> header or from the token query parameter, and documents that the header wins when both are present. extract_bearer_token_from_header reads the header with to_str().ok(). A value with bytes outside visible ASCII makes to_str() fail, and .ok() turns that into "no header", so the middleware silently falls back to the query parameter.
Observed with a throwaway in-process test (token tok):
| Request | Result |
|---|---|
Unreadable Authorization value and ?token=tok |
200, the handler runs |
Authorization: Basic tok and ?token=tok, or Authorization: bearer tok |
500, unknown token provided |
An unreadable header is ignored, while a readable header with another scheme is rejected. This is an inconsistency with the documented contract, not a bypass: the query token must still be valid, and a request without a valid token is rejected in every case observed.
The token query parameter is a legacy authentication method planned for removal in version 2 of the API, because a URL is exposed over plain HTTP and is often written to logs, which makes the token easier to leak. An unreadable header should not fall back to that weaker path.
Decision
Reject: answer unknown token provided, like any other non-Bearer value, and never fall back to the query parameter.
Scope
- Make
extract_bearer_token_from_headerreject an unreadableAuthorizationheader withAuthError::UnknownTokenProvided. - Add a unit test of the middleware for this behavior.
- Update the module documentation of
auth.rsif the contract text changes. - Out of scope: changing the status or body of the failures (
500with the legacy body, kept for backward compatibility).
Acceptance Criteria
- An unreadable
Authorizationheader is rejected withunknown token provided, even with a validtokenquery parameter. - A unit test protects the behavior and fails for the previous behavior.
-
linter alland the package tests pass. - Manual reproduction and a like-for-like recheck on the real tracker are recorded in the issue-local
manual-verification-evidence.md.
Found while planning the authentication middleware tests of #2637.
Activity
docs(issues): [torrust#2653] record the real-tracker reproduction bef…
docs(pr-reviews): [torrust#2653] add the review audit for PR 2654
docs(pr-reviews): [torrust#2653] record the resolved F1 thread of PR …
docs(issues): add issue specification for #2653
Merge #2654: docs(issues): [#2653] add issue specification for the un…
test(axum-rest-api-server): [#2653] reject an unreadable Authorizatio…
fix(axum-rest-api-server): [#2653] reject an unreadable Authorization…
docs(issues): [#2653] record the regression test and the final recheck
chore(issues): archive closed issue #2653 spec to docs/issues/closed
Merge #2658: chore(issues): archive closed issue #2653 spec to docs/i…
Merge torrust#2656: fix(axum-rest-api-server): [torrust#2653] reject …
docs(issues): [#2637] re-measure the package after #2449 and #2653 an…
test(axum-rest-api-server): [#2637] cover the token authentication mi…
Merge #2667: test(axum-rest-api-server): [#2637] cover the authentica…
Merge #2772: test(axum-rest-api-server): [#2637] final slice: crate r…
Merge #2781: chore(issues): archive closed issue #2637 spec to docs/i…