diff

package
v0.195.0 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2026 License: Apache-2.0 Imports: 18 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package diff compares two Draugr scan results and classifies every finding as new, fixed, or unchanged, the security delta of a change (typically a PR's head vs the base branch). It powers `draugr diff` and its differential gate ("fail only on findings this change introduces").

Inputs are SARIF reports (the results.sarif that `draugr scan -o` writes): SARIF is Draugr's complete, structured result currency, whereas the JSON summary can be trimmed by --min-priority.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Formats

func Formats() []string

Formats lists the diff output formats, sorted.

func Render

func Render(w io.Writer, format string, r Result, opts Options) error

Render writes the diff in the named format. Unknown formats error.

func Views added in v0.121.0

func Views() []string

Views lists them, sorted, for the flag's own help.

Types

type Cause added in v0.193.0

type Cause struct {
	// Input is what changed: exposure, criticality, severity (the scanner's own rating), minimum
	// (the control's minimum severity), escalation (a signal that raised the severity), floor (a
	// band held up by the control or a converter), reachability (an unreachable verdict that lowered
	// it) or cap (a band held down because a lockfile pins the reference).
	Input string `json:"input"`
	// From and To are the input as the base and the head recorded it. Empty where that scan recorded
	// none: a component that declared no exposure, a finding no signal raised.
	From string `json:"from,omitempty"`
	To   string `json:"to,omitempty"`
	// Text is the cause as the console prints it: `exposure public → internal`, `↑ KEV`.
	Text string `json:"text"`
}

Cause is one input to a finding's band that the two scans recorded differently: what the component declared, what the scanner rated it, or a signal that moved it in one scan and not the other.

Read from what each scan stamped on the finding, so a cause is only ever a difference both reports record. A band that moved with every recorded input the same, as a change to the ranking's own settings moves one, has no cause, and the row says nothing rather than guessing.

type Change added in v0.121.0

type Change string

Change is what happened to a finding between the two scans.

One vocabulary for the four states, so a renderer, a gate and a reader all name them the same way. Unchanged is not here: it is the pre-existing backlog this exists to leave out.

const (
	ChangeNew        Change = "new"
	ChangeUnaccepted Change = "unaccepted"
	ChangeAccepted   Change = "accepted"
	ChangeFixed      Change = "fixed"
)

The four states a diff reports, and the mark each one carries.

func (Change) Mark added in v0.121.0

func (c Change) Mark() string

Mark is the sign this change wears in a listing.

A glyph as well as a word, because a column of words is read and a column of marks is scanned, and the four are told apart at a glance where a reader is looking for the new ones among sixty.

func (Change) NeedsSomebody added in v0.121.0

func (c Change) NeedsSomebody() bool

NeedsSomebody reports whether this change is work or a decision rather than good news.

A fix is the one state nobody has to do anything about. The rest are something introduced, something whose acceptance ended, or something somebody chose to live with, and each of those wants a person.

type Dropped added in v0.189.0

type Dropped struct {
	// Component is the base component the control was on, empty for a project-wide control.
	Component string
	// Control is the control's name.
	Control string
	// Narrowed says how the head stopped reading part of what the base read, where a component still
	// runs the control there: its paths, an ignore entry, its revision or the control's settings.
	// Zero where the control no longer runs on some part the base read, which the reader is told as
	// the control stopped.
	Narrowed Narrowing
}

Dropped is a control the base ran that the head does not.

type Entry added in v0.121.0

type Entry struct {
	Change Change
	sarif.Result
	// contains filtered or unexported fields
}

Entry is one finding and what happened to it.

type FileChange added in v0.189.2

type FileChange struct {
	// Repository is the URL of the repository whose checkout held a scanner's file, empty for a VEX
	// document or a setting's file.
	Repository string
	// Path is as the run records it: from the root of the checkout, or as the descriptor writes it.
	Path string
	// Added is set where the base read no such file.
	Added bool
	// BaseUnread is set where the base report could not have recorded the file, written by a
	// release that recorded no scanner's file below a checkout's root, and the file could not be
	// read at the base's commit either, with why. Held as added, since nothing shows the base had it.
	BaseUnread Unread
	// FetchBase is the command that fetches the base's commit into the checkout the comparison ran
	// in, set where BaseUnread is CommitMissing and the checkout has a remote to fetch it from.
	FetchBase string
}

FileChange is a file the head adds, or reads with other content than the base did, that can set findings aside or change what a scanner finds.

type Gate added in v0.121.0

type Gate struct {
	FailOn         sarif.Severity
	FailOnPriority string
	// Base is the base scan's config.gate.failOnNew where it is in force because the head's own
	// gate is weaker, in another vocabulary, or absent, and empty where the head's decides alone.
	// A change's descriptor is part of the change, so a pull request that deletes or loosens the
	// gate is judged by the gate its base already enforces until it merges.
	Base string
	// Head is the head's config.gate.failOnNew beside Base, and empty where it sets none.
	Head string
	// HeadUnread is set where no descriptor was read for the head, so an empty Head says nothing
	// about the change: `draugr diff` run where no descriptor is in reach compares two reports and
	// cannot know what the change's descriptor sets.
	HeadUnread bool
}

Gate is the differential rule a run applied, carried on the result so every rendering can state what the verdict was measured against.

Empty when nothing was asked for, which is a real state and not a default: `draugr diff` with no gate reports and exits 0, and a report claiming a verdict nobody asked for would be inventing one.

func (Gate) BaseNote added in v0.188.1

func (g Gate) BaseNote() string

BaseNote says why the base's gate applies, and empty where it does not.

func (Gate) Sentence added in v0.121.0

func (g Gate) Sentence() string

Sentence is the rule in one clause, for a reader who was not there when it ran.

func (Gate) Stated added in v0.121.0

func (g Gate) Stated() bool

Stated reports whether a gate was asked for at all.

type Held added in v0.189.0

type Held struct {
	// Unrecorded is set where the base report recorded no declaration, a report written by a
	// release that did not record one. The head's declaration then decides alone, and a reader is
	// told so rather than left to assume the base's applied.
	Unrecorded bool
	// Raised are the new findings the base's classification ranks above the head's, each at the band
	// it now has, with the band the head's classification gave it.
	Raised []Rerank
	// Dropped are the controls the base turned on that the head does not, where the head still
	// declares what they covered or has stopped declaring it.
	Dropped []Dropped
	// Unscanned are the base's findings from a dropped control. They are not fixed: nobody looked.
	Unscanned []sarif.Result
	// SelfAccepted are new findings set aside by an exclusion the base did not have. They are listed
	// as accepted, and the gate counts them as new until the exclusion merges.
	SelfAccepted []sarif.Result
	// Files are the files outside the descriptor that the head adds or reads with other content
	// than the base did: a VEX document, a scanner's own configuration file such as
	// `.gitleaksignore`, or a ruleset a setting names. Each can set findings aside or change what a
	// scanner finds, which the comparison cannot see past, so any one of them fails the change
	// until it merges. One list rather than one per control, since a VEX document or an ignore file
	// applies across controls.
	Files []FileChange

	// Trusted is set where the run was told to trust the change's descriptor, as
	// --trust-descriptor-change tells it. The head's declaration then decides the gate: Raised,
	// Dropped and SelfAccepted say what the base's would have decided, and decide nothing.
	Trusted bool
	// Digest names the head's declaration, the value --trust-descriptor-change takes to trust it.
	// Empty where the caller did not set it.
	Digest string
	// TrustNamedOther is a --trust-descriptor-change value that names another declaration than the
	// head's, so the base's decided. A switch left in a pipeline after its pull request merged
	// names the descriptor that merged, and the next change to it is held to the base again.
	TrustNamedOther string
	// BaseVersion is the release that wrote the base report, set where a file in Files is
	// BaseUnread, so the run can say which release did not record it.
	BaseVersion string
	// contains filtered or unexported fields
}

Held is what a comparison's base declaration decided beyond what the head's declared.

The head's descriptor is part of the change under review. Read alone, a pull request that declares its component less exposed, turns a control off, narrows what its scan reads or excludes the finding it adds is judged by the declaration it introduces, and passes a gate its base would have failed it on. So the base's declaration stands beside the head's until the change merges: a new finding ranks by the more exposed and more critical of the two classifications, a control the base turned on is still expected over everything it read, and only an exclusion the base already had accepts a new finding. The same holds for the files outside the descriptor that set findings aside or decide what a scanner finds: one the change adds or edits fails it until it merges.

func (Held) DroppedSentence added in v0.189.0

func (h Held) DroppedSentence() string

DroppedSentence is the clause a failing run exits with for what the base held, empty where it held nothing: the controls the change stopped, those it narrowed, then the files it added or changed.

func (Held) Fails added in v0.189.0

func (h Held) Fails() bool

Fails reports whether what the base held decides the verdict by itself: a control the base ran is not running, so what the change adds where it ran was never looked for, or the change adds or edits a file that can set its own findings aside. Never on a run told to trust the change's descriptor.

func (Held) Notes added in v0.189.0

func (h Held) Notes() []string

Notes are the sentences that say what the base's declaration decided, in the order a reader needs them: what failed the change first, then what moved a finding's rank or count. Empty where the base decided nothing beyond the head.

type Move added in v0.142.2

type Move struct {
	sarif.Result
	Was sarif.Result
}

Move is a finding in a different component in the head from the one it had in the base: the head's copy, with the base's beside it, so a row can name both components and both priorities. A priority that changed with the component is the new component's classification, not a sign the finding is new.

type Narrowing added in v0.189.2

type Narrowing struct {
	// Paths are the base's paths the head no longer reads all of, as a checkout selects them, with
	// wholeRepository standing for a base that read the whole repository.
	Paths []string
	// Ignore are the ignore entries the head added over the part, as written.
	Ignore []string
	// Revisions are the revisions the head reads the repository at in place of the base's.
	Revisions []RevisionChange
	// Settings are the keys of the control's settings the head added, removed or changed, sorted:
	// a scanner's block or a setting of the control's own.
	Settings []string
}

Narrowing is how a change narrowed what a control reads of a repository a component declares, or changed how the control runs, while still running it.

A reader told "this change stops secrets on api" after adding one ignore line would look for a control turned off and find none, so the run names what narrowed instead.

func (Narrowing) String added in v0.189.2

func (n Narrowing) String() string

String names what changed as a reader says it: "paths no longer reading all of services/api; ignore src/client.py; revision pinned to v2; settings retirejs and trivyFs changed".

type ObjectChange added in v0.178.0

type ObjectChange struct {
	sarif.Result
	// Joined are the objects the head lists and the base did not, and Left the reverse, each sorted.
	Joined, Left []string
}

ObjectChange is a check that lists the objects failing it, failing in both scans, whose list changed: the head's copy, and the objects that joined it and left it.

A check is one finding per place, so the comparison pairs it on its objects and calls it new when one joined and unchanged otherwise. Neither says which object, and the message, which lists every object failing the check, cannot be read for it either: most of what it lists was there before.

type Options added in v0.121.0

type Options struct {
	View View
	// Top caps the listing. Zero shows everything, which is the default here and not on `scan`:
	// a diff is already narrowed to what one change did, and hiding part of that removes the thing
	// the command exists to show. The flag is for the dependency bump that introduces forty.
	Top int
	// Declared is where the head's descriptor declares each image, for the sarif format, which places
	// a new finding on an image on that line. Nil leaves such a finding with no location.
	Declared sarif.Declarations
}

Options are what the caller asked for, beyond the comparison itself.

type PriorityCounts

type PriorityCounts struct {
	P1 int `json:"p1"`
	P2 int `json:"p2"`
	P3 int `json:"p3"`
	P4 int `json:"p4"`
}

PriorityCounts tallies findings by action band. Unprioritized findings are not counted here.

type Ranker added in v0.189.0

type Ranker func(res sarif.Result, exposure saga.Exposure, criticality saga.Criticality) string

Ranker is the band a finding takes on a component classified with exposure and criticality, ranked the way the scan that reported it ranked it.

type Rerank added in v0.181.0

type Rerank struct {
	sarif.Result
	// From is the band the finding had in the base, "" where the base did not prioritize it.
	From string
	// Was is the base's copy, which records the inputs the base ranked it by. Zero where the base
	// copy is not known, and Causes is then nil.
	Was sarif.Result
	// contains filtered or unexported fields
}

Rerank is a finding whose band changed between the scans and nothing else did: the head's copy, with the band the base gave it.

func (Rerank) Causes added in v0.193.0

func (r Rerank) Causes() []Cause

Causes is what the two scans recorded differently among the inputs to this finding's band, in the order the ranking applies them: the component's classification, the rating, then each signal. Nil where the base copy is not known or nothing recorded differs.

func (Rerank) Raised added in v0.181.0

func (r Rerank) Raised() bool

Raised reports whether the head ranks the finding more urgent than the base did.

type Result

type Result struct {
	New       []sarif.Result // present and active in head, absent from base
	Fixed     []sarif.Result // active in base, absent from head
	Unchanged []sarif.Result // active in both at the same band, or suppressed in both (head copy)
	// Accepted is a finding somebody excused between the two scans, and one that arrived already
	// excused by a rule that was already there.
	//
	// Its own category because accepting a risk is not fixing it, and reporting it as a fix tells
	// a reviewer the opposite of what happened: nobody removed the finding, somebody decided to
	// live with it. That decision is the change most worth a second pair of eyes, and it used to
	// read as good news.
	Accepted []sarif.Result
	// Unaccepted is a finding that was excused in the base and counts again in the head, an
	// exclusion removed, or one that reached its expiry date.
	//
	// Distinct from New because nobody introduced it. It was known, it was accepted, and the
	// acceptance ended; reporting it as a fresh discovery loses the part somebody needs to act on,
	// which is that a decision has to be made again.
	//
	// Named for the decision rather than for the finding. "Reopened" is issue-tracker vocabulary
	// for something that was fixed and came back, and nothing here was ever fixed.
	Unaccepted []sarif.Result
	// Moved is a finding the head reports under a different component from the base, in the same
	// repository, file, tool and rule, and in the same state: counting in both, or excused in both.
	//
	// A pull request that divides a repository into components differently changes every finding's
	// component and nothing else. Reported as fixed under the old component and new under the
	// other, each one would trip a gate on new findings. The gate does not read Moved, the same way
	// it does not read Accepted.
	Moved []Move
	// Reranked is a finding counting in both scans, in the same component, whose priority band
	// changed: declared exposure or criticality, an exploitation signal, a reachability verdict.
	//
	// Kept out of Unchanged rather than counted in it as well. A band change is often what the
	// comparison was run to see, and a total that holds both the findings that changed band and the
	// ones that did not reads as "nothing changed" to anybody who adds the headline up. Every finding
	// in head stays in exactly one state. The gate does not read Reranked: nobody introduced these.
	Reranked []Rerank
	// Objects are the checks listing objects whose list changed between the scans, with what joined
	// and left each, in the same state in both: counting in both, or excused in both.
	Objects []ObjectChange
	// Rules is what the scanners said about the rules these findings cite, carried over from the
	// reports being compared.
	//
	// A diff that keeps only results keeps only identifiers. `CVE-2018-1000656` in a table is a
	// string to copy into a search box, and the same id uploaded to code scanning arrives with no
	// description and whatever link can be guessed from its shape, while the scanner that found it
	// published both. Keeping the rules is what lets a reader click the finding instead of looking it
	// up.
	Rules map[string]sarif.Rule
	// AutomationID is the head report's automationDetails.id, which analysis the head scan is, empty
	// where it names none. The sarif format writes it, so code scanning files a pull request's new
	// findings under the category of the scan the branch will receive once the change merges, and
	// compares them with that analysis rather than finding none to compare with.
	AutomationID string
	// Gate is the differential rule this run applied, so every rendering can say what the verdict
	// was measured against rather than leaving a reader to assume it.
	Gate Gate
	// Tripped is the new findings that met the gate. Empty where none did, and where no gate was
	// asked for at all, which are different states the Gate itself tells apart.
	Tripped []sarif.Result
	// NoBase marks a comparison with no base scan, as for the pull request that adds the
	// descriptor. Every finding at the head is then new, which a reader would otherwise take for a
	// change that introduced all of them.
	NoBase bool
	// Held is what the base's declaration decided beyond the head's: see HoldToBase.
	Held Held
	// contains filtered or unexported fields
}

Result is the classified delta between a base and a head report.

func Compare

func Compare(base, head sarif.Report) Result

Compare classifies every finding across the two reports by stable identity.

Five outcomes rather than three, because a suppression is a state a finding can be in and not a reason to stop tracking it. A finding that became suppressed was accepted, not fixed; one that stopped being suppressed came back, and nobody introduced it.

func CompareWithoutBase added in v0.152.1

func CompareWithoutBase(head sarif.Report) Result

CompareWithoutBase is the comparison with no base scan: nothing at the base, so every finding active in head is new.

func (Result) Changed added in v0.121.0

func (r Result) Changed() []Entry

Changed is every finding this change touched, worst first.

One list rather than four, because a reviewer asks "what did this change do" and gets the answer in priority order, where four sections make them read the same ranking four times and compare across the gaps. The lists stay separately addressable for the gate and the SARIF upload, which ask about the new findings alone.

func (Result) Counted added in v0.121.1

func (r Result) Counted() Result

Counted drops a second scanner's copy of a flaw the first one already accounts for, from every state a reader is shown a number for.

Two scanners over one target report one flaw twice, and the scan already answers for that: the copy carries correlation.countedUnder, and every count a person reads leaves it out. A diff that did not would put two numbers for one change in front of the same reader, nine in the terminal and fourteen on the pull request, with nothing on either screen saying which is the arithmetic.

The copies are dropped from the lists rather than from the report. renderSARIF works from the uncounted result, and sarif.ForCodeScanning leaves the copies out of what code scanning receives, where the counted finding's correlation.alsoFoundBy names the other scanners. The head scan's own SARIF keeps every scanner's finding.

The gate already asked the right question, so Tripped is carried through untouched.

func (Result) GateNew

func (r Result) GateNew(failOn sarif.Severity, failOnPriority string) []sarif.Result

GateNew returns the new findings that meet the differential gate: ranked severity at or above failOn (when set) OR priority at or above failOnPriority (when set). An empty threshold disables that dimension. With both empty, nothing is returned.

The ranked severity is the one the scan's own severity gate judges and its report prints, after the control's minimum severity and every signal, read from what results.sarif recorded. A gate on new findings that read the scanner's rating would pass a raised finding the scan itself fails.

func (Result) HelpURI added in v0.83.0

func (r Result) HelpURI(ruleID string) string

HelpURI is where a reader can look up a rule: what the scanner published, or a URL derived from a well-known identifier scheme. Empty when neither applies. A wrong link is worse than none.

func (Result) HoldToBase added in v0.189.0

func (r Result) HoldToBase(base, head engine.Declarations, baseRecorded bool, rank Ranker) Result

HoldToBase holds the comparison to the declaration its base recorded, as Held describes.

base and head are what each scan declared, baseRecorded whether the base report recorded it at all, and rank ranks a finding as its scan did under another classification.

func (Result) NarrowNew added in v0.82.0

func (r Result) NarrowNew(band string) Result

NarrowNew drops new findings below a priority band, leaving fixed and unchanged alone.

Only the new ones, because they are what the diff is reporting and what a reviewer is asked to act on; fixed and unchanged are context, and a count of them that moved with a threshold would mean something different on every run.

A finding the scanner never prioritized is kept. An empty Priority means prioritization did not run for it, not that it ranked low. Dropping it would hide a finding for the reason it was hardest to judge.

func (Result) OnlyRepository added in v0.85.0

func (r Result) OnlyRepository(ref string) Result

OnlyRepository keeps the new findings a given repository's checkout can actually anchor.

Paths are repository-relative, so a finding from another repository uploaded against this one resolves to a same-named file here, an annotation on a line that does not have that problem. That is wrong rather than merely noisy, and there is no case where it is wanted, so this is not offered as a preference.

A finding with no repository is kept. Not everything Draugr reports comes from a checkout: an image finding is located at an image reference and belongs to no repository, and dropping those would remove most of a container scan from the surface a reviewer reads.

func (Result) TrustChange added in v0.189.0

func (r Result) TrustChange(base, head engine.Declarations, baseRecorded bool, rank Ranker) Result

TrustChange is the comparison judged by the head's declaration, as --trust-descriptor-change asks for a change that edits its own descriptor on purpose. Held still says what the base's declaration would have decided, so the run states what the trust waived, and a base finding from a dropped control still reads as not scanned rather than fixed, because nobody looked for it.

type RevisionChange added in v0.189.2

type RevisionChange struct {
	From, To string
}

RevisionChange is a repository's revision as the base pinned it and as the head pins it, each empty where it pins none.

type ScanView added in v0.165.0

type ScanView struct {
	// Result is the comparison, classified and gated as `draugr diff` would classify and gate it.
	Result Result
}

ScanView draws a scan's comparison with its base inside that scan's console report, in the shape this package prints for `draugr diff`. A scan given a --base and a gate on new findings is judged on its change, and one layout for "what did this change introduce" is one a reader learns once.

func (ScanView) States added in v0.178.0

func (v ScanView) States() fixlist.ChangeStates

States sorts the comparison's findings by what happened to each, every one counted once.

func (ScanView) WriteListing added in v0.165.0

func (v ScanView) WriteListing(w io.Writer, view fixlist.View, top int) bool

WriteListing draws what changed in the view asked for, capped at top rows where top is positive: the CHANGED table, one line each under compact, or WHAT TO DO under actions, then the findings that moved and the ones whose band changed. It writes nothing where nothing changed, which the scan's verdict line already says, and reports whether it wrote.

func (ScanView) WriteStrips added in v0.165.0

func (v ScanView) WriteStrips(w io.Writer, label string, whole [4]int)

WriteStrips draws a band strip for each state a finding can be in against the base, then one more labeled strip with the counts in whole. No blank line follows, so the scan's report can put what qualifies the counts directly beneath them.

type SeverityCounts added in v0.63.0

type SeverityCounts struct {
	Critical int `json:"critical"`
	High     int `json:"high"`
	Medium   int `json:"medium"`
	Low      int `json:"low"`
}

SeverityCounts tallies findings by Draugr's normalized severity band.

Bands rather than SARIF levels, because a diff is read next to the scan report it came from and the two have to agree. error/warning/note is the wire vocabulary of the file; a reader deciding whether a pull request made things worse is thinking in critical/high/medium/low.

type Unread added in v0.189.4

type Unread string

Unread is why a file the base report could not have recorded was not read at the base's commit.

const (
	// CommitMissing is a base commit the checkout the comparison runs in does not hold, as in a
	// shallow clone. Fetching it settles the file.
	CommitMissing Unread = "commitMissing"
	// CommitUnknown is a base report that names no commit for the repository, or read it with
	// uncommitted changes that no commit holds, or a directory git cannot read. Only a scan of the
	// base by a release that records the file settles it.
	CommitUnknown Unread = "commitUnknown"
)

type View added in v0.121.0

type View string

View is how much of each row a listing draws, the same three names `draugr scan` takes and with the same meaning, so a reader who has learned one has learned the other.

const (
	ViewFindings View = "findings"
	ViewActions  View = "actions"
	ViewCompact  View = "compact"
)

The three views.

Jump to

Keyboard shortcuts

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