A FastAPI-based application that detects sensitive content in user prompts using a custom implementation of the Aho-Corasick string matching algorithm.
- Custom Aho-Corasick Implementation: Built from scratch without external Aho-Corasick libraries
- Sensitive Content Detection: Identifies keywords like NIK, email, phone, password, credit card, etc.
- Case-Insensitive Matching: Detects sensitive content regardless of case
- FastAPI REST API: Clean API endpoints for prompt checking
- Comprehensive Testing: Full test suite with pytest
- Real-time Protection: Blocks sensitive prompts and provides detailed match information
app/
βββ main.py # FastAPI entry point
βββ api.py # API routes
βββ core/
β βββ __init__.py
β βββ aho_corasick.py # Manual Aho-Corasick implementation
β βββ checker.py # check_prompt logic + dummy LLM forward
βββ tests/
βββ test_checker.py # Example test cases
-
Create virtual environment:
python -m venv venv
-
Activate virtual environment:
- Windows (PowerShell):
venv\Scripts\Activate.ps1
- Windows (Command Prompt):
venv\Scripts\activate
- Linux/macOS:
source venv/bin/activate
- Windows (PowerShell):
-
Install dependencies:
pip install -r requirements.txt
uvicorn app.main:app --reloadThe API will be available at http://localhost:8000
- Swagger UI:
http://localhost:8000/docs - ReDoc:
http://localhost:8000/redoc
Check if a prompt contains sensitive content.
Request Body:
{
"prompt": "What is my password?"
}Response (Sensitive):
{
"status": "SENSITIVE",
"matches": [
{
"keyword": "password",
"position": 10
}
]
}Response (Safe):
{
"status": "SAFE",
"matches": [],
"response": "LLM response: What is the weather today?"
}Health check endpoint.
Get the list of monitored sensitive keywords.
Run the test suite:
pytestRun tests with verbose output:
pytest -vRun tests with coverage:
pytest --cov=appThe core algorithm consists of three main phases:
- Trie Construction: Build a trie (prefix tree) from all sensitive keywords
- Failure Link Construction: Create failure links for efficient pattern matching using BFS
- Pattern Matching: Scan input text character by character, following failure links when necessary
Default sensitive keywords include:
- NIK (Indonesian National ID)
- phone
- password
- credit card
- ssn
- social security
- bank account
- pin
- cvv
- passport
- driver license
- api key
- token
- secret
- private key
- confidential
- classified
from app.core.checker import PromptChecker
# Create checker instance
checker = PromptChecker()
# Check a safe prompt
result = checker.check_prompt("What is the weather today?")
print(result) # {"status": "SAFE", "matches": [], "response": "LLM response: ..."}
# Check a sensitive prompt
result = checker.check_prompt("My password is secret123")
print(result) # {"status": "SENSITIVE", "matches": [{"keyword": "password", "position": 3}]}# Check a prompt
curl -X POST "http://localhost:8000/api/v1/check" \
-H "Content-Type: application/json" \
-d '{"prompt": "What is my password?"}'- Clone the repository
- Create and activate virtual environment
- Install dependencies:
pip install -r requirements.txt - Run tests:
pytest - Start development server:
uvicorn app.main:app --reload
Modify the DEFAULT_SENSITIVE_KEYWORDS list in app/core/checker.py or create a custom PromptChecker instance with your own keyword list.
- The system performs case-insensitive matching
- All patterns are converted to lowercase for consistent matching
- The system blocks prompts containing any sensitive keywords
- In production, consider additional security measures like rate limiting and authentication
# Push to GitHub first (sudah done!)
# Deploy to Vercel
# 1. Go to vercel.com (NO CREDIT CARD NEEDED!)
# 2. Continue with GitHub
# 3. Import repo: XenchinRyu7/SecurePrompt
# 4. Deploy! (1-2 menit)# Free tier but needs credit card verification
# 750 hours/month, sleep after 15min idle# Build and run locally
docker build -t secureprompt .
docker run -p 8000:8000 securepromptπ Detailed Guides:
- Vercel Deploy Guide - Recommended (No Credit Card!)
- General Deployment Options
π API is LIVE at: https://secure-prompt.vercel.app
Available endpoints:
- Health check:
GET /api/health - Keywords list:
GET /api/keywords - Check prompt:
POST /api/check - API root:
GET /
We welcome contributions! SecurePrompt is an open-source project and we'd love to have you contribute.
- Fork the repository
- Create a feature branch:
git checkout -b feature/amazing-feature - Make your changes and add tests
- Ensure all tests pass:
pytest app/tests/ - Submit a pull request
- π Performance optimization of Aho-Corasick algorithm
- π Additional language support for keyword detection
- π Benchmarking and performance metrics
- π Documentation improvements
- π Bug fixes and edge cases
See CONTRIBUTING.md for detailed guidelines.
This project is licensed under the MIT License - see the LICENSE file for details.
- β Commercial use allowed
- β Modification allowed
- β Distribution allowed
- β Private use allowed
- β No warranty provided
- β License and copyright notice required
- Aho-Corasick Algorithm - Alfred V. Aho and Margaret J. Corasick (1975)
- FastAPI - Sebastian Ramirez and contributors
- Vercel - For free serverless hosting
- Open Source Community - For inspiration and support
- π Bug Reports: GitHub Issues
- π¬ Discussions: GitHub Discussions
- π§ Contact: @XenchinRyu7
If this project helped you, please consider:
- β Starring the repository
- π΄ Forking and contributing
- π’ Sharing with others
- π Sponsoring development
Made with β€οΈ for secure AI applications
SecurePrompt - Protecting sensitive information in AI prompts since 2025 π‘οΈ