Spaces:
Sleeping
Sleeping
File size: 7,179 Bytes
b4deece | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 | import json
import random
# 1. Define 5 clean Python code snippets
SNIPPETS = [
{
"id": "login_func",
"description": "Standard login function with parameterized database query",
"code": """def login(username, password):
# Secure parameterized query
query = "SELECT * FROM users WHERE username = ? AND password = ?"
user = db.execute(query, (username, password))
if user:
return True
return False"""
},
{
"id": "money_transfer",
"description": "Transfer money between accounts with balance check",
"code": """def transfer(from_account, to_account, amount):
if from_account.balance >= amount:
from_account.balance -= amount
to_account.balance += amount
return True
return False"""
},
{
"id": "password_saver",
"description": "Save a password securely after hashing",
"code": """import hashlib
def save_password(password):
# Hash password before saving
hashed = hashlib.sha256(password.encode()).hexdigest()
db.save_user_password(hashed)"""
},
{
"id": "find_max",
"description": "Find the maximum value in a list",
"code": """def find_max(numbers):
if not numbers:
return None
max_val = numbers[0]
for i in range(len(numbers)):
if numbers[i] > max_val:
max_val = numbers[i]
return max_val"""
},
{
"id": "word_counter",
"description": "Count occurrences of words in a string",
"code": """def count_words(text):
words = text.split()
counts = {}
for word in words:
counts[word] = counts.get(word, 0) + 1
return counts"""
}
]
# 2. Define 4 bug injector functions
def inject_off_by_one(snippet):
"""Changes range(len(x)) to range(len(x) - 1)"""
if "range(len(" in snippet["code"]:
# Find the specific range pattern
import re
pattern = r"range\(len\((.*?)\)\)"
if re.search(pattern, snippet["code"]):
buggy_code = re.sub(pattern, r"range(len(\1) - 1)", snippet["code"])
return {
"snippet_id": snippet["id"],
"buggy_code": buggy_code,
"bug": {
"type": "logic",
"severity": 3,
"description": "Off-by-one error: the loop range is shortened, so the last element will be skipped.",
"line_hint": None # Will calculate in build
}
}
return None
def inject_sql_injection(snippet):
"""Changes parameterized query to f-string interpolation"""
if snippet["id"] == "login_func" and "SELECT" in snippet["code"]:
buggy_code = snippet["code"].replace(
'query = "SELECT * FROM users WHERE username = ? AND password = ?"',
'query = f"SELECT * FROM users WHERE username = \'{username}\' AND password = \'{password}\'"'
).replace(
'user = db.execute(query, (username, password))',
'user = db.execute(query)'
)
return {
"snippet_id": snippet["id"],
"buggy_code": buggy_code,
"bug": {
"type": "security",
"severity": 5,
"description": "SQL Injection vulnerability: using f-strings for database queries allows attackers to execute arbitrary SQL.",
"line_hint": None
}
}
return None
def inject_wrong_operator(snippet):
"""Changes >= to > in a balance check"""
if "balance >= amount" in snippet["code"]:
buggy_code = snippet["code"].replace("balance >= amount", "balance > amount")
return {
"snippet_id": snippet["id"],
"buggy_code": buggy_code,
"bug": {
"type": "logic",
"severity": 4,
"description": "Logic error: balance check uses '>' instead of '>=', preventing transfers when balance matches exactly.",
"line_hint": None
}
}
return None
def inject_plain_password(snippet):
"""Removes hashing so password is stored as plain text"""
if "hashlib.sha256" in snippet["code"]:
lines = snippet["code"].splitlines()
new_lines = []
for line in lines:
if "import hashlib" in line:
continue
if "hashed =" in line:
new_lines.append(line.replace("hashlib.sha256(password.encode()).hexdigest()", "password"))
continue
if "db.save_user_password(hashed)" in line:
new_lines.append(line)
continue
new_lines.append(line)
buggy_code = "\n".join(new_lines)
return {
"snippet_id": snippet["id"],
"buggy_code": buggy_code,
"bug": {
"type": "security",
"severity": 5,
"description": "Security flaw: password is stored in plain text without any hashing mechanism.",
"line_hint": None
}
}
return None
def get_line_hint(original, buggy):
"""Helper to find the first line number that differs"""
orig_lines = original.splitlines()
bug_lines = buggy.splitlines()
for i, (o, b) in enumerate(zip(orig_lines, bug_lines)):
if o != b:
return i + 1
return 1
# 3. Define build_bug_bank()
def build_bug_bank():
bank = []
injectors = [
inject_off_by_one,
inject_sql_injection,
inject_wrong_operator,
inject_plain_password
]
episode_counter = 1
for snippet in SNIPPETS:
for inject in injectors:
result = inject(snippet)
if result:
# Add metadata
result["episode_id"] = f"ep_{episode_counter:03d}"
# Calculate line hint if not provided
if result["bug"]["line_hint"] is None:
result["bug"]["line_hint"] = get_line_hint(snippet["code"], result["buggy_code"])
bank.append(result)
episode_counter += 1
return bank
# 4. Define save_bank()
def save_bank(bank):
episodes = []
answers = []
for entry in bank:
# What the AI sees
episodes.append({
"episode_id": entry["episode_id"],
"snippet_id": entry["snippet_id"],
"code": entry["buggy_code"]
})
# The private answer key
answers.append({
"episode_id": entry["episode_id"],
"snippet_id": entry["snippet_id"],
"bug": entry["bug"]
})
with open("episodes.json", "w") as f:
json.dump(episodes, f, indent=4)
with open("answers.json", "w") as f:
json.dump(answers, f, indent=4)
# 5. Main block
if __name__ == "__main__":
print("Building Bug Bank...")
bug_bank = build_bug_bank()
save_bank(bug_bank)
print(f"Success! Created {len(bug_bank)} training episodes.")
print("Files saved: episodes.json, answers.json")
|