|
| 1 | +module DependencyAuditTasks |
| 2 | + |
| 3 | +open System |
| 4 | +open System.Diagnostics |
| 5 | +open System.IO |
| 6 | +open System.Text.Json |
| 7 | + |
| 8 | +open BlackFox.Fake |
| 9 | + |
| 10 | +open BasicTasks |
| 11 | +open ProjectInfo |
| 12 | + |
| 13 | +type private Vulnerability = |
| 14 | + { Project: string |
| 15 | + Package: string |
| 16 | + Version: string |
| 17 | + Severity: string |
| 18 | + AdvisoryUrl: string } |
| 19 | + |
| 20 | +type private Suppression = |
| 21 | + { Project: string |
| 22 | + Package: string |
| 23 | + AdvisoryUrl: string |
| 24 | + Expires: DateTime |
| 25 | + Reason: string } |
| 26 | + |
| 27 | +let private tryProperty (name: string) (element: JsonElement) = |
| 28 | + let mutable value = Unchecked.defaultof<JsonElement> |
| 29 | + if element.TryGetProperty(name, &value) then Some value else None |
| 30 | + |
| 31 | +let private requiredString name element = |
| 32 | + match tryProperty name element with |
| 33 | + | Some value when value.ValueKind = JsonValueKind.String -> value.GetString() |
| 34 | + | _ -> failwithf "Dependency-audit suppression is missing string property '%s'." name |
| 35 | + |
| 36 | +let private repositoryPath path = |
| 37 | + let absolute = Path.GetFullPath(path) |
| 38 | + Path.GetRelativePath(Environment.CurrentDirectory, absolute).Replace('\\', '/') |
| 39 | + |
| 40 | +let private readSuppressions () = |
| 41 | + let path = "build/dependency-audit-suppressions.json" |
| 42 | + use document = JsonDocument.Parse(File.ReadAllText(path)) |
| 43 | + |
| 44 | + document.RootElement.GetProperty("suppressions").EnumerateArray() |
| 45 | + |> Seq.map (fun item -> |
| 46 | + let expiresText = requiredString "expires" item |
| 47 | + let mutable expires = DateTime.MinValue |
| 48 | + |
| 49 | + if not (DateTime.TryParseExact(expiresText, "yyyy-MM-dd", null, Globalization.DateTimeStyles.None, &expires)) then |
| 50 | + failwithf "Dependency-audit suppression expiry '%s' must use yyyy-MM-dd." expiresText |
| 51 | + |
| 52 | + let suppression = |
| 53 | + { Project = requiredString "project" item |
| 54 | + Package = requiredString "package" item |
| 55 | + AdvisoryUrl = requiredString "advisoryUrl" item |
| 56 | + Expires = expires |
| 57 | + Reason = requiredString "reason" item } |
| 58 | + |
| 59 | + if String.IsNullOrWhiteSpace suppression.Reason then |
| 60 | + failwith "Dependency-audit suppressions require a non-empty review reason." |
| 61 | + |
| 62 | + suppression) |
| 63 | + |> List.ofSeq |
| 64 | + |
| 65 | +let private runAudit () = |
| 66 | + let startInfo = ProcessStartInfo() |
| 67 | + startInfo.FileName <- "dotnet" |
| 68 | + startInfo.UseShellExecute <- false |
| 69 | + startInfo.RedirectStandardOutput <- true |
| 70 | + startInfo.RedirectStandardError <- true |
| 71 | + |
| 72 | + [ "list" |
| 73 | + solutionFile |
| 74 | + "package" |
| 75 | + "--vulnerable" |
| 76 | + "--include-transitive" |
| 77 | + "--format" |
| 78 | + "json" |
| 79 | + "--no-restore" ] |
| 80 | + |> List.iter startInfo.ArgumentList.Add |
| 81 | + |
| 82 | + use auditProcess = Process.Start(startInfo) |
| 83 | + let stdout = auditProcess.StandardOutput.ReadToEndAsync() |
| 84 | + let stderr = auditProcess.StandardError.ReadToEndAsync() |
| 85 | + auditProcess.WaitForExit() |
| 86 | + let output = stdout.Result |
| 87 | + let errors = stderr.Result |
| 88 | + |
| 89 | + if auditProcess.ExitCode <> 0 then |
| 90 | + failwithf "dotnet dependency audit failed (exit %d):\n%s\n%s" auditProcess.ExitCode output errors |
| 91 | + |
| 92 | + use document = JsonDocument.Parse(output) |
| 93 | + |
| 94 | + [ for project in document.RootElement.GetProperty("projects").EnumerateArray() do |
| 95 | + let projectPath = project.GetProperty("path").GetString() |> repositoryPath |
| 96 | + |
| 97 | + match tryProperty "frameworks" project with |
| 98 | + | None -> () |
| 99 | + | Some frameworks -> |
| 100 | + for framework in frameworks.EnumerateArray() do |
| 101 | + for packageGroup in [ "topLevelPackages"; "transitivePackages" ] do |
| 102 | + match tryProperty packageGroup framework with |
| 103 | + | None -> () |
| 104 | + | Some packages -> |
| 105 | + for package in packages.EnumerateArray() do |
| 106 | + match tryProperty "vulnerabilities" package with |
| 107 | + | None -> () |
| 108 | + | Some vulnerabilities -> |
| 109 | + for vulnerability in vulnerabilities.EnumerateArray() do |
| 110 | + yield |
| 111 | + { Project = projectPath |
| 112 | + Package = package.GetProperty("id").GetString() |
| 113 | + Version = package.GetProperty("resolvedVersion").GetString() |
| 114 | + Severity = vulnerability.GetProperty("severity").GetString() |
| 115 | + AdvisoryUrl = vulnerability.GetProperty("advisoryurl").GetString() } ] |
| 116 | + |
| 117 | +let private matches (suppression: Suppression) (vulnerability: Vulnerability) = |
| 118 | + String.Equals(suppression.Project, vulnerability.Project, StringComparison.OrdinalIgnoreCase) |
| 119 | + && String.Equals(suppression.Package, vulnerability.Package, StringComparison.OrdinalIgnoreCase) |
| 120 | + && String.Equals(suppression.AdvisoryUrl, vulnerability.AdvisoryUrl, StringComparison.OrdinalIgnoreCase) |
| 121 | + |
| 122 | +/// Audits direct and transitive NuGet packages. Every reported advisory fails |
| 123 | +/// the build unless an exact project/package/advisory suppression with a review |
| 124 | +/// reason and unexpired date exists; stale or unused suppressions also fail. |
| 125 | +let dependencyAudit = |
| 126 | + BuildTask.create "DependencyAudit" [ buildSolution ] { |
| 127 | + let today = DateTime.UtcNow.Date |
| 128 | + let suppressions = readSuppressions () |
| 129 | + let expired = suppressions |> List.filter (fun item -> item.Expires < today) |
| 130 | + |
| 131 | + if not (List.isEmpty expired) then |
| 132 | + expired |
| 133 | + |> List.map (fun item -> sprintf "%s / %s / %s" item.Project item.Package item.AdvisoryUrl) |
| 134 | + |> String.concat "\n" |
| 135 | + |> failwithf "Dependency-audit suppressions have expired:\n%s" |
| 136 | + |
| 137 | + let vulnerabilities = runAudit () |
| 138 | + let unsuppressed = vulnerabilities |> List.filter (fun item -> suppressions |> List.exists (fun s -> matches s item) |> not) |
| 139 | + let unused = suppressions |> List.filter (fun item -> vulnerabilities |> List.exists (matches item) |> not) |
| 140 | + |
| 141 | + if not (List.isEmpty unused) then |
| 142 | + unused |
| 143 | + |> List.map (fun item -> sprintf "%s / %s / %s" item.Project item.Package item.AdvisoryUrl) |
| 144 | + |> String.concat "\n" |
| 145 | + |> failwithf "Dependency-audit suppressions no longer match an advisory; remove them:\n%s" |
| 146 | + |
| 147 | + for vulnerability in vulnerabilities do |
| 148 | + match suppressions |> List.tryFind (fun suppression -> matches suppression vulnerability) with |
| 149 | + | Some suppression -> |
| 150 | + printfn |
| 151 | + "SUPPRESSED %s %s %s (%s; expires %s): %s" |
| 152 | + vulnerability.Package |
| 153 | + vulnerability.Version |
| 154 | + vulnerability.AdvisoryUrl |
| 155 | + vulnerability.Project |
| 156 | + (suppression.Expires.ToString("yyyy-MM-dd")) |
| 157 | + suppression.Reason |
| 158 | + | None -> () |
| 159 | + |
| 160 | + if not (List.isEmpty unsuppressed) then |
| 161 | + unsuppressed |
| 162 | + |> List.map (fun item -> |
| 163 | + sprintf |
| 164 | + "%s %s [%s] in %s — %s" |
| 165 | + item.Package |
| 166 | + item.Version |
| 167 | + item.Severity |
| 168 | + item.Project |
| 169 | + item.AdvisoryUrl) |
| 170 | + |> String.concat "\n" |
| 171 | + |> failwithf "Unsuppressed vulnerable dependencies:\n%s" |
| 172 | + |
| 173 | + printfn "Dependency audit passed (%d reviewed suppression(s))." suppressions.Length |
| 174 | + } |
0 commit comments