Policy evaluation and PII redaction service for GovernsAI. This service provides real-time policy evaluation and PII detection/redaction for AI tool usage.
- Policy Evaluation: Real-time evaluation of AI tool usage against configurable policies
- PII Detection & Redaction: Advanced PII detection using Microsoft Presidio with regex fallback
- API Key Authentication: Secure API key-based authentication
- Rate Limiting: Redis-based rate limiting (optional)
- Database Storage: SQLAlchemy-based storage with PostgreSQL/SQLite support
- Docker Support: Production-ready Docker containerization
-
Install dependencies:
pip install -r requirements.txt python -m spacy download en_core_web_sm
-
Run the service:
python start.py # or uvicorn app.main:app --reload --host 0.0.0.0 --port 8080 -
Test the service:
curl -X GET http://localhost:8080/api/v1/health
-
Build the image:
docker build -t governsai-precheck . -
Run the container:
docker run -p 8080:8080 governsai-precheck
GET /api/v1/healthReturns service status and version information.
POST /api/v1/precheck
X-Governs-Key: your-api-key
Content-Type: application/json
{
"tool": "web.fetch",
"scope": "net.external",
"raw_text": "Please fetch data from https://example.com for user@example.com",
"tags": ["research"],
"corr_id": "req-123"
}Response:
{
"decision": "transform",
"payload": {
"url": "https://example.com",
"email": "u***@example.com"
},
"reasons": ["pii.redacted:email"],
"policy_id": "net-redact-presidio",
"ts": 1703123456
}POST /api/v1/postcheck
X-Governs-Key: your-api-key
Content-Type: application/jsonSimilar to precheck but for post-execution validation.
The service can be configured via environment variables:
| Variable | Default | Description |
|---|---|---|
APP_BIND |
0.0.0.0:8080 |
Server bind address |
DB_URL |
sqlite:///./local.db |
Database connection URL |
USE_PRESIDIO |
true |
Enable Presidio PII detection |
PRESIDIO_MODEL |
en_core_web_sm |
spaCy model for Presidio |
WEBHOOK_URL |
None |
Webhook URL for dashboard events |
PRECHECK_DLQ |
/tmp/precheck.dlq.jsonl |
Dead letter queue file path |
The service uses Microsoft Presidio for advanced PII detection with the following capabilities:
- Email addresses: Detected and masked
- Phone numbers: International format detection and masking
- Credit cards: Luhn algorithm validation and masking
- API keys: Custom patterns for various API key formats
- JWT tokens: Detection and redaction
- SSN: US Social Security Number detection
- IP addresses: IPv4/IPv6 detection
If Presidio fails to initialize, the service falls back to regex-based detection for basic PII types (email, phone, credit card).
The service emits fire-and-forget webhook events for all precheck and postcheck decisions to enable dashboard integration and audit logging.
{
"userId": "user123",
"tool": "web.fetch",
"scope": "net.external",
"decision": "transform",
"policyId": "net-redact-presidio",
"reasons": ["pii.redacted:email"],
"payloadHash": "sha256_hash_of_payload",
"latencyMs": 45,
"timestamp": 1703123456,
"correlationId": "req-123",
"tags": ["research"],
"direction": "precheck"
}- Set
WEBHOOK_URLenvironment variable to enable webhook emission - Failed webhook deliveries are written to DLQ file (configurable via
PRECHECK_DLQ) - Webhook includes retry logic with exponential backoff (0.5s, 1s, 2s)
Use the provided webhook test URL for development:
export WEBHOOK_URL="https://webhook-test.com/1508b1ea2414ed242d2b8abf6ea66616"The following tools are automatically denied:
python.execbash.execcode.execshell.exec
Tools with network scope or web/http prefixes trigger PII redaction:
web.*http.*fetch.*request.*net.*scope
# Install development dependencies
pip install -r requirements-dev.txt
# Download spaCy model
python -m spacy download en_core_web_smpytest tests/black app/ tests/
isort app/ tests/mypy app/- Bind to
0.0.0.0:8080 - Use PostgreSQL database
- Use Redis for rate limiting
- Set
PUBLIC_BASEfor external access
- Bind to
127.0.0.1:7071 - Use SQLite database
- Optional Redis
- Local development setup
Elastic License 2.0 - see LICENSE file for details.