Newsletter ·
What Rust teaches us about security
Issue #5
Learn Rust. Write code you can trust.
I was reading Astral’s excellent article on securing against supply-chain attacks.
What struck me was how many of the security principles discussed align with Rust's design principles.
- Immutability by default.
- Explicit lifetime constraints.
- Limited scopes.
- Explicit permissions.
There is a common philosophy behind all of these:
Don’t grant more freedom than is strictly necessary.
Immutability by default
In Rust, variables are immutable unless you explicitly declare them mutable.
This might seem restrictive coming from Python. But it makes you think about whether something really needs to change.
Similarly, as the article explains, pinning GitHub Actions to immutable commit hashes rather than mutable tags reduces the risk of something changing without you knowing.
The fewer things that can change unexpectedly, the easier a system is to reason about.
Least privilege
In Rust, there is an important distinction between &T and &mut T.
Having access to something doesn’t automatically give you permission to modify it.
Compare that with granting permissions to a CI/CD pipeline, a service account or an AI agent.
Does it really need write access?
Does it need access to the entire filesystem?
Does it need all those credentials?
Every unnecessary permission increases the potential blast radius and attack surface.
Rust's borrowing rules reinforce this mindset.
Limited lifetimes
Rust makes you think explicitly about how long references remain valid.
Security engineering encourages a similar mindset: prefer short-lived credentials, narrow scopes and temporary access over permanent privileges.
And when a resource goes out of scope in Rust, its cleanup is generally handled automatically.
Tying resource cleanup to scope reduces the risk of forgetting to release a lock, close a file or free memory.
Whenever possible, design systems so that safety doesn’t depend on someone remembering to do something later.
Safe defaults and explicit escape hatches
Rust requires you to explicitly use unsafe for certain operations whose memory safety the compiler cannot verify.
This doesn’t eliminate risk, but it makes certain boundaries visible and easier to audit.
I increasingly think about AI agents in the same way.
Rather than giving an agent unrestricted access and instructing it to be careful, start with restricted capabilities and explicitly grant additional permissions where needed.
Enforce the boundaries in a more systematic way, not just prompt by prompt.
A 51-year-old idea
These principles aren’t new.
In their 1975 paper, The Protection of Information in Computer Systems, Jerome Saltzer and Michael Schroeder described several fundamental security design principles, including least privilege, fail-safe defaults and economy of mechanism (simplicity).
Beyond the language
Of course, Rust’s ownership model doesn’t prevent supply-chain attacks, and its compiler is not a security sandbox.
But what I appreciate about Rust is how it reinforces a particular engineering mindset.
Students starting our cohort this week also mentioned developing this mindset as one of their learning goals.
Some of its key principles:
- Minimize mutable state.
- Restrict access and lifetimes.
- Make dangerous operations explicit.
- Reduce implicit behavior.
- Make invalid states unrepresentable where possible.
These are lessons you can take back to your Python applications, system architectures and AI agent implementations.
As we delegate more code writing to AI agents, these ideas are becoming even more important to understand and apply.
As AI makes producing code increasingly cheap, we believe there is tremendous value in studying languages that force us to think more carefully about the code we produce.
Good engineering is not just about making the right things possible. It’s about making the wrong things impossible where we can.
— Bob & Jim