Sparticuz / chromium

Chromium (x86-64) for Serverless Platforms
MIT License
846 stars 57 forks source link

Browser crashes when a navigational request is aborted using --single-process flag #253

Open heaven opened 3 months ago

heaven commented 3 months ago

This is rather a question, not a bug report.

The main problem is when intercepting a navigation request (e.g. a link to a page points or redirects to a pdf or another type of document we don't want to).

For example a simple approach:

  // Enable request interceptions
  await page.setRequestInterception(true);

  // do not load images and fonts
  await page.on("request", (request) => {
    if (request.isInterceptResolutionHandled()) return;

    if (request.isNavigationRequest())
      request.abort("blockedbyclient");
    else
      request.continue();
  });

Or intercepting the response:

  const client = await page.createCDPSession();
  const cTypeHeader = "content-type";
  const cTypeExp = /\bhtml\b/i;
  const errorReason = "BlockedByClient";

  // intercept request when response headers were received
  await client.send('Fetch.enable', {
    patterns: [{ urlPattern: '*', resourceType: 'Document', requestStage: 'Response' }],
  });

  await client.on('Fetch.requestPaused', async evt => {
    const requestId = evt.requestId;
    const status = evt.responseStatusCode;

    if (status >= 200 && status <= 299) {
      const headers = evt.responseHeaders || [];
      const contentType = headers.find(h => h.name.toLowerCase() == cTypeHeader)?.value;

      // Do not allow anything other than html document.
      if (contentType && !cTypeExp.test(contentType))
        return await client.send('Fetch.failRequest', { requestId, errorReason }).catch(none);
    }

    await client.send('Fetch.continueRequest', { requestId }).catch(none);
  });

Both lead to the same situation when the browser kills the renderer and occasionally dies as they are in a single process 😀

The bug report is here and I was about to submit one more with crbug.com. But was also wondering if it is possible to run chromium without the --single-process flag. I tried just removing the flag but didn't have much success so far.

Sparticuz commented 3 months ago

This is the comment I have in the code for --single-process

// Needs to be single-process to avoid `prctl(PR_SET_NO_NEW_PRIVS) failed` error