Skip to content

HTTP Security

While HTTP itself is a simple protocol, its use in web applications involves numerous security considerations. Modern web security largely revolves around using HTTPS and implementing various defense mechanisms at the application and protocol levels.

HTTPS (HTTP Secure) is no longer optional; it is essential. It encrypts HTTP messages using Transport Layer Security (TLS), providing:

  • Confidentiality: Protects against eavesdropping by encrypting the data in transit.
  • Integrity: Ensures that data has not been tampered with during transmission.
  • Authentication: Verifies the identity of the web server (and optionally the client) through digital certificates.

Browsers now actively flag non-HTTPS sites as “Not Secure.” Using HTTPS protects against Man-in-the-Middle (MitM) attacks, data sniffing, and content injection.

Servers can send specific HTTP response headers to instruct browsers to enable security features:

  • Strict-Transport-Security (HSTS): Strict-Transport-Security: max-age=31536000; includeSubDomains; preload Instructs browsers to only communicate with the server over HTTPS for a specified duration, preventing protocol downgrade attacks.
  • Content-Security-Policy (CSP): Content-Security-Policy: default-src 'self'; script-src 'self' trusted.cdn.com; img-src *; Helps prevent Cross-Site Scripting (XSS) and other injection attacks by defining allowed sources for content (scripts, styles, images, etc.).
  • X-Content-Type-Options: X-Content-Type-Options: nosniff Prevents browsers from MIME-sniffing a response away from the declared Content-Type, reducing risks from user-uploaded content that might be misinterpreted as executable.
  • X-Frame-Options: X-Frame-Options: DENY or X-Frame-Options: SAMEORIGIN Protects against clickjacking attacks by controlling whether the page can be embedded in <frame>, <iframe>, <embed>, or <object> elements.
  • Referrer-Policy: Referrer-Policy: strict-origin-when-cross-origin or Referrer-Policy: no-referrer Controls how much referrer information (the URL of the previous page) is sent with requests.
  • Permissions-Policy (formerly Feature-Policy): Permissions-Policy: geolocation=(), camera=(), microphone=() Allows fine-grained control over which browser features (e.g., camera, microphone, geolocation) can be used by the page and its iframes.
  • Modern Authentication: Instead of Basic/Digest authentication (which transmit credentials with each request), modern applications often use token-based authentication (e.g., JWTs, OAuth 2.0 bearer tokens). These tokens are typically sent in the Authorization: Bearer <token> request header.
  • Secure Token Handling: Tokens should be short-lived, transmitted only over HTTPS, and stored securely on the client (e.g., HttpOnly, Secure cookies for web; secure storage for mobile apps).
  • Password Security: Never store passwords in plain text. Use strong, salted hashing algorithms (e.g., Argon2, scrypt, bcrypt).
  • Session Management: Use secure session identifiers, regenerate session IDs upon login, and provide mechanisms for session invalidation (logout).

While not strictly HTTP features, these are crucial for application security:

  • Input Validation: Always validate and sanitize all data received from clients (URL parameters, request bodies, headers) on the server-side to prevent injection attacks (SQLi, XSS, command injection).
  • Output Encoding: Encode data appropriately before rendering it in user interfaces to prevent XSS. For example, HTML-encode data displayed in HTML, URL-encode data in URLs, etc.

CORS is an HTTP-header based mechanism that allows a server to indicate any origins (domain, scheme, or port) other than its own from which a browser should permit loading resources. Misconfigured CORS policies (e.g., Access-Control-Allow-Origin: * for authenticated resources) can lead to security vulnerabilities.

When using cookies for session management or storing sensitive information, use security attributes:

  • Secure attribute: Ensures cookies are only sent over HTTPS.
  • HttpOnly attribute: Prevents client-side JavaScript from accessing the cookie, mitigating XSS.
  • SameSite attribute (Strict, Lax, None): Helps protect against Cross-Site Request Forgery (CSRF) attacks by controlling when cookies are sent with cross-origin requests. SameSite=None requires the Secure attribute.
  • Path Traversal: Sanitize user-supplied file paths to prevent access to unintended files on the server (e.g., disallow ../).
  • DNS Spoofing: While largely a network-level concern, applications should be aware that DNS can be manipulated. Using DNSSEC can help.
  • Open Redirects: If your application redirects to a URL provided by the user, validate that URL to prevent redirection to malicious sites.
  • Information Disclosure: Avoid verbose error messages, server version banners (Server header), or stack traces in production, as they can reveal exploitable information.
  • Subresource Integrity (SRI): <script src="..." integrity="sha384-oqVuAfXRKap7fdgcCY5uykM6+R9GqQ8K/uxy9rx7HNQlGYl1kPzQho1wx4JwY8wC" crossorigin="anonymous"></script> Ensures that externally hosted resources (like CDN-hosted JavaScript/CSS) have not been tampered with by providing a cryptographic hash.

Security is an ongoing process. Regularly update dependencies, conduct security audits, and stay informed about new threats and best practices.