sast

package
v3.83.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 2, 2026 License: AGPL-3.0 Imports: 28 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultRegistry = "https://github.com"

DefaultRegistry is the base URL used when --rule-registry is not set.

Variables

View Source
var DefaultRulesFS embed.FS

DefaultRulesFS holds the built-in .rego rule files compiled into the binary. The rules/ directory is relative to this file (internal/sast/rules/).

View Source
var SeverityLabel = map[string]string{
	"critical": "Dangerous",
	"high":     "Risky",
	"medium":   "Quality",
	"low":      "Style",
	"info":     "Tentative",
}

SeverityLabel maps severity to the human-readable semantic label.

View Source
var SeverityToLevel = map[string]string{
	"critical": "error",
	"high":     "error",
	"medium":   "warning",
	"low":      "note",
	"info":     "note",
}

SeverityToLevel maps severity to the default SARIF level when a rule doesn't explicitly set "level" in its metadata.

Functions

func ApplyNosec added in v3.67.0

func ApplyNosec(findings []Finding, rootPath string) ([]Finding, []NosecHit)

ApplyNosec filters findings against nosec source comments and returns the kept findings plus one NosecHit per dropped finding. rootPath anchors the (possibly relative) ArtifactURI values to disk. len(hits) is the number dropped.

func CacheDir

func CacheDir(ref RuleRef) (string, error)

CacheDir returns the OS-native cache directory for a rule repository.

Linux:   ~/.cache/vulnetix/rules/<org>/<repo>/
macOS:   ~/Library/Caches/vulnetix/rules/<org>/<repo>/
Windows: %LOCALAPPDATA%\vulnetix\rules\<org>\<repo>\

func CanonicalLanguage added in v3.35.0

func CanonicalLanguage(label string) string

CanonicalLanguage exposes the alias-collapse used by the language pre-filter (e.g. "node"/"ts" -> "javascript") so callers can normalise catalog labels.

func FetchRuleRepo

func FetchRuleRepo(registry string, ref RuleRef, w io.Writer) (string, error)

FetchRuleRepo clones or pulls a rule repository into the system cache. Returns the local cache path. Prints progress to w.

func Fingerprint

func Fingerprint(ruleID, artifactURI string, startLine int) string

Fingerprint produces a stable hash identifying a finding by rule + location. Used as the dedup key in memory.yaml and the SARIF fingerprints map. Returns the first 16 hex characters of SHA-256("<RuleID>\x00<ArtifactURI>\x00<StartLine>").

func LanguagesForPath added in v3.35.0

func LanguagesForPath(path string) map[string]bool

LanguagesForPath returns the set of canonical ecosystem keys that a single file path maps to, by extension suffix, basename substring, or special filename. Exported so other packages (e.g. the AIBOM detector) can scope per-file content rules to the languages they target, reusing the same extension table as the SAST language pre-filter.

func LoadAllModules

func LoadAllModules(
	defaultFS embed.FS,
	disableDefault bool,
	ruleRefs []RuleRef,
	registry string,
	w io.Writer,
) (map[string]string, error)

LoadAllModules loads default embedded rules and any external --rule repos. If disableDefault is true, embedded rules are skipped. Returns map[filename]source for all loaded .rego files.

func LoadFileContents

func LoadFileContents(input *ScanInput, maxSize int64)

LoadFileContents populates input.FileContents for files matching the given language extensions. Files over maxSize bytes and binary files are skipped.

When LoadOptions is provided the caller can opt into binary inspection (strings + EXIF) and the synthetic content is folded into the same map.

func LoadFileContentsWithOptions added in v3.29.0

func LoadFileContentsWithOptions(input *ScanInput, opts LoadOptions)

LoadFileContentsWithOptions is the full-control variant used by the secrets subcommand.

func MarkConfidenceGap added in v3.65.0

func MarkConfidenceGap(res *SARIFResult, reason string)

MarkConfidenceGap flags a result whose evidence could not be fully verified, with a reason stating exactly what was unverifiable and why.

func MergeGitHistoryEntries added in v3.29.0

func MergeGitHistoryEntries(input *ScanInput, entries []secretscan.GitHistoryEntry) int

MergeGitHistoryEntries injects the file versions returned by secretscan.ScanGitHistory into input.FileContents. Returns the number of entries injected. Duplicate keys (same commit, same path) are silently ignored.

func PrintHeadline added in v3.12.1

func PrintHeadline(report *SASTReport)

PrintHeadline prints a bold SAST headline (finding count + severity breakdown) above the analysis table. Used as the top-of-output summary when SCA did not run (so the SCA "X packages | Y vulnerabilities" line is absent).

func PrintHeadlineWithLabel added in v3.27.0

func PrintHeadlineWithLabel(report *SASTReport, label string)

PrintHeadlineWithLabel prints a bold findings headline using the scan family label supplied by the caller.

func PrintPrettySummary

func PrintPrettySummary(report *SASTReport, resultsOnly bool)

PrintPrettySummary prints a styled SAST findings table to stdout. If resultsOnly is true, stays silent when there are no findings.

func PrintPrettySummaryWithTitle added in v3.27.0

func PrintPrettySummaryWithTitle(report *SASTReport, resultsOnly bool, title string)

PrintPrettySummaryWithTitle prints a styled findings table with a caller supplied heading. Container/IaC/Secrets subcommands share the SAST engine but should not call their output "SAST".

func ResolveURL

func ResolveURL(registry string, ref RuleRef) string

ResolveURL builds the git clone URL from a registry base URL and rule reference.

func ResolvedFingerprints

func ResolvedFingerprints(oldLog *SARIFLog, newFindings []Finding) []string

ResolvedFingerprints returns fingerprints present in the old SARIF log but absent from the new findings. These represent resolved findings.

func WriteSARIF

func WriteSARIF(log *SARIFLog, path string) error

WriteSARIF serializes a SARIF log to the given file path.

Types

type BuildOptions added in v3.29.0

type BuildOptions struct {
	MaxDepth int
	Excludes []string

	// IgnoreGit, when true, skips the .git directory entirely. The default
	// is false: the secrets subcommand walks .git to surface credentials
	// that exist only in past commits.
	IgnoreGit bool

	// IgnoreGlobs is an additional set of glob patterns to exclude. The
	// patterns are matched against the relative path and the base name
	// (mirroring --exclude). The CLI's --ignore flag is wired into this
	// slice so that a single --ignore "fixtures/**" is enough.
	IgnoreGlobs []string

	// IgnoreBinaries, when true, skips binary files entirely. When false
	// (the default for the secrets subcommand), binary files are inspected
	// with strings + EXIF and the result is added to FileContents.
	IgnoreBinaries bool

	// GitHistory, when true, walks the git history at rootPath and adds
	// each file version to FileContents under the __git_history__/ prefix.
	// Requires that IgnoreGit be false; if both are set, IgnoreGit wins.
	GitHistory bool

	// GitHistoryMaxCommits caps the number of commits walked.
	GitHistoryMaxCommits int
	// GitHistoryMaxFiles caps the number of file versions emitted.
	GitHistoryMaxFiles int

	// RespectGitignore, when true, prunes files and directories matched by
	// .gitignore files. Defaults false for backwards compatibility; the
	// sast/secrets/containers/iac/cbom/aibom commands set it true unless the
	// user passes their --*-include-ignored override.
	RespectGitignore bool
}

BuildOptions controls how the filesystem is walked and how binary/git content is folded into the scan input. Zero-value options produce the legacy behaviour: text files only, no git history.

type Engine

type Engine struct {
	// contains filtered or unexported fields
}

Engine compiles Rego modules and evaluates them against a filesystem scan.

func NewEngine

func NewEngine(modules map[string]string, scanRoot string) *Engine

NewEngine constructs an Engine with the given Rego modules.

func (*Engine) Evaluate

func (e *Engine) Evaluate(opts EvalOptions) (*SASTReport, error)

Evaluate runs all loaded Rego policies against the filesystem at scanRoot.

func (*Engine) ListRules

func (e *Engine) ListRules() ([]RuleMetadata, error)

ListRules extracts metadata from all loaded rule packages without running detection. Used for --list-default-rules.

type EvalOptions

type EvalOptions struct {
	MaxDepth int
	Excludes []string

	// IgnoreGit, IgnoreGlobs, IgnoreBinaries, GitHistory, etc. are
	// forwarded to BuildScanInputWithOptions / LoadFileContentsWithOptions
	// so the secrets subcommand can enable binary and history scanning
	// without affecting the generic scan command's behaviour.
	IgnoreGit            bool
	IgnoreGlobs          []string
	IgnoreBinaries       bool
	GitHistory           bool
	GitHistoryMaxCommits int
	GitHistoryMaxFiles   int
	MinStringLength      int

	// RespectGitignore prunes files/dirs matched by .gitignore. The
	// sast/secrets/containers/iac commands set it true by default; the
	// user opts out with --<mode>-include-ignored.
	RespectGitignore bool
}

EvalOptions configures the SAST evaluation.

type Finding

type Finding struct {
	RuleID      string        `json:"rule_id"`
	Message     string        `json:"message"`
	ArtifactURI string        `json:"artifact_uri"`
	Severity    string        `json:"severity"`
	Level       string        `json:"level"`
	StartLine   int           `json:"start_line"`
	EndLine     int           `json:"end_line,omitempty"`
	Snippet     string        `json:"snippet"`
	Fingerprint string        `json:"-"`
	Metadata    *RuleMetadata `json:"-"`

	// Test-suite attribution, set post-evaluation by internal/testsuite when the
	// finding's file belongs to the project's test suite. IsTestSuite drives the
	// SARIF `vulnetix/test-*` result properties and the typed wire fields.
	IsTestSuite        bool     `json:"-"`
	TestFramework      string   `json:"-"`
	TestLanguage       string   `json:"-"`
	TestConfidence     string   `json:"-"`
	TestMatchedPattern string   `json:"-"`
	TestEvidence       []string `json:"-"`
}

Finding is unmarshaled from each element of the Rego "findings" set. Detection fields (ArtifactURI, StartLine, Snippet) are set by Rego logic. Fingerprint and Metadata are set by the engine after evaluation.

type LoadOptions added in v3.29.0

type LoadOptions struct {
	// MaxFileSize is the upper bound for any single file's text content
	// (raw or extracted). Files larger than this are skipped entirely.
	MaxFileSize int64
	// IgnoreBinaries, when true, skips binary files. When false (the
	// default for the secrets subcommand), binary files are inspected:
	// printable strings are extracted with the secretscan package and any
	// EXIF/IPTC/XMP metadata is added under __exif__/.
	IgnoreBinaries bool
	// MinStringLength is the minimum run length to surface when extracting
	// strings from binaries. Defaults to secretscan.StringMin (4).
	MinStringLength int
}

LoadOptions configures LoadFileContents. The MaxFileSize and IgnoreBinaries fields correspond directly to the CLI flags of the same name; MaxDepth and Excludes are not relevant here (the walker has already determined the set of files to consider).

type NosecHit added in v3.68.0

type NosecHit struct {
	File      string
	StartLine int
	EndLine   int
	RuleID    string
	Kind      string
	WholeFile bool
	Snippet   string
}

NosecHit records one finding that a nosec directive suppressed. It carries enough to mint/track an org suppression rule: the file + line span of the suppressed code, the rule id that was silenced, the rule Kind (so the caller can map it to a scanner category), whether a whole-file (line-1) directive did the suppressing, and the code snippet for drift tracking.

type RuleMetadata

type RuleMetadata struct {
	ID          string   `json:"id"`
	Name        string   `json:"name"`
	Description string   `json:"description"`
	HelpURI     string   `json:"help_uri"`
	Languages   []string `json:"languages"`
	Severity    string   `json:"severity"`
	Level       string   `json:"level"`
	Kind        string   `json:"kind"`
	CWE         []int    `json:"cwe"`
	CAPEC       []string `json:"capec"`
	ATTACKTech  []string `json:"attack_technique"`
	CVSSv4      string   `json:"cvssv4"`
	CWSS        string   `json:"cwss"`
	Tags        []string `json:"tags"`
}

RuleMetadata is unmarshaled from the Rego "metadata" constant object. Every field maps directly to the JSON keys used in the Rego policy.

func (*RuleMetadata) EffectiveLevel

func (m *RuleMetadata) EffectiveLevel() string

EffectiveLevel returns the SARIF level for a rule — the explicit level if set, otherwise derived from severity.

type RuleRef

type RuleRef struct {
	Org  string
	Repo string
}

RuleRef identifies an external rule repository by org and repo name.

func ParseRuleRef

func ParseRuleRef(arg string) (RuleRef, error)

ParseRuleRef parses a "org/repo" string from a --rule flag value.

type SARIFArtifact

type SARIFArtifact = sarif.Artifact

type SARIFArtifactLocation

type SARIFArtifactLocation = sarif.ArtifactLocation

type SARIFInvocation added in v3.65.0

type SARIFInvocation = sarif.Invocation

type SARIFLocation

type SARIFLocation = sarif.Location

type SARIFLog

type SARIFLog = sarif.Log

func BuildSARIF

func BuildSARIF(findings []Finding, rules []RuleMetadata, toolVersion string) *SARIFLog

BuildSARIF converts findings and rules into a SARIF 2.1.0 log.

func LoadExistingSARIF

func LoadExistingSARIF(path string) (*SARIFLog, error)

LoadExistingSARIF reads a SARIF log from disk. Returns nil if the file does not exist.

type SARIFMessage

type SARIFMessage = sarif.Message

type SARIFNotification added in v3.65.0

type SARIFNotification = sarif.Notification

type SARIFPhysicalLocation

type SARIFPhysicalLocation = sarif.PhysicalLocation

type SARIFPropertyBag

type SARIFPropertyBag = sarif.PropertyBag

type SARIFRegion

type SARIFRegion = sarif.Region

type SARIFReportingDescriptor

type SARIFReportingDescriptor = sarif.ReportingDescriptor

type SARIFResult

type SARIFResult = sarif.Result

type SARIFRun

type SARIFRun = sarif.Run

type SARIFSnippet

type SARIFSnippet = sarif.Snippet

type SARIFTool

type SARIFTool = sarif.Tool

type SARIFToolDriver

type SARIFToolDriver = sarif.ToolComponent

type SASTReport

type SASTReport struct {
	Findings    []Finding
	Rules       []RuleMetadata
	RulesLoaded int // rules after filtering (kind/id) that were evaluated
	RulesTotal  int // rules loaded pre-filter (builtin + --rule repos)
	// Degradations lists capabilities that ran reduced or not at all during
	// this evaluation ("couldn't verify X because Y"). They are surfaced as
	// SARIF toolExecutionNotifications so a report consumer can distinguish
	// "scanned clean" from "not fully scanned".
	Degradations []string
}

SASTReport holds the results of a SAST evaluation run.

type ScanInput

type ScanInput struct {
	// FileSet maps each relative file path to true for O(1) existence checks in Rego.
	FileSet map[string]bool `json:"file_set"`
	// DirsByLanguage maps language name to directories containing that language's indicator files.
	DirsByLanguage map[string][]string `json:"dirs_by_language"`
	// FileContents maps relative path to file text. Populated lazily for small files
	// when content-level rules are present. Files over MaxFileSize and binary files are
	// skipped unless binary inspection is enabled (see FileScanOptions).
	FileContents map[string]string `json:"file_contents,omitempty"`
	// ScanRoot is the absolute path being scanned (for display; rules use relative paths).
	ScanRoot string `json:"scan_root"`
}

ScanInput is serialized to JSON and passed as the OPA input document.

func BuildScanInput

func BuildScanInput(rootPath string, maxDepth int, excludes []string) (*ScanInput, error)

BuildScanInput walks the filesystem at rootPath and builds the OPA input document.

func BuildScanInputWithOptions added in v3.29.0

func BuildScanInputWithOptions(rootPath string, opts BuildOptions) (*ScanInput, error)

BuildScanInputWithOptions is the full-control entry point. It replaces BuildScanInput when the caller needs to enable binary or git-history inspection (the secrets subcommand does, the generic scan does not).

Directories

Path Synopsis
Command secretsgen renders the high-fidelity secret-detection rule set and its documentation from a single source of truth: catalog.json.
Command secretsgen renders the high-fidelity secret-detection rule set and its documentation from a single source of truth: catalog.json.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL