Rust Service Security
Use this skill when a Rust service handles identity, credentials, sessions,
cookies, privileged routes, or sensitive user data. Prefer explicit threat
checks and tests over optimistic handler code.
Core Workflow
- Identify the protected asset: account access, admin area, session state,
password change, reset token, private data, or privileged operation.
- Trace the whole flow: request parsing, credential lookup, verification,
session mutation, redirects/responses, logs, and tests.
- Keep secrets out of logs and debug output. Use secret wrappers for passwords,
tokens, signing keys, and session secrets.
- Run password hashing and verification with memory-hard algorithms and move
CPU-heavy work off async reactor threads.
- Prevent user enumeration. Failed login and password reset flows should not
reveal whether the account exists.
- Set cookie and session attributes deliberately:
HttpOnly, Secure,
SameSite, path, lifetime, signing/encryption, and store backend.
- Protect routes at middleware or extractor boundaries, not by repeating
ad-hoc checks in every handler.
- Add tests for happy path, failed auth, missing session, forged cookie or
token, and logout/session rotation behavior.
Password Rules
Read references/password-auth.md before implementing or changing password
storage or verification.
- Store PHC strings produced by Argon2id or the project's chosen password
hasher; never store plaintext or reversible encrypted passwords.
- Generate salts with a secure RNG.
- Use constant, generic responses for invalid credentials.
- Use
tokio::task::spawn_blocking or a dedicated blocking abstraction for
expensive password operations.
- Keep password hash parameters configurable enough to upgrade over time.
Cookie And Session Rules
Read references/cookies-sessions.md when setting, reading, deleting, signing,
or persisting cookies and sessions.
- Never store raw credentials in cookies.
- Prefer opaque session IDs backed by a server-side store for privileged apps.
- Rotate or renew session state after login and privilege changes.
- Delete sessions server-side on logout when using a server-side store.
- Test security attributes on
Set-Cookie headers.
Route Protection
Read references/auth-middleware.md when adding admin routes, extractors,
guards, or framework middleware.
Keep handlers typed:
pub async fn admin_dashboard(user: AuthenticatedUser) -> Result<HttpResponse, AppError> {
// Handler can assume authentication succeeded.
Ok(HttpResponse::Ok().finish())
}
Avoid optional user lookups in handlers that require authentication; make the
missing-user case impossible at the handler signature when the framework allows
it.
Reference Files
references/password-auth.md: Argon2, PHC strings, user enumeration, and
blocking password work.
references/cookies-sessions.md: cookie flags, signed messages, server-side
sessions, and logout.
references/auth-middleware.md: route guards, extractors, and typed session
access.
1---2name: rust-service-security3description: Use when adding, changing, testing, or reviewing security-sensitive Rust web service behavior, especially login flows, password hashing, credential checks, session cookies, flash messages, admin route protection, auth middleware, user enumeration defenses, or moving CPU-heavy password work off async executors.4license: MIT5compatibility: Codex, Claude Code, and other Agent Skills-compatible clients.6metadata:7 version: "0.1.0"8 displayName: Rust Service Security9 category: Rust10 tags: rust,security,authentication,sessions,passwords11---1213# Rust Service Security1415Use this skill when a Rust service handles identity, credentials, sessions,16cookies, privileged routes, or sensitive user data. Prefer explicit threat17checks and tests over optimistic handler code.1819## Core Workflow20211. Identify the protected asset: account access, admin area, session state,22 password change, reset token, private data, or privileged operation.232. Trace the whole flow: request parsing, credential lookup, verification,24 session mutation, redirects/responses, logs, and tests.253. Keep secrets out of logs and debug output. Use secret wrappers for passwords,26 tokens, signing keys, and session secrets.274. Run password hashing and verification with memory-hard algorithms and move28 CPU-heavy work off async reactor threads.295. Prevent user enumeration. Failed login and password reset flows should not30 reveal whether the account exists.316. Set cookie and session attributes deliberately: `HttpOnly`, `Secure`,32 `SameSite`, path, lifetime, signing/encryption, and store backend.337. Protect routes at middleware or extractor boundaries, not by repeating34 ad-hoc checks in every handler.358. Add tests for happy path, failed auth, missing session, forged cookie or36 token, and logout/session rotation behavior.3738## Password Rules3940Read `references/password-auth.md` before implementing or changing password41storage or verification.4243- Store PHC strings produced by Argon2id or the project's chosen password44 hasher; never store plaintext or reversible encrypted passwords.45- Generate salts with a secure RNG.46- Use constant, generic responses for invalid credentials.47- Use `tokio::task::spawn_blocking` or a dedicated blocking abstraction for48 expensive password operations.49- Keep password hash parameters configurable enough to upgrade over time.5051## Cookie And Session Rules5253Read `references/cookies-sessions.md` when setting, reading, deleting, signing,54or persisting cookies and sessions.5556- Never store raw credentials in cookies.57- Prefer opaque session IDs backed by a server-side store for privileged apps.58- Rotate or renew session state after login and privilege changes.59- Delete sessions server-side on logout when using a server-side store.60- Test security attributes on `Set-Cookie` headers.6162## Route Protection6364Read `references/auth-middleware.md` when adding admin routes, extractors,65guards, or framework middleware.6667Keep handlers typed:6869```rust70pub async fn admin_dashboard(user: AuthenticatedUser) -> Result<HttpResponse, AppError> {71 // Handler can assume authentication succeeded.72 Ok(HttpResponse::Ok().finish())73}74```7576Avoid optional user lookups in handlers that require authentication; make the77missing-user case impossible at the handler signature when the framework allows78it.7980## Reference Files8182- `references/password-auth.md`: Argon2, PHC strings, user enumeration, and83 blocking password work.84- `references/cookies-sessions.md`: cookie flags, signed messages, server-side85 sessions, and logout.86- `references/auth-middleware.md`: route guards, extractors, and typed session87 access.