Skip to content

Commit 3ee4104

Browse files
committed
Start adding a whitelist for rustc dependencies
1 parent 2cb8c5f commit 3ee4104

File tree

4 files changed

+82
-9
lines changed

4 files changed

+82
-9
lines changed

src/Cargo.lock

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/tools/tidy/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,8 @@
22
name = "tidy"
33
version = "0.1.0"
44
authors = ["Alex Crichton <alex@alexcrichton.com>"]
5+
6+
[dependencies]
7+
serde = "1.0.8"
8+
serde_derive = "1.0.8"
9+
serde_json = "1.0.2"

src/tools/tidy/src/deps.rs

Lines changed: 67 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ use std::fs::File;
1414
use std::io::Read;
1515
use std::path::Path;
1616

17+
use std::process::Command;
18+
19+
use serde_json;
20+
1721
static LICENSES: &'static [&'static str] = &[
1822
"MIT/Apache-2.0",
1923
"MIT / Apache-2.0",
@@ -44,31 +48,68 @@ static EXCEPTIONS: &'static [&'static str] = &[
4448
"clippy_lints", // MPL-2.0 rls
4549
];
4650

51+
// Whitelist of crates rustc is allowed to depend on. Avoid adding to the list if possible.
52+
static WHITELIST: &'static [(&'static str, &'static str)] = &[];
53+
54+
// Some type for Serde to deserialize the output of `cargo metadata` to...
55+
56+
#[derive(Deserialize)]
57+
struct Output {
58+
packages: Vec<Package>,
59+
_resolve: String,
60+
}
61+
62+
#[derive(Deserialize)]
63+
struct Package {
64+
_id: String,
65+
name: String,
66+
version: String,
67+
_source: Option<String>,
68+
_manifest_path: String,
69+
}
70+
71+
/// Checks the dependency at the given path. Changes `bad` to `true` if a check failed.
72+
///
73+
/// Specifically, this checks that the license is correct and that the dependencies are on the
74+
/// whitelist.
4775
pub fn check(path: &Path, bad: &mut bool) {
76+
// Check licences
4877
let path = path.join("vendor");
4978
assert!(path.exists(), "vendor directory missing");
5079
let mut saw_dir = false;
51-
'next_path: for dir in t!(path.read_dir()) {
80+
for dir in t!(path.read_dir()) {
5281
saw_dir = true;
5382
let dir = t!(dir);
5483

5584
// skip our exceptions
56-
for exception in EXCEPTIONS {
57-
if dir.path()
85+
if EXCEPTIONS.iter().any(|exception| {
86+
dir.path()
5887
.to_str()
5988
.unwrap()
6089
.contains(&format!("src/vendor/{}", exception))
61-
{
62-
continue 'next_path;
63-
}
90+
}) {
91+
continue;
6492
}
6593

6694
let toml = dir.path().join("Cargo.toml");
67-
if !check_license(&toml) {
68-
*bad = true;
69-
}
95+
*bad = *bad || !check_license(&toml);
7096
}
7197
assert!(saw_dir, "no vendored source");
98+
99+
// Check dependencies
100+
let deps = get_deps(&path);
101+
*bad = *bad
102+
|| deps.iter().any(
103+
|&Package {
104+
ref name,
105+
ref version,
106+
..
107+
}| {
108+
WHITELIST
109+
.iter()
110+
.all(|&(wname, wversion)| name != wname || version != wversion)
111+
},
112+
);
72113
}
73114

74115
fn check_license(path: &Path) -> bool {
@@ -109,3 +150,20 @@ fn extract_license(line: &str) -> String {
109150
"bad-license-parse".into()
110151
}
111152
}
153+
154+
fn get_deps(path: &Path) -> Vec<Package> {
155+
// Run `cargo metadata` to get the set of dependencies
156+
let output = Command::new("cargo")
157+
.arg("metadata")
158+
.arg("--format-version")
159+
.arg("1")
160+
.arg("--manifest-path")
161+
.arg(path.join("Cargo.toml"))
162+
.output()
163+
.expect("Unable to run `cargo metadata`")
164+
.stdout;
165+
let output = String::from_utf8_lossy(&output);
166+
let output: Output = serde_json::from_str(&output).unwrap();
167+
168+
output.packages
169+
}

src/tools/tidy/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@
1515
1616
#![deny(warnings)]
1717

18+
extern crate serde;
19+
extern crate serde_json;
20+
#[macro_use]
21+
extern crate serde_derive;
22+
1823
use std::fs;
1924

2025
use std::path::Path;

0 commit comments

Comments
 (0)