Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use global MarkupFormatter in ScriptlerManagement #140

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,6 @@
<artifactId>sshd</artifactId>
</dependency>

<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>antisamy-markup-formatter</artifactId>
</dependency>

<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>git-server</artifactId>
Expand Down Expand Up @@ -90,6 +85,12 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>antisamy-markup-formatter</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>matrix-auth</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import hudson.ExtensionList;
import hudson.Util;
import hudson.markup.MarkupFormatter;
import hudson.markup.RawHtmlMarkupFormatter;
import hudson.model.*;
import hudson.security.AccessControlled;
import hudson.security.Permission;
Expand Down Expand Up @@ -77,8 +76,6 @@ public class ScriptlerManagement extends ManagementLink implements RootAction {
private static final String CAN_BYPASS_APPROVAL = "canByPassScriptApproval";
private static final String SCRIPT = "script";

private static final MarkupFormatter INSTANCE = RawHtmlMarkupFormatter.INSTANCE;

// used in Jelly view
public Permission getScriptlerRunScripts() {
return ScriptlerPermissions.RUN_SCRIPTS;
Expand Down Expand Up @@ -154,7 +151,7 @@ public ScriptlerConfiguration getConfiguration() {
}

public MarkupFormatter getMarkupFormatter() {
return INSTANCE;
return Jenkins.get().getMarkupFormatter();
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package org.jenkinsci.plugins.scriptler;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.markup.MarkupFormatter;
import hudson.markup.RawHtmlMarkupFormatter;
import java.io.IOException;
import java.io.Writer;
import org.junit.jupiter.api.Test;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;

@WithJenkins
class ScriptlerManagementTest {

@Test
void markupFormatter(@SuppressWarnings("unused") JenkinsRule r) throws IOException {
ScriptlerManagement management = new ScriptlerManagement();

// save text
String text = management.getMarkupFormatter().translate("Save text");
assertEquals("Save text", text);

// dangerous text with global formatter
text = management.getMarkupFormatter().translate("<script>alert('PWND!')</script>");
assertEquals("&lt;script&gt;alert(&#039;PWND!&#039;)&lt;/script&gt;", text);

// dangerous text with OWASP formatter
r.jenkins.setMarkupFormatter(RawHtmlMarkupFormatter.INSTANCE);
text = management.getMarkupFormatter().translate("<script>alert('PWND!')</script>");
assertEquals("", text);

// save text with broken formatter
MarkupFormatter formatter = new MarkupFormatter() {
@Override
public void translate(String markup, @NonNull Writer output) throws IOException {
throw new IOException("Oh no!");
}
};
r.jenkins.setMarkupFormatter(formatter);
assertThrows(
IOException.class, () -> management.getMarkupFormatter().translate("<script>alert('PWND!')</script>"));
}
}
Loading