Skip to content

Commit 88dc75e

Browse files
strangelookingnerdtomasbjerre
authored andcommitted
Use global MarkupFormatter for changelog summary
1 parent f100ffd commit 88dc75e

File tree

4 files changed

+50
-22
lines changed

4 files changed

+50
-22
lines changed

pom.xml

+5-4
Original file line numberDiff line numberDiff line change
@@ -225,10 +225,6 @@
225225
<groupId>org.jenkins-ci.plugins</groupId>
226226
<artifactId>script-security</artifactId>
227227
</dependency>
228-
<dependency>
229-
<groupId>org.jenkins-ci.plugins</groupId>
230-
<artifactId>antisamy-markup-formatter</artifactId>
231-
</dependency>
232228
<dependency>
233229
<groupId>org.jenkins-ci.plugins</groupId>
234230
<artifactId>jackson2-api</artifactId>
@@ -244,6 +240,11 @@
244240
<artifactId>junit</artifactId>
245241
<scope>test</scope>
246242
</dependency>
243+
<dependency>
244+
<groupId>org.jenkins-ci.plugins</groupId>
245+
<artifactId>antisamy-markup-formatter</artifactId>
246+
<scope>test</scope>
247+
</dependency>
247248
<dependency>
248249
<groupId>org.assertj</groupId>
249250
<artifactId>assertj-core</artifactId>

src/main/java/org/jenkinsci/plugins/gitchangelog/perform/GitChangelogSummaryDecorator.java

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
package org.jenkinsci.plugins.gitchangelog.perform;
22

3-
import hudson.markup.MarkupFormatter;
4-
import hudson.markup.RawHtmlMarkupFormatter;
53
import hudson.model.Action;
64
import java.io.IOException;
75
import java.util.logging.Level;
86
import java.util.logging.Logger;
7+
import jenkins.model.Jenkins;
98
import org.kohsuke.stapler.export.Exported;
109
import org.kohsuke.stapler.export.ExportedBean;
1110

1211
@ExportedBean(defaultVisibility = 2)
1312
public class GitChangelogSummaryDecorator implements Action {
14-
private static final MarkupFormatter sanitizer = new RawHtmlMarkupFormatter(true);
1513
private static Logger LOG = Logger.getLogger(GitChangelogSummaryDecorator.class.getSimpleName());
1614

1715
private final String text;
@@ -33,7 +31,7 @@ public String getIconFileName() {
3331
@Exported
3432
public String getText() {
3533
try {
36-
return sanitizer.translate(this.text);
34+
return Jenkins.get().getMarkupFormatter().translate(this.text);
3735
} catch (IOException e) {
3836
LOG.log(Level.SEVERE, e.getMessage(), e);
3937
return "";

src/test/java/org/jenkinsci/plugins/gitchangelog/DummyTest.java

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

0 commit comments

Comments
 (0)