tinycodinglm / code /bad006.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 12)
3c88a0f verified
|
Raw History Blame Contribute Delete
2.27 kB
deepseek-harness trace
System You are an expert software engineer and technical assistant. You provide accurate, detailed, and practical answers. When writing code, use best practices, proper error handling, and clear variable names. When analyzing c...

You are an expert software engineer and technical assistant. You provide accurate, detailed, and practical answers. When writing code, use best practices, proper error handling, and clear variable names. When analyzing code, identify bugs, security issues, and performance problems. You have deep knowledge of C/C++, Windows API, Python, and many other technologies.

User

What are common security anti-patterns in code?

Assistant

Critical Mistakes:

  1. Hardcoded Secrets:
python
# NEVER DO THIS
API_KEY = "sk-1234567890abcdef"
DB_PASSWORD = "admin123"
SECRET_KEY = "supersecret"

# Good: environment variables
import os
API_KEY = os.getenv("API_KEY")
  1. SQL Injection:
python
# Bad: string formatting
query = f"SELECT * FROM users WHERE name = '{name}'"

# Good: parameterized queries
query = "SELECT * FROM users WHERE name = %s"
cursor.execute(query, (name,))
  1. XSS (Cross-Site Scripting):
html
<!-- Bad: directly inserting user input -->
<div>{{ user_input }}</div>

<!-- Good: escape output -->
<div>{{ user_input|escape }}</div>
  1. Weak Password Hashing:
python
# Bad
import hashlib
hashed = hashlib.md5(password).hexdigest()

# Good
from bcrypt import hashpw, gensalt
hashed = hashpw(password.encode(), gensalt())
  1. No Input Validation:
python
# Bad: trusting user input
age = int(request.form['age'])

# Good: validate and sanitize
age = request.form.get('age', type=int)
if age is None or age < 0 or age > 150:
    raise ValueError("Invalid age")
  1. Insecure Deserialization:
python
# Bad: never unpickle untrusted data
import pickle
data = pickle.loads(user_input)

# Good: use safe formats like JSON
import json
data = json.loads(user_input)
  1. Logging Secrets:
python
# Bad
logger.info(f"User {username} logged in with password {password}")

# Good
logger.info(f"User {username} login attempt")