-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathgdprEnforcement.js
399 lines (362 loc) · 14.4 KB
/
gdprEnforcement.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/**
* This module gives publishers extra set of features to enforce individual purposes of TCF v2
*/
import {deepAccess, hasDeviceAccess, isArray, logWarn} from '../src/utils.js';
import {config} from '../src/config.js';
import adapterManager, {gdprDataHandler} from '../src/adapterManager.js';
import {find, includes} from '../src/polyfill.js';
import {registerSyncInner} from '../src/adapters/bidderFactory.js';
import {getHook} from '../src/hook.js';
import {validateStorageEnforcement} from '../src/storageManager.js';
import * as events from '../src/events.js';
import CONSTANTS from '../src/constants.json';
const TCF2 = {
'purpose1': { id: 1, name: 'storage' },
'purpose2': { id: 2, name: 'basicAds' },
'purpose7': { id: 7, name: 'measurement' }
}
/*
These rules would be used if `consentManagement.gdpr.rules` is undefined by the publisher.
*/
const DEFAULT_RULES = [{
purpose: 'storage',
enforcePurpose: true,
enforceVendor: true,
vendorExceptions: []
}, {
purpose: 'basicAds',
enforcePurpose: true,
enforceVendor: true,
vendorExceptions: []
}];
export let purpose1Rule;
export let purpose2Rule;
export let purpose7Rule;
export let enforcementRules;
const storageBlocked = [];
const biddersBlocked = [];
const analyticsBlocked = [];
let addedDeviceAccessHook = false;
// Helps in stubbing these functions in unit tests.
export const internal = {
getGvlidForBidAdapter,
getGvlidForUserIdModule,
getGvlidForAnalyticsAdapter
};
/**
* Returns GVL ID for a Bid adapter / an USERID submodule / an Analytics adapter.
* If modules of different types have the same moduleCode: For example, 'appnexus' is the code for both Bid adapter and Analytics adapter,
* then, we assume that their GVL IDs are same. This function first checks if GVL ID is defined for a Bid adapter, if not found, tries to find User ID
* submodule's GVL ID, if not found, tries to find Analytics adapter's GVL ID. In this process, as soon as it finds a GVL ID, it returns it
* without going to the next check.
* @param {{string|Object}} - module
* @return {number} - GVL ID
*/
export function getGvlid(module) {
let gvlid = null;
if (module) {
// Check user defined GVL Mapping in pbjs.setConfig()
const gvlMapping = config.getConfig('gvlMapping');
// For USER ID Module, we pass the submodule object itself as the "module" parameter, this check is required to grab the module code
const moduleCode = typeof module === 'string' ? module : module.name;
// Return GVL ID from user defined gvlMapping
if (gvlMapping && gvlMapping[moduleCode]) {
gvlid = gvlMapping[moduleCode];
return gvlid;
}
gvlid = internal.getGvlidForBidAdapter(moduleCode) || internal.getGvlidForUserIdModule(module) || internal.getGvlidForAnalyticsAdapter(moduleCode);
}
return gvlid;
}
/**
* Returns GVL ID for a bid adapter. If the adapter does not have an associated GVL ID, it returns 'null'.
* @param {string=} bidderCode - The 'code' property of the Bidder spec.
* @return {number} GVL ID
*/
function getGvlidForBidAdapter(bidderCode) {
let gvlid = null;
bidderCode = bidderCode || config.getCurrentBidder();
if (bidderCode) {
const bidder = adapterManager.getBidAdapter(bidderCode);
if (bidder && bidder.getSpec) {
gvlid = bidder.getSpec().gvlid;
}
}
return gvlid;
}
/**
* Returns GVL ID for an userId submodule. If an userId submodules does not have an associated GVL ID, it returns 'null'.
* @param {Object} userIdModule
* @return {number} GVL ID
*/
function getGvlidForUserIdModule(userIdModule) {
return (typeof userIdModule === 'object' ? userIdModule.gvlid : null);
}
/**
* Returns GVL ID for an analytics adapter. If an analytics adapter does not have an associated GVL ID, it returns 'null'.
* @param {string} code - 'provider' property on the analytics adapter config
* @return {number} GVL ID
*/
function getGvlidForAnalyticsAdapter(code) {
return adapterManager.getAnalyticsAdapter(code) && (adapterManager.getAnalyticsAdapter(code).gvlid || null);
}
/**
* This function takes in a rule and consentData and validates against the consentData provided. Depending on what it returns,
* the caller may decide to suppress a TCF-sensitive activity.
* @param {Object} rule - enforcement rules set in config
* @param {Object} consentData - gdpr consent data
* @param {string=} currentModule - Bidder code of the current module
* @param {number=} gvlId - GVL ID for the module
* @returns {boolean}
*/
export function validateRules(rule, consentData, currentModule, gvlId) {
const purposeId = TCF2[Object.keys(TCF2).filter(purposeName => TCF2[purposeName].name === rule.purpose)[0]].id;
// return 'true' if vendor present in 'vendorExceptions'
if (includes(rule.vendorExceptions || [], currentModule)) {
return true;
}
// get data from the consent string
const purposeConsent = deepAccess(consentData, `vendorData.purpose.consents.${purposeId}`);
const vendorConsent = deepAccess(consentData, `vendorData.vendor.consents.${gvlId}`);
const liTransparency = deepAccess(consentData, `vendorData.purpose.legitimateInterests.${purposeId}`);
/*
Since vendor exceptions have already been handled, the purpose as a whole is allowed if it's not being enforced
or the user has consented. Similar with vendors.
*/
const purposeAllowed = rule.enforcePurpose === false || purposeConsent === true;
const vendorAllowed = rule.enforceVendor === false || vendorConsent === true;
/*
Few if any vendors should be declaring Legitimate Interest for Device Access (Purpose 1), but some are claiming
LI for Basic Ads (Purpose 2). Prebid.js can't check to see who's declaring what legal basis, so if LI has been
established for Purpose 2, allow the auction to take place and let the server sort out the legal basis calculation.
*/
if (purposeId === 2) {
return (purposeAllowed && vendorAllowed) || (liTransparency === true);
}
return purposeAllowed && vendorAllowed;
}
/**
* This hook checks whether module has permission to access device or not. Device access include cookie and local storage
* @param {Function} fn reference to original function (used by hook logic)
* @param {Number=} gvlid gvlid of the module
* @param {string=} moduleName name of the module
*/
export function deviceAccessHook(fn, gvlid, moduleName, result) {
result = Object.assign({}, {
hasEnforcementHook: true
});
if (!hasDeviceAccess()) {
logWarn('Device access is disabled by Publisher');
result.valid = false;
fn.call(this, gvlid, moduleName, result);
} else {
const consentData = gdprDataHandler.getConsentData();
if (consentData && consentData.gdprApplies) {
if (consentData.apiVersion === 2) {
const curBidder = config.getCurrentBidder();
// Bidders have a copy of storage object with bidder code binded. Aliases will also pass the same bidder code when invoking storage functions and hence if alias tries to access device we will try to grab the gvl id for alias instead of original bidder
if (curBidder && (curBidder != moduleName) && adapterManager.aliasRegistry[curBidder] === moduleName) {
gvlid = getGvlid(curBidder);
} else {
gvlid = getGvlid(moduleName) || gvlid;
}
const curModule = moduleName || curBidder;
let isAllowed = validateRules(purpose1Rule, consentData, curModule, gvlid);
if (isAllowed) {
result.valid = true;
fn.call(this, gvlid, moduleName, result);
} else {
curModule && logWarn(`TCF2 denied device access for ${curModule}`);
result.valid = false;
storageBlocked.push(curModule);
fn.call(this, gvlid, moduleName, result);
}
} else {
// The module doesn't enforce TCF1.1 strings
result.valid = true;
fn.call(this, gvlid, moduleName, result);
}
} else {
result.valid = true;
fn.call(this, gvlid, moduleName, result);
}
}
}
/**
* This hook checks if a bidder has consent for user sync or not
* @param {Function} fn reference to original function (used by hook logic)
* @param {...any} args args
*/
export function userSyncHook(fn, ...args) {
const consentData = gdprDataHandler.getConsentData();
if (consentData && consentData.gdprApplies) {
if (consentData.apiVersion === 2) {
const curBidder = config.getCurrentBidder();
const gvlid = getGvlid(curBidder);
let isAllowed = validateRules(purpose1Rule, consentData, curBidder, gvlid);
if (isAllowed) {
fn.call(this, ...args);
} else {
logWarn(`User sync not allowed for ${curBidder}`);
storageBlocked.push(curBidder);
}
} else {
// The module doesn't enforce TCF1.1 strings
fn.call(this, ...args);
}
} else {
fn.call(this, ...args);
}
}
/**
* This hook checks if user id module is given consent or not
* @param {Function} fn reference to original function (used by hook logic)
* @param {Submodule[]} submodules Array of user id submodules
* @param {Object} consentData GDPR consent data
*/
export function userIdHook(fn, submodules, consentData) {
if (consentData && consentData.gdprApplies) {
if (consentData.apiVersion === 2) {
let userIdModules = submodules.map((submodule) => {
const gvlid = getGvlid(submodule.submodule);
const moduleName = submodule.submodule.name;
let isAllowed = validateRules(purpose1Rule, consentData, moduleName, gvlid);
if (isAllowed) {
return submodule;
} else {
logWarn(`User denied permission to fetch user id for ${moduleName} User id module`);
storageBlocked.push(moduleName);
}
return undefined;
}).filter(module => module)
fn.call(this, userIdModules, { ...consentData, hasValidated: true });
} else {
// The module doesn't enforce TCF1.1 strings
fn.call(this, submodules, consentData);
}
} else {
fn.call(this, submodules, consentData);
}
}
/**
* Checks if bidders are allowed in the auction.
* Enforces "purpose 2 (Basic Ads)" of TCF v2.0 spec
* @param {Function} fn - Function reference to the original function.
* @param {Array<adUnits>} adUnits
*/
export function makeBidRequestsHook(fn, adUnits, ...args) {
const consentData = gdprDataHandler.getConsentData();
if (consentData && consentData.gdprApplies) {
if (consentData.apiVersion === 2) {
adUnits.forEach(adUnit => {
adUnit.bids = adUnit.bids.filter(bid => {
const currBidder = bid.bidder;
const gvlId = getGvlid(currBidder);
if (includes(biddersBlocked, currBidder)) return false;
const isAllowed = !!validateRules(purpose2Rule, consentData, currBidder, gvlId);
if (!isAllowed) {
logWarn(`TCF2 blocked auction for ${currBidder}`);
biddersBlocked.push(currBidder);
}
return isAllowed;
});
});
fn.call(this, adUnits, ...args);
} else {
// The module doesn't enforce TCF1.1 strings
fn.call(this, adUnits, ...args);
}
} else {
fn.call(this, adUnits, ...args);
}
}
/**
* Checks if Analytics adapters are allowed to send data to their servers for furhter processing.
* Enforces "purpose 7 (Measurement)" of TCF v2.0 spec
* @param {Function} fn - Function reference to the original function.
* @param {Array<AnalyticsAdapterConfig>} config - Configuration object passed to pbjs.enableAnalytics()
*/
export function enableAnalyticsHook(fn, config) {
const consentData = gdprDataHandler.getConsentData();
if (consentData && consentData.gdprApplies) {
if (consentData.apiVersion === 2) {
if (!isArray(config)) {
config = [config]
}
config = config.filter(conf => {
const analyticsAdapterCode = conf.provider;
const gvlid = getGvlid(analyticsAdapterCode);
const isAllowed = !!validateRules(purpose7Rule, consentData, analyticsAdapterCode, gvlid);
if (!isAllowed) {
analyticsBlocked.push(analyticsAdapterCode);
logWarn(`TCF2 blocked analytics adapter ${conf.provider}`);
}
return isAllowed;
});
fn.call(this, config);
} else {
// This module doesn't enforce TCF1.1 strings
fn.call(this, config);
}
} else {
fn.call(this, config);
}
}
/**
* Compiles the TCF2.0 enforcement results into an object, which is emitted as an event payload to "tcf2Enforcement" event.
*/
function emitTCF2FinalResults() {
// remove null and duplicate values
const formatArray = function (arr) {
return arr.filter((i, k) => i !== null && arr.indexOf(i) === k);
}
const tcf2FinalResults = {
storageBlocked: formatArray(storageBlocked),
biddersBlocked: formatArray(biddersBlocked),
analyticsBlocked: formatArray(analyticsBlocked)
};
events.emit(CONSTANTS.EVENTS.TCF2_ENFORCEMENT, tcf2FinalResults);
}
events.on(CONSTANTS.EVENTS.AUCTION_END, emitTCF2FinalResults);
/*
Set of callback functions used to detect presence of a TCF rule, passed as the second argument to find().
*/
const hasPurpose1 = (rule) => { return rule.purpose === TCF2.purpose1.name }
const hasPurpose2 = (rule) => { return rule.purpose === TCF2.purpose2.name }
const hasPurpose7 = (rule) => { return rule.purpose === TCF2.purpose7.name }
/**
* A configuration function that initializes some module variables, as well as adds hooks
* @param {Object} config - GDPR enforcement config object
*/
export function setEnforcementConfig(config) {
const rules = deepAccess(config, 'gdpr.rules');
if (!rules) {
logWarn('TCF2: enforcing P1 and P2 by default');
enforcementRules = DEFAULT_RULES;
} else {
enforcementRules = rules;
}
purpose1Rule = find(enforcementRules, hasPurpose1);
purpose2Rule = find(enforcementRules, hasPurpose2);
purpose7Rule = find(enforcementRules, hasPurpose7);
if (!purpose1Rule) {
purpose1Rule = DEFAULT_RULES[0];
}
if (!purpose2Rule) {
purpose2Rule = DEFAULT_RULES[1];
}
if (purpose1Rule && !addedDeviceAccessHook) {
addedDeviceAccessHook = true;
validateStorageEnforcement.before(deviceAccessHook, 49);
registerSyncInner.before(userSyncHook, 48);
// Using getHook as user id and gdprEnforcement are both optional modules. Using import will auto include the file in build
getHook('validateGdprEnforcement').before(userIdHook, 47);
}
if (purpose2Rule) {
getHook('makeBidRequests').before(makeBidRequestsHook);
}
if (purpose7Rule) {
getHook('enableAnalyticsCb').before(enableAnalyticsHook);
}
}
config.getConfig('consentManagement', config => setEnforcementConfig(config.consentManagement));