config

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2026 License: Apache-2.0 Imports: 25 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 config resolves TokenHound configuration once per run. Layers, lowest first: defaults, the project tokenhound.yaml at the scan root (or --config), the user file <user config dir>/tokenhound/config.yaml, TOKENHOUND_* environment variables and flags. Each set leaf replaces the one below it; lists replace.

Trust tiers (AD-24): the project file may come from a hostile repository, so network.*, alerts.*, hook.* and ping are ignored from it; it may not disable verification or set timeout, its limits (up to 4x their default) and verify.timeout may only rise above the trusted value, its grade.f_threshold_usd_per_day and verify.max_concurrency may only fall below it, its durations are bounded and its fail_on may only tighten the policy (all applied after every other layer); ${NAME} references in it stay literal and its paths must stay inside the project root. new_only: true is ignored from it too (it loosens fail_on). Every ignored value gives a warning that names the key and never the value.

Ignore files (docs/schema/ignore.v1.md): the project ignore file (ignore_file, or <root>/.tokenhound-ignore) and the user machine ignore file <user config dir>/tokenhound/ignore are parsed into Resolved.Ignore. A configured file that is missing, and a malformed rule, are issues "<file>:<line>: <message>" that never repeat the rule.

Errors and warnings never carry config values, environment values or foreign error text (AD-3). The package never imports the public tokenhound package: the public config package maps Resolved onto tokenhound.Options.

Index

Constants

View Source
const (
	// ProjectFile is the project-tier config file at the scan root.
	ProjectFile = "tokenhound.yaml"
	// UserFile is the user-tier config file under the user config directory.
	UserFile = "tokenhound/config.yaml"
	// EnvPrefix starts every environment variable the config reads.
	EnvPrefix = "TOKENHOUND_"
	// SupportedVersion is the only config version this binary reads.
	SupportedVersion = 1
)

File names and limits.

View Source
const (
	DefaultVerifyConcurrency = 8
	DefaultVerifyTimeout     = 10 * time.Second
	DefaultFThresholdUSD     = 500
	DefaultMaxFileBytes      = 10 << 20
	DefaultArchiveMaxBytes   = 256 << 20
	DefaultArchiveMaxDepth   = 5
	DefaultArchiveMaxEntries = 10000
	DefaultDecodeMaxDepth    = 3
	DefaultHookMode          = "block"
	DefaultHookOnError       = "warn-allow"
)

Defaults.

View Source
const (
	// DetailProjectTier: the key is honoured only from the user tier.
	DetailProjectTier = "project_tier"
	// DetailFailOnLoosening: a project fail_on value would loosen the policy.
	DetailFailOnLoosening = "fail_on_loosening"
	// DetailGateLoosening: a project value would weaken the scan behind the gate
	// (verification off, a timeout, new_only on, a lower limit or verify timeout, a
	// higher grade threshold or verify.max_concurrency).
	DetailGateLoosening = "gate_loosening"
	// DetailEnvReference: a ${NAME} reference in the project tier is kept as a
	// literal (the value is used, not ignored).
	DetailEnvReference = "env_reference"
	// DetailAbsolutePath: a project path is absolute.
	DetailAbsolutePath = "absolute_path"
	// DetailParentTraversal: a project path climbs out of the root.
	DetailParentTraversal = "parent_traversal"
	// DetailProjectLimitCap: a project limit is above what the project tier may
	// raise it to (4x the default, at most the schema maximum).
	DetailProjectLimitCap = "project_limit_cap"
)

Detail codes of config warnings.

View Source
const (
	FlagNoVerify         = "--no-verify"
	FlagFailOnStatus     = "--fail-on-status"
	FlagFailOnWarnings   = "--fail-on-warnings"
	FlagFailOnMinDollars = "--fail-on-min-dollars"
	FlagTimeout          = "--timeout"
	FlagNewOnly          = "--new-only"
)

Flag names, for issues about flag values.

View Source
const (
	// IgnoreFile is the default project ignore file at the scan root.
	IgnoreFile = ".tokenhound-ignore"
	// UserIgnoreFile is the user-tier machine ignore file under the user config
	// directory.
	UserIgnoreFile = "tokenhound/ignore"
)

Ignore file names (docs/schema/ignore.v1.md).

View Source
const BaselineFile = ".tokenhound-baseline.json"

BaselineFile is the default baseline file at the scan root (docs/schema/baseline.v1.json).

Variables

This section is empty.

Functions

func DisplayPath added in v0.2.0

func DisplayPath(cwd, file string) string

DisplayPath is how a file is named to the user: relative to cwd with forward slashes when it is inside cwd, else its absolute slash path.

func EnvNames

func EnvNames() []string

EnvNames returns every environment variable the config reads, in table order.

func ParseBaseline added in v0.2.0

func ParseBaseline(data []byte, display string) (*model.Baseline, []yamlschema.Issue)

ParseBaseline parses and validates one baseline file. display names the file in issues, which read "<display>: <field>: <message>" (no line) and never contain a value from the file.

func ParseIgnore added in v0.2.0

func ParseIgnore(data []byte, display string, scope model.IgnoreScope, prefix, source string) ([]model.IgnoreRule, []yamlschema.Issue)

ParseIgnore parses one ignore file. display names the file in issues; scope is project or machine; prefix ("" or a slash path ending in "/") is prepended to the path and loc patterns of a project file that sits in a subdirectory of the root; source is recorded in each rule's Source. Issues read "<display>:<line>: <message>" and never contain the rule value.

Types

type Alerts

type Alerts struct {
	SlackWebhookEnv string
	TeamsWebhookEnv string
	WebhookURLEnv   string
	OnStatuses      []model.Status
	Timeout         time.Duration
}

Alerts is the alerts block (user tier only).

type Error

type Error struct {
	Issues []yamlschema.Issue
}

Error is a config that cannot be used. Each issue prints as "file:line: field: message" (or "NAME: field: message" for an environment variable or flag).

func BaselinePath added in v0.2.0

func BaselinePath(res Resolved, root, cwd string) (string, *Error)

BaselinePath returns the baseline file of a resolved configuration: the baseline key from any tier (a project-tier value already kept lexically inside the root), or <root>/.tokenhound-baseline.json. A project-tier or default file that resolves through a symlink to a place outside root (the file itself, or the directory it would be written to) is an issue: a repository must not make TokenHound read or overwrite a file outside it. cwd names the file in the issue.

func (*Error) Error

func (e *Error) Error() string

Error returns one line per issue.

type FailOn

type FailOn struct {
	Statuses         []model.Status
	MinDollarsAtRisk float64
	Warnings         []string
}

FailOn is the fail_on block.

type Flags

type Flags struct {
	// Verify is false for --no-verify.
	Verify           *bool
	FailOnStatus     *string
	FailOnWarnings   *string
	FailOnMinDollars *string
	Timeout          *string
	// NewOnly is --new-only.
	NewOnly *bool
}

Flags are the command line values that override config. A nil field is not set. String fields hold the raw flag text: lists are comma separated.

type Grade

type Grade struct {
	FThresholdUSDPerDay float64
}

Grade is the grade block.

type Hook

type Hook struct {
	Mode    string
	OnError string
	// SessionScan is hook.session_scan: the Claude Code session-end scan runs.
	SessionScan bool
}

Hook is the hook block (user tier only).

type Input

type Input struct {
	// Root is the project root: the scan root directory ("" means Cwd).
	Root string
	// ConfigFile replaces <Root>/tokenhound.yaml when set (--config); it is still
	// project tier. A relative path is relative to Cwd and is displayed as given;
	// relative paths inside it resolve against Root.
	ConfigFile string
	// Flags override everything else.
	Flags Flags
	// LookupEnv reads the environment (nil means os.LookupEnv).
	LookupEnv func(string) (string, bool)
	// UserConfigDir returns the user config directory (nil means os.UserConfigDir).
	// An error skips the user tier.
	UserConfigDir func() (string, error)
	// ReadFile reads a config file or the CA bundle (nil means a bounded os read).
	ReadFile func(string) ([]byte, error)
	// Cwd resolves relative environment and flag paths ("" means os.Getwd).
	Cwd string
	// UserOnly reads no project tier at all: no tokenhound.yaml (ConfigFile is
	// ignored), no project ignore file and no project baseline file. The user file,
	// the user machine ignore file, the environment and flags still apply. The
	// Claude Code session-end hook sets it, so a project cannot change the scan.
	UserOnly bool
	// SkipBaseline leaves the baseline file unread (Resolved.Baseline nil, no
	// baseline_missing warning): for the baseline verb, which must never read the
	// baseline it replaces (AD-25), and for verbs that do not compare findings.
	SkipBaseline bool
	// IgnoreFile replaces the project ignore file (ignore_file from every tier, or
	// <root>/.tokenhound-ignore) when set (--ignore-file). Like ConfigFile its content
	// is project tier (its rules have project scope and patterns relative to Root),
	// but the caller names the path, so it is not held inside Root. A relative path
	// is relative to Cwd. A missing file is an issue.
	IgnoreFile string
	// BaselineFile replaces the baseline file (baseline from every tier, or
	// <root>/.tokenhound-baseline.json) when set (--baseline). A relative path is
	// relative to Cwd and the path is not held inside Root. A missing file is no
	// baseline (with new_only, a baseline_missing warning).
	BaselineFile string
}

Input is what Resolve reads. Every lookup is injectable so tests are hermetic.

type Limits

type Limits struct {
	MaxFileBytes      int64
	ArchiveMaxBytes   int64
	ArchiveMaxDepth   int
	ArchiveMaxEntries int
	DecodeMaxDepth    int
}

Limits is the limits block.

type Network

type Network struct {
	HTTPSProxy string
	NoProxy    string
	CABundle   []byte
	AllowHosts []string
}

Network is the network block (user tier only). CABundle holds the file's bytes.

type Path

type Path struct {
	Path string
	Tier Tier
}

Path is a resolved config path and the tier that set it.

type Resolved

type Resolved struct {
	Verify  Verify
	FailOn  FailOn
	Timeout time.Duration
	Grade   Grade
	Limits  Limits
	Network Network
	Hook    Hook
	Alerts  Alerts
	Ping    bool
	// Paths holds the resolved absolute path of each set path key (ignore_file,
	// baseline, import.gitleaks, import.trufflehog, custom_detectors) and the tier
	// it came from, so later stories can apply their own tier rules (Story 1.9 drops
	// verify blocks from project-tier custom detectors).
	Paths map[string]Path
	// Ignore holds the parsed rules of the project ignore file (ignore_file, or
	// <root>/.tokenhound-ignore) followed by those of the user machine ignore file
	// <user config dir>/tokenhound/ignore.
	Ignore []model.IgnoreRule
	// NewOnly is new_only (--new-only): baselined findings do not count for fail_on.
	NewOnly bool
	// Baseline is the parsed baseline file (baseline, or
	// <root>/.tokenhound-baseline.json); nil when the file is absent or SkipBaseline
	// is set.
	Baseline *model.Baseline
}

Resolved is the effective configuration.

func Defaults

func Defaults(ci bool) Resolved

Defaults returns the defaults, with fail_on.warnings [timeout, file_too_large] when ci is set: a scan that was cut short, or that skipped a file because it is over limits.max_file_bytes (a key in it would go unseen), must not pass a CI gate. The project tier can only add to fail_on, never drop these (see docs/adr/0002).

func Resolve

func Resolve(in Input) (Resolved, []model.Warning, error)

Resolve resolves in into the effective configuration and its warnings, or returns an *Error listing every problem.

type Tier

type Tier string

Tier says where a value came from.

const (
	TierDefault Tier = "default"
	TierProject Tier = "project"
	TierUser    Tier = "user"
	TierEnv     Tier = "env"
	TierFlag    Tier = "flag"
)

Tiers, lowest precedence first.

type Verify

type Verify struct {
	Enabled        bool
	MaxConcurrency int
	Timeout        time.Duration
}

Verify is the verify block.

Jump to

Keyboard shortcuts

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