Skip to content

Do not reveal the API token in logs - #1442

Merged
josecelano merged 1 commit into
torrust:developfrom
josecelano:1441-api-token-revealed-in-logs-via-tracing-instrumentation
Apr 11, 2025
Merged

josecelano merged 1 commit into
torrust:developfrom
josecelano:1441-api-token-revealed-in-logs-via-tracing-instrumentation

Conversation

@josecelano

Copy link
Copy Markdown
Member

Do not reveal the API token in logs.

@josecelano
josecelano requested a review from da2ce7 April 11, 2025 14:59
@josecelano josecelano added Bug Incorrect Behavior Security Publicly Connected to Security labels Apr 11, 2025
@josecelano

Copy link
Copy Markdown
Member Author

ACK 4205c71

@codecov

codecov Bot commented Apr 11, 2025 •

Copy link
Copy Markdown

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 84.54%. Comparing base (9ee15a5) to head (4205c71).
Report is 2 commits behind head on develop.

Additional details and impacted files
@@           Coverage Diff            @@
##           develop    #1442   +/-   ##
========================================
  Coverage    84.54%   84.54%           
========================================
  Files          254      254           
  Lines        19189    19189           
  Branches     19189    19189           
========================================
  Hits         16224    16224           
  Misses        2694     2694           
  Partials       271      271           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@josecelano
josecelano merged commit 8a36e45 into torrust:develop Apr 11, 2025
@josecelano josecelano linked an issue Apr 11, 2025 that may be closed by this pull request
@josecelano josecelano self-assigned this Apr 11, 2025

@da2ce7 da2ce7 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a post-merge review of the change from #1442 as it stands at a94c3ced8 on the default branch.

What still runs: the one-line change, #[instrument(skip(configuration))] on AppContainer::initialize, is unchanged from 4205c71 at src/container.rs:64, now on an async fn that returns a Result. The span records no fields, so the Configuration and the API access tokens it holds never enter it. Two later changes added protection of their own: b7af6fa made the tokens SecretString, whose Debug renders SecretBox<str>([REDACTED]), and 8cfc07b redacts ?token= in the REST API request event and span. That second change also closed the other path by which, at merge time, the token still reached the logs the description refers to (the raw request URI logged at INFO); that is history, not a finding.

[Nit] src/bootstrap/app.rs:76 still carries #[instrument(skip())] on initialize_global_services(configuration: &Configuration). An empty skip() skips nothing, so the span records the whole Configuration through Debug: the pattern this pull request removed from src/container.rs:64. On the production startup path (src/app.rs:114 to src/bootstrap/app.rs:64) the span is created before any subscriber exists and records nothing, and today Debug redacts the tokens and the database password, so nothing secret leaks at this commit. Sequence where it does record: a thread-scoped subscriber is current when initialize_global_services is entered (for example a test under capture_logs_in_scope from packages/test-helpers/src/logging.rs) and logging.trace_filter is not off; the Logging initialized event at packages/configuration/src/v3_0_0/logging.rs:101 is then emitted inside the span and carries configuration=Configuration { ... } (every setting, database host, user and paths included) as its span context, and the token values are kept out only by their type, not by the span. Smallest fix: #[instrument(skip(configuration))] (or skip_all) at src/bootstrap/app.rs:76, matching src/container.rs:64.

Checked and found sound at a94c3ced8: every other #[instrument] that receives the API tokens or a container holding them skips them (packages/axum-rest-api-server/src/routes.rs:42, src/bootstrap/jobs/tracker_apis.rs:72 and :116, src/app.rs:264); AccessTokens is HashMap<String, SecretString> (packages/configuration/src/lib.rs:38) with a Debug redaction test at packages/configuration/src/v3_0_0/tracker_api.rs:128; the startup configuration log goes through to_redacted_json (src/bootstrap/app.rs:66); the auth middleware (packages/axum-rest-api-server/src/v1/middlewares/auth.rs) logs neither the presented nor the configured token and compares in constant time; redact_uri (packages/axum-server/src/request_log.rs) matches token after percent-decoding, as the query extractor decodes it, and in any case; the empty skip() on setup and initialize_static (src/bootstrap/app.rs:53, :90) and on check_fn (packages/axum-rest-api-server/src/server.rs:252) records only an optional file path, nothing, and a service binding.

This branch was previously deployed

1 inactive deployment
coverage — 4205c718 Deployed Apr 11, 2025 by josecelano
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Bug Incorrect Behavior Security Publicly Connected to Security

Projects

None yet

Development

Successfully merging this pull request may close these issues.

API token revealed in logs via tracing instrumentation

2 participants