HTTP 服务默认监听 8088、gRPC 默认 8089,端口冲突需用 PF_PORT / PF_GRPC_PORT 改;核心包默认读取 rules/gitleaks.toml,路径不对要传空字符串走内置兜底规则;熵兜底可能误伤 git SHA 和长 base64 串,需调阈值或加白名单;不做人名地名机构名识别,需要 NER 的场景不适用。
privacy-filter
packymeMIT更新放缓用 Go 写的 LLM 隐私网关,在请求进出模型前以毫秒级延迟自动识别并脱敏 PII 和密钥,防止敏感数据泄露给大模型。
LLM privacy gateway in Go — millisecond-latency PII and secret redaction. Used in production by PackyCode.
go build -o bin/server-http ./cmd/http
./bin/server-http纯 Go 实现,不需要模型、GPU 或 CGO,编译为单个静态二进制,CPU 即可运行;README 未说明具体 CPU 或内存要求。
纯 Go 实现的 LLM 隐私网关,在请求到达大模型前毫秒级脱敏 PII 与密钥
用 Go 写的 LLM 隐私网关,在请求进出模型前以毫秒级延迟自动识别并脱敏 PII 和密钥,防止敏感数据泄露给大模型。
核心能力
- 用正则识别邮箱、电话、身份证、银行卡和 IP 等结构化 PII
- 结合 gitleaks 规则集、上下文正则与香农熵兜底识别密钥凭证
- 合并去重叠命中区间后单次重建文本,占位符按实体类型标注
- 提供核心包、HTTP 与 gRPC 三种接入方式,后两者为薄封装
- Go regexp 线性时间匹配,无 ReDoS 回溯风险
容易踩的坑
暂无 release 数据(可能没有发版,或尚未同步)。
用户点评
登录用户的真实使用记录,可按版本和硬件筛选还没有人点评。第一个说出真实体验的人最有价值。
Privacy Filter (Go)
English | 简体中文
Strip sensitive user data (PII / secrets) from text before it reaches an LLM. Pure Go, no model, no GPU, no CGO — a single static binary, millisecond latency on text of any length.
🌐 Running in production at PackyCode — the privacy-compliance component of an API relay service.
Three ways to use it
- Core package:
import "privacyfilter/filter"straight into your gateway — redaction is one function call, no HTTP hop. - HTTP service:
cmd/http, REST API. - gRPC service:
cmd/grpc, interface inproto/filter.proto.
The latter two are thin wrappers around the filter core package.
Two detection layers
| Layer | Covers | Technique |
|---|---|---|
| Structured PII | Email, phone, national ID, bank card (Luhn-checked), IP | Regex |
| Secrets / credentials | API keys, tokens, private keys, passwords written in prose, unknown high-entropy strings | gitleaks ruleset (keyword pre-filter) + contextual regex + Shannon-entropy fallback |
Each layer emits (start, end, placeholder) spans → spans are merged and de-overlapped → the text is rebuilt in a single pass.
Placeholders are typed and carry the entity kind — [邮箱] (email), [电话] (phone), [身份证] (national ID), [银行卡] (bank card), [IP], [密钥] (secret) — and are irreversible (no un-redaction).
No person / place / organization name recognition — that needs an NER model, which costs seconds of CPU time on long text and was removed per requirements. High-risk identity data (national ID, bank card, secrets, etc.) is fully covered by regex.
Layout
privacy-filter/
├── go.mod / go.sum
├── filter/ core package (import directly from a gateway)
│ ├── filter.go Filter / New / Redact
│ ├── pii.go structured PII
│ ├── secrets.go gitleaks + context + entropy
│ └── filter_test.go
├── cmd/
│ ├── http/main.go HTTP service
│ └── grpc/main.go gRPC service
├── proto/filter.proto gRPC interface definition
├── gen/filterpb/ protoc-generated code
├── rules/gitleaks.toml gitleaks ruleset
├── scripts/fetch_rules.sh ruleset update script
└── Dockerfile
Build
go build -o bin/server-http ./cmd/http
go build -o bin/server-grpc ./cmd/grpc
go test ./... # run all tests
Usage
1. Core package (recommended for gateways)
import "privacyfilter/filter"
// Create once at startup; concurrency-safe, reuse globally.
f, err := filter.New("rules/gitleaks.toml") // pass "" to use the built-in fallback rules
// Per request
res := f.Redact(userPrompt)
forwardToLLM(res.Redacted) // forward the redacted text to the LLM
filter.Result: Redacted (redacted text), Hit, Count, Entities (hit details,
including type and byte offsets).
To consume this package from your own gateway module: put it in the same monorepo, or add
replace privacyfilter => ../privacy-filterto the gateway's go.mod. Thefilterpackage depends only onBurntSushi/toml.
2. HTTP service
./bin/server-http # default :8088
curl http://127.0.0.1:8088/health
curl -X POST http://127.0.0.1:8088/redact -H 'Content-Type: application/json' \
-d '{"text":"我的邮箱是 [email protected],密码是 Hunter2xy"}'
# {"redacted":"我的邮箱是 [邮箱],密码是 [密钥]","hit":true,"count":2,"entities":[...],"elapsed_ms":0.08}
Endpoints: GET /health, POST /redact, POST /redact/batch ({"texts":[...]}).
3. gRPC service
./bin/server-grpc # default :8089
Service filter.v1.PrivacyFilter, methods Redact / RedactBatch, defined in proto/filter.proto.
Generate a client from that proto on the gateway side. To regenerate the code in this repo:
protoc -I. --go_out=. --go_opt=module=privacyfilter \
--go-grpc_out=. --go-grpc_opt=module=privacyfilter proto/filter.proto
Configuration (environment variables)
| Variable | Default | Description |
|---|---|---|
PF_PORT | 8088 | HTTP listen port |
PF_GRPC_PORT | 8089 | gRPC listen port |
PF_GITLEAKS_TOML | rules/gitleaks.toml | path to the gitleaks rules file |
Performance (local benchmark, synthetic high-density PII text — worst case)
| Text length | Latency |
|---|---|
| ~50 B | ~0.01ms |
| ~2 KB | ~0.46ms |
| ~32 KB | ~9ms |
Both layers are O(n). Real prompts (PII is never this dense) are faster.
Integration notes (gateway side)
- With the core-package import there is no HTTP/gRPC hop, hence no timeout and no fail-open/closed concerns.
- If you use the HTTP/gRPC service: set a 150–300ms timeout; on failure, prefer fail-closed (reject the request rather than forwarding the raw text).
Notes
- All 222 gitleaks rules compile natively in Go (Go's
regexpis RE2, the same engine gitleaks uses; an earlier Python port lost 26 rules to RE2-incompatible syntax). - Go
regexpruns in linear time — no catastrophic backtracking (ReDoS) risk. - gitleaks does not support look-around assertions, so digit boundaries for phone / national ID etc. are enforced by manual post-match validation.
- No person / place / organization recognition. If added later, prefer rules (a Chinese-address regex is feasible; names are better anchored by context).
- The entropy fallback can mis-flag git SHAs, long base64 strings, etc. — tune the threshold or add an allowlist.