š 4.8 Application Security Issues
Overview
Application security involves protecting secrets, encrypting data in transit and at rest, and handling data securely.
1. Secret Protection
š Managing Secrets Securely
Secrets include API keys, passwords, tokens, and certificates that must be protected.
# ā Hardcoded secrets
API_KEY = "sk_live_1234567890abcdef"
DB_PASSWORD = "MyP@ssw0rd123"
# ā Secrets in Git
git add config.py
# ā Secrets in Docker image
ENV DB_PASSWORD=secret123
# ā Logging secrets
print(f"Password: {password}")# ā
Use environment variables
import os
API_KEY = os.getenv('API_KEY')
DB_PASSWORD = os.getenv('DB_PASSWORD')
# ā
Use .env files (add to .gitignore)
from dotenv import load_dotenv
load_dotenv()
# ā
Secret management tools
# - HashiCorp Vault
# - AWS Secrets Manager
# - Azure Key Vault2. Encryption
š Encryption in Storage and Transport
A. Encryption in Transit
Protects data while moving over the network.
| Protocol | Port | Use Case |
|---|---|---|
| HTTPS/TLS | 443 | Web traffic, REST APIs |
| SSH | 22 | Secure remote access |
| VPN | Various | Encrypted tunnel |
# Python - Enforce HTTPS
import requests
# ā
Always use HTTPS
response = requests.get('https://api.example.com/data')
# ā
Verify SSL
response = requests.get('https://api.example.com', verify=True)B. Encryption at Rest
Protects stored data.
# Python - Encryption with Fernet from cryptography.fernet import Fernet key = Fernet.generate_key() cipher = Fernet(key) # Encrypt encrypted = cipher.encrypt(b"Sensitive data") # Decrypt decrypted = cipher.decrypt(encrypted) # Password Hashing (bcrypt) import bcrypt password = b"user_password" salt = bcrypt.gensalt() hashed = bcrypt.hashpw(password, salt)
3. Data Handling
šļø Secure Data Handling
# ā
Input Validation
import re
def validate_email(email):
pattern = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'
return bool(re.match(pattern, email))
# ā
Parameterized queries (prevent SQL injection)
import sqlite3
cursor.execute("SELECT * FROM users WHERE id = ?", (user_id,))
# ā
Escape HTML (prevent XSS)
import html
safe_output = html.escape(user_input)š 4.9 Infrastructure Components
Overview
Modern applications rely on infrastructure components for routing, security, and high availability.
1. Firewall
š”ļø Network Security Barrier
Controls incoming and outgoing network traffic based on security rules.
Functions
- Packet Filtering: Allow/deny based on IP, port, protocol
- Stateful Inspection: Track connection states
- NAT: Network Address Translation
- IPS/IDS: Intrusion prevention/detection
# Firewall Rules Example # Allow HTTPS from anywhere Source: 0.0.0.0/0 Port: 443 Protocol: TCP Action: ALLOW # Allow SSH from admin network only Source: 10.0.0.0/8 Port: 22 Protocol: TCP Action: ALLOW # Deny all other traffic Source: 0.0.0.0/0 Port: * Protocol: * Action: DENY
2. DNS (Domain Name System)
š Domain Name Resolution
Translates domain names to IP addresses.
DNS Record Types
| Record | Purpose | Example |
|---|---|---|
| A | IPv4 address | example.com ā 192.0.2.1 |
| AAAA | IPv6 address | example.com ā 2001:db8::1 |
| CNAME | Alias | www.example.com ā example.com |
3. Load Balancer
āļø Traffic Distribution
Distributes incoming traffic across multiple servers.
Algorithms
| Algorithm | Description |
|---|---|
| Round Robin | Distribute sequentially |
| Least Connections | Send to server with fewest connections |
| IP Hash | Based on client IP |
# Nginx Load Balancer
upstream backend {
least_conn;
server app1.example.com:8080;
server app2.example.com:8080;
server app3.example.com:8080;
}
server {
listen 80;
location / {
proxy_pass http://backend;
}
}4. Reverse Proxy
š Gateway to Backend
Forwards client requests to appropriate backend servers.
Functions
- Request Routing: Route based on URL path
- SSL Termination: Handle HTTPS
- Caching: Cache static content
- Compression: Gzip responses
# Nginx Reverse Proxy
server {
listen 80;
location /api/ {
proxy_pass http://api-backend:5000/;
}
location /admin/ {
proxy_pass http://admin-backend:3000/;
}
location / {
proxy_pass http://frontend:8080/;
}
}š Interactive Python Interpreter
- Write Python code in the editor
- Click "ā¶ Run Code" to execute
- Try sample code buttons