Repository navigation
fix(web): harden dashboard auth, CSRF, request limits and WebSockets (#64 H5, H6, H7, H8, M11, M12, M13, M16) - #66
Merged
Conversation
Part of #64 (H5, H6, H7, H8, M11, M12, M13, M16). - H5: `auth` (bcrypt) is a default feature; the stored hash is only ever verified with bcrypt, and a build without it refuses to start with authentication enabled. - H6: failures are counted per client address and username, lockouts expire and the count starts over, and the failure table is bounded. - H7: bcrypt runs on the blocking pool (one per CPU) and successful verifications are remembered for up to 60 s under a keyed hash. - H8: bodies need Content-Type: application/json; cross-origin writes and WebSocket handshakes from browsers are refused (Sec-Fetch-Site / Origin). - M11: 1 MiB JSON body limit, 1000-ID bulk limit, archive listing filtered, counted and paged in SQL. - M12: WebSocket max_message_size and message_buffer_size are enforced and subscriptions accept only known event types. - M13: the pagination offset uses the clamped limit, with saturating math. - M16: constant-time username comparison, bcrypt always runs, and CORS is only granted to configured allowed_origins. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This was referenced Oct 9, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Part of #64 (workstream W1). Fixes H5, H6, H7, H8, M11, M12, M13 and M16, all in
hammerwork-web.What changed
auth(bcrypt) is now a default feature. The stored hash is only ever verified with bcrypt; the plaintext fallback is gone. A build withoutauthrejects every password, andDashboardConfig::validate(called byWebDashboard::newand the binary) refuses to start with authentication enabled.warp::addr::remote(), never a header; IPv6 grouped by /64) and by whether the username is the configured one. Every made-up username shares one record per address. A lockout expires afterlockout_durationand the count then starts over; a successful login clears it. The table is capped at 10,000 entries: expired ones are evicted first, then the stalest.spawn_blocking, limited by a semaphore to one verification per CPU. Successful verifications are cached as HMAC-SHA256(random per-process key, username, password) → expiry, formin(60 s, session_timeout). Only correct credentials are cached, and the cache is bounded.session_timeout = 0turns caching off.securitymodule.json_body()replaceswarp::body::json()on every JSON route and requiresContent-Type: application/json(otherwise 415).same_origin_writes()on/apirefuses POST/PUT/DELETE requests that a browser sent from another origin (403). It trustsSec-Fetch-Sitewhen present; otherwise it comparesOriginwithHost, andOrigin: nullis refused. Requests with neither header (curl, scripts) pass, and so do origins listed in the newallowed_origins/--allowed-origin.Content-Lengthheader (411). Bulk actions take at most 1,000 IDs (400)./api/archive/jobsnow goes through the newJobHistory::archived_jobs, which filters, counts (COUNT(*)) and pages (LIMIT/OFFSET) in SQL on both backends. The purge dry run counts in SQL as well.max_message_sizeis applied to incoming messages and frames. Each connection's outbound queue is a boundedmpsc::channel(message_buffer_size); when it is full, the message is dropped.Subscribekeeps only the known event types. The/wshandshake gets the same origin check (same_origin, all methods), against cross-site WebSocket hijacking.max_connectionswas already enforced.get_offsetuses the clamped limit (1..=1000) with saturating arithmetic.total_pagesuses integerdiv_ceil. The jobs paginator'spagecomputation saturates too.subtle::ConstantTimeEqover keyed digests), and bcrypt runs whether or not the username matched. CORS no longer usesallow_any_origin(): it is granted only toallowed_origins, andenable_corswithout any origin is a startup error.Also fixed:
handle_auth_rejectionnow checks 405 last, so a too-large body reports 413 instead of a sibling route's 405, and it mapsLengthRequiredto 411.Why
authbecame a default feature (H5)I chose the least surprising option:
cargo install hammerwork-web): authentication just works with the bcrypt hash the README documents.--no-default-features: these can no longer silently treat the hash as a password. They fail at startup with a message naming the feature and the alternatives (rebuild withauth, or--no-auth).Making
bcryptunconditional would also work. I kept the feature so that existingfeatures = ["auth"]users don't break, and builds without auth stay possible. The existing fail-closed behaviour (auth enabled with no password) and--no-authare unchanged.Behaviour changes for users
--cors/enable_cors = truenow needs at least one--allowed-origin/allowed_originsentry. Without one, startup fails.Content-Type: application/jsonand aContent-Lengthheader. The dashboard's own JS already does, and so do curl-H 'Content-Type: application/json' -d …, reqwest.json()and similar clients./ws, unless they are listed inallowed_origins.server::app_routestakesAllowedOriginsas a new last argument.api::archive::archive_routesrequires aJobHistoryqueue (bothJobQueue<Postgres>andJobQueue<MySql>implement it).AuthStategainscheck(client_ip, user, pass) -> Verdict,is_locked_out_fromandtracked_clients.verify_credentialsandis_locked_outare kept.DashboardConfig::allowed_origins(with#[serde(default)], so old config files still load) andwith_allowed_origin.The hammerwork-web README (configuration, security, request limits, building) and
CHANGELOG.mdare updated.Not addressed
M12's last bullet ("no server code ever publishes events to the socket") is a missing feature, not a memory bound. It is left for separate work.
Tests
Each finding has a regression test:
auth::the_stored_hash_is_never_a_passwordconfig::builds_without_bcrypt_refuse_to_enable_authenticationandmain::authentication_needs_the_auth_feature(both in builds withoutauth)an_expired_lockout_starts_the_count_overlockout_is_per_client_addressthe_filter_locks_out_the_failing_address_onlythe_failure_table_is_boundedipv6_clients_are_grouped_by_networkbcrypt_runs_off_the_async_threads: on a current-thread runtime, another task keeps ticking during a cost-10 verification.successful_verifications_are_remembered_brieflysecurity::*unit testsserver::cross_site_writes_are_refusedrequest_hardeninginapi_tests(full router, both databases)server::oversized_bodies_are_refusedhistory::archive_filters_become_sql_conditionsapi_tests::request_hardening(PostgreSQL and MySQL)websocket_messages_are_size_limitedwebsocket_handshakes_from_other_origins_are_refusedoutgoing_messages_are_bounded_per_connectionsubscriptions_ignore_unknown_event_typesapi::the_offset_uses_the_clamped_limit_and_never_overflowslimit=5000&page=2andu32::MAXpaging through the routercorrect_credentials_pass_and_wrong_ones_do_not(username variants)config::cors_is_only_granted_to_listed_originsmain::cors_needs_explicit_originsResults:
cargo test -p hammerwork-web --all-features -- --include-ignored --skip mysql(PostgreSQL): all pass (116 lib, 12 bin, 2 api, 4 binary, 17 integration, 43 doc).cargo test -p hammerwork-web --all-features -- --include-ignored mysql(MySQL): all pass.cargo test -p hammerwork-web(default features): all pass.cargo test -p hammerwork-web --no-default-features --features postgres --lib --bins: all pass.cargo fmt --all: clean.cargo clippy --workspace --all-targets --all-features --locked -- -D warnings: clean. Also clean for-p hammerwork-webwith default features and with--no-default-features --features postgres.cargo metadata --locked: OK. The new direct dependencies (hmac0.12,sha20.10,rand0.8,subtle2.6) were already inCargo.lock.🤖 Generated with Claude Code