Crawlee—A web scraping and browser automation library for Node.js to build reliable crawlers. In JavaScript and TypeScript. Extract data for AI, LLMs, RAG, or GPTs. Download HTML, PDF, JPG, PNG, and other files from websites. Works with Puppeteer, Playwright, Cheerio, JSDOM, and raw HTTP. Both headful and headless mode. With proxy rotation.
TypeScript apifyautomationcrawlercrawlingheadlessheadless-chromejavascriptnodejsnpmplaywrightpuppeteerscraperscrapingtypescriptweb-crawlerweb-crawlingweb-scraping
fix(http-client): do not forward credentials across cross-origin redirects (#4137)
Suliman Abdulrazzaqf13d4990e850
diff --git a/packages/http-client/src/base-http-client.ts b/packages/http-client/src/base-http-client.tsindex 9b13ad047..2d4c1d0e7 100644--- a/packages/http-client/src/base-http-client.ts+++ b/packages/http-client/src/base-http-client.ts@@ -170,6 +170,13 @@ export abstract class BaseHttpClient implements BaseHttpClientInterface {const nextHeaders = new Headers();currentRequest.headers.forEach((value, key) => nextHeaders.set(key, value));+ // Like native `fetch`, do not forward credentials to a different origin+ if (nextUrl.origin !== new URL(currentRequest.url).origin) {+ for (const header of ['authorization', 'proxy-authorization', 'cookie']) {+ nextHeaders.delete(header);+ }+ }+return new Request(nextUrl.toString(), {method: nextMethod,headers: nextHeaders,diff --git a/test/core/base-http-client.test.ts b/test/core/base-http-client.test.tsindex 5e4084ffc..2edc199e7 100644--- a/test/core/base-http-client.test.ts+++ b/test/core/base-http-client.test.ts@@ -144,6 +144,80 @@ describe('BaseHttpClient cookie handling', () => {});});+describe('BaseHttpClient credentials on redirects', () => {+ const credentials = {+ 'authorization': 'Bearer secret',+ 'proxy-authorization': 'Basic secret',+ 'cookie': 'token=secret',+ };++ const echoCredentials = (req: http.IncomingMessage, res: http.ServerResponse) => {+ res.setHeader('content-type', 'application/json');+ res.end(+ JSON.stringify({+ 'authorization': req.headers.authorization ?? null,+ 'proxy-authorization': req.headers['proxy-authorization'] ?? null,+ 'cookie': req.headers.cookie ?? null,+ }),+ );+ };++ let target: http.Server;+ let redirector: http.Server;+ let redirectorUrl: string;++ beforeAll(async () => {+ target = http.createServer(echoCredentials);+ await new Promise<void>((resolve) => target.listen(resolve));+ // A different host, so that the cookie jar does not match it either+ const targetUrl = `http://localhost:${(target.address() as AddressInfo).port}`;++ redirector = http.createServer((req, res) => {+ const { pathname } = new URL(req.url!, 'http://localhost');++ if (pathname === '/cross-origin') {+ res.writeHead(302, { location: `${targetUrl}/echo` }).end();+ } else if (pathname === '/same-origin') {+ res.writeHead(302, { location: '/echo' }).end();+ } else {+ echoCredentials(req, res);+ }+ });+ await new Promise<void>((resolve) => redirector.listen(resolve));+ redirectorUrl = `http://127.0.0.1:${(redirector.address() as AddressInfo).port}`;+ });++ afterAll(async () => {+ await new Promise((resolve) => redirector.close(resolve));+ await new Promise((resolve) => target.close(resolve));+ });++ test('does not forward credential headers to a different origin', async () => {+ const response = await httpClient.sendRequest(+ new Request(`${redirectorUrl}/cross-origin`, { headers: credentials }),+ );++ expect(await response.json()).toEqual({ 'authorization': null, 'proxy-authorization': null, 'cookie': null });+ });++ test('does not send cookies of the previous origin to a different origin', async () => {+ const cookieJar = new CookieJar();+ await cookieJar.setCookie('session=secret', redirectorUrl);++ const response = await httpClient.sendRequest(new Request(`${redirectorUrl}/cross-origin`), { cookieJar });++ expect(await response.json()).toMatchObject({ cookie: null });+ });++ test('keeps credential headers on a same-origin redirect', async () => {+ const response = await httpClient.sendRequest(+ new Request(`${redirectorUrl}/same-origin`, { headers: credentials }),+ );++ expect(await response.json()).toEqual(credentials);+ });+});+describe('BaseHttpClient TLS error handling', () => {class CapturingHttpClient extends BaseHttpClient {lastFetchOptions?: RequestInit & CustomFetchOptions;