Guardrails CLI for scanning repositories
Project description
Topcoder Guardrails Backend
Overview
This project is a secure, modular backend for code analysis, policy enforcement, and audit logging. It uses FastAPI and includes endpoints for code analysis, health checks, and a dashboard for audit logs.
Features
- REST API for code analysis (
/analyze) - Health check endpoint (
/health) - Audit dashboard (
/dashboard) - Modular rulepacks for different sectors
- Security, coding standards, license/IP, and AI review checks
- Repo license detection (local scans)
- Cross-file duplicate code detection (batch scans)
- AI review with PR/commit context when available
Requirements
- Python 3.9+
- See
backend/requirements.txtfor all dependencies
Deployed URL
https://topcoder-production.up.railway.app
Installation
- Clone the repository.
- Navigate to the backend directory:
cd Topcoder/backend
- (Recommended) Create a virtual environment:
python -m venv .venv source .venv/bin/activate # On Windows: .venv\Scripts\activate
- Install dependencies:
pip install -r requirements.txt
Configuration
This app is per-user, not global. Settings are scoped by user token when SETTINGS_SCOPE=user.
Local-only (optional):
- Set
OPENAI_API_KEYonly if you are running locally and want AI review without using the settings UI/CLI.
Railway note (important):
- Railway containers use an ephemeral filesystem on redeploys. If you rely on the settings UI/CLI to store the API key, it will be lost unless you persist the settings file.
- Fix: use a persistent key file + settings file on a Railway volume. Example: attach a volume at
/data, then setSETTINGS_KEY_PATH=/data/settings.keyandSETTINGS_STORE_PATH=/data/settings.enc. The key file is created on first boot and reused on redeploys. - If you do not want to persist a file, set
OPENAI_API_KEYas an environment variable in Railway so the key is always present after redeploys.
Guardrails Backend
FastAPI-based service that analyzes code, applies policy/rulepacks, and reports results for PRs, commits, and local scans. It ships with a settings UI, audit logging, and a lightweight CLI for scanning any repository.
What it does
- Analyzes code via REST endpoints and returns structured findings.
- Enforces security, coding standards, license/IP, sector rules, and AI review policy.
- Stores audit logs and provides summaries.
- Supports rulepacks and repo-level overrides.
- Integrates with a GitHub App for PR and push scans.
Challenge requirement coverage
- Secure coding guardrails with OWASP/CWE mappings.
- Copilot-aware flagging and stricter handling for AI-generated code.
- Configurable coding standards (YAML/JSON in repo config).
- AI-assisted review with explainable findings.
- License/IP checks with restricted license detection.
- Policy-based enforcement (advisory/warning/blocking) with override label.
- PR/commit scanning via GitHub App with inline comments and summaries.
- Traceability with audit IDs, export, and resolution events.
- Async scan flow for large PRs.
- Pluggable rulepacks per industry (finance, healthcare, public sector, telecom, government).
How people use it
Website-only:
- Open
/settings/uion your deployment. - Save your OpenAI API key.
- Set AI mode and auto-fix defaults.
- Use the GitHub App integration or call the API directly.
CLI (most common):
pip install guardrails-cli- Open
/settings/uionce and save your API key. - Generate a user token (CLI or UI):
- CLI:
guardrails settings --issue-user-token - UI:
/settings/uishows and can regenerate the token
- CLI:
- Paste a CLI token into
/settings/uiif you want to override the auto-generated token for this browser. guardrails scan <repo-path> --user <token>(path optional; defaults to current directory)
Project layout
- backend/ — FastAPI service and rule engine
- github-app/ — GitHub App integration
- src/guardrails_cli/ — Published CLI package
- docs/ — Architecture notes
Requirements
- Python 3.9+
- Dependencies in backend/requirements.txt
Local setup
cd Topcoder/backend
python -m venv .venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate
pip install -r requirements.txt
Run the server
uvicorn main:app --reload --host 127.0.0.1 --port 8000
Configuration (environment variables)
Core settings:
- OPENAI_API_KEY (optional) — used when no per-user key exists
- SETTINGS_SCOPE (default: global) — global | user | ip
- SETTINGS_TOKEN (optional) — protects settings endpoints
- SETTINGS_ENC_KEY (recommended) — encrypts persisted settings
- SETTINGS_KEY_PATH (optional) — path to a persistent encryption key file (auto-created when missing)
- SETTINGS_STORE_PATH (default: settings.enc) — encrypted settings file
- REQUIRE_AI_REVIEW_DEFAULT (default: false)
Audit logging:
- AUDIT_LOG_ENABLED (default: true)
- AUDIT_LOG_PATH (default: audit_log.jsonl)
- AUDIT_LOG_STORE_OUTPUT (default: true)
- Stored output is sanitized to avoid retaining code snippets or patches
Data residency:
- DATA_RESIDENCY (optional)
GitHub App integration
The GitHub App scans PRs and pushes, posts comments/checks, and reads repo overrides from .guardrails/config.yml|yaml|json.
Environment variables:
- BACKEND_URL (required)
- OVERRIDE_LABEL (optional, default: guardrails-override)
- MAX_FILES (optional, default: 100)
- MAX_FILE_BYTES (optional, default: 200000)
- USE_ASYNC_SCAN (optional, default: false)
CLI usage
Install:
pip install guardrails-cli
Scan:
guardrails scan <repo-path> --user <token>
# Or, from inside your repo:
guardrails scan --user <token>
Fix modes:
- Full fix (AI rewrite + safe fixes):
guardrails scan --full-fix --user <token> - Safe fix only:
guardrails scan --safe-fix --user <token> - No fixes:
guardrails scan --no-fix --user <token>
Notes:
- Hosted URL: https://topcoder-production.up.railway.app
- Override with GUARDRAILS_API_URL or --api
- Use --autofix to apply safe local fixes
- Use --no-ai to disable AI review for a run
CLI settings (no UI required)
python guardrails.py settings --generate-local-key
python guardrails.py settings --set-api-key <key>
python guardrails.py settings --ai-mode require|allow
python guardrails.py settings --fix-mode full|safe|none
guardrails settings --issue-user-token
python guardrails.py settings --verify
API endpoints
- GET /health
- GET /
- GET /dashboard
- GET /settings
- POST /settings/token
- GET /settings/ui
- POST /settings/api-key
- POST /settings/ai-mode
- POST /settings/autofix-mode
- POST /settings/override-allowed
- POST /analyze
- POST /analyze-batch
- POST /scan/async
- GET /scan/status/{job_id}
- GET /report/summary
- GET /report/trends
- GET /rulepacks
- POST /rulepacks
- GET /audit/export
- POST /audit/resolve
- GET /docs
Testing
pytest
GET /report/summary— Audit summary counts
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file guardrails_cli-0.1.17.tar.gz.
File metadata
- Download URL: guardrails_cli-0.1.17.tar.gz
- Upload date:
- Size: 13.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8f4a2bb2b4d6646d20d16f584eb0384a61998406af3f458077f15d6512da590f
|
|
| MD5 |
019497b712484f66705a20d7cad9a2dc
|
|
| BLAKE2b-256 |
6efe7b4afd5c5bce75200492dae3756759fc827e462f0a3e93169ec9fd07715d
|
File details
Details for the file guardrails_cli-0.1.17-py3-none-any.whl.
File metadata
- Download URL: guardrails_cli-0.1.17-py3-none-any.whl
- Upload date:
- Size: 13.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0bea532361f421b16d2f78e66e34c9eeec36ca8f8671a4412d4a48d1139adf2a
|
|
| MD5 |
daf0805d803987bf04fcb183dfce1e4a
|
|
| BLAKE2b-256 |
5364376b7bced3093f92b6648289b0b57bb31abbe1c05e5e4dfc28acbb6d285a
|