|
23 | 23 | */
|
24 | 24 | package hudson.tasks.junit;
|
25 | 25 |
|
| 26 | +import com.gargoylesoftware.htmlunit.AlertHandler; |
| 27 | +import com.gargoylesoftware.htmlunit.Page; |
| 28 | +import com.gargoylesoftware.htmlunit.html.HtmlPage; |
| 29 | +import com.gargoylesoftware.htmlunit.html.HtmlTable; |
| 30 | +import com.gargoylesoftware.htmlunit.html.HtmlTableCell; |
26 | 31 | import hudson.model.FreeStyleBuild;
|
27 | 32 | import hudson.model.FreeStyleProject;
|
28 | 33 | import hudson.model.Project;
|
29 | 34 | import hudson.model.Result;
|
30 | 35 | import org.junit.Before;
|
31 | 36 | import org.junit.Rule;
|
32 | 37 | import org.junit.Test;
|
| 38 | +import org.jvnet.hudson.test.Issue; |
33 | 39 | import org.jvnet.hudson.test.JenkinsRule;
|
34 | 40 | import org.jvnet.hudson.test.recipes.LocalData;
|
35 | 41 |
|
36 | 42 | import java.util.List;
|
| 43 | +import java.util.Optional; |
37 | 44 |
|
38 | 45 | import static org.junit.Assert.assertEquals;
|
39 | 46 | import static org.junit.Assert.assertNotNull;
|
| 47 | +import static org.junit.Assert.assertNull; |
40 | 48 | import static org.junit.Assert.assertTrue;
|
41 | 49 |
|
42 | 50 | public class HistoryTest {
|
@@ -113,4 +121,44 @@ public void testFailedSince() throws Exception {
|
113 | 121 | assertTrue("eleanor failed", !eleanorCase.isPassed());
|
114 | 122 | assertEquals("eleanor has failed since build 3", 3, eleanorCase.getFailedSince());
|
115 | 123 | }
|
| 124 | + |
| 125 | + @LocalData |
| 126 | + @Test @Issue("SECURITY-2760") |
| 127 | + public void testXSS() throws Exception { |
| 128 | + assertNotNull("project should exist", project); |
| 129 | + |
| 130 | + FreeStyleBuild build4 = project.getBuildByNumber(4); |
| 131 | + TestResult tr = build4.getAction(TestResultAction.class).getResult(); |
| 132 | + |
| 133 | + tr.setDescription("<script>alert(\"<XSS>\")</script>"); |
| 134 | + build4.save(); //Might be unnecessary |
| 135 | + |
| 136 | + try (final JenkinsRule.WebClient webClient = rule.createWebClient()) { |
| 137 | + Alerter alerter = new Alerter(); |
| 138 | + webClient.setJavaScriptEnabled(true); |
| 139 | + webClient.getOptions().setThrowExceptionOnScriptError(false); //HtmlUnit finds a syntax error in bootstrap 5 |
| 140 | + webClient.setAlertHandler(alerter); //This catches any alert dialog popup |
| 141 | + |
| 142 | + final HtmlPage page = webClient.getPage(build4, "testReport/history/"); |
| 143 | + assertNull(alerter.message); //No alert dialog popped up |
| 144 | + assertNull(alerter.page); |
| 145 | + final HtmlTable table = (HtmlTable) page.getElementById("testresult"); |
| 146 | + final Optional<HtmlTableCell> descr = table.getRows().stream().flatMap(row -> row.getCells().stream()) |
| 147 | + .filter(cell -> cell.getTextContent().equals("<script>alert(\"<XSS>\")</script>")) //cell.getTextContent() seems to translate back from > to < etc. |
| 148 | + .findFirst(); |
| 149 | + assertTrue("Should have found the description", descr.isPresent()); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + static class Alerter implements AlertHandler { |
| 154 | + |
| 155 | + Page page = null; |
| 156 | + String message = null; |
| 157 | + |
| 158 | + @Override |
| 159 | + public void handleAlert(final Page page, final String message) { |
| 160 | + this.page = page; |
| 161 | + this.message = message; |
| 162 | + } |
| 163 | + } |
116 | 164 | }
|
0 commit comments