Malicious dependencies can inject arbitrary JavaScript into cargo-generated timing reports (GHSA-wrrj-h57r-vx9p)
Aliases: CVE-2023-40030
Published: 2023-08-24T22:15:51Z
Affected: [{'package': {'ecosystem': 'crates.io', 'name': 'cargo'}, 'ecosystem_specific': {'affected_functions': ['']}, 'ranges': [{'type': 'ECOSYSTEM', 'events': [{'introduced': '1.60.0'}, {'fixed': '1.72'}]}]}]

The Rust Security Response WG was notified that Cargo did not escape Cargo feature names when including them in the report generated by `cargo build --timings`. A malicious package included as a dependency may inject nearly arbitrary HTML here, potentially leading to XSS if the report is subsequently uploaded somewhere.

The severity of this vulnerability is "low" for users relying on dependencies from git, local paths, or alternative registries. Users who solely depend on crates.io are unaffected.


Note that **by design** Cargo allows code execution at build time, due to build scripts and procedural macros. The vulnerability in this advisory allows performing a subset of the possible damage in a harder to track down way. Your dependencies must still be trusted if you want to be protected from attacks, as it's possible to perform the same attacks with build scripts and procedural macros.

# Overview

Rust 1.60.0 [introduced](https://blog.rust-lang.org/2022/04/07/Rust-1.60.0.html#cargo---timings) `cargo build --timings`, which produces a report of how long the different steps of the build process took. It includes lists of Cargo features for each crate.

Prior to Rust 1.72, Cargo feature names were allowed to contain almost any characters (with some exceptions as used by the feature syntax), but it would produce a future incompatibility warning about them [since Rust 1.49](https://github.com/rust-lang/cargo/pull/8814). crates.io is far more stringent about what it considers a valid feature name and has not allowed such feature names.

As the feature names were included unescaped in the timings report, they could be used to inject Javascript into the page, for example with a feature name like `features = ["<img src='' onerror=alert(0)"]`. If this report were subsequently uploaded to a domain that uses credentials, the injected Javascript could access resources from the website visitor.

This issue was fixed by [turning the future incompatibility warning into an error](https://github.com/rust-lang/cargo/pull/12291).

# Affected versions

The vulnerability is present in all versions of Cargo after and including 1.60.0. Rust 1.72, to be released on August 24, will include a fix for it.

Users whose dependencies are entirely on crates.io are unaffected.

# Mitigations

We recommend users always excercise care in which package they download, by only including trusted dependencies in their projects. Please note that even with these vulnerabilities fixed, by design Cargo allows arbitrary code execution at build time thanks to build scripts and procedural macros: a malicious dependency will be able to cause damage regardless of these vulnerabilities.

crates.io has server-side checks preventing this attack, and there are no packages on crates.io exploiting these vulnerabilities. crates.io users still need to excercise care in choosing their dependencies though, as remote code execution is allowed by design there as well.