Skip to content

Commit 019bfd4

Browse files
ricticjohnjbarton
authored andcommittedSep 9, 2019
feat(client): Add trusted types support (#3360)
With this change, Karma tests can be run with an enforced Trusted Types policy. This change consists of using safer APIs (appendChild and textContent instead of innerHTML), as well as creating a policy for client/karma.js which a test's Trusted Types CSP policy can then explicitly allow. This policy is used internally where karma does potentially dangerous operations like loading scripts. More info about the proposed Trusted Types standard at https://github.com/WICG/trusted-types
1 parent fa6be15 commit 019bfd4

File tree

2 files changed

+40
-6
lines changed

2 files changed

+40
-6
lines changed
 

‎client/karma.js

+29-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,33 @@ function Karma (socket, iframe, opener, navigator, location) {
1414
var resultsBufferLimit = 50
1515
var resultsBuffer = []
1616

17+
// This is a no-op if not running with a Trusted Types CSP policy, and
18+
// lets tests declare that they trust the way that karma creates and handles
19+
// URLs.
20+
//
21+
// More info about the proposed Trusted Types standard at
22+
// https://github.com/WICG/trusted-types
23+
var policy = {
24+
createURL: function (s) {
25+
return s
26+
},
27+
createScriptURL: function (s) {
28+
return s
29+
}
30+
}
31+
var trustedTypes = window.trustedTypes || window.TrustedTypes
32+
if (trustedTypes) {
33+
policy = trustedTypes.createPolicy('karma', policy)
34+
if (!policy.createURL) {
35+
// Install createURL for newer browsers. Only browsers that implement an
36+
// old version of the spec require createURL.
37+
// Should be safe to delete all reference to createURL by
38+
// February 2020.
39+
// https://github.com/WICG/trusted-types/pull/204
40+
policy.createURL = function (s) { return s }
41+
}
42+
}
43+
1744
// This variable will be set to "true" whenever the socket lost connection and was able to
1845
// reconnect to the Karma server. This will be passed to the Karma server then, so that
1946
// Karma can differentiate between a socket client reconnect and a full browser reconnect.
@@ -80,7 +107,7 @@ function Karma (socket, iframe, opener, navigator, location) {
80107
if (ele.tagName && ele.tagName.toLowerCase() === 'script') {
81108
var tmp = ele
82109
ele = document.createElement('script')
83-
ele.src = tmp.src
110+
ele.src = policy.createScriptURL(tmp.src)
84111
ele.crossOrigin = tmp.crossOrigin
85112
}
86113
ele.onload = function () {
@@ -95,7 +122,7 @@ function Karma (socket, iframe, opener, navigator, location) {
95122
}
96123
// run in iframe
97124
} else {
98-
iframe.src = url
125+
iframe.src = policy.createURL(url)
99126
}
100127
}
101128

‎client/updater.js

+11-4
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,20 @@ function StatusUpdater (socket, titleElement, bannerElement, browsersElement) {
55
if (!browsersElement) {
66
return
77
}
8-
var items = []
98
var status
9+
10+
// clear browsersElement
11+
while (browsersElement.firstChild) {
12+
browsersElement.removeChild(browsersElement.firstChild)
13+
}
14+
1015
for (var i = 0; i < browsers.length; i++) {
1116
status = browsers[i].isConnected ? 'idle' : 'executing'
12-
items.push('<li class="' + status + '">' + browsers[i].name + ' is ' + status + '</li>')
17+
var li = document.createElement('li')
18+
li.setAttribute('class', status)
19+
li.textContent = browsers[i].name + ' is ' + status
20+
browsersElement.appendChild(li)
1321
}
14-
browsersElement.innerHTML = items.join('\n')
1522
}
1623

1724
function updateBanner (status) {
@@ -20,7 +27,7 @@ function StatusUpdater (socket, titleElement, bannerElement, browsersElement) {
2027
return
2128
}
2229
var paramStatus = param ? status.replace('$', param) : status
23-
titleElement.innerHTML = 'Karma v' + VERSION + ' - ' + paramStatus
30+
titleElement.textContent = 'Karma v' + VERSION + ' - ' + paramStatus
2431
bannerElement.className = status === 'connected' ? 'online' : 'offline'
2532
}
2633
}

0 commit comments

Comments
 (0)
Please sign in to comment.