-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathenforcer.js
747 lines (683 loc) · 23.7 KB
/
enforcer.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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
/**
* @license
* Copyright 2017 Google Inc. All Rights Reserved.
*
* Licensed under the W3C SOFTWARE AND DOCUMENT NOTICE AND LICENSE.
*
* https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
*/
/* eslint-disable no-unused-vars */
import {DIRECTIVE_NAME, TrustedTypeConfig} from './data/trustedtypeconfig.js';
import {TrustedTypes, setAllowedPolicyNames} from './trustedtypes.js';
/* eslint-enable no-unused-vars */
import {installFunction, installSetter, installSetterAndGetter}
from './utils/wrapper.js';
const {apply} = Reflect;
const {
getOwnPropertyNames,
getOwnPropertyDescriptor,
hasOwnProperty,
getPrototypeOf,
isPrototypeOf,
} = Object;
const {slice} = String.prototype;
const UrlConstructor = URL.prototype.constructor;
// This is not available in release Firefox :(
// https://developer.mozilla.org/en-US/docs/Web/API/SecurityPolicyViolationEvent
// https://bugzilla.mozilla.org/show_bug.cgi?id=1432523
const SecurityPolicyViolationEvent = window['SecurityPolicyViolationEvent'] ||
null;
/**
* Parses URL, catching all the errors.
* @param {string} url URL string to parse.
* @return {URL|null}
*/
function parseUrl_(url) {
try {
return new UrlConstructor(url, document.baseURI || undefined);
} catch (e) {
return null;
}
}
/**
* Checks if the URL is a HTTP(s) URL.
* @param {string} url The URL to check.
* @return {boolean} True iff the value is a http(s) URL.
*/
function isHttpUrl_(url) {
const parsedUrl = parseUrl_(url);
if (!parsedUrl) {
return false;
}
return parsedUrl.protocol == 'http:' || parsedUrl.protocol == 'https:';
}
/**
* A map of attribute names to allowed types.
* @type {!Object<string, !Object<string, !Function>>}
*/
let SET_ATTRIBUTE_TYPE_MAP = {
// TODO(slekies): Add SVG Elements here
// TODO(koto): Figure out what to to with <link>
'HTMLAnchorElement': {
'href': TrustedTypes.TrustedURL,
},
'HTMLAreaElement': {
'href': TrustedTypes.TrustedURL,
},
'HTMLBaseElement': {
'href': TrustedTypes.TrustedURL,
},
'HTMLButtonElement': {
'formaction': TrustedTypes.TrustedURL,
},
'HTMLSourceElement': {
'src': TrustedTypes.TrustedURL,
},
'HTMLImageElement': {
'src': TrustedTypes.TrustedURL,
// TODO(slekies): add special handling for srcset
},
'HTMLTrackElement': {
'src': TrustedTypes.TrustedURL,
},
'HTMLMediaElement': {
'src': TrustedTypes.TrustedURL,
},
'HTMLInputElement': {
'src': TrustedTypes.TrustedURL,
'formaction': TrustedTypes.TrustedURL,
},
'HTMLFrameElement': {
'src': TrustedTypes.TrustedURL,
},
'HTMLIFrameElement': {
'src': TrustedTypes.TrustedURL,
'srcdoc': TrustedTypes.TrustedHTML,
},
'HTMLLinkElement': {
'href': TrustedTypes.TrustedScriptURL,
},
'HTMLObjectElement': {
'data': TrustedTypes.TrustedScriptURL,
'codebase': TrustedTypes.TrustedScriptURL,
},
'HTMLEmbedElement': {
'src': TrustedTypes.TrustedScriptURL,
},
'HTMLScriptElement': {
'src': TrustedTypes.TrustedScriptURL,
'text': TrustedTypes.TrustedScript,
},
'HTMLElement': {
},
};
// Add inline event handlers property names.
for (let name of getOwnPropertyNames(HTMLElement.prototype)) {
if (name.slice(0, 2) === 'on') {
SET_ATTRIBUTE_TYPE_MAP['HTMLElement'][name] = TrustedTypes.TrustedScript;
}
}
/**
* Map of type names to type checking function.
* @type {!Object<string,!Function>}
*/
const TYPE_CHECKER_MAP = {
'TrustedHTML': TrustedTypes.isHTML,
'TrustedURL': TrustedTypes.isURL,
'TrustedScriptURL': TrustedTypes.isScriptURL,
'TrustedScript': TrustedTypes.isScript,
};
/**
* Map of type names to type producing function.
* @type {Object<string,string>}
*/
const TYPE_PRODUCER_MAP = {
'TrustedHTML': 'createHTML',
'TrustedURL': 'createURL',
'TrustedScriptURL': 'createScriptURL',
'TrustedScript': 'createScript',
};
/**
* @type {function(string):?TrustedTypePolicy}
*/
const getExposedPolicy = TrustedTypes.getExposedPolicy;
/* eslint-disable no-unused-vars */
/**
* @typedef {TrustedTypePolicy}
* @property {function(string):TrustedHTML} createHTML
* @property {function(string):TrustedURL} createURL
* @property {function(string):TrustedScriptURL} createScriptURL
* @property {function(string):TrustedScript} createScript
*/
let TrustedTypePolicy = {};
/* eslint-enable no-unused-vars */
/**
* A map of HTML attribute to element property names.
* @type {!Object<string, string>}
*/
const ATTR_PROPERTY_MAP = {
'codebase': 'codeBase',
'formaction': 'formAction',
};
/**
* An object for enabling trusted type enforcement.
*/
export class TrustedTypesEnforcer {
/**
* @param {!TrustedTypeConfig} config The configuration for
* trusted type enforcement.
*/
constructor(config) {
/**
* A configuration for the trusted type enforcement.
* @private {!TrustedTypeConfig}
*/
this.config_ = config;
/**
* @private {Object<string, function(*): *|undefined>}
*/
this.originalSetters_ = {};
}
/**
* Wraps HTML sinks with an enforcement setter, which will enforce
* trusted types and do logging, if enabled.
*
*/
install() {
setAllowedPolicyNames(this.config_.allowedPolicyNames);
if (!this.config_.isEnforcementEnabled && !this.config_.isLoggingEnabled) {
return;
}
this.wrapSetter_(Element.prototype, 'innerHTML', TrustedTypes.TrustedHTML);
this.wrapSetter_(Element.prototype, 'outerHTML', TrustedTypes.TrustedHTML);
if ('ShadowRoot' in window) {
this.wrapSetter_(ShadowRoot.prototype, 'innerHTML',
TrustedTypes.TrustedHTML);
}
this.wrapWithEnforceFunction_(Range.prototype, 'createContextualFragment',
TrustedTypes.TrustedHTML, 0);
this.wrapWithEnforceFunction_(Element.prototype, 'insertAdjacentHTML',
TrustedTypes.TrustedHTML, 1);
if (getOwnPropertyDescriptor(Document.prototype, 'write')) {
// Chrome
this.wrapWithEnforceFunction_(Document.prototype, 'write',
TrustedTypes.TrustedHTML, 0);
this.wrapWithEnforceFunction_(Document.prototype, 'open',
TrustedTypes.TrustedURL, 0);
} else {
// Firefox
this.wrapWithEnforceFunction_(HTMLDocument.prototype, 'write',
TrustedTypes.TrustedHTML, 0);
this.wrapWithEnforceFunction_(HTMLDocument.prototype, 'open',
TrustedTypes.TrustedURL, 0);
}
this.wrapWithEnforceFunction_(window, 'open', TrustedTypes.TrustedURL, 0);
if ('DOMParser' in window) {
this.wrapWithEnforceFunction_(DOMParser.prototype, 'parseFromString',
TrustedTypes.TrustedHTML, 0);
}
this.wrapWithEnforceFunction_(window, 'setInterval',
TrustedTypes.TrustedScript, 0);
this.wrapWithEnforceFunction_(window, 'setTimeout',
TrustedTypes.TrustedScript, 0);
this.wrapSetAttribute_();
this.installScriptWrappers_();
this.installPropertySetWrappers_();
}
/**
* Removes the original setters.
*/
uninstall() {
setAllowedPolicyNames(['*']);
if (!this.config_.isEnforcementEnabled && !this.config_.isLoggingEnabled) {
return;
}
this.restoreSetter_(Element.prototype, 'innerHTML');
this.restoreSetter_(Element.prototype, 'outerHTML');
if ('ShadowRoot' in window) {
this.restoreSetter_(ShadowRoot.prototype, 'innerHTML');
}
this.restoreFunction_(Range.prototype, 'createContextualFragment');
this.restoreFunction_(Element.prototype, 'insertAdjacentHTML');
this.restoreFunction_(Element.prototype, 'setAttribute');
this.restoreFunction_(Element.prototype, 'setAttributeNS');
if (getOwnPropertyDescriptor(Document.prototype, 'write')) {
this.restoreFunction_(Document.prototype, 'write');
this.restoreFunction_(Document.prototype, 'open');
} else {
this.restoreFunction_(HTMLDocument.prototype, 'write');
this.restoreFunction_(HTMLDocument.prototype, 'open');
}
this.restoreFunction_(window, 'open');
if ('DOMParser' in window) {
this.restoreFunction_(DOMParser.prototype, 'parseFromString');
}
this.restoreFunction_(window, 'setTimeout');
this.restoreFunction_(window, 'setInterval');
this.uninstallPropertySetWrappers_();
this.uninstallScriptWrappers_();
}
/**
* Installs wrappers for setting properties of script elements.
*/
installScriptWrappers_() {
// HTMLScript element has no own setters for crucial properties, we have to
// reuse ones from HTMLElement.
this.wrapSetter_(HTMLScriptElement.prototype, 'innerText',
TrustedTypes.TrustedScript, HTMLElement.prototype);
this.wrapSetter_(HTMLScriptElement.prototype, 'textContent',
TrustedTypes.TrustedScript, Node.prototype);
}
/**
* Uninstalls wrappers for setting properties of script elements.
*/
uninstallScriptWrappers_() {
this.restoreSetter_(HTMLScriptElement.prototype, 'innerText',
HTMLElement.prototype);
this.restoreSetter_(HTMLScriptElement.prototype, 'textContent',
Node.prototype);
}
/**
* Installs wrappers for directly setting properties
* based on SET_ATTRIBUTE_TYPE_MAP.
* @private
*/
installPropertySetWrappers_() {
/* eslint-disable guard-for-in */
for (let type of getOwnPropertyNames(SET_ATTRIBUTE_TYPE_MAP)) {
for (let attribute of getOwnPropertyNames(SET_ATTRIBUTE_TYPE_MAP[type])) {
const property = apply(hasOwnProperty, ATTR_PROPERTY_MAP, [attribute]) ?
ATTR_PROPERTY_MAP[attribute] : attribute;
this.wrapSetter_(window[type].prototype, property,
SET_ATTRIBUTE_TYPE_MAP[type][attribute]);
}
}
}
/**
* Uninstalls wrappers for directly setting properties
* based on SET_ATTRIBUTE_TYPE_MAP.
* @private
*/
uninstallPropertySetWrappers_() {
/* eslint-disable guard-for-in */
for (let type of getOwnPropertyNames(SET_ATTRIBUTE_TYPE_MAP)) {
for (let attribute of getOwnPropertyNames(SET_ATTRIBUTE_TYPE_MAP[type])) {
const property = attribute in ATTR_PROPERTY_MAP ?
ATTR_PROPERTY_MAP[attribute] : attribute;
this.restoreSetter_(window[type].prototype, property);
}
}
}
/** Wraps set attribute with an enforcement function. */
wrapSetAttribute_() {
let that = this;
this.wrapFunction_(
Element.prototype,
'setAttribute',
/**
* @this {TrustedTypesEnforcer}
* @param {function(!Function, ...*)} originalFn
* @return {*}
*/
function(originalFn, ...args) {
return that.setAttributeWrapper_
.bind(that, this, originalFn)
.apply(that, args);
});
this.wrapFunction_(
Element.prototype,
'setAttributeNS',
/**
* @this {TrustedTypesEnforcer}
* @param {function(!Function, ...*)} originalFn
* @return {*}
*/
function(originalFn, ...args) {
return that.setAttributeNSWrapper_
.bind(that, this, originalFn)
.apply(that, args);
});
}
/**
* Returns the required type for the setAtrtibute call.
* @param {!Object} context The object to infer the type of attribute of.
* @param {string} attrName The attribute name.
* @return {Function} The type to enforce, or null if no contract is found.
*/
getRequiredTypeForAttribute_(context, attrName) {
let ctor = context.constructor;
do {
let type = ctor && ctor.name &&
SET_ATTRIBUTE_TYPE_MAP[ctor.name] &&
SET_ATTRIBUTE_TYPE_MAP[ctor.name][attrName];
if (type || ctor == HTMLElement) {
// Stop at HTMLElement.
return type;
}
// Explore the prototype chain.
} while (ctor = getPrototypeOf(ctor));
return null;
}
/**
* Enforces type checking for Element.prototype.setAttribute.
* @param {!Object} context The context for the call to the original function.
* @param {!Function} originalFn The original setAttribute function.
* @return {*}
*/
setAttributeWrapper_(context, originalFn, ...args) {
// Note(slekies): In a normal application constructor should never be null.
// However, there are no guarantees. If the constructor is null, we cannot
// determine whether a special type is required. In order to not break the
// application, we will not do any further type checks and pass the call
// to setAttribute.
if (context.constructor !== null) {
let attrName = (args[0] = String(args[0])).toLowerCase();
let type = this.getRequiredTypeForAttribute_(context, attrName);
if (type instanceof Function) {
return this.enforce_(
context, 'setAttribute', type, originalFn, 1, args);
}
}
return originalFn.apply(context, args);
}
/**
* Enforces type checking for Element.prototype.setAttributeNS.
* @param {!Object} context The context for the call to the original function.
* @param {!Function} originalFn The original setAttributeNS function.
* @return {*}
*/
setAttributeNSWrapper_(context, originalFn, ...args) {
/**
* @param {string} ns the namespace URL.
* @return {boolean} true iff the given argument is an HTML namespace.
*/
function isHtmlNamespace(ns) {
return true; // TODO(msamuel): implement me
}
// See the note from setAttributeWrapper_ above.
if (context.constructor !== null) {
let ns = (args[0] = String(args[0])).toLowerCase();
let attrName = (args[1] = String(args[1])).toLowerCase();
if (isHtmlNamespace(ns)) {
let type = context.constructor && context.constructor.name &&
SET_ATTRIBUTE_TYPE_MAP[context.constructor.name] &&
SET_ATTRIBUTE_TYPE_MAP[context.constructor.name][attrName];
if (type instanceof Function) {
return this.enforce_(
context, 'setAttributeNS', type, originalFn, 2, args);
}
}
// TODO(msamuel): handle SVG namespace. See TODO(slekies) above.
}
return originalFn.apply(context, args);
}
/**
* Wraps a setter with the enforcement wrapper.
* @param {!Object} object The object of the to-be-wrapped property.
* @param {string} name The name of the property.
* @param {!Function} type The type to enforce.
* @param {number} argNumber Number of the argument to enforce the type of.
* @private
*/
wrapWithEnforceFunction_(object, name, type, argNumber) {
let that = this;
this.wrapFunction_(
object,
name,
/**
* @this {TrustedTypesEnforcer}
* @param {function(!Function, ...*)} originalFn
* @return {*}
*/
function(originalFn, ...args) {
return that.enforce_.call(that, this, name, type, originalFn,
argNumber, args);
});
}
/**
* Wraps an existing function with a given function body and stores the
* original function.
* @param {!Object} object The object of the to-be-wrapped property.
* @param {string} name The name of the property.
* @param {function(!Function, ...*)} functionBody The wrapper function.
*/
wrapFunction_(object, name, functionBody) {
let descriptor = getOwnPropertyDescriptor(object, name);
let originalFn = /** @type function(*):* */ (
descriptor ? descriptor.value : null);
if (!(originalFn instanceof Function)) {
throw new TypeError(
'Property ' + name + ' on object' + object + ' is not a function');
}
let key = this.getKey_(object, name);
if (this.originalSetters_[key]) {
throw new Error('TrustedTypesEnforcer: Double installation detected');
}
installFunction(
object,
name,
/**
* @this {TrustedTypesEnforcer}
* @return {*}
*/
function(...args) {
return functionBody.bind(this, originalFn).apply(this, args);
});
this.originalSetters_[key] = originalFn;
}
/**
* Wraps a setter with the enforcement wrapper.
* @param {!Object} object The object of the to-be-wrapped property.
* @param {string} name The name of the property.
* @param {!Function} type The type to enforce.
* @param {!Object=} descriptorObject If present, will reuse the
* setter/getter from this one, instead of object. Used for redefining
* setters in subclasses.
* @private
*/
wrapSetter_(object, name, type, descriptorObject = undefined) {
if (descriptorObject && !isPrototypeOf.call(descriptorObject, object)) {
throw new Error('Invalid prototype chain');
}
let useObject = descriptorObject || object;
let descriptor = getOwnPropertyDescriptor(useObject, name);
let originalSetter = /** @type {function(*):*} */ (descriptor ?
descriptor.set : null);
if (!(originalSetter instanceof Function)) {
throw new TypeError(
'No setter for property ' + name + ' on object' + object);
}
let key = this.getKey_(object, name);
if (this.originalSetters_[key]) {
throw new Error('TrustedTypesEnforcer: Double installation detected');
}
let that = this;
/**
* @this {TrustedTypesEnforcer}
* @param {*} value
*/
let enforcingSetter = function(value) {
that.enforce_.call(that, this, name, type, originalSetter, 0,
[value]);
};
if (useObject === object) {
installSetter(
object,
name,
enforcingSetter);
} else {
// Since we're creating a new setter in subclass, we also need to
// overwrite the getter.
installSetterAndGetter(
object,
name,
enforcingSetter,
descriptor.get
);
}
this.originalSetters_[key] = originalSetter;
}
/**
* Restores the original setter for the property, as encountered during
* install().
* @param {!Object} object The object of the to-be-wrapped property.
* @param {string} name The name of the property.
* @param {!Object=} descriptorObject If present, will restore the original
* setter/getter from this one, instead of object.
* @private
*/
restoreSetter_(object, name, descriptorObject = undefined) {
let key = this.getKey_(object, name);
if (descriptorObject && !isPrototypeOf.call(descriptorObject, object)) {
throw new Error('Invalid prototype chain');
}
if (!this.originalSetters_[key]) {
throw new Error(
'TrustedTypesEnforcer: Cannot restore (double uninstallation?)');
}
if (descriptorObject) {
// We have to also overwrite a getter.
installSetterAndGetter(object, name, this.originalSetters_[key],
getOwnPropertyDescriptor(descriptorObject, name).get);
} else {
installSetter(object, name, this.originalSetters_[key]);
}
delete this.originalSetters_[key];
}
/**
* Restores the original method of an object, as encountered during install().
* @param {!Object} object The object of the to-be-wrapped property.
* @param {string} name The name of the property.
* @private
*/
restoreFunction_(object, name) {
let key = this.getKey_(object, name);
if (!this.originalSetters_[key]) {
throw new Error(
'TrustedTypesEnforcer: Cannot restore (double uninstallation?)');
}
installFunction(object, name, this.originalSetters_[key]);
delete this.originalSetters_[key];
}
/**
* Returns the key name for caching original setters.
* @param {!Object} object The object of the to-be-wrapped property.
* @param {string} name The name of the property.
* @return {string} Key name.
* @private
*/
getKey_(object, name) {
// TODO(msamuel): Can we use Object.prototype.toString.call(object)
// to get an unspoofable string here?
// TODO(msamuel): fail on '-' in object.constructor.name?
return '' + object.constructor.name + '-' + name;
}
/**
* Logs and enforces TrustedTypes depending on the given configuration.
* @template T
* @param {!Object} context The object that the setter is called for.
* @param {string} propertyName The name of the property.
* @param {!Function} typeToEnforce The type to enforce.
* @param {function(?):T} originalSetter Original setter.
* @param {number} argNumber Number of argument to enforce the type of.
* @param {Array} args Arguments.
* @return {T}
* @private
*/
enforce_(context, propertyName, typeToEnforce, originalSetter, argNumber,
args) {
let value = args[argNumber];
const typeName = '' + typeToEnforce.name;
// If typed value is given, pass through.
if (TYPE_CHECKER_MAP.hasOwnProperty(typeName) &&
TYPE_CHECKER_MAP[typeName](value)) {
return apply(originalSetter, context, args);
}
if (typeToEnforce === TrustedTypes.TrustedScript) {
const isInlineEventHandler =
propertyName == 'setAttribute' ||
propertyName === 'setAttributeNS' ||
apply(slice, propertyName, [0, 2]) === 'on';
// If a function (instead of string) is passed to inline event attribute,
// or set(Timeout|Interval), pass through.
const propertyAcceptsFunctions =
propertyName === 'setInterval' ||
propertyName === 'setTimeout' ||
isInlineEventHandler;
if ((propertyAcceptsFunctions && typeof value === 'function') ||
(isInlineEventHandler && value === null)) {
return apply(originalSetter, context, args);
}
}
// Apply url-allow-http
if (typeToEnforce === TrustedTypes.TrustedURL &&
this.config_.allowHttpUrls) {
const url = '' + value;
if (isHttpUrl_(url)) {
args[argNumber] = url;
return apply(originalSetter, context, args);
}
}
// Apply a fallback policy, if it exists.
const fallback = this.config_.fallbackPolicyName;
if (fallback) {
const fallbackPolicy = getExposedPolicy.call(TrustedTypes, fallback);
if (fallbackPolicy && TYPE_CHECKER_MAP.hasOwnProperty(typeName)) {
let fallbackValue;
let exceptionThrown;
try {
fallbackValue = fallbackPolicy[TYPE_PRODUCER_MAP[typeName]](value);
} catch (e) {
exceptionThrown = true;
}
if (!exceptionThrown) {
args[argNumber] = fallbackValue;
return apply(originalSetter, context, args);
}
}
}
let contextName = context.constructor.name || '' + context;
let message = `Failed to set ${propertyName} on ${contextName}: `
+ `This property requires ${typeName}.`;
if (this.config_.isLoggingEnabled) {
// eslint-disable-next-line no-console
console.warn(message, propertyName, context, typeToEnforce, value);
}
// Unconditionally dispatch an event.
if (typeof SecurityPolicyViolationEvent == 'function') {
let blockedURI = '';
if (typeToEnforce === TrustedTypes.TrustedURL ||
typeToEnforce === TrustedTypes.TrustedScriptURL) {
blockedURI = parseUrl_(value) || '';
if (blockedURI) {
blockedURI = blockedURI.href;
}
}
const valueSlice = apply(slice, '' + value, [0, 40]);
const event = new SecurityPolicyViolationEvent(
'securitypolicyviolation',
{
'bubbles': true,
'blockedURI': blockedURI,
'disposition': this.config_.isEnforcementEnabled ?
'enforce' : 'report',
'documentURI': document.location.href,
'effectiveDirective': DIRECTIVE_NAME,
'originalPolicy': this.config_.cspString,
'statusCode': 0,
'violatedDirective': DIRECTIVE_NAME,
'sample': `${contextName}.${propertyName} ${valueSlice}`,
});
if (context.isConnected) {
context.dispatchEvent(event);
} else { // Fallback - dispatch an event on base document.
document.dispatchEvent(event);
}
}
if (this.config_.isEnforcementEnabled) {
throw new TypeError(message);
} else { // pass-through
return apply(originalSetter, context, args);
}
}
}