2021-10-10 21:31:53 +02:00
|
|
|
{ depot, pkgs, lib, ... }:
|
|
|
|
|
|
|
|
let
|
|
|
|
# dependency imports
|
|
|
|
|
|
|
|
inherit (depot.nix) getBins;
|
|
|
|
inherit (depot.third_party) rustsec-advisory-db;
|
|
|
|
|
|
|
|
bins = getBins pkgs.jq [
|
|
|
|
"jq"
|
|
|
|
] // getBins pkgs.coreutils [
|
|
|
|
"cat"
|
|
|
|
"printf"
|
|
|
|
"tee"
|
|
|
|
"test"
|
|
|
|
"wc"
|
|
|
|
] // getBins pkgs.gnugrep [
|
|
|
|
"grep"
|
|
|
|
] // getBins pkgs.cargo-audit [
|
|
|
|
"cargo-audit"
|
|
|
|
] // getBins pkgs.ansi2html [
|
|
|
|
"ansi2html"
|
|
|
|
] // {
|
|
|
|
eprintf = depot.tools.eprintf;
|
|
|
|
};
|
|
|
|
|
2021-10-15 20:48:57 +02:00
|
|
|
# list of maintainers we may @mention on GitHub
|
|
|
|
maintainerWhitelist = builtins.attrValues {
|
|
|
|
inherit (lib.maintainers)
|
|
|
|
sternenseemann
|
|
|
|
qyliss
|
|
|
|
jk
|
2021-11-11 20:17:42 +01:00
|
|
|
symphorien
|
|
|
|
erictapen
|
|
|
|
expipiplus1
|
2022-01-30 17:06:58 +01:00
|
|
|
;
|
2021-10-15 20:48:57 +02:00
|
|
|
};
|
|
|
|
|
2021-10-10 21:31:53 +02:00
|
|
|
# buildRustPackage handling
|
|
|
|
|
|
|
|
/* Predicate by which we identify rust packages we are interested in,
|
|
|
|
i. e. built using `buildRustPackage`.
|
|
|
|
|
|
|
|
Type :: drv -> bool
|
|
|
|
*/
|
|
|
|
isRustPackage = v: v ? cargoDeps;
|
|
|
|
|
|
|
|
/* Takes a buildRustPackage derivation and returns a derivation which
|
|
|
|
builds extracts the `Cargo.lock` of its `cargoDeps` derivation or
|
|
|
|
`null` if it has none.
|
|
|
|
|
|
|
|
Type: drv -> option<drv>
|
|
|
|
*/
|
|
|
|
# TODO(sterni): support cargoVendorDir?
|
|
|
|
extractCargoLock = drv:
|
|
|
|
if !(drv ? cargoDeps.outPath)
|
|
|
|
then null
|
2022-01-30 17:06:58 +01:00
|
|
|
else
|
|
|
|
pkgs.runCommandNoCC "${drv.name}-Cargo.lock" { } ''
|
|
|
|
if test -d "${drv.cargoDeps}"; then
|
|
|
|
cp "${drv.cargoDeps}/Cargo.lock" "$out"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if test -f "${drv.cargoDeps}"; then
|
|
|
|
tar -xO \
|
|
|
|
--no-wildcards-match-slash --wildcards \
|
|
|
|
-f "${drv.cargoDeps}" \
|
|
|
|
'*/Cargo.lock' \
|
|
|
|
> "$out"
|
|
|
|
fi
|
|
|
|
'';
|
2021-10-10 21:31:53 +02:00
|
|
|
|
|
|
|
# nixpkgs traversal
|
|
|
|
|
|
|
|
# Condition for us to recurse: Either at top-level or recurseForDerivation.
|
2022-01-30 17:06:58 +01:00
|
|
|
recurseInto = path: x: path == [ ] ||
|
2021-10-10 21:31:53 +02:00
|
|
|
(lib.isAttrs x && (x.recurseForDerivations or false));
|
|
|
|
|
|
|
|
# Returns the value or false if an eval error occurs.
|
|
|
|
tryEvalOrFalse = v: (builtins.tryEval v).value;
|
|
|
|
|
|
|
|
/* Traverses nixpkgs as instructed by `recurseInto` and collects
|
|
|
|
the attribute and lockfile derivation of every rust package it
|
|
|
|
encounters into a list.
|
|
|
|
|
|
|
|
Type :: attrs
|
|
|
|
-> list {
|
|
|
|
attr :: list<str>;
|
|
|
|
lock :: option<drv>;
|
|
|
|
maintainers :: list<maintainer>;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
allLockFiles =
|
|
|
|
let
|
|
|
|
go = path: x:
|
|
|
|
let
|
|
|
|
isDrv = tryEvalOrFalse (lib.isDerivation x);
|
|
|
|
doRec = tryEvalOrFalse (recurseInto path x);
|
|
|
|
isRust = tryEvalOrFalse (isRustPackage x);
|
|
|
|
in
|
2022-01-30 17:06:58 +01:00
|
|
|
if doRec then
|
|
|
|
lib.concatLists
|
|
|
|
(
|
|
|
|
lib.mapAttrsToList (n: go (path ++ [ n ])) x
|
|
|
|
) else if isDrv && isRust then [
|
|
|
|
{
|
|
|
|
attr = path;
|
|
|
|
lock = extractCargoLock x;
|
|
|
|
maintainers = x.meta.maintainers or [ ];
|
|
|
|
}
|
|
|
|
] else [ ];
|
|
|
|
in
|
|
|
|
go [ ];
|
2021-10-10 21:31:53 +02:00
|
|
|
|
|
|
|
# Report generation and formatting
|
|
|
|
|
2022-01-30 17:06:58 +01:00
|
|
|
reportFor = { attr, lock, maintainers ? [ ] }:
|
|
|
|
let
|
|
|
|
# naïve attribute path to Nix syntax conversion
|
|
|
|
strAttr = lib.concatStringsSep "." attr;
|
|
|
|
strMaintainers = lib.concatMapStringsSep " " (m: "@${m.github}") (
|
|
|
|
builtins.filter (x: builtins.elem x maintainerWhitelist) maintainers
|
|
|
|
);
|
|
|
|
in
|
2021-10-10 21:31:53 +02:00
|
|
|
if lock == null
|
|
|
|
then pkgs.emptyFile
|
2022-01-30 17:06:58 +01:00
|
|
|
else
|
|
|
|
depot.nix.runExecline "${strAttr}-vulnerability-report" { } [
|
|
|
|
"pipeline"
|
|
|
|
[
|
|
|
|
bins.cargo-audit
|
|
|
|
"audit"
|
|
|
|
"--json"
|
|
|
|
"-n"
|
|
|
|
"--db"
|
|
|
|
rustsec-advisory-db
|
|
|
|
"-f"
|
|
|
|
lock
|
|
|
|
]
|
|
|
|
"importas"
|
|
|
|
"out"
|
|
|
|
"out"
|
|
|
|
"redirfd"
|
|
|
|
"-w"
|
|
|
|
"1"
|
|
|
|
"$out"
|
|
|
|
bins.jq
|
|
|
|
"-rj"
|
|
|
|
"-f"
|
2022-02-03 14:44:03 +01:00
|
|
|
../../../tools/rust-crates-advisory/format-audit-result.jq
|
2022-01-30 17:06:58 +01:00
|
|
|
"--arg"
|
|
|
|
"attr"
|
|
|
|
strAttr
|
|
|
|
"--arg"
|
|
|
|
"maintainers"
|
|
|
|
strMaintainers
|
|
|
|
];
|
2021-10-10 21:31:53 +02:00
|
|
|
|
|
|
|
# GHMF in issues splits paragraphs on newlines
|
2022-01-30 17:06:58 +01:00
|
|
|
description = lib.concatMapStringsSep "\n\n"
|
|
|
|
(
|
|
|
|
builtins.replaceStrings [ "\n" ] [ " " ]
|
|
|
|
) [
|
2021-10-10 21:31:53 +02:00
|
|
|
''
|
|
|
|
The vulnerability report below was generated by
|
|
|
|
[nixpkgs-crate-holes](https://code.tvl.fyi/tree/users/sterni/nixpkgs-crate-holes)
|
|
|
|
which extracts the `Cargo.lock` file of each package in nixpkgs with a
|
|
|
|
`cargoDeps` attribute and passes it to
|
|
|
|
[cargo-audit](https://github.com/RustSec/rustsec/tree/main/cargo-audit)
|
|
|
|
using RustSec's
|
|
|
|
[advisory-db at ${builtins.substring 0 7 rustsec-advisory-db.rev}](https://github.com/RustSec/advisory-db/tree/${rustsec-advisory-db.rev}/).
|
|
|
|
''
|
|
|
|
''
|
|
|
|
Feel free to report any problems or suggest improvements (I have an email
|
|
|
|
address on my profile and hang out on Matrix/libera.chat as sterni)!
|
|
|
|
Tick off any reports that have been fixed in the meantime.
|
|
|
|
''
|
|
|
|
''
|
|
|
|
Note: A vulnerability in a dependency does not necessarily mean the dependent
|
|
|
|
package is vulnerable, e. g. when a vulnerable function isn't used.
|
|
|
|
''
|
|
|
|
];
|
|
|
|
|
|
|
|
runInstructions = ''
|
|
|
|
<details>
|
|
|
|
<summary>
|
|
|
|
Generating Cargo.lock vulnerability reports
|
|
|
|
|
|
|
|
</summary>
|
|
|
|
|
|
|
|
If you have a checkout of [depot](https://code.tvl.fyi/about/), you can generate this report using:
|
|
|
|
|
|
|
|
```
|
|
|
|
nix-build -A users.sterni.nixpkgs-crate-holes.full \
|
|
|
|
--argstr nixpkgsPath /path/to/nixpkgs
|
|
|
|
```
|
|
|
|
|
|
|
|
If you want a more detailed report for a single attribute of nixpkgs, use:
|
|
|
|
|
|
|
|
```
|
|
|
|
nix-build -A users.sterni.nixpkgs-crate-holes.single \
|
|
|
|
--argstr nixpkgsPath /path/to/nixpkgs --arg attr '[ "ripgrep" ]'
|
|
|
|
```
|
|
|
|
|
|
|
|
</details>
|
|
|
|
'';
|
|
|
|
|
|
|
|
defaultNixpkgsArgs = { allowBroken = false; };
|
|
|
|
|
|
|
|
reportForNixpkgs =
|
|
|
|
{ nixpkgsPath
|
|
|
|
, nixpkgsArgs ? defaultNixpkgsArgs
|
|
|
|
}@args:
|
|
|
|
|
|
|
|
let
|
|
|
|
reports = builtins.map reportFor (
|
|
|
|
allLockFiles (import nixpkgsPath nixpkgsArgs)
|
|
|
|
);
|
|
|
|
in
|
|
|
|
|
2022-01-30 17:06:58 +01:00
|
|
|
depot.nix.runExecline "nixpkgs-rust-pkgs-vulnerability-report.md"
|
|
|
|
{
|
|
|
|
stdin = lib.concatMapStrings (report: "${report}\n") reports;
|
|
|
|
} [
|
|
|
|
"importas"
|
|
|
|
"out"
|
|
|
|
"out"
|
|
|
|
"redirfd"
|
|
|
|
"-w"
|
|
|
|
"1"
|
|
|
|
"$out"
|
2021-10-10 21:31:53 +02:00
|
|
|
# Print introduction paragraph for the issue
|
2022-01-30 17:06:58 +01:00
|
|
|
"if"
|
|
|
|
[ bins.printf "%s\n\n" description ]
|
2021-10-10 21:31:53 +02:00
|
|
|
# Print all reports
|
2022-01-30 17:06:58 +01:00
|
|
|
"foreground"
|
|
|
|
[
|
|
|
|
"forstdin"
|
|
|
|
"-E"
|
|
|
|
"report"
|
|
|
|
bins.cat
|
|
|
|
"$report"
|
2021-10-10 21:31:53 +02:00
|
|
|
]
|
|
|
|
# Print stats at the end (mostly as a gimmick), we already know how many
|
|
|
|
# attributes there are and count the attributes with vulnerability by
|
|
|
|
# finding the number of checkable list entries in the output.
|
2022-01-30 17:06:58 +01:00
|
|
|
"backtick"
|
|
|
|
"-E"
|
|
|
|
"vulnerableCount"
|
|
|
|
[
|
|
|
|
"pipeline"
|
|
|
|
[
|
|
|
|
bins.grep
|
|
|
|
"^- \\[ \\]"
|
|
|
|
"$out"
|
2021-10-10 21:31:53 +02:00
|
|
|
]
|
2022-01-30 17:06:58 +01:00
|
|
|
bins.wc
|
|
|
|
"-l"
|
2021-10-10 21:31:53 +02:00
|
|
|
]
|
2022-01-30 17:06:58 +01:00
|
|
|
"if"
|
|
|
|
[
|
2021-10-10 21:31:53 +02:00
|
|
|
bins.printf
|
|
|
|
"\n%s of %s checked attributes have vulnerable dependencies.\n\n"
|
|
|
|
"$vulnerableCount"
|
|
|
|
(toString (builtins.length reports))
|
|
|
|
]
|
2022-01-30 17:06:58 +01:00
|
|
|
"if"
|
|
|
|
[
|
|
|
|
bins.printf
|
|
|
|
"%s\n\n"
|
|
|
|
runInstructions
|
2021-10-10 21:31:53 +02:00
|
|
|
]
|
|
|
|
];
|
|
|
|
|
|
|
|
singleReport =
|
2022-01-30 17:06:58 +01:00
|
|
|
{
|
|
|
|
# Attribute to check: string or list of strings (attr path)
|
2021-10-10 21:31:53 +02:00
|
|
|
attr
|
|
|
|
# Path to importable nixpkgs checkout
|
|
|
|
, nixpkgsPath
|
|
|
|
# Arguments to pass to nixpkgs
|
|
|
|
, nixpkgsArgs ? defaultNixpkgsArgs
|
|
|
|
}:
|
|
|
|
|
|
|
|
let
|
|
|
|
attr' = if builtins.isString attr then [ attr ] else attr;
|
|
|
|
drv = lib.getAttrFromPath attr' (import nixpkgsPath nixpkgsArgs);
|
|
|
|
lockFile = extractCargoLock drv;
|
|
|
|
strAttr = lib.concatStringsSep "." attr';
|
|
|
|
in
|
|
|
|
|
2022-01-30 17:06:58 +01:00
|
|
|
depot.nix.runExecline "${strAttr}-report.html" { } [
|
|
|
|
"importas"
|
|
|
|
"out"
|
|
|
|
"out"
|
|
|
|
"backtick"
|
|
|
|
"-I"
|
|
|
|
"-E"
|
|
|
|
"-N"
|
|
|
|
"report"
|
|
|
|
[
|
|
|
|
bins.cargo-audit
|
|
|
|
"audit"
|
2021-10-10 21:31:53 +02:00
|
|
|
"--quiet"
|
2022-01-30 17:06:58 +01:00
|
|
|
"-n"
|
|
|
|
"--db"
|
|
|
|
rustsec-advisory-db
|
|
|
|
"-f"
|
|
|
|
lockFile
|
2021-10-10 21:31:53 +02:00
|
|
|
]
|
2022-01-30 17:06:58 +01:00
|
|
|
"pipeline"
|
|
|
|
[
|
|
|
|
"ifte"
|
|
|
|
[
|
|
|
|
bins.printf
|
|
|
|
"%s"
|
|
|
|
"$report"
|
|
|
|
]
|
|
|
|
[
|
|
|
|
bins.printf
|
|
|
|
"%s\n"
|
|
|
|
"No vulnerabilities found"
|
2021-10-10 21:31:53 +02:00
|
|
|
]
|
2022-01-30 17:06:58 +01:00
|
|
|
bins.test
|
|
|
|
"-n"
|
|
|
|
"$report"
|
2021-10-10 21:31:53 +02:00
|
|
|
]
|
2022-01-30 17:06:58 +01:00
|
|
|
"pipeline"
|
|
|
|
[
|
|
|
|
bins.tee
|
|
|
|
"/dev/stderr"
|
2021-10-10 21:31:53 +02:00
|
|
|
]
|
2022-01-30 17:06:58 +01:00
|
|
|
"redirfd"
|
|
|
|
"-w"
|
|
|
|
"1"
|
|
|
|
"$out"
|
2021-10-10 21:31:53 +02:00
|
|
|
bins.ansi2html
|
|
|
|
];
|
|
|
|
|
2022-01-30 17:06:58 +01:00
|
|
|
in
|
|
|
|
{
|
2021-10-10 21:31:53 +02:00
|
|
|
full = reportForNixpkgs;
|
|
|
|
single = singleReport;
|
|
|
|
|
|
|
|
inherit
|
|
|
|
extractCargoLock
|
|
|
|
allLockFiles
|
2022-01-30 17:06:58 +01:00
|
|
|
;
|
2021-10-10 21:31:53 +02:00
|
|
|
|
|
|
|
# simple sanity check, doesn't cover everything, but testing the full report
|
|
|
|
# is quite expensive in terms of evaluation.
|
|
|
|
testSingle = singleReport {
|
|
|
|
nixpkgsPath = depot.third_party.nixpkgs.path;
|
|
|
|
attr = [ "ripgrep" ];
|
|
|
|
};
|
|
|
|
|
|
|
|
meta.targets = [ "testSingle" ];
|
|
|
|
}
|