secretscan

package
v0.186.0 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2026 License: BSD-3-Clause Imports: 18 Imported by: 0 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 secretscan finds the places where a daemon takes a secret as a plaintext value (a config field, a flag, an environment variable, a unit file) instead of by reference, and reports them with the denominator that makes the answer worth reading.

PRECISION OVER RECALL

A name heuristic that cries wolf on `max_tokens` gets switched off, and a gate that is switched off protects nothing. So a name is judged by its WORDS (never by a substring), the last word must be the secret word, and a trailing `file`, `path`, `from` or `env` changes the CLASS instead of hiding the finding: `token_file` is a path to a secret, `password_from` is a secretsource reference, and neither is the secret itself. The committed corpus testdata/names.tsv holds the true positives and the false positives the heuristic was written against; adding a name there is how it is amended.

Index

Constants

View Source
const MaxFile = 4 << 20

MaxFile bounds what is read of any one file. A bigger one is reported as unread, never skipped quietly: a scan that could not read says so.

View Source
const SecretsourcePath = "github.com/go-authn/secretsource"

SecretsourcePath is the import path of the fleet's reference resolver.

Variables

This section is empty.

Functions

func ExecGit

func ExecGit(dir string, args ...string) (string, error)

ExecGit is the Git that runs the git binary.

func InlineSecret

func InlineSecret(line string) (name, value string, ok bool)

InlineSecret reads one line of a sample (toml, yaml, env file, a shell command, a JSON object) and returns the name and the value when it sets a plaintext secret to something. Comments and prose are not read.

func Tokens

func Tokens(name string) []string

Tokens splits a name into lower-case words at separators and at case changes: `APIKey` is api, key; `client_secret_file` is client, secret, file.

func URLName added in v0.176.0

func URLName(scheme string) string

URLName is the name a URL-userinfo finding carries: the shape, never the password itself.

func URLSecret added in v0.176.0

func URLSecret(s string) (scheme, password string, ok bool)

URLSecret finds a password written into a URL's userinfo and returns the scheme and the password. A password that is an interpolation (`$PW`, `%s`, `{{.Pw}}`, `<pw>`) or a reference is not a value written out and is skipped.

Types

type Class

type Class string

Class says how a secret input receives its secret.

const (
	// None: the name does not denote a secret.
	None Class = ""
	// Plaintext: the value IS the secret. The class a gate refuses.
	Plaintext Class = "plaintext"
	// Path: a `*_file` style path to a file holding the secret.
	Path Class = "path"
	// Ref: a `*_from` secretsource reference (`systemd-creds:name`, ...).
	Ref Class = "ref"
	// Env: the NAME of an environment variable that holds the secret.
	Env Class = "env"
)

func Classify

func Classify(name string) Class

Classify says whether name denotes a secret input and how it is received.

type Clone

type Clone struct {
	Dir        string    `json:"dir"`
	Org        string    `json:"org,omitempty"`
	Origin     string    `json:"origin,omitempty"` // canonical origin URL; see Key
	Read       bool      `json:"read"`
	SkipReason string    `json:"skip_reason,omitempty"`
	Repo       Repo      `json:"repo"`
	Staleness  Staleness `json:"staleness"`
}

Clone is one directory under the root.

func (Clone) Key

func (c Clone) Key() string

Key is what makes two clones the same repository: the origin URL, or, with none, the clone's own name, so that clones without an origin are never merged into one.

type Denominator

type Denominator struct {
	Seen             int `json:"clones_seen"`
	Read             int `json:"clones_read"`
	Skipped          int `json:"clones_skipped"`
	FilesRead        int `json:"files_read"`
	FilesUnread      int `json:"files_unread"`
	Ignored          int `json:"entries_ignored"` // loose files and dot-directories: not clone candidates
	GitUnreadable    int `json:"git_unreadable"`
	NeverFetched     int `json:"never_fetched"`
	Stale            int `json:"stale"`
	DaemonRepos      int `json:"daemon_repos"`
	DaemonsPlaintext int `json:"daemon_repos_with_plaintext"`
	UsesSecretsource int `json:"uses_secretsource"`
}

Denominator is what the sweep covered. Every count is of something the sweep did, so that "none found" can be told from "none looked at".

type Finding

type Finding struct {
	Kind        string `json:"kind"`   // "input" (what the daemon takes) or "sample" (what a doc shows)
	Source      string `json:"source"` // config-field, flag, env, unit-env, unit-flag, sample, doc
	Class       Class  `json:"class"`
	Name        string `json:"name"`
	File        string `json:"file"`
	Line        int    `json:"line"`
	PermChecked *bool  `json:"perm_checked,omitempty"` // path class only; a heuristic, see ScanRepo
	Placeholder bool   `json:"placeholder,omitempty"`  // sample class only: the value looks like a stand-in
	Exposure    int    `json:"exposure"`               // 1 doc … 4 value stored in a shipped file
}

Finding is one secret input, or one sample that shows a secret value.

func ScanText

func ScanText(rel string, content []byte) []Finding

ScanText reads a file that is not Go: a systemd unit and a launchd plist are INPUTS (what the daemon is started with), everything else is a SAMPLE (what a reader is shown). Markdown is read inside code fences only.

type Git

type Git func(dir string, args ...string) (string, error)

Git runs git in dir and returns its trimmed standard output. It is a parameter so that a test can answer for a clone that is not there; the real one never fetches, because staleness is what the LOCAL refs say.

type GoScan

type GoScan struct {
	Findings     []Finding
	Imports      []string
	HasMain      bool // declares func main
	Serves       bool // listens, serves, or waits for a signal: a daemon marker
	PermEvidence bool // checks a file's permissions, or goes through secretsource
}

GoScan is what one Go source file says.

func ScanGo

func ScanGo(rel string, src []byte) (GoScan, error)

ScanGo reads one Go file. rel is the name findings carry.

type Hit

type Hit struct {
	Clone string
	Org   string
	Finding
}

Hit is a finding with the clone it was found in.

type OrgCount

type OrgCount struct {
	Org    string `json:"org"`
	Inputs int    `json:"inputs"`
}

OrgCount is an organisation and how many plaintext inputs it holds.

type Repo

type Repo struct {
	Daemon           bool      `json:"daemon"`
	Daemons          []string  `json:"daemons,omitempty"` // main packages that serve, and unit/plist files
	GoFiles          int       `json:"go_files"`
	FilesRead        int       `json:"files_read"`
	Unread           []Unread  `json:"unread,omitempty"`
	UsesSecretsource bool      `json:"uses_secretsource"`
	Findings         []Finding `json:"findings,omitempty"` // empty unless Daemon
}

Repo is what one clone says.

func ScanRepo

func ScanRepo(root string, all bool) (Repo, error)

ScanRepo reads one clone. A clone is a daemon when it ships a systemd unit or a launchd plist, or has a main package that serves or waits for a signal in its own directory; a main that only calls into a library server is found through its unit file or not at all, which is a stated limit. Findings are kept for a daemon only, unless all is set: a client's `--token` flag is not a daemon's secret.

perm_checked on a path finding is a HEURISTIC: the package (directory) contains a `.Perm()` or `Mode()&` test, a 0o077-style mask, or imports secretsource. It says "something checks permissions here", not that this path is checked.

func (Repo) Plaintext

func (r Repo) Plaintext() []Finding

Plaintext returns the findings a gate refuses: a plaintext secret INPUT.

type Report

type Report struct {
	Generated   time.Time   `json:"generated"`
	Root        string      `json:"root"`
	Denominator Denominator `json:"denominator"`
	Clones      []Clone     `json:"clones"`
}

Report is the whole sweep.

func Sweep

func Sweep(root string, git Git, now time.Time, staleAfter time.Duration) (Report, error)

Sweep reads every clone under root.

func (Report) Complete

func (r Report) Complete() bool

Complete says whether everything seen was read: no clone skipped, no file unread, no git answer missing. A sweep that is not complete is NOT READ where it is incomplete, and must not be read as clean there.

func (Report) Orgs

func (r Report) Orgs() []OrgCount

Orgs returns the organisations that hold a plaintext secret input, with the number of such inputs, most first. An unknown org is named "(unknown)".

func (Report) Top

func (r Report) Top(n int) []Hit

Top returns the plaintext secret inputs of daemon clones, most exposed first: a value stored in a shipped unit, then a flag (visible to `ps`), then an environment variable or a config field. n <= 0 means all of them.

func (Report) WriteJSON

func (r Report) WriteJSON(w io.Writer) error

WriteJSON writes the report as indented JSON.

func (Report) WriteMarkdown

func (r Report) WriteMarkdown(w io.Writer) error

WriteMarkdown writes the report as Markdown tables.

type Staleness

type Staleness struct {
	NeverFetched bool       `json:"never_fetched"`
	FetchedAt    *time.Time `json:"fetched_at,omitempty"` // .git/FETCH_HEAD mtime
	NoUpstream   bool       `json:"no_upstream"`          // detached, or a branch with no tracking ref
	Ahead        int        `json:"ahead"`
	Behind       int        `json:"behind"`
	Stale        bool       `json:"stale"` // never fetched, older than the limit, or behind
	GitError     string     `json:"git_error,omitempty"`
}

Staleness says how old a clone's view of its remote is.

type Unread

type Unread struct {
	File   string `json:"file"`
	Reason string `json:"reason"`
}

Unread is a file the scan could not read, and why.

Jump to

Keyboard shortcuts

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