chore: publish IntegraCI build modules a11ec75d3e09
This commit is contained in:
1 parent
694710104a
commit
3c2b80914e
78 files changed
+4982
-405
No files matched your search
@@ -0,0 +1,94 @@
|
||||
package main
|
||||
|
||||
// The ruleset a SAST run used, recorded with the scan (#2908, ADR-0093 §8).
|
||||
//
|
||||
// `--rules-dir` accepts any directory and a SARIF report does not say which
|
||||
// rule files produced it, so a near-empty rules directory would post a clean
|
||||
// scan of record. The module therefore digests the directory it mounted and
|
||||
// posts it as `report_data.sast_ruleset_digest`; the platform compares it with
|
||||
// the digest IT computes from the pack it ships (`catalog/sast-rules/rules`,
|
||||
// `apps/api/app/security/services/sast_ruleset.py`) and never with a value the
|
||||
// caller supplies. The two implementations must agree byte for byte: the test
|
||||
// vector in ruleset_test.go is the same one the Python test pins.
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"dagger/sast-scan/internal/dagger"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// rulesetReportKey is the report_data key the digest is posted under
|
||||
// (`security_verdict.SAST_RULESET_KEY` on the platform).
|
||||
const rulesetReportKey = "sast_ruleset_digest"
|
||||
|
||||
// rulesetDigestPrefix names the algorithm, as an image digest does.
|
||||
const rulesetDigestPrefix = "sha256:"
|
||||
|
||||
// rulesetDigest is the digest of a rules tree given as relative POSIX path ->
|
||||
// file bytes. Deterministic: for each path in byte order, the line
|
||||
// `<path>\x00<hex sha256 of the bytes>\n`, and the sha256 of all the lines.
|
||||
// A file's name is part of the digest (renaming a rule file changes it) and
|
||||
// the NUL separator keeps a path from running into its hash. An empty tree has
|
||||
// a digest too (the sha256 of nothing); it is not the platform pack's.
|
||||
//
|
||||
// A path with any hidden segment (".gitkeep", ".github/x.yaml") is skipped, as
|
||||
// the platform does: semgrep does not load hidden config files, and the two
|
||||
// listings (dagger Glob here, Path.rglob in Python) need not agree on whether
|
||||
// they list dotfiles.
|
||||
func rulesetDigest(files map[string][]byte) string {
|
||||
paths := make([]string, 0, len(files))
|
||||
for p := range files {
|
||||
if isHiddenPath(p) {
|
||||
continue
|
||||
}
|
||||
paths = append(paths, p)
|
||||
}
|
||||
sort.Strings(paths)
|
||||
h := sha256.New()
|
||||
for _, p := range paths {
|
||||
sum := sha256.Sum256(files[p])
|
||||
h.Write([]byte(p))
|
||||
h.Write([]byte{0})
|
||||
h.Write([]byte(hex.EncodeToString(sum[:])))
|
||||
h.Write([]byte{'\n'})
|
||||
}
|
||||
return rulesetDigestPrefix + hex.EncodeToString(h.Sum(nil))
|
||||
}
|
||||
|
||||
// isHiddenPath reports whether any segment of a relative path starts with ".".
|
||||
func isHiddenPath(p string) bool {
|
||||
for _, part := range strings.Split(p, "/") {
|
||||
if strings.HasPrefix(part, ".") {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// rulesetDigestOf digests every regular file under dir, the directory semgrep
|
||||
// was given. A listing or read error is an error: a scan whose ruleset cannot
|
||||
// be named is not posted with a guessed one.
|
||||
func rulesetDigestOf(ctx context.Context, dir *dagger.Directory) (string, error) {
|
||||
entries, err := dir.Glob(ctx, "**/*")
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("list the rules directory: %w", err)
|
||||
}
|
||||
files := make(map[string][]byte, len(entries))
|
||||
for _, e := range entries {
|
||||
// Directories are listed with a trailing slash; their files are listed
|
||||
// on their own.
|
||||
if strings.HasSuffix(e, "/") {
|
||||
continue
|
||||
}
|
||||
body, err := dir.File(e).Contents(ctx)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("read rule file %s: %w", e, err)
|
||||
}
|
||||
files[strings.TrimPrefix(e, "./")] = []byte(body)
|
||||
}
|
||||
return rulesetDigest(files), nil
|
||||
}
|
||||
Reference in new issue
Block a user