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

Minor JNLP parsing improving, mostly in testing #375

Merged
merged 2 commits into from
Apr 20, 2020
Merged
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
10 changes: 5 additions & 5 deletions src/main/java/hudson/remoting/Launcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -543,14 +543,14 @@ public List<String> parseJnlpArguments() throws ParserConfigurationException, SA
if(contentType==null || !contentType.startsWith(expectedContentType)) {
// load DOM anyway, but if it fails to parse, that's probably because this is not an XML file to begin with.
try {
dom = loadDom(agentJnlpURL, input);
dom = loadDom(input);
} catch (SAXException e) {
throw new IOException(agentJnlpURL +" doesn't look like a JNLP file; content type was "+contentType);
} catch (IOException e) {
throw new IOException(agentJnlpURL +" doesn't look like a JNLP file; content type was "+contentType);
}
} else {
dom = loadDom(agentJnlpURL, input);
dom = loadDom(input);
}

// exec into the JNLP launcher, to fetch the connection parameter through JNLP.
Expand Down Expand Up @@ -607,11 +607,11 @@ private static byte[] fromHexString(String data) {
return r;
}

private static Document loadDom(URL agentJnlpURL, InputStream is) throws ParserConfigurationException, SAXException, IOException {
static Document loadDom(InputStream is) throws ParserConfigurationException, SAXException, IOException {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
dbf.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
DocumentBuilder db = dbf.newDocumentBuilder();
return db.parse(is, agentJnlpURL.toExternalForm());
return db.parse(is);
}

/**
Expand Down
66 changes: 66 additions & 0 deletions src/test/java/hudson/remoting/LauncherTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package hudson.remoting;

import org.junit.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

import javax.xml.parsers.ParserConfigurationException;
import java.io.FileInputStream;
import java.io.IOException;

import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.is;

public class LauncherTest {

@Test
public void loadDom_Standard() throws IOException, SAXException, ParserConfigurationException {
// An example of a standard, regular JNLP file.
FileInputStream jnlpFile = new FileInputStream("src/test/resources/hudson/remoting/test.jnlp");
Document document = Launcher.loadDom(jnlpFile);
Element documentElement = document.getDocumentElement();
assertThat(documentElement.getNodeName(), is("jnlp"));
assertThat(documentElement.getChildNodes().getLength(), is(9));
}

@Test
public void loadDom_Lol() throws IOException, SAXException, ParserConfigurationException {
// A JNLP containing the Billion Laughs DTD
FileInputStream jnlpFile = new FileInputStream("src/test/resources/hudson/remoting/lol.jnlp");
shouldFailWithDoctype(jnlpFile);
}

@Test
public void loadDom_XxeFile() throws IOException, SAXException, ParserConfigurationException {
// A JNLP containing an file-type XXE
FileInputStream jnlpFile = new FileInputStream("src/test/resources/hudson/remoting/xxe_file.jnlp");
shouldFailWithDoctype(jnlpFile);
}

@Test
public void loadDom_XxeHttp() throws IOException, SAXException, ParserConfigurationException {
// A JNLP containing an http-type XXE
FileInputStream jnlpFile = new FileInputStream("src/test/resources/hudson/remoting/xxe_http.jnlp");
shouldFailWithDoctype(jnlpFile);
}

@Test
public void loadDom_EmbeddedDoctype() throws IOException, SAXException, ParserConfigurationException {
// A JNLP containing an embedded doctype
FileInputStream jnlpFile = new FileInputStream("src/test/resources/hudson/remoting/embedded_doctype.jnlp");
shouldFailWithDoctype(jnlpFile);
}

private void shouldFailWithDoctype(FileInputStream jnlpFile) throws ParserConfigurationException, SAXException, IOException {
try {
Launcher.loadDom(jnlpFile);
fail("Dom loading should have failed.");
} catch (SAXParseException spe) {
assertThat(spe.getMessage(), containsString("DOCTYPE is disallowed"));
}
}

}
29 changes: 29 additions & 0 deletions src/test/resources/hudson/remoting/embedded_doctype.jnlp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<!DOCTYPE note [
<!ENTITY nbsp "&#xA0;">
<!ENTITY writer "Writer: Donald Duck.">
<!ENTITY copyright "Copyright: W3Schools.">
]>
<jnlp codebase="http://jenkins:8080/computer/local/" spec="1.0+">
<information>
<title>Agent for local</title>
<vendor>Jenkins project</vendor>
<homepage href="https://jenkins-ci.org/"></homepage>
</information>
<security>
<all-permissions></all-permissions>
</security>
<resources>
<j2se version="1.8+"></j2se>
<jar href="http://jenkins:8080/jnlpJars/remoting.jar"></jar>
</resources>
<application-desc main-class="hudson.remoting.jnlp.Main">
<argument>secret_key</argument>
<argument>local</argument>
<argument>-workDir</argument>
<argument>/jenkins/nodes/local</argument>
<argument>-internalDir</argument>
<argument>remoting</argument>
<argument>-url</argument>
<argument>http://jenkins:8080/</argument>
</application-desc>
</jnlp>
39 changes: 39 additions & 0 deletions src/test/resources/hudson/remoting/lol.jnlp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0"?>
<!DOCTYPE lolz [
<!ENTITY lol "lol">
<!ELEMENT lolz (#PCDATA)>
<!ENTITY lol1 "&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;">
<!ENTITY lol2 "&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;">
<!ENTITY lol3 "&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;">
<!ENTITY lol4 "&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;">
<!ENTITY lol5 "&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;">
<!ENTITY lol6 "&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;">
<!ENTITY lol7 "&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;">
<!ENTITY lol8 "&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;">
<!ENTITY lol9 "&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;">
]>
<lolz>&lol9;</lolz>
<jnlp codebase="http://jenkins:8080/computer/local/" spec="1.0+">
<information>
<title>Agent for local</title>
<vendor>Jenkins project</vendor>
<homepage href="https://jenkins-ci.org/"></homepage>
</information>
<security>
<all-permissions></all-permissions>
</security>
<resources>
<j2se version="1.8+"></j2se>
<jar href="http://jenkins:8080/jnlpJars/remoting.jar"></jar>
</resources>
<application-desc main-class="hudson.remoting.jnlp.Main">
<argument>secret_key</argument>
<argument>local</argument>
<argument>-workDir</argument>
<argument>/jenkins/nodes/local</argument>
<argument>-internalDir</argument>
<argument>remoting</argument>
<argument>-url</argument>
<argument>http://jenkins:8080/</argument>
</application-desc>
</jnlp>
3 changes: 3 additions & 0 deletions src/test/resources/hudson/remoting/note.dtd
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<!ELEMENT syllabus (#PCDATA)>
<!ELEMENT author (#PCDATA)>
<!ELEMENT booktitle (#PCDATA)>
24 changes: 24 additions & 0 deletions src/test/resources/hudson/remoting/test.jnlp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<jnlp codebase="http://jenkins:8080/computer/local/" spec="1.0+">
<information>
<title>Agent for local</title>
<vendor>Jenkins project</vendor>
<homepage href="https://jenkins-ci.org/"></homepage>
</information>
<security>
<all-permissions></all-permissions>
</security>
<resources>
<j2se version="1.8+"></j2se>
<jar href="http://jenkins:8080/jnlpJars/remoting.jar"></jar>
</resources>
<application-desc main-class="hudson.remoting.jnlp.Main">
<argument>secret_key</argument>
<argument>local</argument>
<argument>-workDir</argument>
<argument>/jenkins/nodes/local</argument>
<argument>-internalDir</argument>
<argument>remoting</argument>
<argument>-url</argument>
<argument>http://jenkins:8080/</argument>
</application-desc>
</jnlp>
26 changes: 26 additions & 0 deletions src/test/resources/hudson/remoting/xxe_file.jnlp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE note SYSTEM "file:src/test/resources/hudson/remoting/note.dtd">
<jnlp codebase="http://jenkins:8080/computer/local/" spec="1.0+">
<information>
<title>Agent for local</title>
<vendor>Jenkins project</vendor>
<homepage href="https://jenkins-ci.org/"></homepage>
</information>
<security>
<all-permissions></all-permissions>
</security>
<resources>
<j2se version="1.8+"></j2se>
<jar href="http://jenkins:8080/jnlpJars/remoting.jar"></jar>
</resources>
<application-desc main-class="hudson.remoting.jnlp.Main">
<argument>secret_key</argument>
<argument>local</argument>
<argument>-workDir</argument>
<argument>/jenkins/nodes/local</argument>
<argument>-internalDir</argument>
<argument>remoting</argument>
<argument>-url</argument>
<argument>http://jenkins:8080/</argument>
</application-desc>
</jnlp>
30 changes: 30 additions & 0 deletions src/test/resources/hudson/remoting/xxe_http.jnlp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE foo [
<!ELEMENT foo ANY>
<!ENTITY xxe SYSTEM
"http://localhost">
]>
<jnlp codebase="http://jenkins:8080/computer/local/" spec="1.0+">
<information>
<title>Agent for local</title>
<vendor>Jenkins project</vendor>
<homepage href="https://jenkins-ci.org/"></homepage>
</information>
<security>
<all-permissions></all-permissions>
</security>
<resources>
<j2se version="1.8+"></j2se>
<jar href="http://jenkins:8080/jnlpJars/remoting.jar"></jar>
</resources>
<application-desc main-class="hudson.remoting.jnlp.Main">
<argument>secret_key</argument>
<argument>local</argument>
<argument>-workDir</argument>
<argument>/jenkins/nodes/local</argument>
<argument>-internalDir</argument>
<argument>remoting</argument>
<argument>-url</argument>
<argument>http://jenkins:8080/</argument>
</application-desc>
</jnlp>