--- name: mandatory-secure-web-skills description: Mandatory secure coding rules and vulnerability prevention for web applications covering input validation, output encoding, session management, and auth. license: Apache-2.0 --- # Mandatory Secure Web Coding Rules This document defines mandatory secure coding rules for web applications to prevent critical vulnerabilities (SQL injection, XSS, CSRF, insecure session management, path traversal, etc.). --- ## 1. Secure Coding Guidelines * **Input Validation & Sanitization:** Treat all external data as untrusted. Validate inputs against an allow-list of expected types, lengths, and formats to prevent SQL injection and cross-site scripting (XSS). * **Output Encoding:** Convert data into a safe format before sending it to the client. This ensures that browsers treat potentially malicious scripts as plain text rather than executable code. * **Authentication & Authorization:** Require strong authentication for non-public pages. Use modern protocols like OAuth 2.0 or OpenID Connect to verify users, and JSON Web Tokens (JWT) for secure, stateless session management. Never hardcode JWT secrets or API keys in source code. * **Password Management:** Use established libraries for password hashing (e.g., Argon2 or bcrypt) with unique salts, and never store passwords in plaintext. * **Principle of Least Privilege:** Grant users and processes only the minimum permissions necessary to perform their functions. Use Role-Based Access Control (RBAC) to manage these permissions centrally. * **Secure Session Management:** Generate cryptographically strong, random session identifiers on the server. Protect these tokens by using the `HttpOnly`, `Secure`, and `SameSite` flags on cookies and enforcing short inactivity timeouts. * **Secure Architecture (BFF Pattern):** Use a Backend-for-Frontend (BFF) to allow the frontend to communicate with a secure, server-side BFF layer rather than directly with public APIs, keeping sensitive keys off the client. * **Path & File System Security:** Never trust user input (including uploaded filenames and extracted file metadata like ID3 tags/formats) in file paths. Sanitize inputs (e.g., using `path.basename()`) to strip traversal sequences (`../`, `..\`) before passing them to file system sinks. Avoid custom sanitization like `split('/')` which fails against Windows backslashes. When restricting access to specific directories, resolve paths fully and strictly verify the directory boundary (e.g., enforce a trailing slash when using prefix checks) to prevent partial matching bypasses. * **System Command Execution:** Do not pass unvalidated user input directly to execution sinks (`exec`, `spawn`, etc.). Always validate binary paths and arguments against a strict, hardcoded allow-list. Ensure execution directories are also strictly verified against partial matching bypasses. * **Error Handling & Logging:** Display generic error messages to users while logging detailed diagnostic information securely for developers. Ensure logs do not contain sensitive data like passwords or session tokens. * **Data Encryption:** Encrypt sensitive data both at rest and in transit (using TLS 1.2 or higher). * **Fail Safe:** When something fails, fail close (deny access). * **Cryptography:** Use established libraries and secure primitives. Use authenticated encryption and secure cryptographic hashes. Use secure PRNGs from the OS. --- ## 2. Secure Web Frontend (XSS Prevention) ### Framework-Native Escaping * **Do not trust** data from databases, configuration files, uploaded files, or user requests that the user may control directly or indirectly. * **Escape or validate** untrusted data in all outgoing HTML, JavaScript, CSS, and HTTP headers. * **Rely on framework-native auto-escaping** (e.g., React JSX, Angular interpolation). * **Always quote HTML attributes** when using variables in templates to prevent attribute breakout. * *Vulnerable:* `