Skip to content

network.beforeRequestSent event does not include request cookies #4042

@KuznetsovRoman

Description

@KuznetsovRoman

Specs for "network.beforeRequestSent" event: https://www.w3.org/TR/webdriver-bidi/#event-network-beforeSendRequest

It should be emitted with "network.BaseParameters": https://www.w3.org/TR/webdriver-bidi/#cddl-type-networkbaseparameters

By specs, "network.RequestData" should be created this way: https://www.w3.org/TR/webdriver-bidi/#cddl-type-networkrequestdata

Image

So, there should be "cookie" property with cookies.

But in fact, its empty, even though browser is requesting cookies.

Server example (JS):

import http from 'http';

const PORT = 3000;

const server = http.createServer((req, res) => {
    console.log('req cookie', req.headers.cookie);
  // Get the current page path
  const currentPage = req.url;
  
  // Parse existing cookies
  let cookieValue = null;
  if (req.headers.cookie) {
    req.headers.cookie.split(';').forEach(cookie => {
      const [name, value] = cookie.trim().split('=');
      if (name === 'chrome-bidi-cookie-debug') {
        cookieValue = decodeURIComponent(value);
      }
    });
  }
  
  // Set the user cookie to the current page
  res.setHeader('Set-Cookie', `chrome-bidi-cookie-debug=${encodeURIComponent(currentPage)}`);
  res.setHeader('Content-Type', 'text/plain');
  
  // Send response with the current cookie value
  const responseText = cookieValue !== null ? cookieValue : 'No cookie set yet';
  
  res.statusCode = 200;
  res.end(`User cookie value: ${responseText}`);
});

server.listen(PORT, () => {
  console.log(`Server running at http://localhost:${PORT}/`);
});

Client example (webdriverio):

import { remote } from 'webdriverio'

let browser;

async function main() {
    browser = await remote({
        hostname: "127.0.0.1",
        protocol: "http",
        logLevel: "trace",
        port: 4444,
        path: "/",
        capabilities: {
            browserName: 'chrome',
            'goog:chromeOptions': {
                args: ['headless', 'disable-gpu'],
                binary: "/Users/username/local-browsers/chrome/mac_arm-145.0.7632.26/chrome-mac-arm64/Google Chrome for Testing.app/Contents/MacOS/Google Chrome for Testing"
            }
        }
    })

    await browser.networkAddIntercept({ phases: ['beforeRequestSent']});

    await browser.on('network.beforeRequestSent', async event => {
        if (!event.isBlocked) {
            return;
        }
        console.log('headers has cookie:', JSON.stringify(event.request.headers).includes('chrome-bidi-cookie-debug'));
        console.log('has any cookies', event.request.cookies.length > 0);
        await browser.networkContinueRequest({request: event.request.request});
    });

    await browser.url("http://localhost:3000/test-page-1");
    await browser.url("http://localhost:3000/test-page-3");
    await browser.url("http://localhost:3000/test-page-2");
    await browser.url("http://localhost:3000/test-page-4");
}


main().finally(() => {
    if (browser && browser.deleteSession) {
        browser.deleteSession();
    }
})

Incoming "network.beforeRequestSent" event example:

{
    "context": "36AE127372369C45A31834D68F9FF9A3",
    "initiator": {
        "type": "other"
    },
    "intercepts": [
        "bea19097-a1af-4b6d-945f-ebb34141fa00"
    ],
    "isBlocked": true,
    "navigation": "f55b3899-9be9-4148-8241-833c4e88a97f",
    "redirectCount": 0,
    "request": {
        "bodySize": 0,
        "cookies": [],
        "destination": "document",
        "goog:resourceInitiator": {
            "type": "other"
        },
        "goog:resourceType": "Document",
        "headers": [
            {
                "name": "Accept-Language",
                "value": {
                    "type": "string",
                    "value": "en-US,en;q=0.9"
                }
            },
            {
                "name": "Upgrade-Insecure-Requests",
                "value": {
                    "type": "string",
                    "value": "1"
                }
            },
            {
                "name": "User-Agent",
                "value": {
                    "type": "string",
                    "value": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/145.0.0.0 Safari/537.36"
                }
            },
            {
                "name": "sec-ch-ua",
                "value": {
                    "type": "string",
                    "value": "\"Chromium\";v=\"145\", \"Not:A-Brand\";v=\"99\""
                }
            },
            {
                "name": "sec-ch-ua-mobile",
                "value": {
                    "type": "string",
                    "value": "?0"
                }
            },
            {
                "name": "sec-ch-ua-platform",
                "value": {
                    "type": "string",
                    "value": "\"macOS\""
                }
            }
        ],
        "headersSize": 306,
        "initiatorType": null,
        "method": "GET",
        "request": "D8DD3CF41CDC71F46A9FA8182E62E87B",
        "timings": {
            "connectEnd": 0,
            "connectStart": 0,
            "dnsEnd": 0,
            "dnsStart": 0,
            "fetchStart": 0,
            "redirectEnd": 0,
            "redirectStart": 0,
            "requestStart": 0,
            "requestTime": 0,
            "responseEnd": 0,
            "responseStart": 0,
            "timeOrigin": 1769773533010,
            "tlsStart": 0
        },
        "url": "http://localhost:3000/test-page-4"
    },
    "timestamp": 1769773533010
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions