Skip to content

Traffic: refuse a plain-http maxmind_url, and discard a download that Turn off overtook - #923

Open
schmitch wants to merge 1 commit into
mainfrom
claude/772-maxmind-http-and-turn-off-race
Open

schmitch wants to merge 1 commit into
mainfrom
claude/772-maxmind-http-and-turn-off-race

Conversation

@schmitch

Copy link
Copy Markdown
Member

Fixes #772

Clear-text license key: an http:// traffic.maxmind_url is refused

Of the two options the issue allows, this refuses rather than warns: startup fails with an http:// traffic.maxmind_url, unless its host is loopback (localhost, 127.0.0.1, [::1]).

  • A warning in the log does not stop the account ID and license key from going out in clear with every download; a refusal does.
  • The only plain-http use in the repository is the integration tests' mock of MaxMind (wiremock on 127.0.0.1). No documentation, compose file, chart or e2e setup names an http:// mirror. The loopback exception keeps the tests as they are and allows a mirror on the same machine. It is the rule credential forwarding already has for its URL (validate_credential_forward_url).
  • The traffic statistics (Admin → Traffic: daily request counts per repository and client, with country and ASN, over all instances #709) are not released yet (the workspace is at 2.1.0, this ships in 2.2.0), so no existing configuration breaks on upgrade. There is therefore no upgrade fragment, only 772.fixed.md.

docs/deployment.md (Traffic statistics) and forge.toml.example say that the URL must be https://, and why.

Turn off races a download: the download is discarded

update() now stores its databases only while the state is still the one it claimed.

  • claim() returns the claimed state (Claim) instead of a bool, and update() takes it. The claimed state carries its own last_attempt, so it differs from every other state the row holds: credentials removed, credentials removed and stored again with the same account (set_credentials clears last_attempt), or a later claim.
  • After both databases are downloaded and checked, update() swaps the state from the claim to the successful one (replace_setting, a compare-and-swap on the forge_setting row). Only if that swap succeeds does it put the databases. A download that lost the race puts nothing, writes no last_success, and is logged as discarded.
  • The check is against the shared state in the database, not this process's memory, so it holds when Turn off is clicked on another instance.
  • The swap runs in a transaction that stays open while the databases are put and loaded. It holds the state's row, so a DELETE /admin/traffic/geoip that arrives at that moment waits for it, then deletes the state and the databases. Nothing is put after forget() has deleted them. The cost is one pooled connection held for the two puts.
  • A failed download records its last_error by the same swap, so it is never written over credentials removed or set since, nor over a state that cannot be read (the guarantee the earlier read-again had).

Admin API and web UI

POST /admin/traffic/geoip/update still answers 200 with the status when its download was discarded; the status then shows the account removed, or no download yet. The settings screen (Admin → Traffic → Settings → Geolocation) showed "Downloaded: every instance uses the databases within ten minutes" for any successful answer without an error. It now shows that only when the answer carries a download's time, and otherwise says that nothing was downloaded because the account was removed or saved again while the download ran. No other screen changes: the http:// refusal is a startup error, not something the UI sets.

Tests

  • config::tests::a_plain_http_maxmind_url_is_refused_unless_loopback: https and loopback http are accepted, other http is refused with a message naming the key, other schemes are refused.
  • turning_geoip_off_discards_a_download_in_flight (crates/server/tests/traffic.rs): the mock delays the second archive, DELETE runs while the download waits for it, and afterwards the bucket holds no database, the state is gone and the answer shows geolocation off.
  • credentials_set_again_discard_the_download_started_before: DELETE then PUT of the same account during the download; the old download stores nothing, and a new Download now works.
  • a_failed_download_is_not_recorded_over_a_state_it_did_not_claim replaces a_download_recorded_over_a_state_it_cannot_read_keeps_the_account_id, for the new signature.
  • The three tests that store or delete the databases take a lock: the test servers of one binary share a bucket, and the databases have one key each in it. Without the lock the new tests failed about one run in six on another test's databases.
  • traffic.test.tsx: the screen says that a download overtaken by Turn off stored nothing.

Against the code before the fix, the config test fails (http://mirror.example.com is accepted) and both in-flight tests fail with traffic/v1/geoip/asn.mmdb put back in the bucket.

Checks run locally, all passing: cargo test -p forge-server --test traffic (6 tests, ten runs in a row), cargo test -p forge-server --lib (296), cargo xtask check (fmt and clippy -D warnings), cargo xtask changelog --check, cargo xtask unused, cargo xtask doc-check, and in web/: pnpm tsc, pnpm knip, vitest run src/routes/admin/traffic (8). The full workspace test suite, the e2e suites and the Playwright suite were not run locally.


Generated by Claude Code

… overtook

traffic.maxmind_url must be https: every download sends the MaxMind
account ID and license key there by HTTP Basic. An http:// URL is refused
at startup unless its host is loopback (a mirror on the same machine, and
the tests' mock), as credential forwarding does for its URL.

A download stores its databases only while the state is still the one it
claimed. claim() returns the claimed state, and update() swaps from it to
the successful state in a transaction that stays open while the databases
are put and loaded: credentials forgotten or set again during the
download, on any instance, make the swap fail and the download is
discarded, and forget() waits on the row for a download that is storing.
A failure is recorded by the same swap, never over a state the download
did not claim.

Admin -> Traffic -> Settings says when Download now stored nothing
because the account was removed or saved again meanwhile.

Fixes #772

Co-Authored-By: Claude Fable 5.2 <[email protected]>
Claude-Session: https://claude.ai/code/session_012e6DJbVCXv53RS9Xaf8uxc

This branch has not been deployed

No deployments
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.

Traffic: an http:// maxmind_url sends the MaxMind license key in clear, and Turn off can be undone by a download in flight

2 participants