Skip to content

Commit

Permalink
Merge pull request #180 from cowlicks/fix-chrome-cookies
Browse files Browse the repository at this point in the history
Update webrequest infospeck fixes chrome changes
  • Loading branch information
cowlicks authored Jul 7, 2019
2 parents 7b6f30a + 0ae1f2d commit af9f8fd
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 24 deletions.
7 changes: 4 additions & 3 deletions selenium/cookies.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
'use strict';

const cookieParser = require('cookie-parser'),
const express = require('express'),
cookieParser = require('cookie-parser'),
{requestRecorderMiddleware, baseTestApp} = require('./utils');

let fpcookie = {name: '1pname', value: '1pvalue'},
tpcookie = {name: '3pname', value: '3pvalue'};


function cookieSetterApp(cookieName, cookieValue) {
let app = requestRecorderMiddleware();
function cookieSetterApp(cookieName, cookieValue, app = express()) {
app.use(cookieParser());
app.use((req, res, next) => {
res.cookie(cookieName, cookieValue);
next();
});
requestRecorderMiddleware(app);
return app;
}

Expand Down
18 changes: 9 additions & 9 deletions selenium/integration_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ const {newDriver, startApp, stopApp, firstPartyHost} = require('./utils'),
{etagApp} = require('./etags');

describe('etag tests', function() {
beforeEach(function() {
beforeEach(async function() {
this.app = etagApp();
this.driver = newDriver();
this.driver = await newDriver();
startApp(this.app);
});
afterEach(function() {
Expand All @@ -18,21 +18,20 @@ describe('etag tests', function() {
});
it('blocks etags', async function() {
let {app, driver} = this;
await driver.get('about:blank');
await driver.get(firstPartyHost);
await driver.get(firstPartyHost);
let req1 = await app.firstParty.requests.next();
await driver.get(firstPartyHost); // etag gets set
await driver.get(firstPartyHost); // browser sends if-none-match to check if etag still valid
let req = await app.firstParty.requests.next();
//let req3 = await app.thirdParty.requests.next();
assert.isTrue(req1.headers.hasOwnProperty('if-none-match'), 'allows 1st party etags on first visit');
assert.isTrue(req.headers.hasOwnProperty('if-none-match'), 'allows 1st party etags on first visit');
// known failure on chrome due to lack of access to caching headers in chrome webrquest api
//assert.isFalse(req3.headers.hasOwnProperty('if-none-match'), 'blocks 3rd party etags headers on first visit');
});
});

describe('cookie tests', function() {
beforeEach(function() {
beforeEach(async function() {
this.app = cookieApp();
this.driver = newDriver();
this.driver = await newDriver();
startApp(this.app);
});
afterEach(function() {
Expand All @@ -42,6 +41,7 @@ describe('cookie tests', function() {

it('blocks cookies', async function() {
let {app, driver} = this;

await driver.get(firstPartyHost);
let request = await app.firstParty.requests.next();
// no cookies initially
Expand Down
25 changes: 16 additions & 9 deletions selenium/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const sw = require('selenium-webdriver'),
{createServer} = require('http'),
path = require('path'),
vhost = require('vhost'),
express = require('express');

Expand All @@ -10,7 +11,7 @@ function startApp(app, port=PORT) {
app.server.listen(port);
}

const path = '../src/.',
const srcDir = '../src/.',
PORT = 8000,
host = (hostname, port) => `${hostname}:${port}`,
firstPartyHostname = 'firstparty.local',
Expand All @@ -33,7 +34,7 @@ function stopApp(app) {
* 127.0.0.1 thirdparty.local
*/

function loadDriverWithExtension(extPath) {
async function loadDriverWithExtension(extPath) {
let chromeOptions = sw.Capabilities.chrome();
chromeOptions.set("chromeOptions", {"args": [
`--load-extension=${extPath}`,
Expand All @@ -45,11 +46,13 @@ function loadDriverWithExtension(extPath) {
.build();
}

function newDriver() {
return loadDriverWithExtension(path);
async function newDriver() {
const srcPath = path.resolve(__dirname, srcDir);
return await loadDriverWithExtension(srcPath);
}

class Channel {
// async stack datastructure
constructor() {
this.items = [];
this.waiting = [];
Expand All @@ -63,12 +66,16 @@ class Channel {
});
}
}

// Get the item from the top of the stack, or wait for an item if there are none.
async next() {
return await this.popQueue();
}

// Push an item onto the stack.
push(item) {
if (this.waiting.length > 0) {
this.waiting.shift()(item);
this.waiting.pop()(item);
} else {
this.items.push(item);
}
Expand All @@ -87,7 +94,7 @@ function requestRecorderMiddleware(app = express()) {
}

function firstPartyApp(app = express(), tpHost = thirdPartyHost) {
app.get('/', (req, res) => {
app.get('*', (req, res) => {
return res.send(
`<script type="text/javascript" src="http://${tpHost}/tracker.js"></script>`
);
Expand All @@ -96,7 +103,7 @@ function firstPartyApp(app = express(), tpHost = thirdPartyHost) {
}

function thirdPartyApp(app = express()) {
app.get('/tracker.js', (req, res) => {
app.get('*', (req, res) => {
return res.send('console.log("third party script")');
});
return app;
Expand All @@ -106,8 +113,8 @@ function thirdPartyApp(app = express()) {
function baseTestApp(fpApp, tpApp, app = express(), fpHostname = firstPartyHostname, tpHostname = thirdPartyHostname) {
let firstParty = firstPartyApp(fpApp),
thirdParty = thirdPartyApp(tpApp);
app.use(vhost(fpHostname, firstParty));
app.use(vhost(tpHostname, thirdParty));
app.all('/', vhost(fpHostname, firstParty));
app.all('/tracker.js', vhost(tpHostname, thirdParty));
Object.assign(app, {firstParty, thirdParty});
return app;
}
Expand Down
6 changes: 3 additions & 3 deletions src/js/webrequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,19 @@ class WebRequest {
onBeforeRequest.addListener(
this.onBeforeRequest.bind(this),
{urls: ["<all_urls>"]},
["blocking"]
["blocking"],
);

onBeforeSendHeaders.addListener(
this.onBeforeSendHeaders.bind(this),
{urls: ["<all_urls>"]},
["blocking", "requestHeaders"]
["blocking", "requestHeaders", "extraHeaders"],
);

onHeadersReceived.addListener(
this.onHeadersReceived.bind(this),
{urls: ["<all_urls>"]},
["blocking", "responseHeaders"]
["blocking", "responseHeaders", "extraHeaders"],
);
}

Expand Down

0 comments on commit af9f8fd

Please sign in to comment.