Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bypass CSP(Content-Security-Policy) using puppeteer's API page.addScriptTag?

scenario:

I use puppeteer launched chrome in headless mode, and call page.addScriptTag with an cross-domain javascript file. Now if the opening site has csp set and restricts only same origin javascript tags, how can I bypass this using puppeteer API?

like image 276
johnzh Avatar asked Oct 19 '17 15:10

johnzh


People also ask

How do I ignore content security policy?

Click the extension icon to disable Content-Security-Policy header for the tab. Click the extension icon again to re-enable Content-Security-Policy header. Use this only as a last resort. Disabling Content-Security-Policy means disabling features designed to protect you from cross-site scripting.

Can you bypass CSP?

If the application is using angular JS and scripts are loaded from a whitelisted domain. It is possible to bypass this CSP policy by calling callback functions and vulnerable class.

How do I disable CSP in Chrome?

Click the extension icon to re-enable CSP headers. Click the extension icon again to disable CSP headers. Use this only as a last resort. Disabling CSP means disabling features designed to protect you from cross-site scripting.

How do I disable security CSP in Firefox?

Turn off the CSP for your entire browser in Firefox by disabling security. csp. enable in the about:config menu. Note: You must log in to the ELM instance in the new tab of the same browser before you access the resource or configuration picker through Publishing Document Builder.


2 Answers

Use:

await page.setBypassCSP(true)

Documentation

like image 172
HaNdTriX Avatar answered Sep 22 '22 13:09

HaNdTriX


This is my first stackoverflow contribution so have mercy on me. I found this work around to allow you to get past CSP, Here.

The basic idea is that you intercept page requests and use a library like node-fetch to make the request and disable the CSP header when passing it back to chrome.

Here's the snippet that initially came from the github issue tracker.

Replace "example.com" with the website that needs to have CSP disabled.

const fetch = require('node-fetch')

const requestInterceptor = async (request) => {
  try {
    const url = request.url()
    const requestHeaders = request.headers()
    const acceptHeader = requestHeaders.accept || ''
    if (url.includes("example.com") && (acceptHeader.includes('text/html'))) {
      const cookiesList = await page.cookies(url)
      const cookies = cookiesList.map(cookie => `${cookie.name}=${cookie.value}`).join('; ')
      delete requestHeaders['x-devtools-emulate-network-conditions-client-id']
      if (requestHeaders.Cookie) {
        requestHeaders.cookie = requestHeaders.Cookie
        delete requestHeaders.Cookie
      }
      const theseHeaders = Object.assign({'cookie': cookies}, requestHeaders, {'accept-language': 'en-US,en'})

      const init = {
        body: request.postData(),
        headers: theseHeaders,
        method: request.method(),
        follow: 20,
      }
      const result = await fetch(
        url,
        init,
      )
      const resultHeaders = {}
      result.headers.forEach((value, name) => {
        if (name.toLowerCase() !== 'content-security-policy') {
          resultHeaders[name] = value
        } else {
          console.log('CSP', `omitting CSP`, {originalCSP: value})
        }
      })
      const buffer = await result.buffer()
      await request.respond({
        body: buffer,
        resultHeaders,
        status: result.status,
      })
    } else {
      request.continue();
    }
  } catch (e) {
    console.log("Error while disabling CSP", e);
    request.abort();
  }
}

await page.setRequestInterception(true)
page.on('request', requestInterceptor)
like image 22
Daniel Morris Avatar answered Sep 20 '22 13:09

Daniel Morris