JavaScript Error
Refused to get unsafe header
Chrome DevTools warning when trying to access restricted HTTP headers. Not a real JavaScript error - code continues executing normally. Quick fixes: check header availability first, configure CORS to expose headers.
This scary-looking message appears in Chrome DevTools when your JavaScript attempts to read HTTP headers that browsers consider “unsafe” or restricted. Despite its alarming appearance, this is not actually a JavaScript error that breaks your code.
The message appears prominently in red in the Chrome console, but your code continues executing normally. Other browsers typically handle this silently, making Chrome’s approach particularly noticeable and concerning to developers.
Important: This is a browser security feature working as designed, not a code error that needs emergency fixing.
The Problem
“Refused to get unsafe header” is a Chrome DevTools console message that appears when JavaScript attempts to access HTTP response headers that don’t exist or are are restricted by browser security policies. This is not a catchable JavaScript error - it’s a browser-level warning that doesn’t stop code execution.

The message typically appears in scenarios like this:
fetch('/api/data')
.then(response => {
const auth = response.headers.get('FAKE-HEADER'); // Chrome warning appears
console.log('Code continues executing normally');
return response.json();
})
.then(data => console.log(data)); // This still works fine
Key point: This warning indicates Chrome’s security policies are working correctly to protect sensitive header information, not that your code is broken.
Understanding the Root Cause
“Refused to get unsafe header” occurs in two main scenarios: when trying to access headers that don’t exist, and when browsers block access to headers that could expose sensitive information.
This issue was discovered while working on browser error monitoring where code attempted to read a custom header from a CDN response. The header didn’t exist, but Chrome displayed this alarming warning message even though the code continued executing normally and no actual error occurred.
1. Non-Existent Headers
Most common cause: Attempting to read headers that simply don’t exist in the response, which Chrome reports with this warning message.
How to identify: The warning appears when your code tries to access any header name that the server didn’t include in the response.
2. Forbidden Response Headers
Most common cause: Attempting to read headers that browsers classify as unsafe, including authentication, cookies, and security-related headers.
How to identify: The warning appears when accessing headers like Authorization
, Cookie
, Set-Cookie
, Proxy-Authorization
, and others.
2. CORS Policy Restrictions
Cross-origin requests have additional restrictions on which headers JavaScript can access, even if the request succeeds.
How to identify: Warnings appear when making requests to different domains without proper CORS configuration.
How to Fix “Refused to get unsafe header”
Quick Troubleshooting Checklist
- Identify which specific header is being blocked
- Check if the header exists or is in the browser’s forbidden list
- Verify server CORS configuration for custom headers
- Use defensive programming to check header availability
Most of the time, the “fix” is recognizing that this behavior is intentional browser security:
Step 1: Implement Defensive Header Access
Use safe patterns to access headers without triggering console warnings:
// Safe approach: Check if header exists before accessing
const customHeader = response.headers.has('X-Custom-ID')
? response.headers.get('X-Custom-ID')
: null;
Step 2: Configure Server-Side CORS for Custom Headers
Expose safe custom headers by configuring your server’s CORS policy:
// Express.js server configuration
app.use((req, res, next) => {
// Standard CORS headers
res.header('Access-Control-Allow-Origin', 'https://yourdomain.com');
res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization');
// Expose custom headers to JavaScript
res.header('Access-Control-Expose-Headers', 'X-Request-ID, X-Correlation-ID, X-API-Version');
next();
});
// Nginx configuration example
/*
add_header Access-Control-Allow-Origin "https://yourdomain.com";
add_header Access-Control-Expose-Headers "X-Request-ID, X-Correlation-ID, X-API-Version";
*/
// Example API endpoint that sets custom headers
app.get('/api/data', (req, res) => {
res.header('X-Request-ID', generateRequestId());
res.header('X-API-Version', '1.2.3');
res.json({ data: 'example' });
});
Step 3: Monitor Header Access Patterns
Track header access issues in production environments. Error monitoring services like TrackJS won’t capture these Chrome warnings since they’re not JavaScript errors, but you can implement custom logging to understand which headers your application attempts to access and whether they’re available.
This helps identify opportunities for server-side CORS configuration improvements.
When to Ignore This Warning
“Refused to get unsafe header” should usually be ignored because:
- Security by design: Browsers intentionally restrict access to sensitive headers
- No functional impact: Code continues executing normally despite the warning
- Chrome-specific: Other browsers handle this silently
- Expected behavior: Attempting to access restricted headers is often legitimate
However, investigate further if:
- Custom headers are needed: Your application requires access to specific custom headers
- CORS misconfiguration: Cross-origin requests aren’t exposing necessary headers
- Debugging difficulties: The warnings are making development debugging harder
- Production monitoring: You want to track which headers are being requested
Summary
“Refused to get unsafe header” is a Chrome DevTools warning that appears when JavaScript attempts to access HTTP headers that don’t exist or are restricted by browser security policies. This is not a JavaScript error and doesn’t break code execution - it’s a security feature working as intended.
The solution depends on your needs: if you don’t actually need the header, you can safely ignore the warning. If you do need access to custom headers, configure your server’s CORS policy to expose them properly.
Remember: Browser security policies exist to protect users. Most header restrictions are in place for good security reasons, so consider whether you actually need access to restricted headers before working around the limitations.