Skip to content

Commit bd35dca

Browse files
Use global MarkupFormatter in ScriptlerManagement
1 parent bc93044 commit bd35dca

File tree

3 files changed

+53
-9
lines changed

3 files changed

+53
-9
lines changed

pom.xml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,6 @@
5858
<artifactId>sshd</artifactId>
5959
</dependency>
6060

61-
<dependency>
62-
<groupId>org.jenkins-ci.plugins</groupId>
63-
<artifactId>antisamy-markup-formatter</artifactId>
64-
</dependency>
65-
6661
<dependency>
6762
<groupId>org.jenkins-ci.plugins</groupId>
6863
<artifactId>git-server</artifactId>
@@ -90,6 +85,12 @@
9085
<scope>test</scope>
9186
</dependency>
9287

88+
<dependency>
89+
<groupId>org.jenkins-ci.plugins</groupId>
90+
<artifactId>antisamy-markup-formatter</artifactId>
91+
<scope>test</scope>
92+
</dependency>
93+
9394
<dependency>
9495
<groupId>org.jenkins-ci.plugins</groupId>
9596
<artifactId>matrix-auth</artifactId>

src/main/java/org/jenkinsci/plugins/scriptler/ScriptlerManagement.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import hudson.ExtensionList;
2929
import hudson.Util;
3030
import hudson.markup.MarkupFormatter;
31-
import hudson.markup.RawHtmlMarkupFormatter;
3231
import hudson.model.*;
3332
import hudson.security.AccessControlled;
3433
import hudson.security.Permission;
@@ -77,8 +76,6 @@ public class ScriptlerManagement extends ManagementLink implements RootAction {
7776
private static final String CAN_BYPASS_APPROVAL = "canByPassScriptApproval";
7877
private static final String SCRIPT = "script";
7978

80-
private static final MarkupFormatter INSTANCE = RawHtmlMarkupFormatter.INSTANCE;
81-
8279
// used in Jelly view
8380
public Permission getScriptlerRunScripts() {
8481
return ScriptlerPermissions.RUN_SCRIPTS;
@@ -154,7 +151,7 @@ public ScriptlerConfiguration getConfiguration() {
154151
}
155152

156153
public MarkupFormatter getMarkupFormatter() {
157-
return INSTANCE;
154+
return Jenkins.get().getMarkupFormatter();
158155
}
159156

160157
/**
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package org.jenkinsci.plugins.scriptler;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
6+
import edu.umd.cs.findbugs.annotations.NonNull;
7+
import hudson.markup.MarkupFormatter;
8+
import hudson.markup.RawHtmlMarkupFormatter;
9+
import java.io.IOException;
10+
import java.io.Writer;
11+
import org.junit.jupiter.api.Test;
12+
import org.jvnet.hudson.test.JenkinsRule;
13+
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
14+
15+
@WithJenkins
16+
class ScriptlerManagementTest {
17+
18+
@Test
19+
void markupFormatter(@SuppressWarnings("unused") JenkinsRule r) throws IOException {
20+
ScriptlerManagement management = new ScriptlerManagement();
21+
22+
// save text
23+
String text = management.getMarkupFormatter().translate("Save text");
24+
assertEquals("Save text", text);
25+
26+
// dangerous text with global formatter
27+
text = management.getMarkupFormatter().translate("<script>alert('PWND!')</script>");
28+
assertEquals("&lt;script&gt;alert(&#039;PWND!&#039;)&lt;/script&gt;", text);
29+
30+
// dangerous text with OWASP formatter
31+
r.jenkins.setMarkupFormatter(RawHtmlMarkupFormatter.INSTANCE);
32+
text = management.getMarkupFormatter().translate("<script>alert('PWND!')</script>");
33+
assertEquals("", text);
34+
35+
// save text with broken formatter
36+
MarkupFormatter formatter = new MarkupFormatter() {
37+
@Override
38+
public void translate(String markup, @NonNull Writer output) throws IOException {
39+
throw new IOException("Oh no!");
40+
}
41+
};
42+
r.jenkins.setMarkupFormatter(formatter);
43+
assertThrows(
44+
IOException.class, () -> management.getMarkupFormatter().translate("<script>alert('PWND!')</script>"));
45+
}
46+
}

0 commit comments

Comments
 (0)