12855 shaares
325 private links
325 private links
Full of great explanations about securing (hardening) Rust code.
- treat panic behavior as part of your API. Decide explicitly whether panics should unwind or abort, and avoid uncontrolled panics.
- enable stricter Clippy lints (such as indexing and arithmetic checks) when panic freedom is important.
- use a panic hook to shutdown gracefully, set reports, cleanup, collect diagnostics and
- sanitize panic (and logging) messages while logging.
veilis a good crate for that. - avoid unbounded recursion. Prefer iterative algorithms or use depth limits.
- releases builds behave differently. They should be accordingly tested.
- audit dependencies with cargo-audit and cargo-deny
- secure allocation (for defense-in-depth when using unsafe code or FFI) with
mimallocor similar. Measure performance before enabling it globally. - use minimal runtime images if needed (rust distroless). It reduces the attack surface.
- use multi-stage builds with
cargo-chef,--lockedand proper.dockerignore - avoid alpine musl lib c as it introduces subtle runtime differences; at least be aware of them.
- use Linux
landlockto restrict filesystem access even after compromission - never run as root unless absolutely necessary. Drop as much Linux capabilities as possible.
- use Miri to detect undefined behavior, invalid pointer usage and unsafe-ode bugs.
- handle SIGTERM/SIGINT properly. Stop accepting new work, finish inflight requests, flush buffers and then exit cleanly
- protect external dependencies (database, API, cache) with circuit breakers so repeated failures don't cascade
- put explicit limits on everything: upload size, request bodies, queues, timeouts, thread counts,
- expose two health endpoints: a liveness probe and a readiness probe. The liveness probe checks if the process is alive at all, while the readiness probe checks if the process is healthy enough to handle traffic.
- Use fuzzing (
cargo-fuzz,honggfuzz), coverage (cargo-llvm-cov), unsafe detection (cargo-geiger), and memory-analysis tools alongside Rust's compile-time guarantees.