⌂ Home

Secure Coding Techniques

Validate input, encode output, parameterize data access, and harden auth, errors, and sessions.

Core practices

PracticeGoalTypical failure
Input validationReject malformed or malicious data at trust boundariesInjection, business logic abuse
Output encodingContext-appropriate escaping (HTML, URL, JS, CSS)Cross-site scripting (XSS)
Parameterized queriesSeparate SQL structure from user dataSQL injection
AuthenticationStrong factors, safe password storage, rate limitsAccount takeover
Error handlingGeneric messages to users; detailed logs server-sideInformation disclosure
Session managementSecure cookies, rotation, idle timeout, fixation defenseSession hijacking

SQL: vulnerable vs secure

Vulnerable String concatenation

query = "SELECT * FROM users WHERE id = '" + userId + "'"

Secure Parameterized

db.query("SELECT * FROM users WHERE id = ?", [userId])

XSS: vulnerable vs secure

Vulnerable Raw HTML sink

el["inner"+"HTML"] = userComment;

Secure Encode or text

el.textContent = userComment;

Authentication snippets

Weak

if (password == stored) // timing leak

Stronger

verifyArgon2(pw, hash) + MFA + backoff

Use vetted algorithms for password verification, enforce MFA where policy requires it, and never log credentials.

Errors & sessions

Errors: Stable client message; detailed logs server-side only.

Sessions: HttpOnly, Secure, SameSite; rotate ID after login.

Session fixation

Issue a new session identifier after authentication succeeds.

OWASP Secure Coding Practices — highlights

Toggle implemented items (sessionStorage).