privacy-filter

packymeMIT更新放缓

用 Go 写的 LLM 隐私网关,在请求进出模型前以毫秒级延迟自动识别并脱敏 PII 和密钥,防止敏感数据泄露给大模型。

LLM privacy gateway in Go — millisecond-latency PII and secret redaction. Used in production by PackyCode.

暂无版本340star42fork同步于 2026-11-07 07:32 UTC

怎么上手

由 README 提炼,执行前请确认
go build -o bin/server-http ./cmd/http
./bin/server-http
硬件要求按 README 说明

纯 Go 实现,不需要模型、GPU 或 CGO,编译为单个静态二进制,CPU 即可运行;README 未说明具体 CPU 或内存要求。

下载:源码 zip·GitHub Releases(安装包)本站不镜像安装包

项目说明

由 README 自动翻译提炼

纯 Go 实现的 LLM 隐私网关,在请求到达大模型前毫秒级脱敏 PII 与密钥

用 Go 写的 LLM 隐私网关,在请求进出模型前以毫秒级延迟自动识别并脱敏 PII 和密钥,防止敏感数据泄露给大模型。

核心能力

  • 用正则识别邮箱、电话、身份证、银行卡和 IP 等结构化 PII
  • 结合 gitleaks 规则集、上下文正则与香农熵兜底识别密钥凭证
  • 合并去重叠命中区间后单次重建文本,占位符按实体类型标注
  • 提供核心包、HTTP 与 gRPC 三种接入方式,后两者为薄封装
  • Go regexp 线性时间匹配,无 ReDoS 回溯风险

容易踩的坑

HTTP 服务默认监听 8088、gRPC 默认 8089,端口冲突需用 PF_PORT / PF_GRPC_PORT 改;核心包默认读取 rules/gitleaks.toml,路径不对要传空字符串走内置兜底规则;熵兜底可能误伤 git SHA 和长 base64 串,需调阈值或加白名单;不做人名地名机构名识别,需要 NER 的场景不适用。

内容由模型根据 README 生成,可能不完整或不准确,以官方文档为准。看原文 README

最近发版

0 个版本

暂无 release 数据(可能没有发版,或尚未同步)。

用户点评

登录用户的真实使用记录,可按版本和硬件筛选
共 0 条

    还没有人点评。第一个说出真实体验的人最有价值。

    README 原文

    未翻译

    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

    1. Core package: import "privacyfilter/filter" straight into your gateway — redaction is one function call, no HTTP hop.
    2. HTTP service: cmd/http, REST API.
    3. gRPC service: cmd/grpc, interface in proto/filter.proto.

    The latter two are thin wrappers around the filter core package.


    Two detection layers

    LayerCoversTechnique
    Structured PIIEmail, phone, national ID, bank card (Luhn-checked), IPRegex
    Secrets / credentialsAPI keys, tokens, private keys, passwords written in prose, unknown high-entropy stringsgitleaks 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-filter to the gateway's go.mod. The filter package depends only on BurntSushi/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)

    VariableDefaultDescription
    PF_PORT8088HTTP listen port
    PF_GRPC_PORT8089gRPC listen port
    PF_GITLEAKS_TOMLrules/gitleaks.tomlpath to the gitleaks rules file

    Performance (local benchmark, synthetic high-density PII text — worst case)

    Text lengthLatency
    ~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 regexp is RE2, the same engine gitleaks uses; an earlier Python port lost 26 rules to RE2-incompatible syntax).
    • Go regexp runs 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.