Rust Web Security Delivery
Establish attacker, asset, and trust boundaries first; then select crates and middleware. Rust's memory safety does not automatically provide authentication, authorization, protocol enforcement, or business logic security; default to deny all, and use negative testing to prove control is effective.
Confirming the Security Contract
Before modifications:
- Identify service exposure surfaces, callers, browser/API/service-to-service traffic, and management endpoints;
- Determine where TLS terminates, which proxies are trusted, and how client IP/host/scheme originates;
- Define identity providers, authentication protocols, token/session types/lifecycles;
- Specify roles, scopes, object ownership, tenant boundaries, and sensitive business operations;
- Manage key storage/renewal for cookies, passwords, API keys, webhooks;
- Configure CORS, CSRF, redirects, uploads, outbound URLs, and webhook scenarios;
- Define rate limiting budgets per body size/parallelism/timeout requirements and abuse resistance policies;
- Establish audit event definitions, privacy standards, compliance needs, and incident response procedures;
- Lock versions of
axum, tower, rustls, cookie crates along with Rust MSRV.
When security requirements are incomplete, explicitly state assumptions and uncovered risks. Do not bypass validation by weakening tests to achieve pass/fail outcomes.
Workflow
1. Establish Attack Surface and Baselines
rustc --version --verbose
cargo metadata --format-version 1
cargo tree -e features
cargo test --all-targets
Enumerate all routes, methods, extractors, middleware proxies callbacks management endpoints outbound requests. Identify authentication context establishment points authorization decision points session/token storage key loading and audit logging locations. Run existing repository security gates; install or upgrade security tools must comply with project constraints.
2. Write Threat Model and Authorization Matrix
Record at minimum:
Asset -> Entry Point -> Trust Boundary -> Attacker Capability -> Control -> Verification Evidence
Construct subject × action × resource × tenant × context matrices for each sensitive operation. Authentication only proves "who"; authorization must verify in every request whether the user can perform this action on that object. Default deny; do not rely solely on login checks or frontend button visibility.
Object-level, attribute-level, and tenant-based authorization details are available at Threat Model and Authorization.
3. Establish Identity in a Single Boundary
- Use mature IdP/protocols with maintained crates; do not invent token/password protocols yourself;
- Convert authentication results into minimal
Principal/claims contexts;
- Failures of auth middleware must terminate requests without anonymous identity fallbacks to silent downgrade;
- Return 401 for missing or invalid credentials, and 403 when identity is known but permissions are denied;
- Management, internal, and user identities require distinct audiences, scopes, or credentials;
- For high-risk operations requiring re-authentication, MFA, or transactional confirmation without relying on stale sessions.
4. Correctly Validate Tokens, Sessions, and Passwords
JWT validation must enforce fixed allowed algorithms verifying signatures, issuer, audience, expiry, not-before, token type/scope requirements; different token types require mutually exclusive verification rules with JWKS caching/renewal handling. Decoding headers/claims does not constitute valid authentication.
Cookie sessions should include Secure, HttpOnly flags aligned to threat models for SameSite, minimal Path; typically omit broad Domain. Rotate session IDs after login and privilege escalation; define server-side invalidation and absolute/idle expiration upon logout or revocation.
Password hashing uses maintained password-hashing APIs with Argon2id-like parameters measured per deployment size; avoid generic fast hashes. Expensive KDFs must not block async runtime workers. API keys/tokens should be displayed once, stored in verifiable forms with scope prefixes, expiry, and renewal capabilities. OAuth/OIDC/JWT/session/password/API key details are covered at Authentication Tokens and Sessions.
5. Authorize Each Resource Boundary
- Authorization inputs include trusted principals, actions, actual resource attributes, and tenant;
- Query by tenant first, then object/field permissions to prevent BOLA/IDOR attacks;
- Management roles must have explicit scopes; do not treat
is_admin as a global escape hatch;
- Batch operations authorize per-object or via equivalent database policies;
- Write DTOs use allowlists preventing mass assignment of sensitive fields like role, tenant, owner;
- Cached authorization results include policy version, subject, resource, action, and expiration.
Database Row Level Security (RLS) provides defense-in-depth but cannot replace application-level strategies and testing.
6. Harden Browser and Transport Boundaries
- Enforce full HTTPS coverage with HSTS, certificates, mTLS, and TLS policies validated at the actual termination layer;
- Trust only forwarded headers explicitly injected by trusted proxies to avoid host/scheme/IP spoofing;
- CORS defines browser read strategies rather than server-side access control; use precise origin allowlists when credentials are present;
Implement CSRF token defenses alongside state-changing cookie-based auth endpoints; rely on SameSite cookies solely to align protection against threats within the specific threat model.
Set Content Security Policy (CSP), frame, content-type, referrer, and cache headers matching the response body type. Apply rate limits on body size, header manipulation, uploads, pagination, parallelism, timeouts, and expensive operations. Prevent SSRF via user-controllable URL/webhook restrictions: limit scheme/host/port; parse all addresses to block private networks/metadata targets; re-verify after redirects.
Outbound responses also enforce size/type/time constraints. See Browser Transport and Input for specific checks.
7. Protect Secrets and Establish Audit
- Inject secrets from controlled configuration/secret managers into code, image layers, errors, or logs only; use
secrecy types to reduce accidental exposure via Debug serialization without claiming memory isolation equivalence;
- Explicitly define key ID, owner, purpose, algorithm, creation/expiry/renewal/cancellation policies;
- Mark/sanitize sensitive body content before trace logging; audit login failures, permission denials, policy/role/key changes and high-risk operations.
Audit records must include actor, action, target, outcome, request ID, and timestamp without secrets. Prevent log injection attacks by limiting the number of attacker-controllable labels. Rotating or revoking production keys, sessions, users, tokens alters external state; only explicitly authorized actions may execute these changes.
8. Prove Control via Negative Testing
Cover at least:
- Unauthenticated/expired requests with invalid issuer/audience/algorithms/token types;
- Tenant-level permission gaps (same tenant vs cross-tenant), object ownership, and field-level privilege escalation;
- Session fixation, logout/revoke scenarios, absolute/idle expiration policies;
- CSRF/CORS preflight attacks against malicious origins/cookie attributes;
- SSRF via private networks, IPv6, DNS multi-address resolution, redirects, non-HTTP schemes;
- Large body sizes, slow requests, high concurrency costs and expensive authentication flows;
- Error responses leaking trace IDs or audit log secrets;
- Dependency vulnerabilities and feature/crypto backend changes.
See Secrets Audit and Testing for test/audit/supply chain documentation.
Common Gates
cargo fmt --all -- --check
cargo check --all-targets
cargo test --all-targets
cargo clippy --all-targets -- -D warnings
cargo audit
cargo audit requires the tool and advisory data to be available. If either is unavailable, report that the audit was not run rather than claiming a pass. Supplement it with cargo deny, DAST, proxy/TLS configuration tests, and authorization-matrix validation according to repository policy.
Completion Criteria
- Threat model, trust boundaries, and authorization matrices are explicit;
- All sensitive routes default to deny all and verify object/tenant/action ownership;
- Tokens/sessions/passwords/API keys use mature protocols with complete verification;
- TLS/proxies/CORS/CSRF/header/resource limits align with actual deployment configurations;
- Secrets do not appear in responses/logs; rotation/revoke policies are explicit;
- Negative testing and dependency audits pass; unverified IdP, proxies, TLS or production configs explicitly marked.
Handoff Boundaries
| Primary Responsibility |
Owner |
| Router, extractor, handler, middleware, shutdown logic |
rust-web |
| User/session/tenant/API key persistence and transactions |
rust-database |
| Async KDF isolation, timeouts, cancellation concurrency limits |
rust-concurrency |
| Test layering, fuzz testing coverage/performance gates |
rust-testing |
| Dependency features, crypto backend, build/release configuration |
rust-cargo-build |
| Cryptography/FFI/unsafe implementation review |
rust-unsafe-ffi |
On-Demand Resources
- Threat Model and Authorization: Read when designing permissions, tenant boundaries, sensitive business flows.
- Authentication Tokens and Sessions: Access OIDC/JWT/cookie sessions/passwords/API keys during implementation.
- Browser Transport and Input: Configure TLS/proxies/CORS/CSRF/header/uploads/SSRF checks when deploying services.
- Secrets Audit and Testing: Handle secrets, logs, negative testing, dependency audits during implementation.
- Crypto Dependency Boundaries: Select AEAD/KDF/constant-time/clearing crates; review nonce/transcript/WASM or protocol versions when implementing cryptographic features.
- Example Scenarios: Read for end-to-end task decomposition templates requiring production-ready examples.
examples/golden-authorization/: Offline compilation and validation of default deny/object/tenant authorization golden tests available here.
1---2name: rust-web-security3description: Threat-model, implement, review, and test Rust web security, including authentication, object and tenant authorization, sessions, cookies, JWT and OIDC validation, CSRF, CORS, SSRF, input limits, secrets, cryptographic dependency boundaries, audit logging, and negative security tests. Use when users ask to secure or audit Rust web services, tokens, browser transport, multi-tenant access, secret handling, or security incident fixes.4---56# Rust Web Security Delivery78Establish attacker, asset, and trust boundaries first; then select crates and middleware. Rust's memory safety does not automatically provide authentication, authorization, protocol enforcement, or business logic security; default to deny all, and use negative testing to prove control is effective.910## Confirming the Security Contract1112Before modifications:13- Identify service exposure surfaces, callers, browser/API/service-to-service traffic, and management endpoints;14- Determine where TLS terminates, which proxies are trusted, and how client IP/host/scheme originates;15- Define identity providers, authentication protocols, token/session types/lifecycles;16- Specify roles, scopes, object ownership, tenant boundaries, and sensitive business operations;17- Manage key storage/renewal for cookies, passwords, API keys, webhooks;18- Configure CORS, CSRF, redirects, uploads, outbound URLs, and webhook scenarios;19- Define rate limiting budgets per body size/parallelism/timeout requirements and abuse resistance policies;20- Establish audit event definitions, privacy standards, compliance needs, and incident response procedures;21- Lock versions of `axum`, `tower`, `rustls`, `cookie` crates along with Rust MSRV.2223When security requirements are incomplete, explicitly state assumptions and uncovered risks. Do not bypass validation by weakening tests to achieve pass/fail outcomes.2425## Workflow2627### 1. Establish Attack Surface and Baselines28```bash29rustc --version --verbose30cargo metadata --format-version 131cargo tree -e features32cargo test --all-targets33```34Enumerate all routes, methods, extractors, middleware proxies callbacks management endpoints outbound requests. Identify authentication context establishment points authorization decision points session/token storage key loading and audit logging locations. Run existing repository security gates; install or upgrade security tools must comply with project constraints.3536### 2. Write Threat Model and Authorization Matrix37Record at minimum:38```text39Asset -> Entry Point -> Trust Boundary -> Attacker Capability -> Control -> Verification Evidence40```41Construct subject × action × resource × tenant × context matrices for each sensitive operation. Authentication only proves "who"; authorization must verify in every request whether the user can perform this action on that object. Default deny; do not rely solely on login checks or frontend button visibility.4243Object-level, attribute-level, and tenant-based authorization details are available at [Threat Model and Authorization](references/threat-model-and-authorization.md).4445### 3. Establish Identity in a Single Boundary46- Use mature IdP/protocols with maintained crates; do not invent token/password protocols yourself;47- Convert authentication results into minimal `Principal`/claims contexts;48- Failures of auth middleware must terminate requests without anonymous identity fallbacks to silent downgrade;49- Return 401 for missing or invalid credentials, and 403 when identity is known but permissions are denied;50- Management, internal, and user identities require distinct audiences, scopes, or credentials;51- For high-risk operations requiring re-authentication, MFA, or transactional confirmation without relying on stale sessions.5253### 4. Correctly Validate Tokens, Sessions, and Passwords54JWT validation must enforce fixed allowed algorithms verifying signatures, issuer, audience, expiry, not-before, token type/scope requirements; different token types require mutually exclusive verification rules with JWKS caching/renewal handling. Decoding headers/claims does not constitute valid authentication.5556Cookie sessions should include `Secure`, `HttpOnly` flags aligned to threat models for `SameSite`, minimal `Path`; typically omit broad `Domain`. Rotate session IDs after login and privilege escalation; define server-side invalidation and absolute/idle expiration upon logout or revocation.5758Password hashing uses maintained password-hashing APIs with Argon2id-like parameters measured per deployment size; avoid generic fast hashes. Expensive KDFs must not block async runtime workers. API keys/tokens should be displayed once, stored in verifiable forms with scope prefixes, expiry, and renewal capabilities. OAuth/OIDC/JWT/session/password/API key details are covered at [Authentication Tokens and Sessions](references/auth-tokens-and-sessions.md).5960### 5. Authorize Each Resource Boundary61- Authorization inputs include trusted principals, actions, actual resource attributes, and tenant;62- Query by tenant first, then object/field permissions to prevent BOLA/IDOR attacks;63- Management roles must have explicit scopes; do not treat `is_admin` as a global escape hatch;64- Batch operations authorize per-object or via equivalent database policies;65- Write DTOs use allowlists preventing mass assignment of sensitive fields like role, tenant, owner;66- Cached authorization results include policy version, subject, resource, action, and expiration.6768Database Row Level Security (RLS) provides defense-in-depth but cannot replace application-level strategies and testing.6970### 6. Harden Browser and Transport Boundaries71- Enforce full HTTPS coverage with HSTS, certificates, mTLS, and TLS policies validated at the actual termination layer;72- Trust only forwarded headers explicitly injected by trusted proxies to avoid host/scheme/IP spoofing;73- CORS defines browser read strategies rather than server-side access control; use precise origin allowlists when credentials are present;74Implement CSRF token defenses alongside state-changing cookie-based auth endpoints; rely on SameSite cookies solely to align protection against threats within the specific threat model.7576Set Content Security Policy (CSP), frame, content-type, referrer, and cache headers matching the response body type. Apply rate limits on body size, header manipulation, uploads, pagination, parallelism, timeouts, and expensive operations. Prevent SSRF via user-controllable URL/webhook restrictions: limit scheme/host/port; parse all addresses to block private networks/metadata targets; re-verify after redirects.7778Outbound responses also enforce size/type/time constraints. See [Browser Transport and Input](references/browser-transport-and-input.md) for specific checks.7980### 7. Protect Secrets and Establish Audit81- Inject secrets from controlled configuration/secret managers into code, image layers, errors, or logs only; use `secrecy` types to reduce accidental exposure via Debug serialization without claiming memory isolation equivalence;82- Explicitly define key ID, owner, purpose, algorithm, creation/expiry/renewal/cancellation policies;83- Mark/sanitize sensitive body content before trace logging; audit login failures, permission denials, policy/role/key changes and high-risk operations.8485Audit records must include actor, action, target, outcome, request ID, and timestamp without secrets. Prevent log injection attacks by limiting the number of attacker-controllable labels. Rotating or revoking production keys, sessions, users, tokens alters external state; only explicitly authorized actions may execute these changes.8687### 8. Prove Control via Negative Testing88Cover at least:891. Unauthenticated/expired requests with invalid issuer/audience/algorithms/token types;902. Tenant-level permission gaps (same tenant vs cross-tenant), object ownership, and field-level privilege escalation;913. Session fixation, logout/revoke scenarios, absolute/idle expiration policies;924. CSRF/CORS preflight attacks against malicious origins/cookie attributes;935. SSRF via private networks, IPv6, DNS multi-address resolution, redirects, non-HTTP schemes;946. Large body sizes, slow requests, high concurrency costs and expensive authentication flows;957. Error responses leaking trace IDs or audit log secrets;968. Dependency vulnerabilities and feature/crypto backend changes.9798See [Secrets Audit and Testing](references/secrets-audit-and-testing.md) for test/audit/supply chain documentation.99100## Common Gates101```bash102cargo fmt --all -- --check103cargo check --all-targets104cargo test --all-targets105cargo clippy --all-targets -- -D warnings106cargo audit107```108`cargo audit` requires the tool and advisory data to be available. If either is unavailable, report that the audit was not run rather than claiming a pass. Supplement it with `cargo deny`, DAST, proxy/TLS configuration tests, and authorization-matrix validation according to repository policy.109110## Completion Criteria111- Threat model, trust boundaries, and authorization matrices are explicit;112- All sensitive routes default to deny all and verify object/tenant/action ownership;113- Tokens/sessions/passwords/API keys use mature protocols with complete verification;114- TLS/proxies/CORS/CSRF/header/resource limits align with actual deployment configurations;115- Secrets do not appear in responses/logs; rotation/revoke policies are explicit;116- Negative testing and dependency audits pass; unverified IdP, proxies, TLS or production configs explicitly marked.117118## Handoff Boundaries119120| Primary Responsibility | Owner |121|---|---|122| Router, extractor, handler, middleware, shutdown logic | `rust-web` |123| User/session/tenant/API key persistence and transactions | `rust-database` |124| Async KDF isolation, timeouts, cancellation concurrency limits | `rust-concurrency` |125| Test layering, fuzz testing coverage/performance gates | `rust-testing` |126| Dependency features, crypto backend, build/release configuration | `rust-cargo-build` |127| Cryptography/FFI/unsafe implementation review | `rust-unsafe-ffi` |128129## On-Demand Resources130- [Threat Model and Authorization](references/threat-model-and-authorization.md): Read when designing permissions, tenant boundaries, sensitive business flows.131- [Authentication Tokens and Sessions](references/auth-tokens-and-sessions.md): Access OIDC/JWT/cookie sessions/passwords/API keys during implementation.132- [Browser Transport and Input](references/browser-transport-and-input.md): Configure TLS/proxies/CORS/CSRF/header/uploads/SSRF checks when deploying services.133- [Secrets Audit and Testing](references/secrets-audit-and-testing.md): Handle secrets, logs, negative testing, dependency audits during implementation.134- [Crypto Dependency Boundaries](references/crypto-dependency-boundaries.md): Select AEAD/KDF/constant-time/clearing crates; review nonce/transcript/WASM or protocol versions when implementing cryptographic features.135- [Example Scenarios](examples/examples.md): Read for end-to-end task decomposition templates requiring production-ready examples.136137`examples/golden-authorization/`: Offline compilation and validation of default deny/object/tenant authorization golden tests available here.