Help us shape the future of Go! Take the 2026 Go Developer Survey

pii

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2026 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package pii finds personal data in response bodies and headers before they are written as public (into git). It looks for e-mail addresses, phone numbers, Serbian personal numbers (JMBG), payment card numbers and IBANs; numbers are checked against their checksum to keep false alarms rare. Texts are also read URL-decoded, with HTML entities resolved and with base64 runs decoded. Findings never carry the full value.

Index

Constants

View Source
const Credential = "credential"

Credential is the kind of a credential ScanResponse finds.

Variables

View Source
var Kinds = []string{"email", "phone", "jmbg", "card", "iban"}

Kinds are the kinds of personal data a Detector finds.

Functions

func Kind

func Kind(k string) bool

Kind reports whether k is one of Kinds.

func Mask

func Mask(s string) string

Mask keeps the first character and the length class of a value.

Types

type Detector

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

Detector finds personal data, except values matching an allow pattern.

func New

func New(allow []string) (*Detector, error)

New builds a detector; patterns are regular expressions of allowed values.

func (*Detector) Allowed

func (d *Detector) Allowed(s string) bool

Allowed reports whether s matches an allow pattern: a value the project declared not personal.

func (*Detector) Headers

func (d *Detector) Headers(h [][2]string) ([]Finding, [][2]string)

Headers finds personal data in header values (a Location with an e-mail address, a Content-Disposition with a JMBG in the file name). It returns the findings and the headers with that data hidden, letters as x and digits as 0 (the whole value when only a decoded form held it), so the record that goes into git does not hold it either. h is not changed.

func (*Detector) Scan

func (d *Detector) Scan(body []byte) []Finding

Scan looks at every string in a JSON body (keys included) and at its integers, or at the whole body when it is not JSON. It walks the body without building it (jsonx.Walk): the tree of a body of a few megabytes would take many times that. The texts it reads are views of body, not copies (a body read off the heap stays there); findings copy what they keep.

func (*Detector) ScanResponse

func (d *Detector) ScanResponse(body []byte) []Finding

ScanResponse is Scan for a response body that would be stored in git: it also finds credentials (kind "credential"), which must not be committed either. In JSON: a JWT anywhere, a string under a key named as a credential (access_token, csrf, api_key, signature) that is shaped like one, also as the value of an object whose name says so ({"name":"api_key","value":…}, AWS's Key and Value), and a URL with a credential in its query or fragment (a signed or presigned URL, a link with its token). In another body, see ScanText.

func (*Detector) ScanText

func (d *Detector) ScanText(where, text string) []Finding

ScanText is ScanResponse for a text that is not JSON, such as a part of a document; where names it in the findings. Its credentials are a JWT, a URL with one in its query or fragment, and in a form body (access_token=…&token_type=bearer) or in XML a pair, element or attribute named as a credential that is shaped like one.

func (*Detector) Spans

func (d *Detector) Spans(s string) []Span

Spans finds the personal data in a text, by kind (e-mail, JMBG, card, IBAN, phone) and then by position; matches of different kinds may overlap. The text is read as it is, not decoded.

type Finding

type Finding struct {
	Kind   string `json:"kind"`
	Where  string `json:"where"`  // JSON path, "$" for a body that is not JSON, or header:<name>
	Sample string `json:"sample"` // masked
}

Finding is one value that looks like personal data.

type Span

type Span struct {
	Kind       string
	Start, End int
}

Span is where a value that looks like personal data sits in a text: the byte offsets of the match and its kind.

Jump to

Keyboard shortcuts

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