Skip to content

Commit f6c805a

Browse files
[JENKINS-74897] Address CSP violations (#335)
* [JENKINS-74897] Extract inline handlers from `TestNGTestResultBuildAction/reportDetails.groovy` * [JENKINS-74897] Extract inline scripts from `PackageResult/reportDetail.groovy` * [JENKINS-74897] Extract inline JavaScript from `ClassResult/reportDetail.groovy` * [JENKINS-74897] Restore hrefs on `a` tags for `cursor: pointer` --------- Co-authored-by: Mark Waite <mark.earl.waite@gmail.com>
1 parent 2ed8667 commit f6c805a

7 files changed

Lines changed: 87 additions & 35 deletions

File tree

src/main/resources/hudson/plugins/testng/TestNGTestResultBuildAction/reportDetail.groovy

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ l = namespace(lib.LayoutTagLib)
88
t = namespace("/lib/hudson")
99
st = namespace("jelly:stapler")
1010

11-
script(src: "${app.rootUrl}/plugin/testng-plugin/js/toggle_table.js")
12-
script(src: "${app.rootUrl}/plugin/testng-plugin/js/toggle_mthd_summary.js")
11+
script(src: "${resURL}/plugin/testng-plugin/js/toggle_table.js")
12+
script(src: "${resURL}/plugin/testng-plugin/js/toggle_mthd_summary.js")
1313

1414
h2("Failed Tests")
1515

1616
if (my.result.failCount != 0) {
17-
a(href: "javascript:toggleTable('fail-tbl')") {
17+
a(href: "", class: "testng-toggle-table", "data-toggle-table-id": "fail-tbl") {
1818
text("hide/expand the table")
1919
}
2020
table(id:"fail-tbl", border:"1px", class:"pane sortable") {
@@ -34,10 +34,12 @@ if (my.result.failCount != 0) {
3434
def failedTestSafeUpUrl = Functions.jsStringEscape(failedTest.upUrl)
3535
tr() {
3636
td(align: "left") {
37-
a(id: "${failedTest.id}-showlink", href:"javascript:showStackTrace('${failedTestSafeId}', '${failedTestSafeUpUrl}/summary')") {
37+
a(href: "", id: "${failedTest.id}-showlink", class: "testng-show-stack-trace",
38+
"data-failed-test-safe-id": "${failedTestSafeId}", "data-failed-test-safe-up-url": "${failedTestSafeUpUrl}/summary") {
3839
text(">>>")
3940
}
40-
a(style: "display:none", id: "${failedTest.id}-hidelink", href:"javascript:hideStackTrace('${failedTestSafeId}')") {
41+
a(href: "", style: "display:none", id: "${failedTest.id}-hidelink", class: "testng-hide-stack-trace",
42+
"data-failed-test-safe-id": "${failedTestSafeId}") {
4143
text("<<<")
4244
}
4345
text(" ")
@@ -76,7 +78,7 @@ if (my.result.skippedConfigCount != 0) {
7678

7779
h2("All Tests (grouped by their packages)")
7880

79-
a(href:"javascript:toggleTable('all-tbl')") {
81+
a(href: "", class: "testng-toggle-table", "data-toggle-table-id": "all-tbl") {
8082
text("hide/expand the table")
8183
}
8284

@@ -158,7 +160,7 @@ table(id:"all-tbl", border:"1px", class:"pane sortable") {
158160
* @return nothing
159161
*/
160162
def printMethods(type, tableName, methodList, showMoreArrows) {
161-
a(href: "javascript:toggleTable('${tableName}')") {
163+
a(href: "", class: "testng-toggle-table", "data-toggle-table-id": "${tableName}") {
162164
text("hide/expand the table")
163165
}
164166
table(id:tableName, border:"1px", class:"pane sortable") {
@@ -176,10 +178,12 @@ def printMethods(type, tableName, methodList, showMoreArrows) {
176178
tr() {
177179
td(align: "left") {
178180
if (showMoreArrows) {
179-
a(id: "${method.id}-showlink", href:"javascript:showStackTrace('${methodSafeId}', '${methodSafeUpUrl}/summary')") {
181+
a(href: "", id: "${method.id}-showlink", class: "testng-show-stack-trace",
182+
"data-failed-test-safe-id": "${methodSafeId}", "data-failed-test-safe-up-url": "${methodSafeUpUrl}/summary") {
180183
text(">>>")
181184
}
182-
a(style: "display:none", id: "${method.id}-hidelink", href:"javascript:hideStackTrace('${methodSafeId}')") {
185+
a(href: "", style: "display:none", id: "${method.id}-hidelink", class: "testng-hide-stack-trace",
186+
"data-failed-test-safe-id": "${methodSafeId}") {
183187
text("<<<")
184188
}
185189
text(" ")

src/main/resources/hudson/plugins/testng/results/ClassResult/reportDetail.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ for (group in my.testRunMap.values()) {
5353
if (method.groups || method.testInstanceName || method.parameters?.size() > 0) {
5454
div(id:"${method.safeName}_1", style:"display:inline") {
5555
text(" (")
56-
a(href:"javascript:showMore(\"${methodJsSafeName}\")") {
56+
a(href: "", class: "testng-show-more", "data-method-name": "${methodJsSafeName}") {
5757
raw("&hellip;")
5858
}
5959
text(")")
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Loads data for all the methods
2+
function showAllExecMthds() {
3+
thisPkgResult.getAllSortedTestMethodsByStartTime(function (t) {
4+
document.getElementById("sortedMethods").innerHTML = t.responseObject();
5+
});
6+
document.getElementById("showAllLink").style.display = "none";
7+
}
8+
9+
document.addEventListener("DOMContentLoaded", () => {
10+
// following script loads the initial table data
11+
thisPkgResult.getFirstXSortedTestMethodsByStartTime(function(t) {
12+
document.getElementById('sortedMethods').innerHTML = t.responseObject();
13+
});
14+
15+
const showAllButton = document.querySelector(".testng-show-all-exec-methods");
16+
if (showAllButton !== null) {
17+
showAllButton.addEventListener("click", (event) => {
18+
event.preventDefault();
19+
showAllExecMthds();
20+
});
21+
}
22+
});

src/main/resources/hudson/plugins/testng/results/PackageResult/reportDetail.groovy

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,13 @@ l = namespace(lib.LayoutTagLib)
77
t = namespace("/lib/hudson")
88
st = namespace("jelly:stapler")
99

10-
script(src:"${app.rootUrl}/plugin/testng-plugin/js/toggle_table.js")
10+
script(src:"${resURL}/plugin/testng-plugin/js/toggle_table.js")
1111
//see https://issues.jenkins-ci.org/browse/JENKINS-18867 & https://issues.jenkins-ci.org/browse/JENKINS-18875
12-
st.bind(var:"thisPkgResult", value:my)
13-
script() {
14-
text("//Loads data for all the methods")
15-
text("\nfunction showAllExecMthds() {")
16-
text("\nthisPkgResult.getAllSortedTestMethodsByStartTime(function(t) {")
17-
text("\ndocument.getElementById('sortedMethods').innerHTML = t.responseObject();")
18-
text("\n})")
19-
text("\ndocument.getElementById(\"showAllLink\").style.display = \"none\"; }")
20-
}
12+
st.bind(var:"thisPkgResult", value: my)
13+
st.adjunct(includes: "hudson.plugins.testng.results.PackageResult.report-detail")
2114

2215
h2("All Classes")
23-
a(href:"javascript:toggleTable('allClasses')") {
16+
a(href: "", class: "testng-toggle-table", "data-toggle-table-id": "allClasses") {
2417
text("hide/expand the table")
2518
}
2619

@@ -100,13 +93,13 @@ if (my.sortedTestMethodsByStartTime) {
10093
div(id:"showAllLink") {
10194
p() {
10295
text("Showing only first ${my.MAX_EXEC_MTHD_LIST_SIZE} test methods. ")
103-
a(href:"javascript:showAllExecMthds()") {
96+
a(href: "", class: "testng-show-all-exec-methods") {
10497
text("Click to see all")
10598
}
10699
}
107100
}
108101
}
109-
a(href:"javascript:toggleTable('exec-tbl')") {
102+
a(href: "", class: "testng-toggle-table", "data-toggle-table-id": "exec-tbl") {
110103
text("hide/expand the table")
111104
}
112105
table(border:"1px", class:"pane sortable", id:"exec-tbl") {
@@ -136,12 +129,3 @@ if (my.sortedTestMethodsByStartTime) {
136129
} else {
137130
div("No Tests found or all Tests were skipped")
138131
}
139-
140-
//following script loads the initial table data
141-
script() {
142-
text("\nvar foo = ")
143-
st.bind(value:my)
144-
text("\nfoo.getFirstXSortedTestMethodsByStartTime(function(t) {")
145-
text("\ndocument.getElementById('sortedMethods').innerHTML = t.responseObject();")
146-
text("\n})")
147-
}

src/main/webapp/js/show_more.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
11
function showMore(id) {
22
document.getElementById(id + "_1").style.display = "none";
33
document.getElementById(id + "_2").style.display = "";
4-
}
4+
}
5+
6+
document.addEventListener("DOMContentLoaded", () => {
7+
document.querySelectorAll(".testng-show-more").forEach((button) => {
8+
button.addEventListener("click", (event) => {
9+
event.preventDefault();
10+
const { methodName } = event.target.dataset;
11+
12+
showMore(methodName);
13+
})
14+
});
15+
});

src/main/webapp/js/toggle_mthd_summary.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,24 @@ function hideStackTrace(id) {
1414
document.getElementById(id).style.display = "none";
1515
document.getElementById(id + "-showlink").style.display = "";
1616
document.getElementById(id + "-hidelink").style.display = "none";
17-
}
17+
}
18+
19+
document.addEventListener("DOMContentLoaded", () => {
20+
document.querySelectorAll(".testng-show-stack-trace").forEach((button) => {
21+
button.addEventListener("click", (event) => {
22+
event.preventDefault();
23+
const { failedTestSafeId, failedTestSafeUpUrl } = event.target.closest(".testng-show-stack-trace").dataset;
24+
25+
showStackTrace(failedTestSafeId, failedTestSafeUpUrl);
26+
});
27+
});
28+
29+
document.querySelectorAll(".testng-hide-stack-trace").forEach((button) => {
30+
button.addEventListener("click", (event) => {
31+
event.preventDefault();
32+
const { failedTestSafeId } = event.target.closest(".testng-hide-stack-trace").dataset;
33+
34+
hideStackTrace(failedTestSafeId);
35+
});
36+
});
37+
});

src/main/webapp/js/toggle_table.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,15 @@ function toggleTable(id) {
55
} else if (document.getElementById(id).style.display == "") {
66
document.getElementById(id).style.display = "none";
77
}
8-
}
8+
}
9+
10+
document.addEventListener("DOMContentLoaded", () => {
11+
document.querySelectorAll(".testng-toggle-table").forEach((toggle) => {
12+
toggle.addEventListener("click", (event) => {
13+
event.preventDefault();
14+
const { toggleTableId } = event.target.dataset;
15+
16+
toggleTable(toggleTableId);
17+
});
18+
});
19+
});

0 commit comments

Comments
 (0)