Documentation
¶
Overview ¶
Package semgrep shells out to a host-installed semgrep binary, running fendix's bundled rule pack and mapping every match to a fendix Finding (TASK-116). Replaces the embedded python/analyzers/semgrep_runner.py path so the engine no longer requires Python at runtime for Semgrep checks.
Behavioural parity with python/analyzers/semgrep_runner.py:
- SEC-<RULE_ID> finding IDs (uppercased, '-' and '.' → '_').
- Severity: rule metadata.fendix_severity wins; else map Semgrep's ERROR/WARNING/INFO → HIGH/MEDIUM/LOW.
- Confidence: rule metadata.confidence; default MEDIUM.
- Category: rule metadata.category; default "code".
- References: rule metadata.cwe (string or list).
- Evidence: matched source lines, truncated at 200 chars + "...".
- 120s default timeout per invocation, mirroring Python.
Distribution: rules are embedded into the binary via //go:embed and extracted to a per-process temp directory on first Scan call so the user's host semgrep can read them as files (semgrep --config requires a path on disk; it does not read stdin or in-memory rules). That directory is owned by the process, not by any one Scan; call Cleanup() on the way out (the CLI does, after the command tree returns) to remove it. Cleanup is refcounted, so it will not delete rules out from under a scan that is still running.
Graceful absence: if semgrep is not on $PATH the package returns ErrSemgrepUnavailable and the orchestrator logs an "install semgrep for X% more checks" notice and continues — matches the existing posture for missing Python.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrCodePathMissing = errors.New("semgrep: code path does not exist")
ErrCodePathMissing signals codePath is empty or doesn't exist — silent-skip parity with the Python `if not code_path.exists(): return`.
var ErrFatal = errors.New("semgrep: fatal error")
ErrFatal is wrapped into the error returned when semgrep ran but the run was fatal by Semgrep's own contract (see classifyRun): an exit other than 0 or 1, a death by signal, or an error of level "error" in its JSON. The orchestrator records it failed/execution_error. Findings semgrep still reported are returned with it.
var ErrMalformedOutput = errors.New("semgrep: no JSON results envelope")
ErrMalformedOutput is wrapped into the error returned when semgrep exited as if it succeeded but stdout is not a JSON results envelope. With --json a working semgrep always writes one, so zero findings cannot be assumed; the orchestrator records failed/malformed_output.
ErrSemgrepUnavailable signals that no `semgrep` binary was found on $PATH. Callers errors.Is-check it and emit the install-hint log line without treating it as a scan failure.
var ErrTimeout = errors.New("semgrep: timed out")
ErrTimeout is wrapped into the error returned when the semgrep process exceeds defaultTimeout, so the orchestrator can classify it as a timeout rather than a generic execution error.
var LookPath = exec.LookPath
LookPath wraps exec.LookPath and is var-not-func so tests can inject a fake binary location without touching $PATH.
Functions ¶
func Cleanup ¶
func Cleanup() error
Cleanup removes the temp directory holding the extracted rule pack.
Call it once on the way out of a process that may have scanned: the fendix CLI does so after its command tree returns, and long-lived embedders (e.g. a webhook server) should call it on their shutdown path. Without it the extracted directory outlives the process and accumulates in $TMPDIR.
Safe in every state: a no-op when nothing was extracted, and when scans are still in flight the removal is deferred until the last one finishes rather than yanking --config away from a running semgrep. A scan after a Cleanup re-extracts.
func Scan ¶
Scan runs the bundled fendix rule pack against codePath via the host's semgrep binary. Returns ErrSemgrepUnavailable when semgrep isn't installed; ErrCodePathMissing when codePath doesn't exist.
Other failures return a wrapped error — ErrTimeout, ErrFatal (a fatal run by Semgrep's own contract; see classifyRun) or ErrMalformedOutput (no JSON results envelope) — and the orchestrator records semgrep failed and continues, so one broken analyzer cannot stop the scan, and is never mistaken for a clean run.
The provided ctx is honoured: cancellation kills the semgrep subprocess. A 120s deadline is layered onto ctx if ctx has no shorter deadline already.
func ScanWithAllowlist ¶
func ScanWithAllowlist(ctx context.Context, codePath string, allow *gitdiff.Allowlist) ([]evidence.Evidence, error)
ScanWithAllowlist is Scan scoped to a diff-aware file allowlist. A nil allowlist scans the whole codePath (full-scan parity). When set, the changed files are passed to semgrep as `--include` path filters so the engine only analyses the diff — the semgrep half of `fendix scan --diff`.
semgrep's --include takes a glob/path relative to the scan root; passing the exact relative path of each changed file restricts the run to those files. An empty (non-nil) allowlist scopes to zero files, so we return nothing rather than run semgrep with no --include — which would scan the WHOLE tree, the exact opposite of the diff intent (the historical footgun).
Types ¶
type CatalogRule ¶
type CatalogRule struct {
ID string
Pack string
Message string
// Title and Remediation are the rule's static public copy
// (metadata.fendix_title / fendix_remediation). The runtime message may
// carry Semgrep metavariables ('$FUNC') that only a real finding fills in,
// so the published catalog must not print it raw. Empty when the message
// is already static.
Title string
Remediation string
Severity string
Confidence string
Category string
CWEs []string
Languages []string
}
CatalogRule is the public contract metadata encoded in a bundled Semgrep rule. Pattern bodies stay private to the executable rule pack.
func CatalogRules ¶
func CatalogRules() ([]CatalogRule, error)
CatalogRules parses the embedded rule pack used by the scanner itself.
type Report ¶
type Report struct {
// Incomplete counts the non-fatal, per-file problems Semgrep reported
// (level "warn": a file that did not parse fully, a per-file or per-rule
// timeout, a per-file memory limit), by Semgrep error type. The run is
// still ok; findings in those files may be missing.
Incomplete map[string]int
}
Report is what one Semgrep run said about itself besides its findings.
func ScanWithReport ¶
func ScanWithReport(ctx context.Context, codePath string, allow *gitdiff.Allowlist) ([]evidence.Evidence, Report, error)
ScanWithReport is ScanWithAllowlist that also returns the run's Report. On ErrFatal it returns whatever findings semgrep still reported, so no evidence is lost while the run is recorded failed.