carcabot / tiktok-signature

Generate tiktok signature token using node
733 stars 283 forks source link

page.addScriptTag: Refused to execute inline script because it violates the following Content Security Policy directive: "script-src... #186

Closed PATAPOsha closed 1 year ago

PATAPOsha commented 1 year ago

I was using the same Dockerfile a week ago and everything was ok, but a couple of days ago it started to throw error at server initialization. After starting docker container, server throws this error and don't receive any requests:

2023-02-28T09:17:41: PM2 log: Launching in no daemon mode
2023-02-28T09:17:41: PM2 log: App [listen:0] starting in -fork mode-
2023-02-28T09:17:41: PM2 log: App [listen:0] online
TikTok Signature server started on PORT 8080
page.addScriptTag: Refused to execute inline script because it violates the following Content Security Policy directive: "script-src 'nonce-8c42cfcfa4e3b532f51b2162f0410218' 'unsafe-eval' sf16-website-login.neutral.ttwstatic.com s20.tiktokcdn.com *.tiktokcdn-us.com". Either the 'unsafe-inline' keyword, a hash ('sha256-R4ufFOUcupzwMryKhtrFfzT1nsMuxb3od7pbH29dUbc='), or a nonce ('nonce-...') is required to enable inline execution.
Error
    at Object.captureStackTrace (/usr/node_modules/playwright-chromium/lib/utils/stackTrace.js:48:19)
    at Connection.sendMessageToServer (/usr/node_modules/playwright-chromium/lib/client/connection.js:69:48)
    at Proxy.<anonymous> (/usr/node_modules/playwright-chromium/lib/client/channelOwner.js:64:61)
    at /usr/node_modules/playwright-chromium/lib/client/frame.js:264:70
    at async Frame._wrapApiCall (/usr/node_modules/playwright-chromium/lib/client/channelOwner.js:77:28)
    at async /usr/index.js:78:7
page.addScriptTag: Refused to execute inline script because it violates the following Content Security Policy directive: "script-src 'nonce-8c42cfcfa4e3b532f51b2162f0410218' 'unsafe-eval' sf16-website-login.neutral.ttwstatic.com s20.tiktokcdn.com *.tiktokcdn-us.com". Either the 'unsafe-inline' keyword, a hash ('sha256-R4ufFOUcupzwMryKhtrFfzT1nsMuxb3od7pbH29dUbc='), or a nonce ('nonce-...') is required to enable inline execution.
Error
    at Object.captureStackTrace (/usr/node_modules/playwright-chromium/lib/utils/stackTrace.js:48:19)
    at Connection.sendMessageToServer (/usr/node_modules/playwright-chromium/lib/client/connection.js:69:48)
    at Proxy.<anonymous> (/usr/node_modules/playwright-chromium/lib/client/channelOwner.js:64:61)
    at /usr/node_modules/playwright-chromium/lib/client/frame.js:264:70
    at async Frame._wrapApiCall (/usr/node_modules/playwright-chromium/lib/client/channelOwner.js:77:28)
    at async /usr/index.js:78:7

If fields below will be empty then issue will be Ignored and Closed

To Reproduce Steps to reproduce the behavior:

PATAPOsha commented 1 year ago

Seems like adding bypassCSP: true, into browser.newContext() fixesthe issue https://playwright.dev/docs/api/class-browser#browser-new-context-option-bypass-csp

my fixed index.js:

let emulateTemplate = {
      ...iPhone11,
      locale: "en-US",
      deviceScaleFactor: Utils.getRandomInt(1, 3),
      isMobile: Math.random() > 0.5,
      hasTouch: Math.random() > 0.5,
      userAgent: this.userAgent,
      bypassCSP: true,   // <-------- fix
    };
    emulateTemplate.viewport.width = Utils.getRandomInt(320, 1920);
    emulateTemplate.viewport.height = Utils.getRandomInt(320, 1920);

    this.context = await this.browser.newContext({
      ...emulateTemplate,
    });

But I'm not sure why it happened. Maybe chrome version was updated automatically and its better to pin playwright/chrome version in the requiremnets...