From 0d7f38ce516bbe862752d8cf55162fba906cbf2a Mon Sep 17 00:00:00 2001 From: Elbert Alias <77259+AliasIO@users.noreply.github.com> Date: Sun, 8 Mar 2020 08:54:07 +1100 Subject: [PATCH] Prevent in-page navigation in NPM/Puppeteer driver --- src/drivers/npm/browsers/puppeteer.js | 41 +++++++++++++++++++++----- src/drivers/npm/package.json | 2 +- src/drivers/webextension/manifest.json | 2 +- 3 files changed, 35 insertions(+), 10 deletions(-) diff --git a/src/drivers/npm/browsers/puppeteer.js b/src/drivers/npm/browsers/puppeteer.js index d5bacd73e..429700046 100644 --- a/src/drivers/npm/browsers/puppeteer.js +++ b/src/drivers/npm/browsers/puppeteer.js @@ -84,14 +84,29 @@ class PuppeteerBrowser extends Browser { page.setDefaultTimeout(this.options.maxWait * 2); + await page.setRequestInterception(true); + page.on('error', error => reject(new Error(`page error: ${error.message || error}`))); + let responseReceived = false; + + page.on('request', (request) => { + if ( + responseReceived + && request.isNavigationRequest() + && request.frame() === page.mainFrame() + && request.url() !== url + ) { + this.log(`abort navigation to ${request.url()}`); + + request.abort('aborted'); + } else { + request.continue(); + } + }); + page.on('response', (response) => { try { - if (response.status() === 301 || response.status() === 302) { - return; - } - if (!this.statusCode) { this.statusCode = response.status(); @@ -105,6 +120,10 @@ class PuppeteerBrowser extends Browser { this.contentType = headers['content-type'] || null; } + + if (response.status() < 300 || response.status() > 399) { + responseReceived = true; + } } catch (error) { reject(new Error(`page error: ${error.message || error}`)); } @@ -114,10 +133,16 @@ class PuppeteerBrowser extends Browser { await page.setUserAgent(this.options.userAgent); - await Promise.race([ - page.goto(url, { waitUntil: 'domcontentloaded' }), - new Promise((_resolve, _reject) => setTimeout(() => _reject(new Error('timeout')), this.options.maxWait)), - ]); + try { + await Promise.race([ + page.goto(url, { waitUntil: 'domcontentloaded' }), + new Promise((_resolve, _reject) => setTimeout(() => _reject(new Error('timeout')), this.options.maxWait)), + ]); + } catch (error) { + if (!this.statusCode) { + throw new Error(error.message || error.toString()); + } + } // eslint-disable-next-line no-undef const links = await page.evaluateHandle(() => Array.from(document.getElementsByTagName('a')).map(({ diff --git a/src/drivers/npm/package.json b/src/drivers/npm/package.json index a2c6d1568..8147d5973 100644 --- a/src/drivers/npm/package.json +++ b/src/drivers/npm/package.json @@ -2,7 +2,7 @@ "name": "wappalyzer", "description": "Uncovers the technologies used on websites", "homepage": "https://www.wappalyzer.com", - "version": "5.9.25", + "version": "5.9.26", "author": "Elbert Alias", "license": "GPL-3.0", "repository": { diff --git a/src/drivers/webextension/manifest.json b/src/drivers/webextension/manifest.json index 8694e124d..b5f852cee 100644 --- a/src/drivers/webextension/manifest.json +++ b/src/drivers/webextension/manifest.json @@ -4,7 +4,7 @@ "author": "Elbert Alias", "homepage_url": "https://www.wappalyzer.com", "description": "Identify web technologies", - "version": "5.9.25", + "version": "5.9.26", "default_locale": "en", "manifest_version": 2, "icons": {