| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | use crate::common::{ |
| 12 | Http, Resp, Server, bin, input_value, launch_with, login, pick_ports, tool_on_path, |
| 13 | write_config_with, |
| 14 | }; |
| 15 | use std::fs; |
| 16 | use std::io::{Cursor, Write}; |
| 17 | use std::path::{Path, PathBuf}; |
| 18 | use std::process::{Command, Output}; |
| 19 | use std::time::{Duration, Instant}; |
| 20 | |
| 21 | const TAHINLI_PW: &str = "tahinli-password-123"; |
| 22 | const BOB_PW: &str = "bob-password-123"; |
| 23 | const ADVISORIES_TOML: &str = "\n[advisories]\nenabled = true\necosystems = [\"crates.io\"]\n"; |
| 24 | const SCAN_TIMEOUT: Duration = Duration::from_secs(30); |
| 25 | |
| 26 | |
| 27 | const OSV_RECORD: &str = r#"{ |
| 28 | "id": "RUSTSEC-TEST-0001", |
| 29 | "summary": "demo is vulnerable before 2.0.0", |
| 30 | "modified": "2026-01-01T00:00:00Z", |
| 31 | "database_specific": {"severity": "HIGH"}, |
| 32 | "affected": [{ |
| 33 | "package": {"ecosystem": "crates.io", "name": "demo"}, |
| 34 | "ranges": [{"type": "SEMVER", "events": [{"introduced": "0"}, {"fixed": "2.0.0"}]}] |
| 35 | }] |
| 36 | }"#; |
| 37 | |
| 38 | const LOCK_OLD: &str = "version = 4\n\n[[package]]\nname = \"demo\"\nversion = \"1.0.0\"\nsource = \"registry+https://github.com/rust-lang/crates.io-index\"\n"; |
| 39 | const LOCK_FIXED: &str = "version = 4\n\n[[package]]\nname = \"demo\"\nversion = \"2.0.0\"\nsource = \"registry+https://github.com/rust-lang/crates.io-index\"\n"; |
| 40 | |
| 41 | |
| 42 | fn git(cwd: &Path, args: &[&str]) -> String { |
| 43 | let out: Output = Command::new("git") |
| 44 | .current_dir(cwd) |
| 45 | .args(["-c", "credential.helper="]) |
| 46 | .args(args) |
| 47 | .env("GIT_TERMINAL_PROMPT", "0") |
| 48 | .env("GIT_AUTHOR_NAME", "tahinli") |
| 49 | .env("GIT_AUTHOR_EMAIL", "tahinli@example.test") |
| 50 | .env("GIT_COMMITTER_NAME", "tahinli") |
| 51 | .env("GIT_COMMITTER_EMAIL", "tahinli@example.test") |
| 52 | .output() |
| 53 | .expect("run git"); |
| 54 | assert!( |
| 55 | out.status.success(), |
| 56 | "git {args:?} failed:\n{}", |
| 57 | String::from_utf8_lossy(&out.stderr) |
| 58 | ); |
| 59 | String::from_utf8_lossy(&out.stdout).trim().to_owned() |
| 60 | } |
| 61 | |
| 62 | |
| 63 | fn commit_lock(repo: &Path, contents: &str, message: &str) -> String { |
| 64 | fs::write(repo.join("Cargo.lock"), contents).expect("write Cargo.lock"); |
| 65 | git(repo, &["add", "Cargo.lock"]); |
| 66 | git( |
| 67 | repo, |
| 68 | &["-c", "commit.gpgsign=false", "commit", "-q", "-m", message], |
| 69 | ); |
| 70 | git(repo, &["rev-parse", "HEAD"]) |
| 71 | } |
| 72 | |
| 73 | |
| 74 | fn dump_zip() -> Vec<u8> { |
| 75 | let mut buf = Cursor::new(Vec::new()); |
| 76 | { |
| 77 | let mut zip = zip::ZipWriter::new(&mut buf); |
| 78 | let options = zip::write::SimpleFileOptions::default(); |
| 79 | zip.start_file("RUSTSEC-TEST-0001.json", options) |
| 80 | .expect("start zip entry"); |
| 81 | zip.write_all(OSV_RECORD.as_bytes()).expect("write record"); |
| 82 | zip.finish().expect("finish zip"); |
| 83 | } |
| 84 | buf.into_inner() |
| 85 | } |
| 86 | |
| 87 | struct Booted { |
| 88 | _server: Server, |
| 89 | _root: tempfile::TempDir, |
| 90 | dir: PathBuf, |
| 91 | http: Http, |
| 92 | port: u16, |
| 93 | } |
| 94 | |
| 95 | async fn boot() -> Booted { |
| 96 | if !tool_on_path("git") { |
| 97 | panic!("`git` was not found on PATH"); |
| 98 | } |
| 99 | |
| 100 | let root = tempfile::Builder::new() |
| 101 | .prefix("adv2e") |
| 102 | .tempdir_in("/tmp") |
| 103 | .expect("temp dir"); |
| 104 | let dir = root.path().to_path_buf(); |
| 105 | |
| 106 | let dump_dir = dir.join("advisories").join("crates.io"); |
| 107 | fs::create_dir_all(&dump_dir).expect("dump dir"); |
| 108 | fs::write(dump_dir.join("all.zip"), dump_zip()).expect("write dump"); |
| 109 | |
| 110 | let ports = pick_ports(); |
| 111 | let config = write_config_with(&dir, ports, ADVISORIES_TOML); |
| 112 | run_admin(&config, "tahinli", &["--admin"], TAHINLI_PW); |
| 113 | run_admin(&config, "bob", &[], BOB_PW); |
| 114 | let (server, ports) = launch_with(&dir, ports, ADVISORIES_TOML).await; |
| 115 | Booted { |
| 116 | _server: server, |
| 117 | _root: root, |
| 118 | dir, |
| 119 | http: Http::new(ports.http), |
| 120 | port: ports.http, |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | fn run_admin(config: &Path, username: &str, extra: &[&str], password: &str) { |
| 125 | let out = Command::new(bin()) |
| 126 | .arg("--config") |
| 127 | .arg(config) |
| 128 | .args(["admin", "create-user", username]) |
| 129 | .args(extra) |
| 130 | .env("FORGE_PASSWORD", password) |
| 131 | .output() |
| 132 | .expect("run admin create-user"); |
| 133 | assert!( |
| 134 | out.status.success(), |
| 135 | "create-user {username} failed:\n{}", |
| 136 | String::from_utf8_lossy(&out.stderr) |
| 137 | ); |
| 138 | } |
| 139 | |
| 140 | async fn create_token(http: &Http, session: &str) -> String { |
| 141 | let page = http.get_cookie("/settings/keys", session).await; |
| 142 | assert_eq!(page.status, 200, "GET /settings/keys"); |
| 143 | let csrf = input_value(&page.body, "csrf"); |
| 144 | let res = http |
| 145 | .post_form( |
| 146 | "/settings/tokens", |
| 147 | &[ |
| 148 | ("csrf", &csrf), |
| 149 | ("name", "advisory-token"), |
| 150 | ("expires", "30"), |
| 151 | ], |
| 152 | Some(session), |
| 153 | ) |
| 154 | .await; |
| 155 | assert_eq!(res.status, 200, "create token: {}", res.body); |
| 156 | let doc = scraper::Html::parse_document(&res.body); |
| 157 | let selector = scraper::Selector::parse("pre.code code").expect("selector"); |
| 158 | doc.select(&selector) |
| 159 | .next() |
| 160 | .expect("token shown after creation") |
| 161 | .text() |
| 162 | .collect::<String>() |
| 163 | .trim() |
| 164 | .to_owned() |
| 165 | } |
| 166 | |
| 167 | async fn new_repo(http: &Http, session: &str, name: &str) { |
| 168 | let page = http.get_cookie("/new", session).await; |
| 169 | assert_eq!(page.status, 200, "GET /new"); |
| 170 | let csrf = input_value(&page.body, "csrf"); |
| 171 | let res = http |
| 172 | .post_form( |
| 173 | "/new", |
| 174 | &[ |
| 175 | ("csrf", &csrf), |
| 176 | ("name", name), |
| 177 | ("description", ""), |
| 178 | ("visibility", "public"), |
| 179 | ("default_branch", "main"), |
| 180 | ], |
| 181 | Some(session), |
| 182 | ) |
| 183 | .await; |
| 184 | assert_eq!(res.status, 303, "create repo {name}: {}", res.body); |
| 185 | } |
| 186 | |
| 187 | |
| 188 | async fn security_page(http: &Http, session: &str) -> Resp { |
| 189 | http.get_cookie("/tahinli/app/security", session).await |
| 190 | } |
| 191 | |
| 192 | |
| 193 | async fn wait_for_text(http: &Http, session: &str, text: &str) -> String { |
| 194 | let deadline = Instant::now() + SCAN_TIMEOUT; |
| 195 | loop { |
| 196 | let page = security_page(http, session).await; |
| 197 | if page.status == 200 && page.body.contains(text) { |
| 198 | return page.body; |
| 199 | } |
| 200 | assert!( |
| 201 | Instant::now() < deadline, |
| 202 | "timed out waiting for {text:?} on the security page (last status {})", |
| 203 | page.status |
| 204 | ); |
| 205 | tokio::time::sleep(Duration::from_millis(200)).await; |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | |
| 210 | async fn post_alert_action(http: &Http, session: &str, action: &str) -> Resp { |
| 211 | let page = security_page(http, session).await; |
| 212 | assert_eq!(page.status, 200, "GET security page"); |
| 213 | let csrf = input_value(&page.body, "csrf"); |
| 214 | http.post_form( |
| 215 | &format!("/tahinli/app/security/{action}"), |
| 216 | &[ |
| 217 | ("csrf", &csrf), |
| 218 | ("advisory", "RUSTSEC-TEST-0001"), |
| 219 | ("ecosystem", "crates.io"), |
| 220 | ("package", "demo"), |
| 221 | ("version", "1.0.0"), |
| 222 | ], |
| 223 | Some(session), |
| 224 | ) |
| 225 | .await |
| 226 | } |
| 227 | |
| 228 | #[tokio::test] |
| 229 | async fn advisory_alert_opens_on_push_and_follows_dismiss_reopen_and_fix() { |
| 230 | let b = boot().await; |
| 231 | let tahinli = login(&b.http, "tahinli", TAHINLI_PW).await; |
| 232 | let token = create_token(&b.http, &tahinli).await; |
| 233 | new_repo(&b.http, &tahinli, "app").await; |
| 234 | |
| 235 | let work = b.dir.join("app-work"); |
| 236 | let remote = format!( |
| 237 | "http://tahinli:{token}@127.0.0.1:{}/tahinli/app.git", |
| 238 | b.port |
| 239 | ); |
| 240 | git( |
| 241 | &b.dir, |
| 242 | &["clone", "-q", &remote, work.to_str().expect("utf-8 path")], |
| 243 | ); |
| 244 | commit_lock(&work, LOCK_OLD, "lock demo 1.0.0"); |
| 245 | git(&work, &["push", "-q", "origin", "HEAD:refs/heads/main"]); |
| 246 | |
| 247 | |
| 248 | let page = wait_for_text(&b.http, &tahinli, "RUSTSEC-TEST-0001").await; |
| 249 | assert!(page.contains(">Open</span>"), "the alert is open"); |
| 250 | assert!(page.contains("demo"), "the vulnerable package is listed"); |
| 251 | |
| 252 | |
| 253 | let dismissed = post_alert_action(&b.http, &tahinli, "dismiss").await; |
| 254 | assert_eq!(dismissed.status, 303, "dismiss: {}", dismissed.body); |
| 255 | let page = wait_for_text(&b.http, &tahinli, ">Dismissed</span>").await; |
| 256 | assert!(page.contains("Reopen")); |
| 257 | |
| 258 | let reopened = post_alert_action(&b.http, &tahinli, "reopen").await; |
| 259 | assert_eq!(reopened.status, 303, "reopen: {}", reopened.body); |
| 260 | wait_for_text(&b.http, &tahinli, ">Open</span>").await; |
| 261 | |
| 262 | let bob = login(&b.http, "bob", BOB_PW).await; |
| 263 | let refused = security_page(&b.http, &bob).await; |
| 264 | assert_eq!(refused.status, 404, "a reader without Write gets 404"); |
| 265 | |
| 266 | |
| 267 | commit_lock(&work, LOCK_FIXED, "lock demo 2.0.0"); |
| 268 | git(&work, &["push", "-q", "origin", "HEAD:refs/heads/main"]); |
| 269 | wait_for_text(&b.http, &tahinli, ">Fixed</span>").await; |
| 270 | } |