Skip to content

AI Security Scanner for Python

Category AI Security
Status ✅ Completed
Repo nextwork-ai-security-scanner-for-python
Original NextWork guide nextwork.ai
Proof Official NextWork write-up: web · PDF

Learning project

Practice work following a NextWork guide, separate from my production projects.

📚 NextWork learning project. This is a practice project I built while working through NextWork's AI Security Scanner for Python guide. It is part of my Learning portfolio and is fully separate from my production projects.

Category: Learning › AI Security
Status: ✅ Completed

Proof: Official NextWork write-up: web · PDF

Overview

This command-line tool sends each Python file, with line numbers, to an LLM along with a security-expert prompt. It parses the JSON findings (line, type, severity, fix) and prints them sorted by severity with colors. samples/vulnerable_app.py has 4 planted bugs (SQL injection, MD5 password hashing, shell injection, and a hard-coded key). samples/safe_app.py is the fixed version.

Summary

The CLI finds all 4 planted vulnerabilities with exact lines, an LLM reviews each one and writes the fix, and severities are color-coded (secret mission). The LLM is a local Ollama model instead of Gemini, with the same prompt and JSON contract.

The checks behind this are in docs/verification.txt.

Skills & tools

LLM-assisted code review, Prompt engineering, Python security, Ollama, CLI design, pytest

What I built

  • scanner.py: the CLI (python scanner.py <file|dir> [--json]) with color-coded severities.
  • samples/: the vulnerable and fixed sample files.
  • test_scanner.py: the static detector (4/4 on the vulnerable file, 0 on the fixed one) plus the LLM review of each finding.
  • docs/scan-output.txt: real scanner output from the verification run.

How to run

ollama pull qwen2.5:1.5b
pip install ollama pytest
python scanner.py samples/
FORCE_COLOR=1 python scanner.py samples/vulnerable_app.py

Verification

I ran the verification command on 2026-12-02. The full output is in docs/verification.txt.

mkdir -p docs && FORCE_COLOR= PATH=/workspace/nextwork-portfolio/.venv/bin:$PATH python scanner.py samples/ | tee docs/scan-output.txt && PATH=/workspace/nextwork-portfolio/.venv/bin:$PATH python -m pytest -q -s -p no:cacheprovider test_scanner.py

Notes

What I learned: my first version sent whole files to the 1.5B model and asked it for findings. It reported 10 "issues" in the vulnerable file and 16 in the fixed file, mostly hallucinated. Grounding fixed it: an AST pass finds candidates with exact line numbers, and the model only confirms each one and writes the fix. Now the fixed file reports 0 and the vulnerable file reports exactly the 4 planted bugs. A bigger model like Gemini can do more of the discovery itself, but grounding still cuts false positives. The model's fix text still needs a human review: in one run it suggested replacing MD5 with SHA-256 for passwords, which is also wrong (use scrypt, bcrypt, or Argon2, as in samples/safe_app.py).

Model/provider substitution: the guide calls the Gemini API. I didn't create a Google AI Studio key, so ask_llm() calls a local open model through Ollama with the same prompt and JSON contract.


Original project: https://nextwork.ai/projects/7303f8f5-e0c4-5469-8727-b19d56edf6f5 · Part of my NextWork Learning portfolio (hub).

Verification output

From docs/verification.txt:

$ mkdir -p docs && FORCE_COLOR= PATH=/workspace/nextwork-portfolio/.venv/bin:$PATH python scanner.py samples/ | tee docs/scan-output.txt && PATH=/workspace/nextwork-portfolio/.venv/bin:$PATH python -m pytest -q -s -p no:cacheprovider test_scanner.py

[CRITICAL] samples/vulnerable_app.py:20  Hard-coded secret (API_KEY assigned a string literal)
           fix: Replace the hardcoded API_KEY with a secure environment variable or a configuration file.
[HIGH]     samples/vulnerable_app.py:8  SQL injection (query built by string concatenation/f-string)
           fix: Use parameterized queries to prevent SQL injection.
[HIGH]     samples/vulnerable_app.py:13  Weak hashing (hashlib.md5 used (unsuitable for passwords))
           fix: Replace hashlib.md5 with a suitable password hashing library like bcrypt or hashlib.sha256.
[HIGH]     samples/vulnerable_app.py:17  Command injection (subprocess.run with a shell)
           fix: Use subprocess.run with the `stdout` and `stderr` arguments to capture the output and handle the command injection vulnerability.

4 finding(s) in 2 file(s)
..{'file': 'samples/vulnerable_app.py', 'line': 20, 'type': 'Hard-coded secret', 'evidence': 'API_KEY assigned a string literal', 'severity': 'CRITICAL', 'llm_confirmed': True, 'fix': 'Replace the hardcoded API_KEY with an environment variable or a secure configuration file.'}
{'file': 'samples/vulnerable_app.py', 'line': 8, 'type': 'SQL injection', 'evidence': 'query built by string concatenation/f-string', 'severity': 'HIGH', 'llm_confirmed': True, 'fix': 'Use parameterized queries to prevent SQL injection.'}
{'file': 'samples/vulnerable_app.py', 'line': 13, 'type': 'Weak hashing', 'evidence': 'hashlib.md5 used (unsuitable for passwords)', 'severity': 'HIGH', 'llm_confirmed': True, 'fix': 'Replace hashlib.md5 with a suitable hashing algorithm like hashlib.sha256 or hashlib.sha512 for password hashing.'}
{'file': 'samples/vulnerable_app.py', 'line': 17, 'type': 'Command injection', 'evidence': 'subprocess.run with a shell', 'severity': 'HIGH', 'llm_confirmed': True, 'fix': 'Replace the shell command with a safer alternative, such as using the `subprocess.run` with `text=True` and `stdout=subprocess.PIPE` to capture the output and handle it safely.'}
.
3 passed in 7.95s

[exit code 0]