Puppeteer request interception credential masking without exposing Authorization header
0 reputation · 18 Jan 2021, 05:48 UTC
0 reputation · 18 Jan 2021, 05:48 UTC
The goal is to prevent an Authorization header from reaching a protected endpoint while keeping the header value inaccessible to the page’s JavaScript context during intercepted requests.
When `page.setRequestInterception(true)` is enabled, the interception listener can read `request.headers()` and thus see the Authorization header, which risks leaking credentials in test logs or through unintended exposure. There is no built‑in API to modify or remove the header without first reading it, and Chrome’s network stack applies different security checks in headless versus headed modes, leading to inconsistent header values.
Is there a supported method to strip or replace the Authorization header in intercepted requests without exposing its value to the page context? Does the observed behavior differ between headless and headed modes, and how can we verify that masking works consistently? Are there Chrome flags or Puppeteer options that provide a secure way to handle credentials for request interception?
29275 reputation · 18 Jan 2021, 16:14 UTC
The most secure method is to intercept the request, clone the headers object, delete the sensitive key, and then resume the request using `request.continue(). This ensures the browser never sees the original credential for that specific network request.
await page.setRequestInterception(true);
page.on('request', async (request) => {
const headers = request.headers();
// Check for the authorization header before attempting modification
if (headers['authorization']) {
// Create a shallow copy to avoid mutating the original request object unexpectedly
const maskedHeaders = { ...headers };
// Strip the sensitive credential
delete maskedHeaders['authorization'];
// Continue the request with the modified headers set
request.continue({ headers: maskedHeaders });
} else {
request.continue();
}
});
The behavior of request interception is generally consistent between headless and headed modes because both utilize the same Chrome DevTools Protocol (CDP) implementation. However, Chrome may sometimes normalize header keys (e.g., lowercase `authorization`) depending on the version and specific request type. To ensure consistent masking, always perform header key checks in a case-insensitive manner within your interceptor logic.
To verify that masking is working correctly and not leaking, follow these checks:
page.on('request', ...) to log the headers being sent to request.continue(). Ensure the authorization key is absent in the Node.js console logs.page.evaluate(() => window....) (if using a custom proxy script). If the masking is successful, the page should have no reference to the intercepted headers.Assumptions: This solution assumes you are using Puppeteer v19+. If you are using a significantly older version, the request.continue syntax may vary slightly, but the logic of header manipulation remains identical.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.