Friday 15 July 2011

javascript - How to solve concurrency problems when trying to print the URLs for which there was an alert? -



javascript - How to solve concurrency problems when trying to print the URLs for which there was an alert? -

i trying utilize phantomjs loop through couple of urls, open pages , check if there alert on of pages. printing urls of pages alert occurred.

the code below:

var page = require('webpage'); var u = ["http://127.0.0.1/dvwa-1.0.8/vulnerabilities/xss_r/?name=<script>alert('xss');</script>", "http://127.0.0.1/dvwa-1.0.8/vulnerabilities/xss_r/?name=abcd"] var url = ""; for(var = 0; < u.length; i++) { url = u[i]; var webpage = page.create(); phantom.addcookie({ 'name':'phpsessid', 'value':'00885b45d9ddda3e757371b177c5959b', 'domain':'127.0.0.1' }); webpage.onalert = function(alertmessage){ console.log("alert url: " + webpage.url); console.log("alert occured message: " + alertmessage); } webpage.open(url, function (status) { console.log("opening url: " + webpage.url); phantom.exit(); }); }

i expect part of output be:

alert url: http://127.0.0.1/dvwa-1.0.8/vulnerabilities/xss_r/?name=<script>alert('xss');</script> alert occured message: xss

but instead, differs each time , shows wrong output like:

alert url: http://127.0.0.1/dvwa-1.0.8/vulnerabilities/xss_r/?name=abcd alert occured message: xss

it looks happens because of concurrency of callbacks.

is there way handle in order ensure output expected? or library not meant used , should seek else?

you create new page in loop new tab far phantomjs runtime concerned. executed concurrently. add together construction , reproducability script, have write little chaining.

something should work:

var page = require('webpage'); var u = ["http://127.0.0.1/dvwa-1.0.8/vulnerabilities/xss_r/?name=<script>alert('xss');</script>", "http://127.0.0.1/dvwa-1.0.8/vulnerabilities/xss_r/?name=abcd"] function each(list, func, done){ var len = list.length, previous = done; for(var = len - 1; >= 0; i--) { (function(item, done){ // iife previous = function(){ func(item, done); }; })(list[i], previous); } previous(); // start chain } each(u, function(url, done){ var webpage = page.create(); phantom.addcookie({ 'name':'phpsessid', 'value':'00885b45d9ddda3e757371b177c5959b', 'domain':'127.0.0.1' }); webpage.onalert = function(alertmessage){ console.log("alert url: " + webpage.url); console.log("alert occured message: " + alertmessage); } webpage.open(url, function (status) { console.log("opening url: " + webpage.url); done(); }); }, function(){ phantom.exit(); });

javascript concurrency callback phantomjs

No comments:

Post a Comment