HTTP security headers help protect AngularJS applications from common web vulnerabilities like Cross-Site Scripting (XSS), Clickjacking, Man-in-the-Middle (MITM) attacks, and data sniffing. Implementing these headers correctly can harden your AngularJS app against security threats.
This guide covers:
- What are HTTP Security Headers?
- Types of Security Headers & Their Implementation
- Content Security Policy (CSP)
- X-Frame-Options
- X-XSS-Protection
- X-Content-Type-Options
- Strict-Transport-Security (HSTS)
- Referrer-Policy
- Permissions-Policy
- Configuring Security Headers in an AngularJS App
- Testing HTTP Security Headers
- Best Practices for Secure AngularJS Apps
1. What Are HTTP Security Headers?
HTTP security headers instruct the browser on how to handle security-related aspects of a webpage. These headers are set by the server and help prevent security threats like:
- Code Injection Attacks (XSS, SQL Injection)
- Clickjacking Attacks
- MIME Type Sniffing Attacks
- Man-in-the-Middle (MITM) Attacks
2. Types of Security Headers & Their Implementation
1๏ธโฃ Content Security Policy (CSP) โ Prevent XSS Attacks
CSP restricts the sources from which scripts, images, styles, and other resources can be loaded.
โ Example Server Configuration (Apache/Nginx/Node.js Express):
Content-Security-Policy: default-src 'self'; script-src 'self' https://trusted.cdn.com
AngularJS Configuration:
Modify the meta
tag in your HTML file:
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self' https://trusted.cdn.com">
๐น Prevents: Injection of malicious scripts (XSS attacks).
๐น Allows: Only trusted sources for JavaScript execution.
2๏ธโฃ X-Frame-Options โ Prevent Clickjacking
X-Frame-Options prevents your app from being embedded inside an iframe to avoid clickjacking attacks.
Example Server Configuration:
X-Frame-Options: DENY
AngularJS Implementation:
To disable iframe embedding:
<meta http-equiv="X-Frame-Options" content="DENY">
๐น Prevents: Clickjacking attacks.
๐น Options:
DENY
โ Blocks all iframes.SAMEORIGIN
โ Allows only iframes from the same origin.ALLOW-FROM https://example.com
โ Allows specific sites.
3๏ธโฃ X-XSS-Protection โ Block XSS Attacks in Older Browsers
Modern browsers already have built-in XSS protection, but this header enforces it.
Example Server Configuration:
X-XSS-Protection: 1; mode=block
๐น Prevents: Execution of malicious scripts in outdated browsers.
๐น Recommended Value: 1; mode=block
4๏ธโฃ X-Content-Type-Options โ Prevent MIME Sniffing
This header stops browsers from guessing file types, reducing the risk of MIME-based attacks.
Example Server Configuration:
X-Content-Type-Options: nosniff
๐น Prevents: Browsers from executing non-JavaScript files as JavaScript.
๐น Always use nosniff
to block MIME sniffing.
5๏ธโฃ Strict-Transport-Security (HSTS) โ Force HTTPS
HSTS enforces HTTPS by blocking HTTP requests and preventing MITM attacks.
Example Server Configuration:
Strict-Transport-Security: max-age=31536000; includeSubDomains; preload
๐น Prevents: Man-in-the-Middle (MITM) attacks.
๐น max-age=31536000 โ Enforces HTTPS for 1 year.
๐น includeSubDomains โ Enforces HTTPS for subdomains.
๐น preload โ Adds the domain to the HSTS preload list.
6๏ธโฃ Referrer-Policy โ Hide Sensitive Referrer Information
Controls how much information is sent in the Referer
header when navigating.
Example Server Configuration:
Referrer-Policy: no-referrer
๐น Prevents: Leakage of sensitive URL data.
๐น Recommended Value: strict-origin-when-cross-origin
(Only send referrer for same-origin requests).
7๏ธโฃ Permissions-Policy โ Restrict Browser Features
Controls access to browser features like camera, microphone, location, geolocation, etc.
Example Server Configuration:
Permissions-Policy: geolocation=(), microphone=()
Prevents: Unauthorized access to userโs hardware capabilities.
3. Configuring Security Headers in an AngularJS App
1. Using Node.js Express to Set Headers
If your AngularJS app is running on an Express backend, use helmet.js
to set security headers.
const helmet = require('helmet');
app.use(helmet());
This automatically sets X-Frame-Options, CSP, HSTS, X-XSS-Protection, etc.
2. Using Apache/Nginx for Security Headers
Modify Apacheโs .htaccess or Nginx configuration:
Header set X-Frame-Options "DENY"
Header set X-XSS-Protection "1; mode=block"
Header set Strict-Transport-Security "max-age=31536000; includeSubDomains; preload"
Header set Referrer-Policy "no-referrer"
Header set Permissions-Policy "geolocation=(), microphone=()"
Ensures security headers are enforced at the server level.
4. Testing HTTP Security Headers
After configuring security headers, test your AngularJS app using:
1. Security Headers Check Online
Use https://securityheaders.com/ to scan your website.
2. Checking in Chrome DevTools
- Open Chrome DevTools (F12).
- Go to Network โ Click on the request.
- Check Response Headers for security headers.
3. Using curl
Command
Run this command to check headers:
curl -I https://yourwebsite.com