security

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2026 License: MIT Imports: 9 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 security holds the guardrails that keep mnemos local-first and secret-free: the SecretScanner that screens captured content before it is written, and the path/exclude rules that bound what the agent can touch.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConfineDir added in v0.3.0

func ConfineDir(root, p string) (abs string, err error)

ConfineDir validates that directory path p resolves within root (or is root itself) and does not escape via traversal or a symlink. It guards a scan/ingest root rather than a single write target, so — unlike ResolveWithin — it permits the root itself and does not reject the internal .mnemos directory. It returns the cleaned absolute path on success.

func ResolveWithin

func ResolveWithin(root, p string, exclude []string) (abs string, uri string, err error)

ResolveWithin validates a caller-supplied path against a write root and returns the cleaned absolute path and the slash-normalized, root-relative URI used as documents.uri. p may be relative to root or absolute; either way the resolved location must stay strictly under root.

It is the single guard for every write/delete the agent can trigger (mnemos.remember custom path, forget, move). Root is the kb (the URI namespace); the index db and models live outside it by construction. It rejects, in order:

  • traversal that escapes root (a "../" prefix after cleaning),
  • a path that resolves to root itself (not a file),
  • a path under a reserved ".mnemos/" directory (defense in depth against a nested anchor; the db and models already sit outside the kb),
  • any URI matching an exclude glob (the security.exclude set),
  • symlink escape: the deepest existing ancestor is resolved with EvalSymlinks and re-checked for containment, so a symlink pointing outside root cannot be used as a write target.

func ValidateAccepts added in v0.4.0

func ValidateAccepts(accepts []Accept) error

ValidateAccepts checks every entry and returns the first problem, prefixed "security: accept #<1-based index>: ". It rejects an unknown rule, an empty value or reason, no paths, an invalid glob, a duplicate entry, and any entry that could never match a finding of its rule: for generic-credential-assignment an identifier outside the keyword set or a value shorter than 16 characters or holding a quote; for other rules an identifier, or a value the rule does not match in full. It does not modify accepts.

Types

type Accept added in v0.4.0

type Accept struct {
	Rule       string   `koanf:"rule"`
	Identifier string   `koanf:"identifier"`
	Value      string   `koanf:"value"`
	Paths      []string `koanf:"paths"`
	Reason     string   `koanf:"reason"`
}

Accept declares one scanner finding that is known not to be a secret. Entries are for non-secret placeholders only (all-zero tokens, documented example keys), never for real credentials. One entry suppresses one finding shape, at the paths it lists, never a whole file: any other finding in the same file is still reported.

Rule is the rule name the finding must carry. For generic-credential-assignment, Identifier is the assigned keyword (case-insensitive, e.g. "token") and Value the quoted value; for any other rule, Identifier is empty and Value is the whole matched substring. Paths are doublestar globs over the slash-separated, tree-root-relative uri. Reason records why the value is safe.

type AcceptReporter added in v0.4.0

type AcceptReporter interface {
	// ScanReport returns what Scan returns, plus one AcceptedFinding per finding
	// an Accept entry dropped.
	ScanReport(path, content string) (kept []Finding, accepted []AcceptedFinding, err error)
}

AcceptReporter is a SecretScanner that also reports the findings it dropped. Callers that audit accepted findings (ingest debug logs) check for it with a type assertion; Scan alone stays the screening contract.

type AcceptedFinding added in v0.4.0

type AcceptedFinding struct {
	Rule   string
	Reason string
}

AcceptedFinding names a finding an Accept entry dropped: the rule and the entry's reason, never the matched value, so it is safe to log.

type AcceptingScanner added in v0.4.0

type AcceptingScanner struct {
	// contains filtered or unexported fields
}

AcceptingScanner wraps a base SecretScanner and drops the findings that an Accept entry declares safe. Construct with NewScanner, after the entries have passed ValidateAccepts.

func (AcceptingScanner) Scan added in v0.4.0

func (s AcceptingScanner) Scan(path, content string) ([]Finding, error)

Scan runs the base scanner and drops each finding matched by an Accept entry on rule, value and path. A pathless scan (path == "") is never exempt. The result is nil when no finding survives.

func (AcceptingScanner) ScanReport added in v0.4.0

func (s AcceptingScanner) ScanReport(path, content string) ([]Finding, []AcceptedFinding, error)

ScanReport is Scan that also returns one AcceptedFinding per dropped finding, in scan order, carrying the first matching entry's reason. Both slices are nil when empty.

type Finding

type Finding struct {
	// Rule is the identifier of the pattern that matched.
	Rule string
	// contains filtered or unexported fields
}

Finding is one secret match in scanned content. Rule names the pattern that fired (e.g. "aws-access-key-id"). The matched substring is held on an unexported field so it cannot leak through serialization or an external caller: the remember tool reports only Rule names and never echoes the value back to the agent. In-package code (and tests) can still read it.

type RegexScanner

type RegexScanner struct{}

RegexScanner detects secrets with a fixed set of compiled patterns. It is the default SecretScanner: cgo-free, dependency-free, and tuned for the bare strings captured via mnemos.remember (where gitleaks' file-context heuristics do not fire). Construct with NewRegexScanner.

func NewRegexScanner

func NewRegexScanner() RegexScanner

NewRegexScanner returns the default regex-based secret scanner.

func (RegexScanner) Scan

func (RegexScanner) Scan(_, content string) ([]Finding, error)

Scan reports every high-signal credential pattern found in content; path is ignored. Two distinct secrets on one line yield two findings; the same substring matched by multiple patterns is reported once, under the first (most specific) rule. The result is nil for clean content.

type SecretScanner

type SecretScanner interface {
	// Scan returns one Finding per detected secret. path is the
	// slash-separated, tree-root-relative uri of content, or "" when content has
	// no path (free-text capture); implementations may ignore it. A clean input
	// yields a nil slice and a nil error. err is non-nil only on an operational
	// failure, not on the presence of a secret.
	Scan(path, content string) ([]Finding, error)
}

SecretScanner screens free-form content for credentials before it is persisted. It is the single swappable seam for secret detection: the regex implementation below can be replaced by a gitleaks-backed one without touching callers.

func NewScanner added in v0.4.0

func NewScanner(accepts []Accept) SecretScanner

NewScanner returns the configured SecretScanner: the plain RegexScanner when accepts is empty (no filtering overhead), otherwise an AcceptingScanner over it. accepts must have passed ValidateAccepts; it is not re-validated here.

Jump to

Keyboard shortcuts

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