Safeguard
Developer Tools•

How do I patch a vulnerable transitive crate with Cargo patch?

Use the patch section to swap a fixed copy of a crate into your whole Rust dependency graph, when a parent is stuck. Rules, steps and cleanup.

Safeguard Security Team
3 min read

Add a [patch.crates-io] entry in your workspace root Cargo.toml that points the vulnerable crate at a fixed git revision or local path. Cargo applies it across the whole graph, including transitive uses, so a stuck parent crate picks up the fix. Remove it once a release ships.

When to reach for it

You have an advisory on a crate three levels down. The parent that pulls it in pins an old range, so a conservative update cannot move it. The fix exists upstream, maybe merged but unreleased. A patch lets you ship safely now without forking the parent.

How the patch works

The Cargo reference explains that a patch adds your copy to the registry for your build. It applies across the whole dependency graph, so one version of the crate resolves for everything. Each entry uses the same syntax as dependencies:

[patch.crates-io]
vulnerable_crate = { git = "https://example.com/org/vulnerable_crate.git", rev = "0123456789abcdef0123456789abcdef01234567" }

The name, URL and hash are placeholders for your real values.

Rules to remember

RuleDetail
Only the top level countsCargo reads patch settings from the workspace root manifest and ignores those in dependencies
Version must fitThe patched copy is used only if it satisfies the requirements in the graph
Libraries repeat itConsumers of your library must add their own patch section
Config can overrideA patch in a Cargo config file takes precedence, but the docs recommend using the manifest

If the patch is not picked up, the reference suggests running a precise update for that crate to the right version.

Steps

  1. Confirm the fix commit and read it. You are putting this code into production.
  2. Check the fixed version number satisfies what the parents ask for.
  3. Add the patch entry with an explicit revision.
  4. Run a build and the test suite, then inspect the lockfile diff.
  5. Re-run your advisory check. The advisory should clear if the version is outside the affected range.
  6. Add a comment with the advisory ID, the upstream issue link and a removal date.
  7. Allow the git URL in your sources policy.

Cleanup

Once a fixed version is published, delete the patch section and update normally. The reference says exactly that for the case where a fix ships on the registry. If the fix is merged but unreleased, point the patch at the upstream repository instead of your fork.

Risks

A patch means you are running code that is not a registry release. A registry version cannot be changed after publication, a git revision can disappear if a repository is rewritten or deleted. Mirror it if the fix is critical.

Try it yourself

Click through it at your own pace, or open it full screen. Look at how a fix is proposed with the evidence a reviewer needs.

Quick check

Does a patch in a dependency's Cargo.toml affect my build?

No. Only the patch section at the workspace root is used.

Should I leave a patch in place indefinitely?

No. Treat it as temporary and set a removal date.

Safeguard can open fix pull requests through autonomous remediation, off by default with every category on Manual, and a free scan is available at gold.safeguard.sh/scan.

Sources

Last reviewed: 19 October 2026.

Never miss an update

Weekly insights on software supply chain security, delivered to your inbox.

Self-healing security runs on Safeguard.

Your first fix PR is minutes away.

No sales call required, even your agent can complete the purchase over MCP.