Skip to content

Commit

Permalink
Merge URL whitelist
Browse files Browse the repository at this point in the history
  • Loading branch information
SamMorrowDrums committed Oct 20, 2016
2 parents c1a18e1 + 1787405 commit 42b7a20
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
11 changes: 11 additions & 0 deletions README.md
Expand Up @@ -192,6 +192,17 @@ var phantom = render({
});
```

## Request whitelist
For security reasons you probably would filter the outgoing requests:

```javascript
var phantom = render({
requestWhitelist: [
'^http://localhost/assets/.*'
]
});
```

## Extra Dependencies

For rendering, PhantomJS requires the `fontconfig` library, which may be missing if you're using Ubuntu Server. To install on Ubuntu:
Expand Down
11 changes: 6 additions & 5 deletions index.js
Expand Up @@ -188,7 +188,7 @@ var pool = function(opts) {

worker.stream.on('data', function(data) {
if (data.log) return dup.push(data);

if (!data.success) worker.errors++;
else worker.errors = 0;

Expand Down Expand Up @@ -239,7 +239,8 @@ var create = function(opts) {
tmp : TMP,
format : 'png',
quality : 100,
listen : '0.0.0.0'
listen : '0.0.0.0',
requestWhitelist: false
};

opts = xtend(defaultOpts,opts);
Expand All @@ -253,7 +254,7 @@ var create = function(opts) {
if (!proxy) return;

if (data.log) return proxy.emit('log', data.log);

if (!data.success && data.tries < opts.retries) {
fs.unlink(data.filename, noop);
data.tries++;
Expand All @@ -266,8 +267,8 @@ var create = function(opts) {
if (!data.success) {
fs.unlink(data.filename, noop);
return proxy.destroy(new Error(
'Render failed (' + data.tries + ' tries) ' +
'Request details: ' + JSON.stringify(data)));
'Render failed (' + data.tries + ' tries) ' +
'Request details: ' + JSON.stringify(data)));
}

eos(proxy, { writable: false }, function() {
Expand Down
15 changes: 15 additions & 0 deletions phantom-process.js
Expand Up @@ -180,6 +180,21 @@ var loop = function() {

if(line.javascriptEnabled === false) page.settings.javascriptEnabled = false;

if(line.requestWhitelist) {
page.onResourceRequested = function(reqData, networkRequest) {
if(line.url === reqData.url) return; // allow self-request
var abort = true;
line.requestWhitelist.forEach(function(rgxp) {
var r = new RegExp(rgxp, 'gi');
if(r.test(reqData.url)) abort = false;
});
if(abort) {
console.log('Deny network request to', reqData.url);
networkRequest.abort();
}
}
}

var onerror = function(message) {
page.log(message);
line.success = false;
Expand Down

0 comments on commit 42b7a20

Please sign in to comment.