-
-
Notifications
You must be signed in to change notification settings - Fork 745
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support service workers/shared workers (#401)
* Added test for inconsistencies between page and service workers * Wrong assert * Added CreepJS test * Fix plugin leak * Add evaluateOnNewDocument() alias * Use value instead of getter for navigator.hardwareConcurrency * Use util functions for navigator.languages * Nicer code in navigator.permissions * Disable SW test * Typo fix * Fix tests * Skip SW fixture in ava * Add workflow matrix entry for pptr 5.5.0 * Revert cat & mouse test * Add separate SW test (skipped for now) * Disable 5.5.0 again
- Loading branch information
Showing
9 changed files
with
168 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
packages/puppeteer-extra-plugin-stealth/test/fixtures/dummy-with-service-worker.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<title>title foo</title> | ||
<!-- Testing evasions with a real html page makes things easier --> | ||
<script> | ||
if ('serviceWorker' in navigator) { | ||
window.addEventListener('load', function() { | ||
navigator.serviceWorker.register('/sw.js').then(function(registration) { | ||
console.log('ServiceWorker registration successful with scope: ', registration.scope); | ||
}, function(err) { | ||
console.log('ServiceWorker registration failed: ', err); | ||
}); | ||
}); | ||
} | ||
</script> | ||
</head> | ||
<body> | ||
<h1>Test page with service worker</h1> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
// Left empty |
112 changes: 112 additions & 0 deletions
112
packages/puppeteer-extra-plugin-stealth/test/service-worker.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
const test = require('ava') | ||
|
||
const { vanillaPuppeteer, addExtra } = require('./util') | ||
const Plugin = require('..') | ||
const http = require('http') | ||
const fs = require('fs') | ||
const path = require('path') | ||
|
||
// Create a simple HTTP server. Service Workers cannot be served from file:// URIs | ||
const httpServer = async () => { | ||
const server = await http | ||
.createServer((req, res) => { | ||
let contents, type | ||
|
||
if (req.url === '/sw.js') { | ||
contents = fs.readFileSync(path.join(__dirname, './fixtures/sw.js')) | ||
type = 'application/javascript' | ||
} else { | ||
contents = fs.readFileSync( | ||
path.join(__dirname, './fixtures/dummy-with-service-worker.html') | ||
) | ||
type = 'text/html' | ||
} | ||
|
||
res.setHeader('Content-Type', type) | ||
res.writeHead(200) | ||
res.end(contents) | ||
}) | ||
.listen(0) // random free port | ||
|
||
return `http://127.0.0.1:${server.address().port}/` | ||
} | ||
|
||
let browser, page, worker | ||
|
||
test.before(async t => { | ||
const address = await httpServer() | ||
console.log(`Server is running on port ${address}`) | ||
|
||
browser = await addExtra(vanillaPuppeteer) | ||
.use(Plugin()) | ||
.launch({ headless: true }) | ||
page = await browser.newPage() | ||
|
||
worker = new Promise(resolve => { | ||
browser.on('targetcreated', async target => { | ||
if (target.type() === 'service_worker') { | ||
resolve(target.worker()) | ||
} | ||
}) | ||
}) | ||
|
||
await page.goto(address) | ||
worker = await worker | ||
}) | ||
|
||
test.after(async t => { | ||
await browser.close() | ||
}) | ||
|
||
test.skip('stealth: inconsistencies between page and worker', async t => { | ||
const pageFP = await page.evaluate(detectFingerprint) | ||
const workerFP = await worker.evaluate(detectFingerprint) | ||
|
||
t.deepEqual(pageFP, workerFP) | ||
}) | ||
|
||
test.serial.skip('stealth: creepjs has good trust score', async t => { | ||
page.goto('https://abrahamjuliot.github.io/creepjs/') | ||
|
||
const score = await ( | ||
await ( | ||
await page.waitForSelector('#fingerprint-data .unblurred') | ||
).getProperty('textContent') | ||
).jsonValue() | ||
|
||
t.true( | ||
parseInt(score) > 80, | ||
`The creepjs score is: ${parseInt(score)}% but it should be at least 80%` | ||
) | ||
}) | ||
|
||
/* global OffscreenCanvas */ | ||
function detectFingerprint() { | ||
const results = {} | ||
|
||
const props = [ | ||
'userAgent', | ||
'language', | ||
'hardwareConcurrency', | ||
'deviceMemory', | ||
'languages', | ||
'platform' | ||
] | ||
props.forEach(el => { | ||
results[el] = navigator[el].toString() | ||
}) | ||
|
||
const canvasOffscreenWebgl = new OffscreenCanvas(256, 256) | ||
const contextWebgl = canvasOffscreenWebgl.getContext('webgl') | ||
const rendererInfo = contextWebgl.getExtension('WEBGL_debug_renderer_info') | ||
results.webglVendor = contextWebgl.getParameter( | ||
rendererInfo.UNMASKED_VENDOR_WEBGL | ||
) | ||
results.webglRenderer = contextWebgl.getParameter( | ||
rendererInfo.UNMASKED_RENDERER_WEBGL | ||
) | ||
|
||
results.timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone | ||
|
||
return results | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters