Stop AI Slop: I Built an Open-Source Security Layer for LLM Coding Agents
As developers, we are shipping code faster than ever thanks to AI assistants like Claude, ChatGPT, and Cursor. But let’s admit an uncomfortable truth: AI makes you faster, but it doesn't automatically make you safer.
AI models prioritize functionality and getting the logic to run over application security. Whenever I ask an AI to write a quick database query or a system utility script, it frequently returns hardcoded secrets, unsafe deserialization methods like pickle, or classic SQL injection vectors.
Instead of manually reviewing every single line or crossing my fingers during CI/CD pipelines, I decided to build a lightweight, Zero-Trust security middleware: Secure-MCP.
What is Secure-MCP?
Secure-MCP is a lightweight, local-first SAST (Static Application Security Testing) middleware designed to intercept and scan LLM-generated Python code before it ever touches a production codebase.
Under the hood, it leverages Python's robust bandit security analyzer, wrapped in a high-performance FastAPI backend, and presented through a clean, modern Glassmorphism web interface.
How It Works (The Architecture)
-
The Request: The developer (or an automated AI agent) submits a generated code snippet to the
/api/v1/scanendpoint. -
Secure Sandboxing: The backend securely writes the snippet into a temporary file (
tempfile), ensuring no persistent disk pollution, and executes a JSON-formatted static analysis scan viabandit. - The Structured Report: It returns a structured JSON payload breaking down vulnerabilities by severity (High, Medium, Low), complete with exact line numbers and offending code snippets.
-
Rate Limiting & Protection: Built with
slowapito prevent abuse and protect public endpoint resources.
A Quick Peek at the Code
Here is a snippet showing how the core scanning service handles temporary file execution and safety cleanups:
python
import tempfile
import subprocess
import json
import os
class BanditScannerService:
@staticmethod
def scan_python_code(code_string: str):
# Securely write to a temporary file
with tempfile.NamedTemporaryFile(mode='w', suffix='.py', delete=False) as temp:
temp.write(code_string)
temp_path = temp.name
try:
# Run Bandit static analysis in JSON format
cmd = ["bandit", "-f", "json", temp_path]
result = subprocess.run(cmd, capture_output=True, text=True)
# Parse the JSON report
report = json.loads(result.stdout) if result.stdout else {"results": []}
# Aggregate severity counts
high, medium, low = 0, 0, 0
for issue in report.get("results", []):
sev = issue.get("issue_severity")
if sev == "HIGH": high += 1
elif sev == "MEDIUM": medium += 1
elif sev == "LOW": low += 1
return {
"status": "completed",
"total_issues": len(report.get("results", [])),
"high_severity": high,
"medium_severity": medium,
"low_severity": low,
"results": report.get("results", [])
}
finally:
# Ensure cleanup happens even if something fails
if os.path.exists(temp_path):
os.remove(temp_path)
Top comments (1)
AI coding security layers should make the policy boundary inspectable. Developers need to know what was blocked, why, and what evidence would change the decision.