Cargo config files pulled in with `include` are not read: a vendored crate is reported as patched, and attested, while cargo builds the unpatched copy

Open
#2,305 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
45/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
rust
Domain
devtools

Research direction

Start with config_layer_paths in core/formats/cargo/source_replacement.rs, then inspect its callers in the Cargo crawler, build cache, hosted rewriter, and user patch gate. The issue supplies three failing tests; run cargo test -p socket-patch-core --lib -- from_an_included_config to reproduce them. Done means all config readers follow Cargo's include order and path rules, while respecting the repository bound described in the planned fix.

Written by the indexing model from the issue text.

Description

agent:triaged arch-audit bug priority:p2

[agent] Found by the architecture refactor routine (ledger discussion #2244, lead L46), on main at cfc870f.

Problem

Cargo reads more config files than socket-patch does. A .cargo/config.toml may pull other files in with the top-level include key, and cargo 1.97.0 follows it on stable (cargo 1.93.1, the toolchain this repository pins, still ignores the key). socket-patch reads .cargo/config or .cargo/config.toml in each ancestor and in $CARGO_HOME, and nothing those files include. So whatever a project sets in an included file is invisible to it, and four mechanisms decide wrongly:

  1. Which copy cargo builds. A cargo vendor project whose source replacement sits in an included file builds from vendor/. An agent-mode scan or apply patches the copy in the shared registry cache instead, which cargo does not build, and reports Applied 1 patch and Patched 1 package, exit 0. apply --check says Patches are in sync, and vex writes a not_affected statement. The build is unpatched.
  2. Which build directory holds the compiled copy. With build.target-dir in an included file, the crate is patched, but its fingerprints in that directory are not deleted, so the next cargo build compiles nothing and the binary keeps the pre-patch code. No cargo_build_cache_stale warning is raised.
  3. Whether the project overrides the crate. A paths override in an included file does not protect the crate: the scan patches the registry copy and vex attests it, while cargo builds the override.
  4. Whether a hosted pin would break an offline build. A hosted scan refuses to pin a crate of a project that builds from a directory source (redirect_cargo_directory_source). With the same source replacement in an included file it writes the pin.

CLI_CONTRACT.md ("Cargo and Go in agent mode") states the rule for the first: "The directory source is the one cargo builds from: the [source.crates-io] replace-with chain of every .cargo/config* cargo reads (the working directory and each ancestor, then $CARGO_HOME's), wherever it points (#338). A vendor/ no config wires is not crawled, and a configured directory that is missing yields nothing rather than the registry copy cargo ignores."

Who it hurts

  • A project that keeps the output of cargo vendor in a file of its own (cargo vendor > .cargo/vendor.toml) and includes it, or shares one config file between several workspaces, or keeps developer-local settings in an optional included file. include exists for these layouts.
  • Their release build: it ships the vulnerable crate while the scan step says Patched 1 package.
  • Anyone who reads the VEX document: it says not_affected for a crate the build takes unpatched.

Evidence on main

The binary is built from cfc870f. cargo is 1.97.0, offline, with a CARGO_HOME of its own that holds fnv-1.0.7. The app prints fnv::FnvHasher::default().finish(); the patch changes the offset basis in lib.rs to 0x1111111111111111, so the output shows which bytes the build linked. The patch API is a local stand-in that serves the one patch.

The source replacement in an included file:

$ cat .cargo/config.toml
include = ["vendor.toml"]
$ cat .cargo/vendor.toml
[source.crates-io]
replace-with = "vendored-sources"

[source.vendored-sources]
directory = "vendor"
$ cargo metadata --offline --format-version 1 | jq -r '.packages[] | select(.name=="fnv") | .manifest_path'
<w>/app/vendor/fnv/Cargo.toml
$ cargo build --offline -q && ./target/debug/app
cbf29ce484222325

$ socket-patch scan --mode agent
Found 1 package (1 cargo)
Applied 1 patch.
  HIGH  [email protected]  CVE-2026-0001
Next steps:
  1. Commit .socket/ to keep the patches.
  2. Run `socket-patch apply` after every `cargo fetch`, locally and in CI. A fresh install removes the patches.

Patched 1 package, fixing 1 vulnerability (1 high).
…                                                           (exit 0)

$ grep -c 0x1111111111111111 vendor/fnv/lib.rs $CARGO_HOME/registry/src/*/fnv-1.0.7/lib.rs
vendor/fnv/lib.rs:0
<w>/cargo/registry/src/index.crates.io-1949cf8c6b5b557f/fnv-1.0.7/lib.rs:1
$ cargo build --offline -q && ./target/debug/app
cbf29ce484222325

$ socket-patch apply --check
Patches are in sync (1 patch checked).                      (exit 0)
$ socket-patch vex --product pkg:generic/app@1 -O - | jq -c '.statements[] | {status, impact_statement}'
{"status":"not_affected","impact_statement":"Patched via Socket patch cccccccc-0000-4000-8000-000000000001"}

The same project with the two tables in .cargo/config.toml itself:

$ socket-patch scan --mode agent
…
Patched 1 package, fixing 1 vulnerability (1 high).         (exit 0)
$ grep -c 0x1111111111111111 vendor/fnv/lib.rs $CARGO_HOME/registry/src/*/fnv-1.0.7/lib.rs
vendor/fnv/lib.rs:1
<w>/cargo/registry/src/index.crates.io-1949cf8c6b5b557f/fnv-1.0.7/lib.rs:0
$ cargo build --offline -q && ./target/debug/app
1111111111111111

The build directory (no vendoring; .cargo/config.toml is include = ["build.toml"], and .cargo/build.toml sets [build] target-dir = "out"):

$ cargo build --offline -q && ./out/debug/app
cbf29ce484222325
$ socket-patch scan --mode agent --json | jq -c '[.events[] | .action], [.warnings[].code]'
["downloaded","applied"] []
$ cargo build --offline && ./out/debug/app          # compiles nothing
cbf29ce484222325

With target-dir = "out" in .cargo/config.toml itself, the build after the scan compiles fnv and app again and prints 1111111111111111.

The override (.cargo/config.toml is include = ["dev.toml"], and .cargo/dev.toml is paths = ["../fnv-fork"]; the fork's constant is 0x2222222222222222):

$ cargo build --offline -q && ./target/debug/app
2222222222222222
$ socket-patch scan --mode agent --json | jq -c '[.events[] | [.action, .errorCode]], [.warnings[].code]'
[["downloaded",null],["applied",null]] []
$ cargo build --offline -q && ./target/debug/app
2222222222222222
$ socket-patch vex --product pkg:generic/app@1 -O - | jq -c '.statements[] | {status, impact_statement}'
{"status":"not_affected","impact_statement":"Patched via Socket patch cccccccc-0000-4000-8000-000000000001"}

With paths in .cargo/config.toml itself the scan skips the crate with user_patch_conflict and writes nothing.

Hosted mode, over the benchmark harness's Cargo project (socket-patch-bench serve cargo/hosted), with a vendor/ directory and the same two layouts:

direct    exit 0  events [skipped pkg:cargo/[email protected] redirect_unconfirmed]  warnings [redirect_cargo_directory_source]
include   exit 0  events [applied pkg:cargo/[email protected]]                       warnings []

What cargo 1.97.0 does with include, measured with cargo metadata over build.target-dir:

Case Result
include = ["a.toml"], include = [{ path = "a.toml" }], [[include]] with path each is followed; a bare string (include = "a.toml") is an error
the including file and an included file set the same key the including file wins
include = ["a.toml", "b.toml"], both set the key b.toml wins
a relative value in .cargo/sub/c.toml resolves against .cargo/: the parent of the included file's directory
config.toml includes a.toml, which includes b.toml followed
{ path = "missing.toml", optional = true } skipped
a required file that is missing, or a path that does not end in .toml cargo fails to load its configuration
include in $CARGO_HOME/config.toml followed

Cause

Three readers of cargo's config, and none knows the key:

  • config_layer_paths (core formats/cargo/source_replacement.rs, line 114) lists .cargo/config or .cargo/config.toml for each ancestor, then $CARGO_HOME's. Its callers are the crawler's resolve_for (core crawlers/cargo_crawler.rs, line 373) and ConfigDirs::read (core utils/cargo_build_cache.rs, line 165), which reads build.target-dir and build.build-dir.
  • The hosted rewriter judges the project's own config file as a single layer (core patch/redirect/mod.rs, line 2163) and asks the crawler for the configs outside the project (crates_io_source_for, called from CLI commands/scan/hosted.rs, line 2045).
  • The project-patch gate reads .cargo/config.toml and .cargo/config by name in each directory it walks (cargo, core patch/user_patches.rs, line 1113).

The failing tests

Three unit tests, each beside the code it exercises. All three fail on main:

// core crawlers/cargo_crawler.rs
#[test]
fn a_directory_source_from_an_included_config_is_the_source() {
    let tmp = tempfile::tempdir().unwrap();
    let root = std::fs::canonicalize(tmp.path()).unwrap();
    std::fs::create_dir_all(root.join(".cargo")).unwrap();
    std::fs::create_dir_all(root.join("vendor")).unwrap();
    std::fs::write(root.join(".cargo/config.toml"), "include = [\"vendor.toml\"]\n").unwrap();
    std::fs::write(
        root.join(".cargo/vendor.toml"),
        "[source.crates-io]\nreplace-with = \"vendored-sources\"\n\n\
         [source.vendored-sources]\ndirectory = \"vendor\"\n",
    )
    .unwrap();
    assert_eq!(
        resolve_for(crate::utils::repo_root::ReadScope::Workspace, &root, None),
        CratesIoSource::Directory(root.join("vendor"))
    );
}

// core utils/cargo_build_cache.rs
#[test]
fn a_target_dir_from_an_included_config_is_a_build_dir() {
    let t = tempfile::tempdir().unwrap();
    let root = std::fs::canonicalize(t.path()).unwrap();
    std::fs::create_dir_all(root.join(".cargo")).unwrap();
    std::fs::create_dir_all(root.join("out")).unwrap();
    std::fs::write(root.join(".cargo/config.toml"), "include = [\"build.toml\"]\n").unwrap();
    std::fs::write(root.join(".cargo/build.toml"), "[build]\ntarget-dir = \"out\"\n").unwrap();
    let d = build_dirs_with(crate::utils::repo_root::ReadScope::Workspace, &root, &|_| None, None);
    assert_eq!(d.dirs, vec[root.join("out")]);
}

// core patch/user_patches.rs
#[tokio::test]
async fn a_cargo_paths_override_from_an_included_config_protects_cargo() {
    let tmp = tempfile::tempdir().unwrap();
    write(tmp.path(), ".cargo/config.toml", "include = [\"dev.toml\"]\n");
    write(tmp.path(), ".cargo/dev.toml", "paths = [\"../fnv-fork\"]\n");
    let index = UserPatchIndex::discover(tmp.path()).await;
    assert!(!index.conflicts("pkg:cargo/[email protected]").is_empty(), "{index:?}");
}
$ cargo test -p socket-patch-core --lib -- from_an_included_config
test utils::cargo_build_cache::tests::a_target_dir_from_an_included_config_is_a_build_dir ... FAILED
test crawlers::cargo_crawler::tests::a_directory_source_from_an_included_config_is_the_source ... FAILED
test patch::user_patches::tests::a_cargo_paths_override_from_an_included_config_protects_cargo ... FAILED

---- utils::cargo_build_cache::tests::a_target_dir_from_an_included_config_is_a_build_dir stdout ----
assertion `left == right` failed
  left: []
 right: ["/tmp/.tmpnpyq9a/out"]

---- crawlers::cargo_crawler::tests::a_directory_source_from_an_included_config_is_the_source stdout ----
assertion `left == right` failed
  left: Registry
 right: Directory("/tmp/.tmpChhZiC/vendor")

---- patch::user_patches::tests::a_cargo_paths_override_from_an_included_config_protects_cargo stdout ----
UserPatchIndex { entries: [] }

Planned fix

One function lists the config files cargo reads, and every reader takes its files from it.

  • config_layer_paths expands each file it lists into that file followed by the files it includes, in cargo's order of precedence: the including file first, then its include entries from last to first, each expanded the same way. A relative value in an included file resolves against the parent of that file's directory. An optional file that is missing is skipped. A cycle stops the expansion.
  • The repository bound stays: in a workspace run an included file outside the git repository root is not read, and the run says so the way it does for a config above the root (config_above_repo).
  • The hosted rewriter and the project-patch gate take their layers from the same list, so a directory source in an included file refuses the pin, and a paths override or a [patch] table in an included file protects what it names.
  • The three tests above, and one end-to-end test with a real cargo build beside tests/e2e_safety_cargo_build.rs, gated on a cargo that follows include.

What the fix changes: which copy an agent-mode apply patches, which fingerprints it deletes, which crates the gate protects and which pins hosted mode refuses, all for projects that use include. A project without the key is read as before.


Generated by Claude Code

Dominant language
Rust
Stars
9
Forks
2
Avg merge
1d 8h
Merged PRs (30d)
313

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from SocketDev/socket-patch

All issues in SocketDev/socket-patch

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.