Skip to content

Commit 5e90cce

Browse files
bsternebrandon
authored andcommitted
Defer scriptEval test until the first time globalEval is called to prevent Content Security Policy inline-script violations. Fixes #7371
1 parent d140ef7 commit 5e90cce

File tree

2 files changed

+35
-25
lines changed

2 files changed

+35
-25
lines changed

src/core.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,7 @@ jQuery.extend({
578578

579579
script.type = "text/javascript";
580580

581-
if ( jQuery.support.scriptEval ) {
581+
if ( jQuery.support.scriptEval() ) {
582582
script.appendChild( document.createTextNode( data ) );
583583
} else {
584584
script.text = data;

src/support.js

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@
44

55
jQuery.support = {};
66

7-
var root = document.documentElement,
8-
script = document.createElement("script"),
9-
div = document.createElement("div"),
10-
id = "script" + jQuery.now();
7+
var div = document.createElement("div");
118

129
div.style.display = "none";
1310
div.innerHTML = " <link/><table></table><a href='/a' style='color:red;float:left;opacity:.55;'>a</a><input type='checkbox'/>";
@@ -64,7 +61,7 @@
6461
deleteExpando: true,
6562
optDisabled: false,
6663
checkClone: false,
67-
scriptEval: false,
64+
_scriptEval: null,
6865
noCloneEvent: true,
6966
boxModel: null,
7067
inlineBlockNeedsLayout: false,
@@ -77,32 +74,45 @@
7774
select.disabled = true;
7875
jQuery.support.optDisabled = !opt.disabled;
7976

80-
script.type = "text/javascript";
81-
try {
82-
script.appendChild( document.createTextNode( "window." + id + "=1;" ) );
83-
} catch(e) {}
84-
85-
root.insertBefore( script, root.firstChild );
86-
87-
// Make sure that the execution of code works by injecting a script
88-
// tag with appendChild/createTextNode
89-
// (IE doesn't support this, fails, and uses .text instead)
90-
if ( window[ id ] ) {
91-
jQuery.support.scriptEval = true;
92-
delete window[ id ];
93-
}
77+
jQuery.support.scriptEval = function() {
78+
if ( jQuery.support._scriptEval === null) {
79+
var root = document.documentElement,
80+
script = document.createElement("script"),
81+
id = "script" + jQuery.now();
82+
83+
script.type = "text/javascript";
84+
try {
85+
script.appendChild( document.createTextNode( "window." + id + "=1;" ) );
86+
} catch(e) {}
87+
88+
root.insertBefore( script, root.firstChild );
89+
90+
// Make sure that the execution of code works by injecting a script
91+
// tag with appendChild/createTextNode
92+
// (IE doesn't support this, fails, and uses .text instead)
93+
if ( window[ id ] ) {
94+
jQuery.support._scriptEval = true;
95+
delete window[ id ];
96+
} else {
97+
jQuery.support._scriptEval = false;
98+
}
99+
100+
root.removeChild( script );
101+
// release memory in IE
102+
root = script = id = null;
103+
}
104+
return jQuery.support._scriptEval;
105+
};
94106

95107
// Test to see if it's possible to delete an expando from an element
96108
// Fails in Internet Explorer
97109
try {
98-
delete script.test;
110+
delete div.test;
99111

100112
} catch(e) {
101113
jQuery.support.deleteExpando = false;
102114
}
103115

104-
root.removeChild( script );
105-
106116
if ( div.attachEvent && div.fireEvent ) {
107117
div.attachEvent("onclick", function click() {
108118
// Cloning a node shouldn't copy over any
@@ -179,7 +189,7 @@
179189

180190
var isSupported = (eventName in el);
181191
if ( !isSupported ) {
182-
el.setAttribute(eventName, "return;");
192+
el.addEventListener(eventName, function() { return; }, true);
183193
isSupported = typeof el[eventName] === "function";
184194
}
185195
el = null;
@@ -191,6 +201,6 @@
191201
jQuery.support.changeBubbles = eventSupported("change");
192202

193203
// release memory in IE
194-
root = script = div = all = a = null;
204+
div = all = a = null;
195205
})();
196206
})( jQuery );

0 commit comments

Comments
 (0)