Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added whitelist for loading external JS + tests #2430

Merged
merged 3 commits into from
May 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions src/adloader.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
var utils = require('./utils');
let _requestCache = {};
import includes from 'core-js/library/fn/array/includes';
import * as utils from './utils';

const _requestCache = {};
const _vendorWhitelist = [
'criteo',
]

/**
* Loads external javascript. Can only be used if external JS is approved by Prebid. See https://github.com/prebid/prebid-js-external-js-template#policy
* Each unique URL will be loaded at most 1 time.
* @param {string} url the url to load
* @param {string} moduleCode bidderCode or module code of the module requesting this resource
*/
Expand All @@ -11,18 +17,23 @@ exports.loadExternalScript = function(url, moduleCode) {
utils.logError('cannot load external script without url and moduleCode');
return;
}
if (!includes(_vendorWhitelist, moduleCode)) {
utils.logError(`${moduleCode} not whitelisted for loading external JavaScript`);
return;
}
// only load each asset once
if (_requestCache[url]) {
return;
}

utils.logWarn(`module ${moduleCode} is loading external JavaScript`);
const script = document.createElement('script');
script.type = 'text/javascript';
script.async = true;

script.src = url;

// add the new script tag to the page
const target = document.head || document.body;
if (target) {
target.appendChild(script);
}
utils.insertElement(script);
_requestCache[url] = true;
};

/**
Expand Down
31 changes: 29 additions & 2 deletions test/spec/adloader_spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,31 @@
import * as utils from 'src/utils';
import * as adLoader from 'src/adloader';

describe('adLoader', function () {
var assert = require('chai').assert,
adLoader = require('../../src/adloader');
let utilsinsertElementStub;
let utilsLogErrorStub;

beforeEach(() => {
utilsinsertElementStub = sinon.stub(utils, 'insertElement');
utilsLogErrorStub = sinon.stub(utils, 'logError');
});

afterEach(() => {
utilsinsertElementStub.restore();
utilsLogErrorStub.restore();
});

describe('loadExternalScript', () => {
it('requires moduleCode to be included on the request', () => {
adLoader.loadExternalScript('someURL');
expect(utilsLogErrorStub.called).to.be.true;
expect(utilsinsertElementStub.called).to.be.false;
});

it('only allows whitelisted vendors to load scripts', () => {
adLoader.loadExternalScript('someURL', 'criteo');
expect(utilsLogErrorStub.called).to.be.false;
expect(utilsinsertElementStub.called).to.be.true;
});
});
});
14 changes: 0 additions & 14 deletions test/spec/modules/appnexusBidAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import { spec } from 'modules/appnexusBidAdapter';
import { newBidder } from 'src/adapters/bidderFactory';
import { deepClone } from 'src/utils';

const adloader = require('../../../src/adloader');

const ENDPOINT = '//ib.adnxs.com/ut/v3/prebid';

describe('AppNexusAdapter', () => {
Expand Down Expand Up @@ -309,18 +307,6 @@ describe('AppNexusAdapter', () => {
})

describe('interpretResponse', () => {
let loadScriptStub;

beforeEach(() => {
loadScriptStub = sinon.stub(adloader, 'loadScript').callsFake((...args) => {
args[1]();
});
});

afterEach(() => {
loadScriptStub.restore();
});

let response = {
'version': '3.0.0',
'tags': [
Expand Down