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
|
//! Advisory risk scoring (§6).
//!
//! The reviewer's attention is the scarcest resource in this system, so every
//! candidate gets a computed level that orders and colours the inbox. It is
//! **advisory only**: there is no auto-approval in v1 (§7.3), so nothing is
//! gated on a score and no heuristic can let something through. A wrong score
//! costs attention, never safety.
//!
//! Every signal is computed from what the accepted release recorded against
//! what the candidate records now — the same "never last seen" rule that makes
//! I2 true for the diff panels.
use crate::db::{self, model::*, Db, Pkgbase};
use crate::error::Result;
use crate::pkgver;
pub mod level {
pub const LOW: &str = "low";
pub const MEDIUM: &str = "medium";
pub const HIGH: &str = "high";
}
/// A build tool in `makedepends` means the build can fetch and compile anything
/// (§16: we do not sandbox the network in v0, so the reviewer should see it).
const FETCHING_BUILD_TOOLS: &[&str] = &["cargo", "npm", "go", "pip", "python", "ruby", "deno", "rust", "nodejs"];
/// Files whose presence in an upstream commit range means the build itself
/// changed, not just the code.
const BUILD_SYSTEM_FILES: &[&str] = &[
"Cargo.toml", "Cargo.lock", "package.json", "setup.py", "pyproject.toml", "Makefile", "CMakeLists.txt",
"meson.build", "go.mod", "Gemfile", "build.gradle",
];
#[derive(Debug, Clone, Default)]
pub struct Assessment {
pub level: String,
pub signals: Vec<String>,
}
impl Assessment {
pub fn is_high(&self) -> bool {
self.level == level::HIGH
}
}
/// Score a candidate against the release it would replace. `None` accepted
/// means a first introduction, which is its own kind of review (§7.1b).
pub fn assess(
db: &Db,
cfg: &crate::config::Config,
pb: &Pkgbase,
new: &Recipe,
new_digests: &[SourceDigest],
accepted: Option<&ReleaseRow>,
) -> Result<Assessment> {
let mut s = SignalSet::default();
let old: Option<Recipe> = match accepted {
Some(r) => db::accepted_recipe(db, r)?,
None => None,
};
match &old {
None => s.add("first-introduction", level::MEDIUM),
Some(old) => {
compare_recipes(old, new, &mut s);
compare_digests(old, new, accepted.unwrap(), new_digests, &mut s);
}
}
// Things only visible from the recipe itself.
if new.skipgpcheck {
s.add("skippgpcheck", level::HIGH);
}
for idx in crate::watch::digests::skip_on_non_vcs(new) {
s.add(&format!("skip-checksum:source-{idx}"), level::HIGH);
}
for md in pkgver::dep_names(&new.makedepends) {
if FETCHING_BUILD_TOOLS.iter().any(|t| md.starts_with(t)) {
s.add(&format!("build-tool:{md}"), level::MEDIUM);
}
}
if new.install.is_some() {
// An `.install` script is arbitrary code that runs on every consumer
// machine at install time.
s.add("install-script", level::HIGH);
}
for opt in &new.options {
if matches!(opt.as_str(), "!debug" | "!strip" | "!buildflags" | "!security" | "!emptydirs") {
s.add(&format!("option:{opt}"), level::MEDIUM);
}
}
// Upstream movement that touches the build system (§6): the diff is in a
// language ecosystem we cannot inspect, so say so.
if let Some(a) = accepted {
for pin in accepted_pins(db, accepted) {
if let Some(old_pin) = a.pins().into_iter().find(|p| p.index == pin.index) {
if !old_pin.commit.is_empty()
&& !pin.commit.is_empty()
&& old_pin.commit != pin.commit
&& upstream_touches_build_system(cfg, pb, &old_pin.commit, &pin.commit)
{
s.add("upstream-build-system", level::MEDIUM);
}
}
}
}
Ok(Assessment { level: s.level(), signals: s.signals })
}
fn accepted_pins(db: &Db, accepted: Option<&ReleaseRow>) -> Vec<UpstreamPin> {
let _ = db;
accepted.map(|a| a.pins()).unwrap_or_default()
}
fn compare_recipes(old: &Recipe, new: &Recipe, s: &mut SignalSet) {
// Version deltas.
if new.epoch != old.epoch && pkgver::vercmp(&new.epoch, &old.epoch) == std::cmp::Ordering::Greater {
s.add("epoch-bump", level::MEDIUM);
}
if new.pkgver == old.pkgver && pkgver::vercmp(&new.pkgrel, &old.pkgrel) == std::cmp::Ordering::Less {
s.add("pkgrel-reset", level::LOW);
}
if pkgver::is_downgrade(&old.pkgver, &new.pkgver) {
s.add("downgrade", level::MEDIUM);
}
// Sources: added/removed entries and host changes.
let old_hosts = hosts(&old.source);
let new_hosts = hosts(&new.source);
for h in &new_hosts {
if !old_hosts.contains(h) {
s.add(&format!("new-source-host:{h}"), level::HIGH);
}
}
if new.source.len() != old.source.len() {
s.add("source-set-changed", level::MEDIUM);
}
// Checksums weakening: a sum that existed and is now SKIP.
for (i, sum) in new.sha256sums.iter().enumerate() {
if sum.eq_ignore_ascii_case("SKIP") {
match old.sha256sums.get(i) {
Some(prev) if prev != "SKIP" && !prev.is_empty() => {
s.add(&format!("sums-to-skip:{i}"), level::HIGH)
}
_ => {}
}
}
}
if !old.validpgpkeys.is_empty() && new.validpgpkeys.is_empty() {
s.add("validpgpkeys-removed", level::HIGH);
}
// Dependencies.
let old_md: std::collections::BTreeSet<String> = pkgver::dep_names(&old.makedepends).into_iter().collect();
for md in pkgver::dep_names(&new.makedepends) {
if !old_md.contains(&md) {
s.add(&format!("new-makedepends:{md}"), level::MEDIUM);
}
}
let old_cd: std::collections::BTreeSet<String> = pkgver::dep_names(&old.checkdepends).into_iter().collect();
let new_cd: std::collections::BTreeSet<String> = pkgver::dep_names(&new.checkdepends).into_iter().collect();
if !old_cd.is_empty() && new_cd.is_empty() {
// Tests silently stopped running.
s.add("checkdepends-removed", level::MEDIUM);
}
// Did the executable part of the recipe change, or only metadata?
if body_changed(&old.pkgbuild, &new.pkgbuild) {
s.add("recipe-code-changed", level::HIGH);
}
if old.install != new.install {
s.add("install-script-changed", level::HIGH);
}
}
/// Digest comparison. Drift with no version change is an `integrity_alert`
/// (§5.3) and never reaches here as a review, so anything that *does* show up
/// alongside a version change is still worth saying.
fn compare_digests(
_old: &Recipe,
_new: &Recipe,
accepted: &ReleaseRow,
new_digests: &[SourceDigest],
s: &mut SignalSet,
) {
let old_digests = accepted.digests();
for n in new_digests {
if let Some(o) = old_digests.iter().find(|d| d.index == n.index) {
if o.digest != n.digest && !n.digest.is_empty() && o.kind != "local" {
s.add(&format!("digest-changed:source-{}", n.index), level::MEDIUM);
}
}
}
}
/// True when the diff touches a `build()`/`package()`/`prepare()`/`check()`
/// body rather than only the variable assignments above them.
fn body_changed(old_src: &str, new_src: &str) -> bool {
fn bodies(text: &str) -> String {
let mut out = String::new();
let mut inside = false;
for line in text.lines() {
let t = line.trim_start();
if t.starts_with("build()")
|| t.starts_with("package")
|| t.starts_with("prepare()")
|| t.starts_with("check()")
{
inside = true;
}
if inside {
out.push_str(t);
out.push('\n');
}
}
out
}
bodies(old_src) != bodies(new_src)
}
fn hosts(sources: &[String]) -> std::collections::BTreeSet<String> {
sources
.iter()
.filter_map(|s| {
let s = s.rsplit("::").next().unwrap_or(s);
let s = s.rsplit('+').next().unwrap_or(s);
let rest = s.split("://").nth(1)?;
rest.split('/').next().map(|h| h.to_string())
})
.collect()
}
fn upstream_touches_build_system(cfg: &crate::config::Config, pb: &Pkgbase, from: &str, to: &str) -> bool {
// Best-effort: if we have the upstream clone cached we can ask it directly.
let cache = cfg.vcs_cache_dir();
if !cache.exists() {
return false;
}
let range = format!("{from}..{to}");
for dir in std::fs::read_dir(&cache).into_iter().flatten().filter_map(|d| d.ok()) {
let path = dir.path();
if let Ok(files) = crate::git::git(Some(&path), &["diff", "--name-only", &range]) {
if files.lines().any(|f| BUILD_SYSTEM_FILES.iter().any(|b| f.ends_with(b))) {
let _ = pb;
return true;
}
}
}
false
}
/// Accumulator that keeps the highest level seen and de-duplicates signals.
#[derive(Debug, Default)]
struct SignalSet {
level: String,
signals: Vec<String>,
}
impl SignalSet {
fn add(&mut self, signal: &str, lvl: &str) {
if !self.signals.contains(&signal.to_string()) {
self.signals.push(signal.to_string());
}
self.level = match (self.level.as_str(), lvl) {
(_, level::HIGH) | (level::HIGH, _) => level::HIGH,
(_, level::MEDIUM) | (level::MEDIUM, _) => level::MEDIUM,
_ => level::LOW,
}
.to_string();
}
fn level(&self) -> String {
if self.level.is_empty() {
level::LOW.to_string()
} else {
self.level.clone()
}
}
}
|