rustsec 0.29.3

Client library for the RustSec security advisory database
Documentation
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
//! This is an intermediate representation used for converting from
//! Cargo-style version selectors (`>=`, `^`, `<`, etc) to OSV ranges.
//! It is an implementation detail and is not exported outside OSV module.

use crate::{Error, ErrorKind::BadParam};
use semver::{Comparator, Op, Prerelease, Version};
use std::fmt::Display;

#[derive(Clone, PartialEq, Eq, Hash, Debug)]
pub(crate) enum Bound {
    Unbounded,
    Exclusive(Version),
    Inclusive(Version),
}

impl Bound {
    /// Returns just the version, ignoring whether the bound is inclusive or exclusive
    pub fn version(&self) -> Option<&Version> {
        match &self {
            Bound::Unbounded => None,
            Bound::Exclusive(v) => Some(v),
            Bound::Inclusive(v) => Some(v),
        }
    }

    /// The handling of `Bound::Unbounded` in this function assumes that
    /// the first bound is start of a range, and the other bound is the end of a range.
    /// **Make sure** this is the way you call it.
    /// This is also why we don't define PartialOrd.
    fn less_or_equal(&self, other: &Bound) -> bool {
        // It's defined on Bound and not UnaffectedRange
        // so that it could be used on bounds from different ranges.
        let start = self;
        let end = other;
        // This appears to be a false positive in Clippy:
        // https://github.com/rust-lang/rust-clippy/issues/7383
        #[allow(clippy::if_same_then_else)]
        if start == &Bound::Unbounded || end == &Bound::Unbounded {
            true
        } else if start.version().unwrap() < end.version().unwrap() {
            true
        } else {
            match (&start, &end) {
                (Bound::Inclusive(v_start), Bound::Inclusive(v_end)) => v_start == v_end,
                (_, _) => false,
            }
        }
    }
}

/// A range of unaffected versions, used by either `patched`
/// or `unaffected` fields in the security advisory.
/// Bounds may be inclusive or exclusive.
/// `start` is guaranteed to be less than or equal to `end`.
/// If `start == end`, both bounds must be inclusive.
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
pub(crate) struct UnaffectedRange {
    start: Bound,
    end: Bound,
}

impl UnaffectedRange {
    pub fn new(start: Bound, end: Bound) -> Result<Self, Error> {
        if start.less_or_equal(&end) {
            Ok(UnaffectedRange { start, end })
        } else {
            Err(format_err!(
                BadParam,
                "Invalid range: start must be <= end; if equal, both bounds must be inclusive"
            ))
        }
    }

    pub fn start(&self) -> &Bound {
        &self.start
    }

    pub fn end(&self) -> &Bound {
        &self.end
    }

    pub fn overlaps(&self, other: &UnaffectedRange) -> bool {
        // range check for well-formed ranges is `(Start1 <= End2) && (Start2 <= End1)`
        self.start.less_or_equal(&other.end) && other.start.less_or_equal(&self.end)
    }
}

impl Display for UnaffectedRange {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match &self.start {
            Bound::Unbounded => f.write_str("[0"),
            Bound::Exclusive(v) => f.write_fmt(format_args!("({}", v)),
            Bound::Inclusive(v) => f.write_fmt(format_args!("[{}", v)),
        }?;
        f.write_str(", ")?;
        match &self.end {
            Bound::Unbounded => f.write_str("∞)"),
            Bound::Exclusive(v) => f.write_fmt(format_args!("{})", v)),
            Bound::Inclusive(v) => f.write_fmt(format_args!("{}]", v)),
        }
    }
}

/// To keep the algorithm simple, we impose several constraints:
/// 1. There is at most one upper and at most one lower bound in each range.
///    Stuff like `>= 1.0, >= 2.0` is nonsense and is not supported.
/// 2. If the requirement is "1.0" or "^1.0" that defines both the lower and upper bound,
///    it is the only one in its range.
/// If any of those constraints are unmet, an error will be returned.
impl TryFrom<&semver::VersionReq> for UnaffectedRange {
    type Error = Error;

    fn try_from(input: &semver::VersionReq) -> Result<Self, Self::Error> {
        if input.comparators.len() > 2 {
            fail!(
                BadParam,
                format!("Too many comparators in version specification: {}", input)
            );
        }
        // If one of the bounds is not specified, it's unbounded,
        // e.g. ["> 0.5"] means the lower bound is 0.5 and there is no upper bound
        let mut start = Bound::Unbounded;
        let mut end = Bound::Unbounded;
        for comparator in &input.comparators {
            match comparator.op {
                // Full list of operators supported by Cargo can be found here:
                // https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html
                // One of the Cargo developers has confirmed that the list is complete:
                // https://internals.rust-lang.org/t/changing-cargo-semver-compatibility-for-pre-releases/14820/14
                // However, `semver` crate recognizes more operators than Cargo supports
                Op::Greater => {
                    if start != Bound::Unbounded {
                        fail!(
                            BadParam,
                            format!("More than one lower bound in the same range: {}", input)
                        );
                    }
                    start = Bound::Exclusive(comp_to_ver(comparator));
                }
                Op::GreaterEq => {
                    if start != Bound::Unbounded {
                        fail!(
                            BadParam,
                            format!("More than one lower bound in the same range: {}", input)
                        );
                    }
                    start = Bound::Inclusive(comp_to_ver(comparator));
                }
                Op::Less => {
                    if end != Bound::Unbounded {
                        fail!(
                            BadParam,
                            format!("More than one upper bound in the same range: {}", input)
                        );
                    }
                    end = Bound::Exclusive(comp_to_ver(comparator));
                }
                Op::LessEq => {
                    if end != Bound::Unbounded {
                        fail!(
                            BadParam,
                            format!("More than one upper bound in the same range: {}", input)
                        );
                    }
                    end = Bound::Inclusive(comp_to_ver(comparator));
                }
                Op::Exact => {
                    if input.comparators.len() != 1 {
                        fail!(BadParam, "Selectors that define an exact version (e.g. '=1.0') must be alone in their range");
                    }
                    start = Bound::Inclusive(comp_to_ver(comparator));
                    end = Bound::Inclusive(comp_to_ver(comparator));
                }
                Op::Caret => {
                    if input.comparators.len() != 1 {
                        fail!(BadParam, "Selectors that define both the upper and lower bound (e.g. '^1.0') must be alone in their range");
                    }
                    let start_version = comp_to_ver(comparator);
                    let mut end_version = if start_version.major == 0 {
                        match (comparator.minor, comparator.patch) {
                            // ^0.0.x
                            (Some(0), Some(patch)) => Version::new(0, 0, patch + 1),
                            // ^0.x and ^0.x.x
                            (Some(minor), _) => Version::new(0, minor + 1, 0),
                            // ^0
                            (None, None) => Version::new(1, 0, 0),
                            (None, Some(_)) => unreachable!(
                                "Comparator specifies patch version but not minor version"
                            ),
                        }
                    } else {
                        Version::new(&start_version.major + 1, 0, 0)
                    };
                    // -0 is the lowest possible prerelease.
                    // If we didn't append it, e.g. ^1.0.0 would match 2.0.0-alpha1
                    end_version.pre = Prerelease::new("0").unwrap();
                    start = Bound::Inclusive(start_version);
                    end = Bound::Exclusive(end_version);
                }
                Op::Tilde => {
                    if input.comparators.len() != 1 {
                        fail!(BadParam, "Selectors that define both the upper and lower bound (e.g. '~1.0') must be alone in their range");
                    }
                    let start_version = comp_to_ver(comparator);
                    let major = comparator.major;
                    let mut end_version = match (comparator.minor, comparator.patch) {
                        (None, None) => Version::new(major + 1, 0, 0),
                        (Some(minor), _) => Version::new(major, minor + 1, 0),
                        (None, Some(_)) => {
                            unreachable!("Comparator specifies patch version but not minor version")
                        }
                    };
                    // -0 is the lowest possible prerelease.
                    // If we didn't append it, e.g. ~1.2 would match 1.3.0-alpha1
                    end_version.pre = Prerelease::new("0").unwrap();
                    start = Bound::Inclusive(start_version);
                    end = Bound::Exclusive(end_version);
                }
                _ => {
                    // the struct is non-exhaustive, we have to do this
                    fail!(
                        BadParam,
                        "Unsupported operator in version specification: '{}'",
                        comparator
                    );
                }
            }
        }
        UnaffectedRange::new(start, end)
    }
}

/// Strips comparison operators from a Comparator and turns it into a Version.
/// Would have been better implemented by `into` but these are foreign types
fn comp_to_ver(c: &Comparator) -> Version {
    Version {
        major: c.major,
        minor: c.minor.unwrap_or(0),
        patch: c.patch.unwrap_or(0),
        pre: c.pre.clone(),
        build: Default::default(),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use semver::VersionReq;

    #[test]
    fn both_unbounded() {
        let range1 = UnaffectedRange {
            start: Bound::Unbounded,
            end: Bound::Unbounded,
        };
        let range2 = UnaffectedRange {
            start: Bound::Unbounded,
            end: Bound::Unbounded,
        };
        assert!(range1.overlaps(&range2));
        assert!(range2.overlaps(&range1));
    }

    #[test]
    fn barely_not_overlapping() {
        let range1 = UnaffectedRange {
            start: Bound::Inclusive(Version::parse("1.2.3").unwrap()),
            end: Bound::Unbounded,
        };
        let range2 = UnaffectedRange {
            start: Bound::Unbounded,
            end: Bound::Exclusive(Version::parse("1.2.3").unwrap()),
        };
        assert!(!range1.overlaps(&range2));
        assert!(!range2.overlaps(&range1));
    }

    #[test]
    fn barely_overlapping() {
        let range1 = UnaffectedRange {
            start: Bound::Inclusive(Version::parse("1.2.3").unwrap()),
            end: Bound::Unbounded,
        };
        let range2 = UnaffectedRange {
            start: Bound::Unbounded,
            end: Bound::Inclusive(Version::parse("1.2.3").unwrap()),
        };
        assert!(range1.overlaps(&range2));
        assert!(range2.overlaps(&range1));
    }

    #[test]
    fn clearly_not_overlapping() {
        let range1 = UnaffectedRange {
            start: Bound::Inclusive(Version::parse("0.1.0").unwrap()),
            end: Bound::Inclusive(Version::parse("0.3.0").unwrap()),
        };
        let range2 = UnaffectedRange {
            start: Bound::Inclusive(Version::parse("1.1.0").unwrap()),
            end: Bound::Inclusive(Version::parse("1.3.0").unwrap()),
        };
        assert!(!range1.overlaps(&range2));
        assert!(!range2.overlaps(&range1));
    }

    #[test]
    fn clearly_overlapping() {
        let range1 = UnaffectedRange {
            start: Bound::Inclusive(Version::parse("0.1.0").unwrap()),
            end: Bound::Inclusive(Version::parse("1.1.0").unwrap()),
        };
        let range2 = UnaffectedRange {
            start: Bound::Inclusive(Version::parse("0.2.0").unwrap()),
            end: Bound::Inclusive(Version::parse("1.3.0").unwrap()),
        };
        assert!(range1.overlaps(&range2));
        assert!(range2.overlaps(&range1));
    }

    #[test]
    fn exact_requirement_10() {
        let input = VersionReq::parse("=1.0").unwrap();
        let expected = UnaffectedRange {
            start: Bound::Inclusive(Version::parse("1.0.0").unwrap()),
            end: Bound::Inclusive(Version::parse("1.0.0").unwrap()),
        };
        let result: UnaffectedRange = (&input).try_into().unwrap();
        assert_eq!(expected, result);
    }

    // Test data for caret requirements is taken from the Cargo spec
    // https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#caret-requirements
    // but adjusted to correctly handle pre-releases under semver precedence rules:
    // https://semver.org/#spec-item-11

    #[test]
    fn caret_requirement_123() {
        let input = VersionReq::parse("^1.2.3").unwrap();
        let expected = UnaffectedRange {
            start: Bound::Inclusive(Version::parse("1.2.3").unwrap()),
            end: Bound::Exclusive(Version::parse("2.0.0-0").unwrap()),
        };
        let result: UnaffectedRange = (&input).try_into().unwrap();
        assert_eq!(expected, result);
    }

    #[test]
    fn caret_requirement_12() {
        let input = VersionReq::parse("^1.2").unwrap();
        let expected = UnaffectedRange {
            start: Bound::Inclusive(Version::parse("1.2.0").unwrap()),
            end: Bound::Exclusive(Version::parse("2.0.0-0").unwrap()),
        };
        let result: UnaffectedRange = (&input).try_into().unwrap();
        assert_eq!(expected, result);
    }

    #[test]
    fn caret_requirement_1() {
        let input = VersionReq::parse("^1").unwrap();
        let expected = UnaffectedRange {
            start: Bound::Inclusive(Version::parse("1.0.0").unwrap()),
            end: Bound::Exclusive(Version::parse("2.0.0-0").unwrap()),
        };
        let result: UnaffectedRange = (&input).try_into().unwrap();
        assert_eq!(expected, result);
    }

    #[test]
    fn caret_requirement_023() {
        let input = VersionReq::parse("^0.2.3").unwrap();
        let expected = UnaffectedRange {
            start: Bound::Inclusive(Version::parse("0.2.3").unwrap()),
            end: Bound::Exclusive(Version::parse("0.3.0-0").unwrap()),
        };
        let result: UnaffectedRange = (&input).try_into().unwrap();
        assert_eq!(expected, result);
    }

    #[test]
    fn caret_requirement_02() {
        let input = VersionReq::parse("^0.2").unwrap();
        let expected = UnaffectedRange {
            start: Bound::Inclusive(Version::parse("0.2.0").unwrap()),
            end: Bound::Exclusive(Version::parse("0.3.0-0").unwrap()),
        };
        let result: UnaffectedRange = (&input).try_into().unwrap();
        assert_eq!(expected, result);
    }

    #[test]
    fn caret_requirement_003() {
        let input = VersionReq::parse("^0.0.3").unwrap();
        let expected = UnaffectedRange {
            start: Bound::Inclusive(Version::parse("0.0.3").unwrap()),
            end: Bound::Exclusive(Version::parse("0.0.4-0").unwrap()),
        };
        let result: UnaffectedRange = (&input).try_into().unwrap();
        assert_eq!(expected, result);
    }

    #[test]
    fn caret_requirement_00() {
        let input = VersionReq::parse("^0.0").unwrap();
        let expected = UnaffectedRange {
            start: Bound::Inclusive(Version::parse("0.0.0").unwrap()),
            end: Bound::Exclusive(Version::parse("0.1.0-0").unwrap()),
        };
        let result: UnaffectedRange = (&input).try_into().unwrap();
        assert_eq!(expected, result);
    }

    #[test]
    fn caret_requirement_0() {
        let input = VersionReq::parse("^0").unwrap();
        let expected = UnaffectedRange {
            start: Bound::Inclusive(Version::parse("0.0.0").unwrap()),
            end: Bound::Exclusive(Version::parse("1.0.0-0").unwrap()),
        };
        let result: UnaffectedRange = (&input).try_into().unwrap();
        assert_eq!(expected, result);
    }

    // Test data for tilde requirements is taken from the Cargo spec
    // https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#tilde-requirements
    // but adjusted to correctly handle pre-releases under semver precedence rules:
    // https://semver.org/#spec-item-11

    #[test]
    fn tilde_requirement_123() {
        let input = VersionReq::parse("~1.2.3").unwrap();
        let expected = UnaffectedRange {
            start: Bound::Inclusive(Version::parse("1.2.3").unwrap()),
            end: Bound::Exclusive(Version::parse("1.3.0-0").unwrap()),
        };
        let result: UnaffectedRange = (&input).try_into().unwrap();
        assert_eq!(expected, result);
    }

    #[test]
    fn tilde_requirement_12() {
        let input = VersionReq::parse("~1.2").unwrap();
        let expected = UnaffectedRange {
            start: Bound::Inclusive(Version::parse("1.2.0").unwrap()),
            end: Bound::Exclusive(Version::parse("1.3.0-0").unwrap()),
        };
        let result: UnaffectedRange = (&input).try_into().unwrap();
        assert_eq!(expected, result);
    }

    #[test]
    fn tilde_requirement_1() {
        let input = VersionReq::parse("~1").unwrap();
        let expected = UnaffectedRange {
            start: Bound::Inclusive(Version::parse("1.0.0").unwrap()),
            end: Bound::Exclusive(Version::parse("2.0.0-0").unwrap()),
        };
        let result: UnaffectedRange = (&input).try_into().unwrap();
        assert_eq!(expected, result);
    }
}