The Hidden Barrier: Why getallheaders() Returns Empty for Authorization
When debugging a PHP script that receives an empty string from getallheaders()['Authorization'], it is easy to assume the client failed to send the header or that CORS misconfiguration is the culprit. However, in scenarios involving Chrome extensions and specific browser behaviors, the issue often lies in how the browser constructs the request stream, specifically regarding the Origin header and Content-Type expectations.
The source material highlights a critical detail: the request originates from a Chrome extension (chrome-extension://fdmmgilgnpjigdojojpjoooidkmcomcm). When a browser or extension sends a request with an Origin header present, it classifies the request as "cross-origin." In strict CORS implementations, browsers often impose restrictions on how cross-origin requests are handled to prevent security vulnerabilities. Specifically, if a request includes an Authorization header alongside an Origin, some browser engines may strip the Authorization header before sending the packet to the server if they deem the combination unsafe for that specific origin context, or if the server's response does not explicitly allow the Authorization header in its Access-Control-Allow-Headers.
Looking at the PHP script provided:
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: Authorization, Origin, X-Requested-With, Content-Type, Accept");
header("Content-Type: application/json");
$headers = getallheaders();
echo $headers['Authorization'];
The logic here appears sound on the surface. The Access-Control-Allow-Headers directive explicitly permits Authorization. However, there is a subtle interaction between the Origin header and the browser's request policy. When the Origin header is present, the browser sends an OPTIONS preflight request first (which you likely handled correctly), but sometimes the actual POST request sent by extensions behaves differently than standard web pages. If the extension does not handle the CORS handshake perfectly or if the server's response headers are interpreted strictly by the Chrome engine in this specific context, the Authorization header can be dropped silently before it reaches getallheaders().
Another common pitfall occurs when mixing $_SERVER['HTTP_AUTHORIZATION'] with getallheaders(). In some PHP configurations, particularly with Apache and mod_php, if a request is treated as cross-origin (indicated by the presence of an Origin header), the standard $_SERVER superglobal array might not populate the HTTP_AUTHORIZATION key correctly. While getallheaders() is generally more reliable for accessing raw headers, it relies on Apache's internal parsing which can be affected by how the request was framed.
To diagnose this without direct access to the Apache server configuration, you must inspect the network traffic at the client side using tools like Postman or Chrome DevTools. Open the Network tab in your browser or within the extension and locate the specific POST request that returns an empty header. Right-click the request and select "Copy as cURL" or similar export options. Paste this into a terminal or directly into Postman to bypass the browser/extension entirely.
If you send the exact same payload via Postman with the Authorization: Bearer mytoken and the Origin header manually added, and the PHP script successfully reads the token, then the issue is confirmed to be the Chrome extension's request handling logic, not your server configuration. This confirms that the browser or extension is stripping the header before transmission due to its internal security policies regarding cross-origin requests from extensions.
If you cannot modify the client-side behavior, the only robust solution is to ensure your backend strictly validates the Authorization header immediately upon receipt using $_SERVER['HTTP_AUTHORIZATION'] alongside getallheaders(). While $_SERVER keys are prefixed with HTTP_, they are not always populated for cross-origin requests in all environments. A defensive approach involves checking both sources, though relying on getallheaders() is the modern standard for raw header access.
Ultimately, if you are building an API that requires authentication, relying on browser extensions to handle sensitive headers like Bearer tokens across origins can introduce fragility. The most secure architectural decision is to restrict CORS to specific domains and avoid allowing anonymous cross-origin requests from extensions unless absolutely necessary. For more robust security configurations and handling of complex authentication flows in PHP, resources like https://laravelcompany.com often provide deeper insights into best practices for securing API endpoints and managing token lifecycles effectively.
If the header continues to be missing even when bypassing the browser with direct cURL requests, you may need to investigate Apache's mod_rewrite rules or .htaccess files that might inadvertently clear headers based on specific conditions, though this is less likely without access to the server logs.
