codesnippet

package
v2.0.5 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2026 License: Apache-2.0 Imports: 12 Imported by: ? modules, 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 codesnippet fills the code of a report's locations from the scanned source, for a producer that has the source in front of it (a CI job, a sensor). A receiver never has the source and never calls it.

A code scanner does not always give the code it flags: Semgrep without a login writes the placeholder "requires login" where the matched lines belong (the importer drops it), trivy names the lines of a misconfiguration and not their text, a taint trace gives one line per step. Fill reads, for every location a finding names (its location, its related locations, its stack frames and every step of its data flow), the flagged lines and a few lines around them, so a reader sees the code in its place.

What is read is bounded and cleaned:

  • only files inside the scanned tree: a path is relative, has no "..", and the file system confines symbolic links (Dir, or an os.Root's FS);
  • only regular files up to Options.MaxFileBytes;
  • at most Options.MaxSnippetLines flagged lines and Options.MaxContextLines lines of context, each within a byte cap;
  • a window with a binary line (NUL, invalid UTF-8) or a line longer than Options.MaxLineBytes (minified code) is not read at all;
  • credentials in the code are masked (ctis.RedactCodeSecrets), and so is every raw secret the caller knows (Options.Known) everywhere in the finding;
  • a secret finding is never read: its snippet is the scanner's masked match, and the file holds the raw secret.

A receiver keys a SAST finding on the hash of its code. When Fill gives a finding its snippet, it also sets the partial fingerprint snippetHash/v1 to the hash of the code as the file holds it (unless the producer set one): masking a credential in the shown code then changes no key.

Code the tool gave is kept as it is. The context around it is read only when the file holds that code at those lines; otherwise the location keeps the tool's code without context (Stats.Mismatched).

Nothing is dropped: a location whose code cannot be read keeps what it had, without code.

Index

Constants

View Source
const (
	DefaultContextLines    = 5
	DefaultMaxSnippetLines = 20
	DefaultMaxSnippetBytes = 4 << 10
	DefaultMaxContextLines = 40
	DefaultMaxContextBytes = 6 << 10
	DefaultMaxLineBytes    = 1 << 10
	DefaultMaxFileBytes    = 16 << 20
	DefaultMaxLocations    = 10000
)

Defaults of Options.

View Source
const Placeholder = "requires login"

Placeholder is what Semgrep without a login writes in place of the matched lines. It is no code: Fill replaces it, or clears it when the file cannot be read.

Variables

This section is empty.

Functions

func Dir

func Dir(root string) (fs.FS, error)

Dir is the scanned tree at root as a file system Fill reads: a name that resolves (through symbolic links) outside root is refused. Go 1.24 and later callers may pass an os.Root's FS instead, which confines every path component atomically.

Types

type Options

type Options struct {
	// ContextLines is how many lines before and after the flagged lines the
	// context holds (DefaultContextLines).
	ContextLines int
	// MaxSnippetLines and MaxSnippetBytes bound the flagged lines kept as
	// the snippet; a longer match keeps its first lines.
	MaxSnippetLines int
	MaxSnippetBytes int
	// MaxContextLines and MaxContextBytes bound the context, flagged lines
	// included; the lines farthest from the flagged ones go first.
	MaxContextLines int
	MaxContextBytes int
	// MaxLineBytes is the longest line read; a window with a longer line
	// gets no code.
	MaxLineBytes int
	// MaxFileBytes is the largest file read.
	MaxFileBytes int64
	// MaxLocations bounds the locations read in one report.
	MaxLocations int
	// Known are raw secret values found in the same scan (a secret
	// scanner's matches): masked everywhere in a finding Fill touched.
	Known []string
}

Options bound what Fill reads. A zero value takes the default.

type Stats

type Stats struct {
	// Locations is how many locations Fill tried to read.
	Locations int
	// Snippets is how many got their flagged lines.
	Snippets int
	// Contexts is how many got surrounding code.
	Contexts int
	// Unreadable is how many could not be read (missing file, outside the
	// tree, binary, too large, past the end).
	Unreadable int
	// Mismatched is how many had code from the tool that the file does not
	// hold at those lines: the tool's code is kept, without context. A
	// caller logs the count.
	Mismatched int
}

Stats counts what Fill did.

func Fill

func Fill(r *ctis.Report, fsys fs.FS, o Options) Stats

Fill reads the code of every location of r's findings from fsys, the scanned tree, whose root is the directory the report's relative paths start at. A location that already has its snippet keeps it; one without context gets it. Paths must be relative to fsys (see the package documentation); an absolute path is not read.

Jump to

Keyboard shortcuts

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