Skip to main content

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.txt for all dependencies

Deployed URL

https://topcoder-production.up.railway.app

Installation

  1. Clone the repository.
  2. Navigate to the backend directory:
    cd Topcoder/backend
    
  3. (Recommended) Create a virtual environment:
    python -m venv .venv
    source .venv/bin/activate  # On Windows: .venv\Scripts\activate
    
  4. 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_KEY only 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 set SETTINGS_KEY_PATH=/data/settings.key and SETTINGS_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_KEY as 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:

  1. Open /settings/ui on your deployment.
  2. Save your OpenAI API key.
  3. Set AI mode and auto-fix defaults.
  4. Use the GitHub App integration or call the API directly.

CLI (most common):

  1. pip install guardrails-cli
  2. Open /settings/ui once and save your API key.
  3. Generate a user token (CLI or UI):
    • CLI: guardrails settings --issue-user-token
    • UI: /settings/ui shows and can regenerate the token
  4. Paste a CLI token into /settings/ui if you want to override the auto-generated token for this browser.
  5. 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>

Notes:

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 --autofix-mode on|off
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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

guardrails_cli-0.1.10.tar.gz (13.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

guardrails_cli-0.1.10-py3-none-any.whl (11.8 kB view details)

Uploaded Python 3

File details

Details for the file guardrails_cli-0.1.10.tar.gz.

File metadata

  • Download URL: guardrails_cli-0.1.10.tar.gz
  • Upload date:
  • Size: 13.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for guardrails_cli-0.1.10.tar.gz
Algorithm Hash digest
SHA256 b74480a72d325ff30a385b0eb040ab7ad73af0ea9ea26a24d99c8aac05fedcb2
MD5 53f395be010382401a810f811afd97ae
BLAKE2b-256 c283f48df7d81a9aafaf1c7c663e0c414f3eaf7f557cc4a1967b63ec4664b6eb

See more details on using hashes here.

File details

Details for the file guardrails_cli-0.1.10-py3-none-any.whl.

File metadata

File hashes

Hashes for guardrails_cli-0.1.10-py3-none-any.whl
Algorithm Hash digest
SHA256 56e9b546a65b76edce4b546a8d04c12050660d75541aec7d9790618cb821c8dc
MD5 1f2485090bf990c30853d9efb6944304
BLAKE2b-256 8f368b705563072e3269c4c4c411a8d3f8e4b126ade8967a4c7c6d8c7333093c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page