HSTS - ayosecu/security-terms GitHub Wiki
HSTS (HTTP Strict Transport Security) is a web security policy mechanism that enforces the use of HTTPS (HTTP Secure) for communication between a browser and a web server. It helps prevent certain attacks, such as man-in-the-middle (MITM) attacks and protocol downgrade attacks, by ensuring that browsers only interact with the website over secure connections.
HSTS is implemented using the Strict-Transport-Security HTTP response header. Once this header is received, the browser will:
- Force HTTPS: Automatically upgrade all HTTP requests to HTTPS for the domain.
- Refuse Insecure Connections: Reject any attempts to connect over HTTP.
- Syntax:
Strict-Transport-Security: max-age=<seconds>; includeSubDomains; preload
- max-age:
- Specifies the duration (in seconds) that the browser should enforce the HSTS policy.
- Example:
Strict-Transport-Security: max-age=31536000
- Enforces HTTPS for 1 year (31,536,000 seconds).
- includeSubDomains (optional):
- Applies the HSTS policy to all subdomains of the main domain.
- Example:
- If set on example.com, it will also enforce HTTPS for sub.example.com.
- preload (optional):
- Indicates the domain should be included in the HSTS Preload List, a list maintained by browsers to enforce HSTS before the first connection.
- Requires both includeSubDomains and a max-age of at least 1 year.
Strict-Transport-Security: max-age=31536000; includeSubDomains; preload
- Prevents Protocol Downgrade Attacks:
- Attackers can force users to downgrade to HTTP, but HSTS ensures HTTPS is always used.
- Mitigates MITM Attacks:
- Ensures data integrity and encryption, reducing the risk of attackers intercepting or modifying traffic.
- Enhances User Trust:
- Demonstrates a commitment to secure communication, improving user confidence.
- Initial HTTP Request Is Vulnerable:
- HSTS cannot protect the very first HTTP request if a user types http://example.com.
- Solution: Redirect HTTP to HTTPS and add the domain to the HSTS Preload List.
- Strict Enforcement Can Cause Issues:
- If misconfigured or applied to non-HTTPS domains, users may be unable to access the site.
- Example:
- Accidentally including includeSubDomains for a subdomain without HTTPS support.
- Browser-Specific:
- Only works with browsers that support HSTS (modern browsers generally do).
- What It Is:
- A list of domains that enforce HSTS before any connection is made.
- Maintained by major browsers (e.g., Chrome, Firefox, Edge).
- How to Apply:
- Ensure HSTS is enabled with max-age=31536000, includeSubDomains, and preload.
- Submit the domain at HSTS Preload.
- Once accepted, all major browsers enforce HSTS for the domain.
- Examples:
- Google: Implements HSTS on all its domains, including www.google.com and subdomains.
- Facebook: Enforces HTTPS through HSTS for its entire ecosystem.
- Banks and Financial Services: Commonly use HSTS to ensure secure communication.
- Protecting login pages and sensitive user data.
- Enforcing HTTPS across corporate and e-commerce platforms.
- Inspect the HTTP response headers in browser developer tools:
- Open DevTools > Network > Look for the Strict-Transport-Security header.
- Test HSTS implementation using:
- Use curl to view headers:
curl -I https://example.com
- Start with a Small max-age:
- Use a small value initially to test the impact (e.g., max-age=86400 for 1 day).
- Gradually Increase Enforcement:
- Once confident, increase the max-age and add includeSubDomains.
- Prepare for Preloading:
- Ensure all subdomains support HTTPS before adding the preload directive.
- Redirect HTTP Traffic:
- Always redirect HTTP to HTTPS before applying HSTS.
- Monitor Traffic:
- Ensure no critical resources are served over HTTP.
Aspect | Details |
---|---|
What is HSTS? | A policy enforcing HTTPS connections to protect against MITM and downgrade attacks. |
Key Header | Strict-Transport-Security. |
Main Directives | max-age, includeSubDomains, preload. |
Benefits | Prevents insecure connections, enhances security, and improves trust. |
Challenges | Initial HTTP vulnerability, misconfiguration risks. |
Popular Users | Google, Facebook, financial institutions. |
HSTS is a crucial tool for securing web communication, enforcing HTTPS across domains, and preventing vulnerabilities associated with unencrypted connections. Proper implementation, monitoring, and consideration of the HSTS Preload List can significantly enhance a website’s security posture.