Skip to content

Commit 6ae572f

Browse files
Brandon Sternebsterne
authored andcommitted
Defer scriptEval test until first use to prevent Content Security Policy inline-script violations from occuring. Fixes #7371.
1 parent 0838bdf commit 6ae572f

File tree

2 files changed

+34
-25
lines changed

2 files changed

+34
-25
lines changed

src/core.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,35 @@ jQuery.extend({
578578

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

581+
// Delay the scriptEval test until the first time we want to call it
582+
// to prevent CSP inline-script violations (See #7371)
583+
if ( jQuery.support.scriptEval == null ) {
584+
var root = document.documentElement,
585+
testScript = document.createElement("script"),
586+
id = "script" + jQuery.now();
587+
testScript.type = "text/javascript";
588+
try {
589+
testScript.appendChild( document.createTextNode( "window." + id + "=1;" ) );
590+
} catch(e) {}
591+
592+
root.insertBefore( testScript, root.firstChild );
593+
594+
// Make sure that the execution of code works by injecting a script
595+
// tag with appendChild/createTextNode
596+
// (IE doesn't support this, fails, and uses .text instead)
597+
if ( window[ id ] ) {
598+
jQuery.support.scriptEval = true;
599+
delete window[ id ];
600+
}
601+
else {
602+
jQuery.support.scriptEval = false;
603+
}
604+
605+
root.removeChild( testScript );
606+
// release memory in IE
607+
root = testScript = id = null;
608+
}
609+
581610
if ( jQuery.support.scriptEval ) {
582611
script.appendChild( document.createTextNode( data ) );
583612
} else {

src/support.js

Lines changed: 5 additions & 25 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,15 @@
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-
}
94-
9577
// Test to see if it's possible to delete an expando from an element
9678
// Fails in Internet Explorer
9779
try {
98-
delete script.test;
80+
delete div.test;
9981

10082
} catch(e) {
10183
jQuery.support.deleteExpando = false;
10284
}
10385

104-
root.removeChild( script );
105-
10686
if ( div.attachEvent && div.fireEvent ) {
10787
div.attachEvent("onclick", function click() {
10888
// Cloning a node shouldn't copy over any
@@ -179,7 +159,7 @@
179159

180160
var isSupported = (eventName in el);
181161
if ( !isSupported ) {
182-
el.setAttribute(eventName, "return;");
162+
el.addEventListener(eventName, function() { return; }, true);
183163
isSupported = typeof el[eventName] === "function";
184164
}
185165
el = null;
@@ -191,6 +171,6 @@
191171
jQuery.support.changeBubbles = eventSupported("change");
192172

193173
// release memory in IE
194-
root = script = div = all = a = null;
174+
div = all = a = null;
195175
})();
196176
})( jQuery );

0 commit comments

Comments
 (0)