Documentation
¶
Overview ¶
Package embargo detects sensitive data in text; the optional mask subpackage applies masking to its findings.
Choosing an API ¶
Use NewScanner to select detectors, Scanner.Scan for bytes, and Scanner.ScanString for strings. For readers, Scanner.ScanReader handles content that fits in memory and Scanner.ScanLines handles line-oriented logs. Each method has a runnable example; its documentation defines offsets, limits, and error behavior.
Confidence ¶
Confidence is a detector-specific heuristic, not a probability or proof that an identifier is assigned or a credential authenticates. A WithMinConfidence threshold of 0.8 selects the high category; 0.4 includes medium confidence. detector.Finding.Level supplies the category, and each detector type documents its scoring rules.
Context-based detectors use nearby keywords rather than checksums. In particular, CVV and card expiry can still pass a high-confidence filter; choose detector options to control what is considered sensitive. WithAll is useful for exploration, while explicit options suit stricter auditing.
How It Works ¶
- Return immediately for empty input.
- Check cached hints with bytes.Contains, folding ASCII letters for case-insensitive matching; detectors without hints always run.
- Run matching detectors' parsers and domain validation, such as BIN, Luhn, and MOD 97 checks.
- Merge findings using the overlap, ordering, and filtering rules in Scanner.Scan.
Hint filtering avoids parser work when a required substring is absent; its benefit depends on the input and enabled detectors. See the benchmarks for recorded measurements and reproduction commands.
Custom Detectors ¶
Wrap a compiled pattern with detector.NewRegex and pass it to WithDetector, as its example shows. A specific hint such as TICKET- lets the scanner skip the regex when that substring is absent. For parsing or validation beyond a regex, implement Detector; its methods define names, exhaustive hints, and finding offsets. Use dedicated parsers when scan cost matters.
Example ¶
package main
import (
"fmt"
"github.com/nao1215/embargo"
)
func main() {
scanner := embargo.NewScanner(embargo.WithPAN(), embargo.WithEmail())
text := "user tanaka@example.com paid with 4532015112830366"
for _, f := range scanner.ScanString(text) {
fmt.Printf("%s: %s (%.2f)\n", f.DetectorName, f.RawValue, f.Confidence)
}
}
Output: pan: 4532015112830366 (1.00) email: tanaka@example.com (1.00)
Index ¶
- type Detector
- type DetectorName
- type Finding
- type Option
- func WithABARouting() Option
- func WithACHTrace() Option
- func WithAWSKey() Option
- func WithAll() Option
- func WithBTC() Option
- func WithBankAccount() Option
- func WithCVV() Option
- func WithCardExpiry() Option
- func WithDetector(d Detector) Option
- func WithETH() Option
- func WithEmail() Option
- func WithGitHubToken() Option
- func WithGoogleAPIKey() Option
- func WithIBAN() Option
- func WithIPAddr() Option
- func WithJPPhone() Option
- func WithJWT() Option
- func WithMerchantID() Option
- func WithMinConfidence(threshold float64) Option
- func WithMyNumber() Option
- func WithPAN() Option
- func WithPaymentToken() Option
- func WithPrivateKeyPEM() Option
- func WithSWIFTBIC() Option
- func WithSlackToken() Option
- func WithSortByPosition() Option
- func WithUKSortCode() Option
- func WithoutDedup() Option
- type Scanner
- type SensitiveKind
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Detector ¶
Detector aliases detector.Detector, the contract for custom detectors.
type DetectorName ¶
type DetectorName = detector.DetectorName
DetectorName aliases detector.DetectorName, a detector identifier.
type Finding ¶
Finding aliases detector.Finding, a match with byte offsets and detector-specific detail.
type Option ¶
type Option func(*Scanner)
Option configures a Scanner through NewScanner.
func WithABARouting ¶
func WithABARouting() Option
WithABARouting enables detector.ABARouting detection.
func WithAll ¶
func WithAll() Option
WithAll enables every built-in detector, including context-based detectors that can produce false positives. Select individual With options when those matches are unwanted.
func WithBankAccount ¶
func WithBankAccount() Option
WithBankAccount enables detector.BankAccount detection.
func WithCardExpiry ¶
func WithCardExpiry() Option
WithCardExpiry enables detector.CardExpiry detection.
func WithDetector ¶
WithDetector registers d; a nil interface is ignored. See Detector for the hint and finding contracts.
Example ¶
package main
import (
"fmt"
"regexp"
"github.com/nao1215/embargo"
"github.com/nao1215/embargo/detector"
)
func main() {
ticket := detector.NewRegex(
"ticket_id", regexp.MustCompile(`TICKET-\d{4}`),
[][]byte{[]byte("TICKET-")}, 0.9,
)
scanner := embargo.NewScanner(embargo.WithDetector(ticket))
for _, f := range scanner.ScanString("see TICKET-4321 for details") {
fmt.Printf("%s: %s (%.1f)\n", f.DetectorName, f.RawValue, f.Confidence)
}
}
Output: ticket_id: TICKET-4321 (0.9)
func WithGitHubToken ¶
func WithGitHubToken() Option
WithGitHubToken enables detector.GitHubToken detection.
func WithGoogleAPIKey ¶
func WithGoogleAPIKey() Option
WithGoogleAPIKey enables detector.GoogleAPIKey detection.
func WithMerchantID ¶
func WithMerchantID() Option
WithMerchantID enables detector.MerchantID detection.
func WithMinConfidence ¶
WithMinConfidence keeps findings with confidence >= threshold after deduplication and sorting. Values below 0 become 0, values above 1 become 1, and 0 (the default) disables filtering.
Example ¶
package main
import (
"fmt"
"github.com/nao1215/embargo"
)
func main() {
text := "user tanaka@example.com bank account 12345678"
for _, threshold := range []float64{0.4, 0.8} {
scanner := embargo.NewScanner(
embargo.WithEmail(), embargo.WithBankAccount(),
embargo.WithMinConfidence(threshold),
)
fmt.Printf("threshold %.1f: %d findings\n", threshold, len(scanner.ScanString(text)))
}
}
Output: threshold 0.4: 2 findings threshold 0.8: 1 findings
func WithPaymentToken ¶
func WithPaymentToken() Option
WithPaymentToken enables detector.PaymentToken detection.
func WithPrivateKeyPEM ¶
func WithPrivateKeyPEM() Option
WithPrivateKeyPEM enables detector.PrivateKeyPEM detection.
func WithSlackToken ¶
func WithSlackToken() Option
WithSlackToken enables detector.SlackToken detection.
func WithSortByPosition ¶
func WithSortByPosition() Option
WithSortByPosition orders findings by start offset ascending, then confidence descending and detector name.
func WithUKSortCode ¶
func WithUKSortCode() Option
WithUKSortCode enables detector.UKSortCode detection.
func WithoutDedup ¶
func WithoutDedup() Option
WithoutDedup retains overlapping findings from all enabled detectors.
type Scanner ¶
type Scanner struct {
// contains filtered or unexported fields
}
Scanner detects sensitive data with configured Detector implementations. Create one with NewScanner; concurrent scans require each registered detector to support concurrent use.
func NewScanner ¶
NewScanner applies opts in order; no options enables no detectors. Nil options are ignored, and the last registration for each detector name wins.
Example ¶
package main
import (
"fmt"
"github.com/nao1215/embargo"
)
func main() {
text := "tanaka@example.com 4532015112830366 from 192.0.2.1"
for _, config := range []struct {
name string
opts []embargo.Option
}{
{"no options", nil},
{"selected", []embargo.Option{embargo.WithPAN(), embargo.WithEmail()}},
{"all built-ins", []embargo.Option{embargo.WithAll()}},
} {
scanner := embargo.NewScanner(config.opts...)
fmt.Printf("%s: %d findings\n", config.name, len(scanner.ScanString(text)))
}
}
Output: no options: 0 findings selected: 2 findings all built-ins: 3 findings
Example (Options) ¶
package main
import (
"fmt"
"regexp"
"github.com/nao1215/embargo"
"github.com/nao1215/embargo/detector"
)
func main() {
// Keep overlapping candidates and process them in input order.
custom := detector.NewRegex("reference", regexp.MustCompile(`REF-\d{4}`), nil, 0.5)
scanner := embargo.NewScanner(
embargo.WithEmail(),
embargo.WithDetector(custom),
embargo.WithoutDedup(),
embargo.WithSortByPosition(),
)
for _, f := range scanner.ScanString("REF-1234@example.com then admin@example.com") {
fmt.Printf("%s [%d:%d] %.1f\n", f.DetectorName, f.Start, f.End, f.Confidence)
}
}
Output: email [0:20] 1.0 reference [0:8] 0.5 email [26:43] 1.0
func (*Scanner) Scan ¶
Scan returns findings in data, or nil for empty input. It keeps the highest-confidence non-overlapping findings, then orders them by confidence descending, start offset ascending, and detector name.
Overlap ties prefer an earlier start, then a later end, then detector name. WithoutDedup retains overlaps; WithSortByPosition changes ordering; WithMinConfidence filters the final results.
Example ¶
package main
import (
"fmt"
"github.com/nao1215/embargo"
)
func main() {
scanner := embargo.NewScanner(embargo.WithPAN())
data := []byte("カード: 4532-0151-1283-0366")
for _, f := range scanner.Scan(data) {
fmt.Printf("%s [%d:%d]: %s\n", f.DetectorName, f.Start, f.End, data[f.Start:f.End])
}
}
Output: pan [11:30]: 4532-0151-1283-0366
func (*Scanner) ScanLines ¶
func (s *Scanner) ScanLines(r io.Reader, fn func(lineNum int, line []byte, findings []Finding)) error
ScanLines calls fn synchronously for each line containing findings, with 1-based line numbers and line-relative byte offsets. The line excludes its LF or CRLF terminator and is a copy that fn may retain.
The buffer starts at 64 KiB and grows to 1 MiB, including space needed for the line terminator. A line that exceeds the buffer limit returns bufio.ErrTooLong; use Scanner.ScanReader for longer lines. Read errors are returned after any callbacks already made.
Example ¶
package main
import (
"fmt"
"strings"
"github.com/nao1215/embargo"
)
func main() {
scanner := embargo.NewScanner(embargo.WithEmail())
input := "safe line\nadmin@example.com\n"
err := scanner.ScanLines(strings.NewReader(input), func(n int, line []byte, findings []embargo.Finding) {
for _, f := range findings {
fmt.Printf("line %d [%d:%d]: %s\n", n, f.Start, f.End, line[f.Start:f.End])
}
})
if err != nil {
fmt.Println("error:", err)
}
}
Output: line 2 [0:17]: admin@example.com
func (*Scanner) ScanReader ¶
ScanReader reads all of r into memory and calls Scanner.Scan. A read error returns nil findings and the error; use Scanner.ScanLines for incremental processing.
Example ¶
package main
import (
"fmt"
"strings"
"github.com/nao1215/embargo"
)
func main() {
scanner := embargo.NewScanner(embargo.WithEmail())
reader := strings.NewReader("safe line\nadmin@example.com\n")
findings, err := scanner.ScanReader(reader)
if err != nil {
fmt.Println("error:", err)
return
}
for _, f := range findings {
fmt.Printf("%s [%d:%d]\n", f.RawValue, f.Start, f.End)
}
}
Output: admin@example.com [10:27]
func (*Scanner) ScanString ¶
ScanString calls Scanner.Scan on the bytes of text.
Example ¶
package main
import (
"fmt"
"github.com/nao1215/embargo"
)
func main() {
scanner := embargo.NewScanner(embargo.WithEmail())
findings := scanner.ScanString("contact admin@example.com for support")
fmt.Printf("found %d email(s)\n", len(findings))
for _, f := range findings {
fmt.Println(f.RawValue)
}
}
Output: found 1 email(s) admin@example.com
type SensitiveKind ¶
type SensitiveKind = detector.SensitiveKind
SensitiveKind aliases detector.SensitiveKind, a finding category.
revive:disable-next-line:exported
Directories
¶
| Path | Synopsis |
|---|---|
|
adapters
|
|
|
grpc
module
|
|
|
logrus
module
|
|
|
otel
module
|
|
|
sentry
module
|
|
|
zap
module
|
|
|
zerolog
module
|
|
|
Package detector provides built-in sensitive-data detectors and the Detector extension interface.
|
Package detector provides built-in sensitive-data detectors and the Detector extension interface. |
|
internal
|
|
|
ascii
Package ascii provides low-level ASCII byte manipulation helpers shared across the embargo module.
|
Package ascii provides low-level ASCII byte manipulation helpers shared across the embargo module. |
|
Package mask replaces sensitive findings using per-detector Mask or uniform MaskAll strategies.
|
Package mask replaces sensitive findings using per-detector Mask or uniform MaskAll strategies. |
