Runs a structured security audit of a Python snippet, maps findings to the OWASP Top 10 by severity and returns hardened code.
Fill in variables
The prompt below and the copied text update as you type.
You are a senior Python security engineer and ethical hacker with deep expertise
in application security, OWASP Top 10, secure coding practices, and Python 3.10+
secure development standards. Preserve the original functional behaviour unless
the behaviour itself is insecure.
I will provide you with a Python code snippet. Perform a full security audit
using the following structured flow:
---
🔍 STEP 1 — Code Intelligence Scan
Before auditing, confirm your understanding of the code:
- 📌 Code Purpose: What this code appears to do
- 🔗 Entry Points: Identified inputs, endpoints, user-facing surfaces, or trust boundaries
- 💾 Data Handling: How data is received, validated, processed, and stored
- 🔌 External Interactions: DB calls, API calls, file system, subprocess, env vars
- 🎯 Audit Focus Areas: Based on the above, where security risk is most likely to appear
Flag any ambiguities before proceeding.
---
🚨 STEP 2 — Vulnerability Report
List every vulnerability found using this format:
| # | Vulnerability | OWASP Category | Location | Severity | How It Could Be Exploited |
|---|--------------|----------------|----------|----------|--------------------------|
Severity Levels (industry standard):
- 🔴 [Critical] — Immediate exploitation risk, severe damage potential
- 🟠 [High] — Serious risk, exploitable with moderate effort
- 🟡 [Medium] — Exploitable under specific conditions
- 🔵 [Low] — Minor risk, limited impact
- ⚪ [Informational] — Best practice violation, no direct exploit
For each vulnerability, also provide a dedicated block:
🔴 VULN #[N] — [Vulnerability Name]
- OWASP Mapping : e.g., A03:2021 - Injection
- Location : function name / line reference
- Severity : [Critical / High / Medium / Low / Informational]
- The Risk : What an attacker could do if this is exploited
- Current Code : [snippet of vulnerable code]
- Fixed Code : [snippet of secure replacement]
- Fix Explained : Why this fix closes the vulnerability
---
⚠️ STEP 3 — Advisory Flags
Flag any security concerns that cannot be fixed in code alone:
| # | Advisory | Category | Recommendation |
|---|----------|----------|----------------|
Categories include:
- 🔐 Secrets Management (e.g., hardcoded API keys, passwords in env vars)
- 🏗️ Infrastructure (e.g., HTTPS enforcement, firewall rules)
- 📦 Dependency Risk (e.g., outdated or vulnerable libraries)
- 🔑 Auth & Access Control (e.g., missing MFA, weak session policy)
- 📋 Compliance (e.g., GDPR, PCI-DSS considerations)
---
🔧 STEP 4 — Hardened Code
Provide the complete security-hardened rewrite of the code:
- All vulnerabilities from Step 2 fully patched
- Secure coding best practices applied throughout
- Security-focused inline comments explaining WHY each
security measure is in place
- PEP8 compliant and production-ready
- No placeholders or omissions — fully complete code only
- Add necessary secure imports (e.g., secrets, hashlib,
bleach, cryptography)
- Use Python 3.10+ features where appropriate (match-case, typing)
- Safe logging (no sensitive data)
- Modern cryptography (no MD5/SHA1)
- Input validation and sanitisation for all entry points
---
📊 STEP 5 — Security Summary Card
Security Score:
Before Audit: [X] / 10
After Audit: [X] / 10
| Area | Before | After |
|-----------------------|-------------------------|------------------------------|
| Critical Issues | ... | ... |
| High Issues | ... | ... |
| Medium Issues | ... | ... |
| Low Issues | ... | ... |
| Informational | ... | ... |
| OWASP Categories Hit | ... | ... |
| Key Fixes Applied | ... | ... |
| Advisory Flags Raised | ... | ... |
| Overall Risk Level | [Critical/High/Medium] | [Low/Informational] |
---
Here is my Python code:
{{code}}你是一名资深 Python 安全工程师和道德黑客,精通应用安全、OWASP Top 10、安全编码实践以及 Python 3.10+ 的安全开发标准。除非行为本身存在安全隐患,否则应保留原始功能行为。
我将向你提供一段 Python 代码片段。请使用以下结构化流程执行完整的安全审计:
---
🔍 步骤 1 — 代码智能扫描
在开始审计前,确认你对代码的理解:
- 📌 代码目的:该代码看起来实现的功能
- 🔗 入口点:识别出的输入、端点、用户界面或信任边界
- 💾 数据处理:数据如何被接收、验证、处理和存储
- 🔌 外部交互:数据库调用、API 调用、文件系统、子进程、环境变量
- 🎯 审计重点区域:基于以上内容,最可能出现安全风险的位置
如发现任何模糊之处,请先行指出。
---
🚨 步骤 2 — 漏洞报告
使用以下格式列出发现的每一个漏洞:
| # | 漏洞 | OWASP 类别 | 位置 | 严重性 | 可能的利用方式 |
|---|------|------------|------|--------|----------------|
严重性等级(行业标准):
- 🔴 [严重] — 存在立即被利用的风险,可能造成严重损害
- 🟠 [高危] — 严重风险,中等难度即可被利用
- 🟡 [中危] — 在特定条件下可被利用
- 🔵 [低危] — 风险较小,影响有限
- ⚪ [信息性] — 违反最佳实践,无直接可利用性
针对每个漏洞,还需提供一个独立区块:
🔴 VULN #[N] — [漏洞名称]
- OWASP 映射 : 例如,A03:2021 - 注入
- 位置 : 函数名 / 行号引用
- 严重性 : [严重 / 高危 / 中危 / 低危 / 信息性]
- 风险描述 : 攻击者在漏洞被利用后可能实现的操作
- 当前代码 : [存在漏洞的代码片段]
- 修复后代码: [安全替代代码片段]
- 修复说明 : 为何此修复能消除该漏洞
---
⚠️ 步骤 3 — 建议警示
标记那些无法仅通过代码修改解决的安全问题:
| # | 建议 | 类别 | 推荐措施 |
|---|------|------|----------|
类别包括:
- 🔐 密钥管理(例如:硬编码 API 密钥、密码存在于环境变量中)
- 🏗️ 基础设施(例如:强制 HTTPS、防火墙规则)
- 📦 依赖项风险(例如:过时或存在漏洞的库)
- 🔑 认证与访问控制(例如:缺少 MFA、会话策略薄弱)
- 📋 合规性(例如:GDPR、PCI-DSS 考量)
---
🔧 步骤 4 — 加固后的代码
提供完整且经过安全加固的代码重写版本:
- 步骤 2 中的所有漏洞均已完全修复
- 全程应用安全编码最佳实践
- 添加以安全为重点的内联注释,解释每一项安全措施存在的原因
- 符合 PEP8 规范,可用于生产环境
- 不得使用占位符或省略内容 —— 必须为完整代码
- 添加必要的安全导入(例如:secrets、hashlib、bleach、cryptography)
- 在适当情况下使用 Python 3.10+ 特性(如 match-case、typing)
- 安全日志记录(不记录敏感数据)
- 使用现代加密算法(禁用 MD5/SHA1)
- 对所有入口点进行输入验证和清理
---
📊 步骤 5 — 安全摘要卡片
安全评分:
审计前:[X] / 10
审计后:[X] / 10
| 区域 | 审计前 | 审计后 |
|-----------------------|-------------------------|------------------------------|
| 严重问题 | ... | ... |
| 高危问题 | ... | ... |
| 中危问题 | ... | ... |
| 低危问题 | ... | ... |
| 信息性问题 | ... | ... |
| 触及的 OWASP 类别 | ... | ... |
| 应用的关键修复 | ... | ... |
| 提出的建议警示 | ... | ... |
| 整体风险等级 | [严重/高危/中危] | [低危/信息性] |
---
以下是我的 Python 代码:
{{code}}