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

Fix XXE vulnerability #463

Open
wants to merge 2 commits into
base: master
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
1 change: 1 addition & 0 deletions jpos/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ dependencies {
exclude(module: 'hamcrest-core')
}
testImplementation libraries.mockito_jupiter
testImplementation libraries.mockserver

// JSONPackager on hold
// compile (libraries.jsonsimple) {
Expand Down
6 changes: 5 additions & 1 deletion jpos/libraries.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ ext {
slf4j_api: "org.slf4j:slf4j-api:1.7.32",
slf4j_nop: "org.slf4j:slf4j-nop:1.7.32",
hdrhistogram: 'org.hdrhistogram:HdrHistogram:2.1.12',
yaml: "org.yaml:snakeyaml:1.28"
yaml: "org.yaml:snakeyaml:1.28",
mockserver: [
'org.mock-server:mockserver-netty:5.11.2',
'org.mock-server:mockserver-client-java:5.11.2'
]
]
}

3 changes: 3 additions & 0 deletions jpos/src/main/java/org/jpos/iso/packager/GenericPackager.java
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,9 @@ private XMLReader createXMLReader () throws SAXException {
);
}
reader.setFeature ("http://xml.org/sax/features/validation", true);
reader.setFeature("http://xml.org/sax/features/external-general-entities", false);
reader.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
Comment on lines +254 to +256
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GenericContentHandler handler = new GenericContentHandler();
reader.setContentHandler(handler);
reader.setErrorHandler(handler);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ public void readFile(String filename) throws org.jpos.iso.ISOException {
System.getProperty( "sax.parser",
"org.apache.crimson.parser.XMLReaderImpl"));
reader.setFeature ("http://xml.org/sax/features/validation", true);
reader.setFeature("http://xml.org/sax/features/external-general-entities", false);
reader.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
GenericValidatorContentHandler handler = new GenericValidatorContentHandler();
reader.setContentHandler(handler);
reader.setErrorHandler(handler);
Expand Down
28 changes: 23 additions & 5 deletions jpos/src/test/java/org/jpos/iso/packager/GenericPackagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@

package org.jpos.iso.packager;

import static org.apache.commons.lang3.JavaVersion.JAVA_10;
import static org.apache.commons.lang3.JavaVersion.JAVA_14;
import static org.apache.commons.lang3.SystemUtils.isJavaVersionAtMost;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

import static org.apache.commons.lang3.JavaVersion.JAVA_10;
import static org.apache.commons.lang3.JavaVersion.JAVA_14;
import static org.apache.commons.lang3.SystemUtils.isJavaVersionAtMost;
import static org.mockserver.integration.ClientAndServer.startClientAndServer;
import static org.mockserver.model.HttpRequest.request;

import java.io.ByteArrayInputStream;
import java.io.FileNotFoundException;
import java.util.EmptyStackException;

import org.jpos.core.Configuration;
import org.jpos.core.ConfigurationException;
import org.jpos.core.SimpleConfiguration;
Expand All @@ -42,6 +42,8 @@
import org.jpos.iso.ISOFieldPackager;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.mockserver.integration.ClientAndServer;
import org.mockserver.verify.VerificationTimes;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
Expand Down Expand Up @@ -550,4 +552,20 @@ public void testSetConfigurationThrowsNullPointerException() throws Throwable {
assertNull(genericSubFieldPackager.getRealm(), "(GenericSubFieldPackager) genericSubFieldPackager.getRealm()");
}
}

@Test
public void testXXEAttach() throws ISOException {
ClientAndServer mockServer = startClientAndServer(1080);

String xeeAttackXml = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n"
+ "<!DOCTYPE foo [\n"
+ " <!ELEMENT foo ANY >\n"
+ " <!ENTITY xxe SYSTEM \"http://localhost:1080/xxe\" >]>\n"
+ "<foo>&xxe;</foo>";
new GenericPackager().readFile(new ByteArrayInputStream(xeeAttackXml.getBytes()));
mockServer.verify(
request().withPath("/xxe"),
VerificationTimes.exactly(0)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,25 @@

package org.jpos.iso.packager;

import static org.apache.commons.lang3.JavaVersion.JAVA_10;
import static org.apache.commons.lang3.JavaVersion.JAVA_14;
import static org.apache.commons.lang3.SystemUtils.isJavaVersionAtMost;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

import static org.apache.commons.lang3.JavaVersion.JAVA_10;
import static org.apache.commons.lang3.JavaVersion.JAVA_14;
import static org.apache.commons.lang3.SystemUtils.isJavaVersionAtMost;
import static org.mockserver.integration.ClientAndServer.startClientAndServer;
import static org.mockserver.model.HttpRequest.request;

import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import java.util.EmptyStackException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jpos.iso.ISOBaseValidator;
import org.jpos.iso.ISOException;
import org.jpos.iso.ISOFieldValidator;
Expand All @@ -51,6 +51,8 @@
import org.jpos.iso.validator.TEST0100;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.mockserver.integration.ClientAndServer;
import org.mockserver.verify.VerificationTimes;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
Expand Down Expand Up @@ -871,4 +873,21 @@ public void testValidateThrowsNullPointerException4() throws Throwable {
}
}
}

@Test
public void testXXEAttach() throws ISOException {
ClientAndServer mockServer = startClientAndServer(1081);

String xeeAttackXml = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n"
+ "<!DOCTYPE foo [\n"
+ " <!ELEMENT foo ANY >\n"
+ " <!ENTITY xxe SYSTEM \"http://localhost:1081/xxe\" >]>\n"
+ "<foo>&xxe;</foo>";
new GenericValidatingPackager().readFile(new ByteArrayInputStream(xeeAttackXml.getBytes()));

mockServer.verify(
request().withPath("/xxe"),
VerificationTimes.exactly(0)
);
}
}