1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
//! GitHub, GitLab, and Bitbucket release source tests.
//!
//! `src/release/sources/{github,gitlab,bitbucket}.rs` are compiled into
//! `release::sources` by the integration wave; until that wiring lands this
//! test includes the modules directly and provides the `crate::` paths they
//! use.
use std::collections::{BTreeMap, HashMap, VecDeque};
use std::sync::{Arc, Mutex};
use anyhow::Result;
use async_trait::async_trait;
use serde_json::{Value, json};
mod release {
pub mod artifact {
pub use vuln_notif::release::artifact::normalize_version;
}
pub mod http {
pub use vuln_notif::release::http::{HTTP_TIMEOUT, USER_AGENT};
}
pub mod source {
pub use vuln_notif::release::source::*;
}
}
#[path = "../src/release/sources/bitbucket.rs"]
mod bitbucket;
#[path = "../src/release/sources/github.rs"]
mod github;
#[path = "../src/release/sources/gitlab.rs"]
mod gitlab;
use bitbucket::BitbucketSource;
use github::{GitHttp, GitResponse, GithubSource};
use gitlab::GitlabSource;
use release::source::{ProjectRef, ReleaseSource, SourceCapabilities, VersionInfo};
const GITHUB_RELEASES: &str = include_str!("fixtures/sources/git/github-releases.json");
const GITHUB_RELEASES_PAGE2: &str = include_str!("fixtures/sources/git/github-releases-page2.json");
const GITHUB_TAGS: &str = include_str!("fixtures/sources/git/github-tags.json");
const GITHUB_RELEASE_V2: &str = include_str!("fixtures/sources/git/github-release-v2.json");
const GITHUB_REPO: &str = include_str!("fixtures/sources/git/github-repo.json");
const GITHUB_REPO_ARCHIVED: &str = include_str!("fixtures/sources/git/github-repo-archived.json");
const GITLAB_RELEASES: &str = include_str!("fixtures/sources/git/gitlab-releases.json");
const GITLAB_TAGS: &str = include_str!("fixtures/sources/git/gitlab-tags.json");
const GITLAB_PROJECT_ARCHIVED: &str =
include_str!("fixtures/sources/git/gitlab-project-archived.json");
const BITBUCKET_TAGS: &str = include_str!("fixtures/sources/git/bitbucket-tags.json");
const BITBUCKET_DOWNLOADS_1: &str =
include_str!("fixtures/sources/git/bitbucket-downloads-page1.json");
const BITBUCKET_DOWNLOADS_2: &str =
include_str!("fixtures/sources/git/bitbucket-downloads-page2.json");
const BITBUCKET_REPO: &str = include_str!("fixtures/sources/git/bitbucket-repo.json");
const GITHUB_REPO_URL: &str = "https://api.github.com/repos/acme/widgets";
const BITBUCKET_REPO_URL: &str = "https://api.bitbucket.org/2.0/repositories/acme/widgets";
fn fixture(raw: &str) -> Value {
serde_json::from_str(raw).expect("fixture JSON")
}
#[derive(Clone)]
struct FakeResponse {
status: u16,
headers: BTreeMap<String, String>,
body: Value,
}
#[derive(Clone, Debug)]
struct FakeCall {
url: String,
headers: Vec<(String, String)>,
}
/// Scripted git-host transport: replies are queued per URL, and every request
/// is recorded so tests can assert pagination and auth headers.
#[derive(Default)]
struct FakeHttp {
replies: Mutex<HashMap<String, VecDeque<FakeResponse>>>,
calls: Mutex<Vec<FakeCall>>,
}
impl FakeHttp {
fn new() -> Arc<Self> {
Arc::new(Self::default())
}
fn enqueue(&self, url: &str, body: Value) {
self.enqueue_response(url, 200, &[], body);
}
fn enqueue_response(&self, url: &str, status: u16, headers: &[(&str, &str)], body: Value) {
self.replies
.lock()
.unwrap()
.entry(url.to_owned())
.or_default()
.push_back(FakeResponse {
status,
headers: headers
.iter()
.map(|(name, value)| ((*name).to_owned(), (*value).to_owned()))
.collect::<BTreeMap<_, _>>(),
body,
});
}
fn calls(&self) -> Vec<FakeCall> {
self.calls.lock().unwrap().clone()
}
fn urls(&self) -> Vec<String> {
self.calls().into_iter().map(|call| call.url).collect()
}
}
#[async_trait]
impl GitHttp for FakeHttp {
async fn get_json(&self, url: &str, headers: &[(String, String)]) -> Result<GitResponse> {
self.calls.lock().unwrap().push(FakeCall {
url: url.to_owned(),
headers: headers.to_vec(),
});
let reply = self
.replies
.lock()
.unwrap()
.get_mut(url)
.and_then(VecDeque::pop_front)
.unwrap_or_else(|| panic!("unexpected HTTP request: {url}"));
Ok(GitResponse {
status: reply.status,
headers: reply.headers,
body: reply.body,
})
}
}
fn github_project() -> ProjectRef {
ProjectRef {
purl_type: "github".to_owned(),
name: "acme/widgets".to_owned(),
purl: "pkg:github/acme/widgets".to_owned(),
}
}
fn gitlab_project() -> ProjectRef {
ProjectRef {
purl_type: "gitlab".to_owned(),
name: "acme/widgets".to_owned(),
purl: "pkg:gitlab/acme/widgets".to_owned(),
}
}
fn bitbucket_project() -> ProjectRef {
ProjectRef {
purl_type: "bitbucket".to_owned(),
name: "acme/widgets".to_owned(),
purl: "pkg:bitbucket/acme/widgets".to_owned(),
}
}
fn version(raw: &str, normalized: &str) -> VersionInfo {
VersionInfo {
raw: raw.to_owned(),
version: normalized.to_owned(),
deprecated: false,
reason: String::new(),
}
}
fn flagged(raw: &str, normalized: &str, reason: &str) -> VersionInfo {
VersionInfo {
raw: raw.to_owned(),
version: normalized.to_owned(),
deprecated: true,
reason: reason.to_owned(),
}
}
fn github_url(suffix: &str) -> String {
format!("{GITHUB_REPO_URL}{suffix}")
}
fn gitlab_url(encoded_path: &str, suffix: &str) -> String {
format!("https://gitlab.com/api/v4/projects/{encoded_path}{suffix}")
}
fn bitbucket_url(suffix: &str) -> String {
format!("{BITBUCKET_REPO_URL}{suffix}")
}
#[tokio::test]
async fn github_versions_merge_releases_and_tags_newest_first() {
let http = FakeHttp::new();
http.enqueue(
&github_url("/releases?per_page=100"),
fixture(GITHUB_RELEASES),
);
http.enqueue(&github_url("/tags?per_page=100"), fixture(GITHUB_TAGS));
let source = GithubSource::new(http.clone(), None);
let versions = source.versions(&github_project()).await.expect("versions");
let names: Vec<&str> = versions.iter().map(|info| info.version.as_str()).collect();
assert_eq!(names, ["3.0.0", "2.1.0-rc.1", "2.0.0", "1.8.0"]);
assert!(versions[0].deprecated);
assert_eq!(versions[0].reason, "draft release");
assert!(versions[1].deprecated);
assert_eq!(versions[1].reason, "prerelease release");
assert!(!versions[2].deprecated);
assert_eq!(versions[2].raw, "v2.0.0");
}
#[tokio::test]
async fn github_versions_follow_link_header_pagination() {
let page2 = github_url("/releases?per_page=100&page=2");
let link = format!("<{page2}>; rel=\"next\", <{page2}>; rel=\"last\"");
let http = FakeHttp::new();
http.enqueue_response(
&github_url("/releases?per_page=100"),
200,
&[("Link", link.as_str())],
fixture(GITHUB_RELEASES),
);
http.enqueue(&page2, fixture(GITHUB_RELEASES_PAGE2));
http.enqueue(&github_url("/tags?per_page=100"), fixture(GITHUB_TAGS));
let source = GithubSource::new(http.clone(), None);
let versions = source.versions(&github_project()).await.expect("versions");
let names: Vec<&str> = versions.iter().map(|info| info.version.as_str()).collect();
assert_eq!(names, ["3.0.0", "2.1.0-rc.1", "2.0.0", "1.9.0", "1.8.0"]);
assert_eq!(
http.urls(),
[
github_url("/releases?per_page=100"),
page2,
github_url("/tags?per_page=100"),
]
);
}
#[tokio::test]
async fn github_artifacts_use_declared_digest_then_url_and_size() {
let http = FakeHttp::new();
http.enqueue(
&github_url("/releases/tags/v2.0.0"),
fixture(GITHUB_RELEASE_V2),
);
let source = GithubSource::new(http.clone(), None);
let artifacts = source
.artifacts(&github_project(), &version("v2.0.0", "2.0.0"))
.await
.expect("artifacts");
assert_eq!(artifacts.len(), 4);
assert_eq!(
artifacts[0].sha256.as_deref(),
Some("9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08")
);
assert_eq!(artifacts[0].size, Some(1234));
assert_eq!(artifacts[1].sha256, None);
assert_eq!(artifacts[1].size, Some(2345));
assert_eq!(artifacts[2].file_name, "widgets-v2.0.0.tar.gz");
assert_eq!(artifacts[2].sha256, None);
assert_eq!(artifacts[2].size, None);
assert_eq!(artifacts[2].mime_type.as_deref(), Some("application/gzip"));
assert!(artifacts[3].url.ends_with("/zipball/v2.0.0"));
}
#[tokio::test]
async fn github_artifacts_fall_back_to_source_archives() {
let http = FakeHttp::new();
http.enqueue_response(
&github_url("/releases/tags/v1.9.0"),
404,
&[],
json!({"message": "Not Found"}),
);
let source = GithubSource::new(http.clone(), None);
let artifacts = source
.artifacts(&github_project(), &version("v1.9.0", "1.9.0"))
.await
.expect("archives");
assert_eq!(artifacts.len(), 2);
assert!(artifacts[0].url.ends_with("/tarball/v1.9.0"));
assert!(artifacts[1].url.ends_with("/zipball/v1.9.0"));
assert!(
artifacts
.iter()
.all(|artifact| artifact.sha256.is_none() && artifact.size.is_none())
);
}
#[tokio::test]
async fn github_rate_limit_403_propagates_without_exposing_token() {
let token = "ghp_super_secret_token";
let http = FakeHttp::new();
http.enqueue_response(
&github_url("/releases?per_page=100"),
403,
&[
("x-ratelimit-remaining", "0"),
("x-ratelimit-reset", "1700000000"),
],
json!({"message": "API rate limit exceeded"}),
);
let source = GithubSource::new(http.clone(), Some(token.to_owned()));
let error = source
.versions(&github_project())
.await
.expect_err("rate limit");
let message = format!("{error:?}");
assert!(message.contains("rate limit"), "{message}");
assert!(message.contains("reset=1700000000"), "{message}");
assert!(!message.contains(token), "{message}");
let calls = http.calls();
assert_eq!(calls.len(), 1);
let authorization = calls[0]
.headers
.iter()
.find(|(name, _)| name == "Authorization")
.map(|(_, value)| value.as_str());
assert_eq!(authorization, Some("Bearer ghp_super_secret_token"));
}
#[tokio::test]
async fn github_statuses_mark_draft_prerelease_and_archived() {
let http = FakeHttp::new();
http.enqueue(GITHUB_REPO_URL, fixture(GITHUB_REPO_ARCHIVED));
let source = GithubSource::new(http.clone(), None);
let statuses = source
.statuses(
&github_project(),
&flagged("v2.1.0-rc.1", "2.1.0-rc.1", "prerelease release"),
)
.await
.expect("statuses");
assert_eq!(statuses.len(), 2);
assert_eq!(statuses[0].status, "deprecated");
assert_eq!(statuses[0].kind.as_deref(), Some("prerelease"));
assert_eq!(statuses[1].status, "archived");
assert_eq!(statuses[1].kind.as_deref(), Some("archived"));
let statuses = source
.statuses(
&github_project(),
&flagged("v3.0.0", "3.0.0", "draft release"),
)
.await
.expect("statuses");
assert_eq!(statuses[0].kind.as_deref(), Some("draft"));
assert_eq!(http.urls(), [GITHUB_REPO_URL.to_owned()]);
}
#[tokio::test]
async fn github_project_meta_reports_repository_homepage_license_description() {
let http = FakeHttp::new();
http.enqueue(GITHUB_REPO_URL, fixture(GITHUB_REPO));
let source = GithubSource::new(http.clone(), None);
let meta = source
.project_meta(&github_project())
.await
.expect("project meta");
assert_eq!(
meta,
json!({
"repository": "https://github.com/acme/widgets",
"homepage": "https://widgets.example",
"license": "MIT",
"description": "Widget build tooling",
})
);
}
#[tokio::test]
async fn gitlab_versions_merge_releases_and_tags() {
let http = FakeHttp::new();
http.enqueue(
&gitlab_url("acme%2Fwidgets", "/releases?per_page=100"),
fixture(GITLAB_RELEASES),
);
http.enqueue(
&gitlab_url("acme%2Fwidgets", "/repository/tags?per_page=100"),
fixture(GITLAB_TAGS),
);
let source = GitlabSource::new(http.clone(), Some("glpat_secret".to_owned()));
let versions = source.versions(&gitlab_project()).await.expect("versions");
let names: Vec<&str> = versions.iter().map(|info| info.version.as_str()).collect();
assert_eq!(names, ["3.0.0", "2.0.0", "1.0.0"]);
assert!(versions[0].deprecated);
assert_eq!(versions[0].reason, "upcoming release");
assert!(!versions[1].deprecated);
let calls = http.calls();
assert!(
calls[0]
.headers
.iter()
.any(|(name, value)| name == "PRIVATE-TOKEN" && value == "glpat_secret")
);
}
#[tokio::test]
async fn gitlab_subgroup_purl_is_url_encoded_and_assets_have_no_digest() {
let subgroup = ProjectRef {
purl_type: "gitlab".to_owned(),
name: "acme/platform/widgets".to_owned(),
purl: "pkg:gitlab/acme/platform/widgets".to_owned(),
};
let encoded = "acme%2Fplatform%2Fwidgets";
let http = FakeHttp::new();
http.enqueue(
&gitlab_url(encoded, "/releases?per_page=100"),
fixture(GITLAB_RELEASES),
);
http.enqueue(
&gitlab_url(encoded, "/repository/tags?per_page=100"),
fixture(GITLAB_TAGS),
);
let source = GitlabSource::new(http.clone(), None);
let versions = source.versions(&subgroup).await.expect("versions");
assert_eq!(
http.urls()[0],
gitlab_url(encoded, "/releases?per_page=100")
);
let releases = fixture(GITLAB_RELEASES);
http.enqueue(
&gitlab_url(encoded, "/releases/v2.0.0"),
releases[1].clone(),
);
let artifacts = source
.artifacts(&subgroup, &versions[1])
.await
.expect("artifacts");
assert_eq!(artifacts.len(), 3);
assert_eq!(artifacts[0].file_name, "widgets-v2.0.0.zip");
assert_eq!(artifacts[0].mime_type.as_deref(), Some("application/zip"));
assert!(
artifacts
.iter()
.all(|artifact| artifact.sha256.is_none() && artifact.size.is_none())
);
assert_eq!(artifacts[2].file_name, "widgets-2.0.0.jar");
}
#[tokio::test]
async fn gitlab_project_meta_and_archived_status_share_one_request() {
let http = FakeHttp::new();
http.enqueue(
&gitlab_url("acme%2Fwidgets", ""),
fixture(GITLAB_PROJECT_ARCHIVED),
);
let source = GitlabSource::new(http.clone(), None);
let meta = source
.project_meta(&gitlab_project())
.await
.expect("project meta");
assert_eq!(
meta,
json!({
"repository": "https://gitlab.com/acme/widgets",
"license": "MIT License",
"description": "Widget build tooling",
})
);
let statuses = source
.statuses(
&gitlab_project(),
&flagged("v3.0.0", "3.0.0", "upcoming release"),
)
.await
.expect("statuses");
assert_eq!(statuses[0].status, "deprecated");
assert_eq!(statuses[0].kind.as_deref(), Some("draft"));
assert_eq!(statuses[1].status, "archived");
assert_eq!(statuses[1].kind.as_deref(), Some("archived"));
assert_eq!(http.urls().len(), 1);
}
#[tokio::test]
async fn bitbucket_versions_come_from_tags_newest_first() {
let http = FakeHttp::new();
http.enqueue(
&bitbucket_url("/refs/tags?pagelen=100"),
fixture(BITBUCKET_TAGS),
);
let source = BitbucketSource::new(http.clone());
let versions = source
.versions(&bitbucket_project())
.await
.expect("versions");
let names: Vec<&str> = versions.iter().map(|info| info.version.as_str()).collect();
assert_eq!(names, ["2.0.0", "1.0.0"]);
assert_eq!(versions[0].raw, "v2.0.0");
}
#[tokio::test]
async fn bitbucket_downloads_paginate_and_carry_url_size_without_digest() {
let page2 =
"https://api.bitbucket.org/2.0/repositories/acme/widgets/downloads?pagelen=100&page=2";
let http = FakeHttp::new();
http.enqueue(
&bitbucket_url("/downloads?pagelen=100"),
fixture(BITBUCKET_DOWNLOADS_1),
);
http.enqueue(page2, fixture(BITBUCKET_DOWNLOADS_2));
let source = BitbucketSource::new(http.clone());
let artifacts = source
.artifacts(&bitbucket_project(), &version("v2.0.0", "2.0.0"))
.await
.expect("artifacts");
assert_eq!(artifacts.len(), 2);
assert_eq!(artifacts[0].file_name, "widgets-2.0.0.tar.gz");
assert_eq!(artifacts[0].size, Some(1234));
assert_eq!(artifacts[0].sha256, None);
assert_eq!(artifacts[1].file_name, "widgets-2.0.0.zip");
assert_eq!(artifacts[1].size, Some(2345));
assert_eq!(
http.urls(),
[bitbucket_url("/downloads?pagelen=100"), page2.to_owned(),]
);
}
#[tokio::test]
async fn bitbucket_capabilities_are_exact_and_statuses_are_typed_unsupported() {
let http = FakeHttp::new();
let source = BitbucketSource::new(http);
assert_eq!(
source.capabilities(),
SourceCapabilities {
versions: true,
artifacts: true,
statuses: false,
project_meta: true,
}
);
let error = source
.statuses(&bitbucket_project(), &version("v1.0.0", "1.0.0"))
.await
.expect_err("no statuses");
assert!(format!("{error:?}").contains("NoStatuses"), "{error:?}");
}
#[tokio::test]
async fn bitbucket_project_meta_reports_repository_homepage_description() {
let http = FakeHttp::new();
http.enqueue(BITBUCKET_REPO_URL, fixture(BITBUCKET_REPO));
let source = BitbucketSource::new(http.clone());
let meta = source
.project_meta(&bitbucket_project())
.await
.expect("project meta");
assert_eq!(
meta,
json!({
"repository": "https://bitbucket.org/acme/widgets",
"homepage": "https://widgets.example",
"description": "Widget build tooling",
})
);
}
#[tokio::test]
async fn missing_purl_components_are_reported_as_unavailable() {
let http = FakeHttp::new();
let bad = |purl_type: &str, purl: &str| ProjectRef {
purl_type: purl_type.to_owned(),
name: String::new(),
purl: purl.to_owned(),
};
let github = GithubSource::new(http.clone(), None);
let error = github
.versions(&bad("github", "pkg:github"))
.await
.expect_err("missing path");
assert!(format!("{error:?}").contains("Unavailable"), "{error:?}");
let gitlab = GitlabSource::new(http.clone(), None);
let error = gitlab
.versions(&bad("gitlab", "pkg:gitlab"))
.await
.expect_err("missing path");
assert!(format!("{error:?}").contains("Unavailable"), "{error:?}");
let bitbucket = BitbucketSource::new(http.clone());
let error = bitbucket
.versions(&bad("bitbucket", "pkg:bitbucket/onlyworkspace"))
.await
.expect_err("missing repository");
assert!(format!("{error:?}").contains("Unavailable"), "{error:?}");
let mismatch = ProjectRef {
purl_type: "github".to_owned(),
name: "acme/widgets".to_owned(),
purl: "pkg:npm/acme/widgets".to_owned(),
};
let error = github
.versions(&mismatch)
.await
.expect_err("wrong purl type");
assert!(
format!("{error:?}").contains("UnknownPurlType"),
"{error:?}"
);
assert!(http.calls().is_empty());
}
#[test]
fn production_transport_constructs() {
let transport = github::ReqwestGitHttp::new().expect("build transport");
let _: Arc<dyn GitHttp> = Arc::new(transport);
}