Search code examples
sslproxyphantomjscasperjscharles-proxy

Proxying casperjs / phantomjs through Charles


I'd like to use casperjs / phantomjs to autofill some web forms. In order to debug my casperjs scripts, I'd like to use the (wonderful) Charles Web Proxy as a man-in-the-middle SSL Proxy, but haven't figured out how to make that work in casperjs / phantomjs.

What works

I've enabled Charles to proxy between Firefox and the remote server. The browser and the remote servers both accept the SSL certs and Charles correctly decodes the HTTPS packets so I can see the traffic between the browser and the remote server. So far so good.

I've also been able to run my casperjs script without the Charles proxy and verified that my script logs me into the remote site properly, as in:

$ casperjs test-login.js
found login form...
logged in...

Also good.

What doesn't work

When I try to direct phantomjs to proxy via Charles, as in:

$ casperjs --web-security=no --proxy=127.0.0.1:8888 test-login.js

... casperjs doesn't even receive the login page, and the following shows up in the Charles console:

URL     https://www.<my server>.com
Status  Failed
Failure No request was made. Possibly the SSL certificate was rejected.
Notes   You may need to configure your browser or application to trust the Charles Root Certificate. See SSL Proxying in the Help menu.

My guess is that phantomjs's browser doesn't like the SSL cert that Charles is offering up, but I thought --web-security=no was supposed to override that.

I'm probably missing something obvious. Anyone been down this path before?

update

I stumbled upon the solution. See below...

ps

$ casperjs --version
1.1.0-beta3
$ phantomjs --version
1.9.8

... and Charles is v 3.10.1


Solution

  • (If answering one's own questions was an Olympic sport, I'd be vying for the gold.)

    Adding --ignore-ssl-errors=true is necessary and sufficient to get things working -- with this, you can see all the traffic passing through Charles:

    $ casperjs --ignore-ssl-errors=true --proxy=127.0.0.1:8888 test-login.js
    found login form...
    logged in...
    

    note

    Word to the wise: if exchanges appear to be going really slowly, make sure that you have not accidentally turned on throttling in Charles. Been there! :)