96 lines
4.0 KiB
Go
96 lines
4.0 KiB
Go
package main
|
|
|
|
// The scan of record (ADR-0093 §4, WP-05): a SARIF that is not evidence of an
|
|
// analysis is refused, and an in-repository suppression does not silence a
|
|
// finding. Written on the build VM, not run there (owner decision 2026-10-11).
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
const oneRule = `{"id":"python.lang.security.audit.eval","defaultConfiguration":{"level":"error"}}`
|
|
|
|
func sarif(rules, results, invocations string) string {
|
|
return `{"version":"2.1.0","runs":[{"tool":{"driver":{"name":"semgrep","rules":[` + rules +
|
|
`]}},"results":[` + results + `],"invocations":[` + invocations + `]}]}`
|
|
}
|
|
|
|
func TestARecordRunThatLoadedNoRulesIsRefused(t *testing.T) {
|
|
// What an air-gapped `--config=auto` leaves behind: well-formed, empty.
|
|
err := checkRecordReport("semgrep", sarif("", "", `{"executionSuccessful":true}`))
|
|
if err == nil || !strings.Contains(err.Error(), "no rules") {
|
|
t.Fatalf("an empty rule set must not be a scan of record, got %v", err)
|
|
}
|
|
}
|
|
|
|
func TestARecordRunWithAToolErrorIsRefused(t *testing.T) {
|
|
for name, inv := range map[string]string{
|
|
"notification": `{"executionSuccessful":true,"toolExecutionNotifications":[{"level":"error","message":{"text":"Failed to download config from https://semgrep.dev/c/auto"}}]}`,
|
|
"unsuccessful": `{"executionSuccessful":false}`,
|
|
} {
|
|
if err := checkRecordReport("semgrep", sarif(oneRule, "", inv)); err == nil {
|
|
t.Errorf("%s: a failed run must not be a scan of record", name)
|
|
}
|
|
}
|
|
if err := checkRecordReport("codeql", `{"runs":[]}`); err == nil {
|
|
t.Error("no run at all must not be a scan of record")
|
|
}
|
|
if err := checkRecordReport("semgrep", "Authentication error"); err == nil {
|
|
t.Error("unparseable output must not be a scan of record")
|
|
}
|
|
}
|
|
|
|
// Legal direction: a clean run that loaded rules is a scan of record, and a
|
|
// warning-level notification does not refuse it.
|
|
func TestACleanRecordRunIsAccepted(t *testing.T) {
|
|
inv := `{"executionSuccessful":true,"toolExecutionNotifications":[{"level":"warning","message":{"text":"Syntax error at line 3 in a.py"}}]}`
|
|
if err := checkRecordReport("semgrep", sarif(oneRule, "", inv)); err != nil {
|
|
t.Fatalf("a clean run must be recorded: %v", err)
|
|
}
|
|
if err := checkRecordReport("sonarqube", "anything"); err != nil {
|
|
t.Fatalf("sonarqube is not SARIF on stdout: %v", err)
|
|
}
|
|
}
|
|
|
|
// CodeQL declares rules on tool.extensions (query packs), not on the driver.
|
|
func TestACodeQLRunWithRulesOnItsExtensionsIsAccepted(t *testing.T) {
|
|
report := `{"runs":[{"tool":{"driver":{"name":"CodeQL","rules":[]},"extensions":[{"name":"codeql/python-queries","rules":[` +
|
|
oneRule + `]}]},"results":[],"invocations":[{"executionSuccessful":true}]}]}`
|
|
if err := checkRecordReport("codeql", report); err != nil {
|
|
t.Fatalf("a CodeQL run with pack rules must be recorded: %v", err)
|
|
}
|
|
empty := `{"runs":[{"tool":{"driver":{"name":"CodeQL"},"extensions":[{"name":"p","rules":[]}]},"results":[]}]}`
|
|
if err := checkRecordReport("codeql", empty); err == nil {
|
|
t.Fatal("a CodeQL run with no rules anywhere must be refused")
|
|
}
|
|
}
|
|
|
|
func TestSemgrepExitCodes(t *testing.T) {
|
|
for _, ok := range []int{0, 1} {
|
|
if !semgrepRanToCompletion(ok) {
|
|
t.Errorf("exit %d is a completed run", ok)
|
|
}
|
|
}
|
|
for _, bad := range []int{2, 3, 4, 7, 13, 14, 127} {
|
|
if semgrepRanToCompletion(bad) {
|
|
t.Errorf("exit %d must not count as a completed run", bad)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestAnInlineSuppressionCountsInTheScanOfRecordOnly(t *testing.T) {
|
|
report := sarif(oneRule,
|
|
`{"ruleId":"python.lang.security.audit.eval","ruleIndex":0,"level":"error","suppressions":[{"kind":"inSource"}]}`,
|
|
`{"executionSuccessful":true}`)
|
|
total, blocking, _, parsed := tallyFindings(report, true)
|
|
if !parsed || total != 1 || blocking != 1 {
|
|
t.Fatalf("scan of record: total=%d blocking=%d parsed=%v, want 1/1/true", total, blocking, parsed)
|
|
}
|
|
// Every other caller keeps honouring it, as before.
|
|
total, blocking, _, _ = tallyFindings(report, false)
|
|
if total != 0 || blocking != 0 {
|
|
t.Fatalf("outside the scan of record: total=%d blocking=%d, want 0/0", total, blocking)
|
|
}
|
|
}
|