package main // The ruleset digest (#2908). Written on the build VM, not run there (owner // decision 2026-10-11). import ( "strings" "testing" ) // The cross-language vector: apps/api/tests/test_sast_ruleset_pin.py pins the // SAME files to the SAME digest. If either side changes its encoding, one of // the two tests fails. const crossLanguageVector = "sha256:8fd4f707f97ff1e9f9d445830c1eaff610c1db0e96c3948039ebcbf7b04c1700" func vectorFiles() map[string][]byte { return map[string][]byte{ "python.yaml": []byte("rules: []\n"), "nested/go.yaml": []byte("x"), } } func TestTheRulesetDigestMatchesThePlatformsEncoding(t *testing.T) { if got := rulesetDigest(vectorFiles()); got != crossLanguageVector { t.Fatalf("digest %s, want %s (the platform computes the pack's digest the same way)", got, crossLanguageVector) } } // Hidden paths are not part of the digest on either side (#2908 review): the // Python twin pins the same vector with the same dotfiles added. func TestHiddenPathsAreNotPartOfTheDigest(t *testing.T) { files := vectorFiles() files[".gitkeep"] = []byte("") files[".hidden/x.yaml"] = []byte("y") files["nested/.keep"] = []byte("z") if got := rulesetDigest(files); got != crossLanguageVector { t.Fatalf("dotfiles changed the digest: %s, want %s", got, crossLanguageVector) } // Legal direction: a visible file with a dot inside its name still counts. files["a.b.yaml"] = []byte("rules: []\n") if rulesetDigest(files) == crossLanguageVector { t.Fatal("a visible rule file named a.b.yaml was skipped") } } func TestTheRulesetDigestIsDeterministic(t *testing.T) { a := rulesetDigest(vectorFiles()) for i := 0; i < 20; i++ { if b := rulesetDigest(vectorFiles()); b != a { t.Fatalf("map order changed the digest: %s vs %s", a, b) } } if !strings.HasPrefix(a, "sha256:") || len(a) != len("sha256:")+64 { t.Fatalf("digest %q is not sha256:<64 hex>", a) } } // Negative direction: every way of running fewer or other rules changes it. func TestAnyChangeToTheRulesetChangesTheDigest(t *testing.T) { base := rulesetDigest(vectorFiles()) cases := map[string]map[string][]byte{ "a file dropped": {"python.yaml": []byte("rules: []\n")}, "a file edited": {"python.yaml": []byte("rules: [] \n"), "nested/go.yaml": []byte("x")}, "a file renamed": {"python2.yaml": []byte("rules: []\n"), "nested/go.yaml": []byte("x")}, "a file added": {"python.yaml": []byte("rules: []\n"), "nested/go.yaml": []byte("x"), "extra.yaml": nil}, "an empty ruleset": {}, // The separator: path and hash cannot run into each other. "bytes moved into the name": {"python.yamlr": []byte("ules: []\n"), "nested/go.yaml": []byte("x")}, } for name, files := range cases { if rulesetDigest(files) == base { t.Errorf("%s: digest unchanged", name) } } } func TestTheRulesetDigestIsPosted(t *testing.T) { // postResults is not callable without an engine; the payload key is the // contract the platform reads (security_verdict.SAST_RULESET_KEY). if rulesetReportKey != "sast_ruleset_digest" { t.Fatalf("report_data key %q changed; the platform reads sast_ruleset_digest", rulesetReportKey) } }