Skip to content
This repository has been archived by the owner on May 30, 2023. It is now read-only.

'unsafe-eval' is not an allowed ... Security Policy directive: "script-src 'self' #13114

Closed
jain0709 opened this issue Mar 27, 2015 · 46 comments
Closed

Comments

@jain0709
Copy link

trying to automate scenario for login https site getting following error :-

[DEBUG] WebPage - evaluateJavaScript result QVariant(QVariantMap, QMap(("status", QVariant(double, 13) ) ( "value" , QVariant(QVariantMap, QMap(("message", QVariant(QString, "Refused to evaluate a string as JavaScript because 'unsafe-eval' is not an allowed source of script in the following Content Security Policy directive: "script-src 'self' ".
") ) ) ) ) ) )
2015-03-27T20:34:57 [DEBUG] HTTP Response - Status Code 500 Internal Server Error

same test is working fine when using chrome browser. Running phantom js as :-

phantomjs --ignore-ssl-errors=true --web-security=false --ssl-protocol=tlsv1 --local-to-remote-url-access=true --webdriver=4444 --debug=true

seems some kind of bug with phantomjs.

@cdroulers
Copy link

I'm having the same issue when trying to log in through IdentityServer3 with PhantomJS 2. Chrome and PhantomJS 1.9.8 work just fine.

@jain0709
Copy link
Author

Thanks for suggestion, It worked with PhantomJS 1.9.8

@zackw
Copy link
Contributor

zackw commented Apr 19, 2015

@jain0709 We don't want you to have to downgrade. If something worked in 1.9.x and doesn't in 2.0, that's something we should fix.

That said, we need quite a bit more information from you before we can do anything about this. Specifically, we need to know what website provoked this error, and how you were using PhantomJS to access it -- not just the command line invocation, but the script. If at all possible, construct a self-contained test script that we can run as

$ phantomjs issue13114.js

and see the same problem you are. It's OK if you need extra files or even a web server, just explain in enough detail that we can make it happen for ourselves.

Also, there's a decent chance this is a situation where the newer Webkit in 2.0 is refusing to do something insecure when the older one would let you get away with it. This is a guess, based on the text of the error message. I could be wrong.

Also also, you shouldn't ever need to use --ignore-ssl-errors=true or --web-security=false -- that suggests to me that you have major problems with the environment you're running PhantomJS in, and you may find that this problem vanishes if you sort those out first.

@cdroulers
Copy link

@zackw I'll see about creating a quick test case soon. The problem I was experiencing is that my app was doing a redirect (via JavaScript) to our authentication server, which was a different page, but on the same domain (virtual directories in IIS). I tried accessing the login page directly and it worked just fine.

cdroulers added a commit to Vooban/phantomjs that referenced this issue Apr 20, 2015
@cdroulers
Copy link

@zackw Here's a commit with a test case that MIGHT be reproducing the issue. If not the right issue, it still is one I think! Vooban@a18eab4

Simply start a two web servers for the folder on port 8000 and 8001 and the output is something like :

success 8000
redirect caught
Operation canceled
http://localhost:8001/index2.html
failed http://localhost:8001/index2.html

@zackw
Copy link
Contributor

zackw commented Apr 20, 2015

@cdroulers That doesn't appear to have anything to do with Content-Security-Policy directives?

@cdroulers
Copy link

@zackw Nope. I caught this while trying to reproduce. I'm still unable to create a test case. The problem happens for me with a C# Selenium test case. Would it be okay to create a repro this way?

@zackw
Copy link
Contributor

zackw commented Apr 20, 2015

@cdroulers I think we can cope with that, if that's what you need to do to make it happen.

@cdroulers
Copy link

@zackw I've narrowed it down to a decent test case. If I try to render a page with a Content-Security-Policy in a phantomjs script, the page loads and renders in a screenshot fine. The JavaScript is not executed.

If I try to load that page with Selenium's RemoteWebDriver though, it fails with the error. I'm trying to create a very small and specific test case to reproduce it. Do you have a suggestion on how to write that test case with selenium in a language that would be easy to run for you? Ruby, Python, etc. I can figure out how to write it!

@zackw
Copy link
Contributor

zackw commented Apr 20, 2015

@cdroulers Anything you can possibly do to reproduce the issue without Selenium is worth doing, but beyond that, don't worry about languages; we're gonna have to rewrite it to fit it into our automated test suite anyway.

"The JavaScript is not executed", though, that might be enough of a clue right there. Which JavaScript exactly was that?

@cdroulers
Copy link

@zackw The JavaScript not being executed is inlined JavaScript, which the Content-Security-Policy header forbids. When I open the page in Chrome, nothing is executed and I get warnings in the developer console.

In Phantom, everything succeeds without warnings (that I can see!), the screenshot is taken properly. In the JavaScript, I try to write in the body, and the text does not appear in the body. So I think PhantomJS is handling everything just fine.

On the other hand, when I simply try to load the page in Selenium and then read the Url or Title of the IWebDriver, it fails with the error in the OP. I'm starting to think it's a problem with the Selenium Web Driver! Do you have any idea how it works for PhantomJS?

@cdroulers
Copy link

master...Vooban:master

Here's a diff of how I got a repro. The issue13114.js works fine (and doesn't execute the JS).

The Selenium test case crashes with the error from the OP. I'm not well versed with Selenium enough to know what exactly happens between the two when driver.Url is called.

@snowyu
Copy link

snowyu commented Feb 24, 2016

this happen on https://github.com now(phantomjs-2.1.1-windows):

 1) Github test open URL Then  I expect that the url is "https://github.com/":
     Uncaught Error: Refused to evaluate a string as JavaScript because 'unsafe-eval' is not an allowed source of script in the
following Content Security Policy directive: "script-src assets-cdn.github.com".
 Uncaught Error: Refused to evaluate a string as JavaScript because 'unsafe-eval' is not an allowed source of script in the
following Content Security Policy directive: "script-src assets-cdn.github.com".

the phantomjs raise error:

[INFO  - 2016-02-24T13:47:49.812Z] GhostDriver - Main - running on port 4444
[INFO  - 2016-02-24T13:48:02.627Z] Session [3937fde0-dafd-11e5-ac6f-115b0bd0fe78] - page.settings - {"XSSAuditingEnabled":false,"javascriptCanCloseWindows":true,"javascriptCanOpenWindows":true,"javascriptEnabled":true,"loadImages":true,"localToRemoteUrlAccessEnabled":false,"userAgent":"Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/538.1 (KHTML, like Gecko) PhantomJS/2.1.1 Safari/538.1","webSecurityEnabled":false}
[INFO  - 2016-02-24T13:48:02.627Z] Session [3937fde0-dafd-11e5-ac6f-115b0bd0fe78] - page.customHeaders:  - {}
[INFO  - 2016-02-24T13:48:02.627Z] Session [3937fde0-dafd-11e5-ac6f-115b0bd0fe78] - Session.negotiatedCapabilities - {"browserName":"phantomjs","version":"2.1.1","driverName":"ghostdriver","driverVersion":"1.2.0","platform":"windows-10-32bit","javascriptEnabled":true,"takesScreenshot":true,"handlesAlerts":false,"databaseEnabled":false,"locationContextEnabled":false,"applicationCacheEnabled":false,"browserConnectionEnabled":false,"cssSelectorsEnabled":true,"webStorageEnabled":false,"rotatable":false,"acceptSslCerts":false,"nativeEvents":true,"proxy":{"proxyType":"direct"}}
[INFO  - 2016-02-24T13:48:02.627Z] SessionManagerReqHand - _postNewSessionCommand - New Session Created: 3937fde0-dafd-11e5-ac6f-115b0bd0fe78
[ERROR - 2016-02-24T13:48:31.217Z] RouterReqHand - _handle.error - {"line":264,"sourceURL":"phantomjs://code/webelement_request_handler.js","stack":"_postValueCommand@phantomjs://code/webelement_request_handler.js:264:52\n_handle@phantomjs://code/webelement_request_handler.js:72:30\n_reroute@phantomjs://code/request_handler.js:61:20\n_handle@phantomjs://code/session_request_handler.js:120:42\n_reroute@phantomjs://code/request_handler.js:61:20\n_handle@phantomjs://code/router_request_handler.js:78:46"}

  phantomjs://platform/console++.js:263 in error

jjanssen pushed a commit to jjanssen/cucumber-boilerplate that referenced this issue Feb 28, 2016
@edneypitta
Copy link

Same happening to me when I call Driver.Url. It worked with version 1.9.8.

Running on Selenium, C#.

@Ocramius
Copy link

Ocramius commented Apr 4, 2016

Same issue here: seems to do that whenever trying to load anything with HTTPS, really.

Same as what @snowyu reported.

@spellfish
Copy link

I'm also seeing this issue as described by @snowyu. Running selenium, ruby.

@Ocramius
Copy link

@traviscooper thanks! Sorry, I misunderstood the solution then, that does indeed work if you have control over the server-side.

@travisjweber
Copy link

Oh yeah, most definitely need that control! Our same tests work with chrome's driver FYI, we were using that locally but it doesn't work with our builds so we [temporarily] relaxed the CSP so Phantom works until we can figure out something longer-term.

@tobias-sc
Copy link

Going through the replies seem temporary fix is to downgrade PhantomJS or modify CSP option for Identity Server....
In our case here we have to use at least 2.1 due its support of SSL certificates, also no access to the Identity Server - what would be the solution in this case?
It is mentioned to remove horseman? If yes, how or what is another option?

Thank you.

@peacetrader
Copy link

Hi, I am using this dependency in my sbt build :
"com.github.detro.ghostdriver" % "phantomjsdriver" % "1.0.1"
and by default it is using this combination :
"browserName":"phantomjs","version":"2.1.1","driverName":"ghostdriver","driverVersion":"1.2.0"
How to use the 1.9.8 version ?.

@alexmherrmann
Copy link

Seeing the same thing trying to interact with stripe website while authorizing an API. Works fine with chrome just like others have said.

@travisjweber
Copy link

We wound up creating an http proxy to strip out the csp directive for running our tests with phantomjs.

@jeff1985
Copy link

jeff1985 commented Mar 7, 2017

We encountered the same problem while trying to implement a behat test for SOFORT payment integration. Any progress here?

@jesg
Copy link
Contributor

jesg commented Mar 7, 2017

Any progress here?

it should work for every command but execute_script and execute_async_script in the next version of ghostdriver.

@javascriptlove
Copy link

In meteor we used this:

if (process.env.NODE_ENV === 'development') {
    BrowserPolicy.content.allowEval(); // for PhantomJS end-to-end testing
}

Which is something like this in raw content-security-policy header:

script-src 'self' 'unsafe-inline' 'unsafe-eval' http://localhost:3000 https://localhost:3000

@bobmshannon
Copy link

Also experiencing this issue.

@ssb22
Copy link

ssb22 commented Apr 10, 2017

In Python's webdriver, even reading current_url (which calls execute("getCurrentUrl")['value']) raises a WebDriverException with the unsafe-eval complaint on affected sites. (Tested on PhantomJS 2.1.1.)

@travisjweber
Copy link

We've switched to using Selenium-Grid with Chrome and Internet Explorer, no more dealing with a headless browser just for our Selenium tests.

@bllevy
Copy link

bllevy commented Aug 4, 2017

Any updates on this issue?

@ssb22
Copy link

ssb22 commented Aug 5, 2017 via email

@stephen-walsh
Copy link

also hitting this issue now 2.5 years later :(

@ssb22
Copy link

ssb22 commented Oct 24, 2017 via email

@ghost ghost removed 2.0 Regression labels Jan 10, 2018
@mesaleh
Copy link

mesaleh commented Jan 12, 2018

PhantomJS latest stable version 2.1.1 has a bug when it tries to get a webpage that has the following response header:
content-security-policy: default-src 'none'

The Fix:
Branch 2.1.1, File: \phantomjs\src\qt\qtwebkit\Source\WebCore\page\ContentSecurityPolicy.cpp, Change line 354:

, m_allowEval(false)

To:

, m_allowEval(true)

Then build.py -r ... Enjoy!

@stale
Copy link

stale bot commented Dec 28, 2019

Due to our very limited maintenance capacity (see #14541 for more details), we need to prioritize our development focus on other tasks. Therefore, this issue will be automatically closed. In the future, if we see the need to attend to this issue again, then it will be reopened. Thank you for your contribution!

@stale stale bot closed this as completed Dec 28, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests