|
12 | 12 | import static java.nio.charset.StandardCharsets.UTF_8;
|
13 | 13 | import static org.junit.Assert.assertEquals;
|
14 | 14 | import static org.junit.Assert.assertNull;
|
| 15 | +import static org.junit.Assert.assertThrows; |
15 | 16 | import static org.junit.Assert.assertTrue;
|
16 | 17 | import static org.junit.Assert.fail;
|
17 | 18 |
|
18 | 19 | import java.io.ByteArrayInputStream;
|
| 20 | +import java.io.File; |
19 | 21 | import java.io.IOException;
|
20 | 22 | import java.net.URI;
|
| 23 | +import java.nio.file.Files; |
| 24 | +import java.nio.file.StandardOpenOption; |
21 | 25 | import java.util.HashSet;
|
22 | 26 | import java.util.Map;
|
23 | 27 | import java.util.Set;
|
@@ -221,4 +225,33 @@ public void testNormalizeEmptyPath() {
|
221 | 225 | testNormalize("", "");
|
222 | 226 | testNormalize("a/b", "a/b");
|
223 | 227 | }
|
| 228 | + |
| 229 | + @Test |
| 230 | + public void testXXE() throws Exception { |
| 231 | + File externalEntity = File.createTempFile("injected", "xml"); |
| 232 | + externalEntity.deleteOnExit(); |
| 233 | + Files.write(externalEntity.toPath(), |
| 234 | + "<evil>injected xml</evil>" |
| 235 | + .getBytes(UTF_8), |
| 236 | + StandardOpenOption.WRITE); |
| 237 | + String baseUrl = "https://git.google.com/"; |
| 238 | + StringBuilder xmlContent = new StringBuilder(); |
| 239 | + xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n") |
| 240 | + .append("<!DOCTYPE booo [ <!ENTITY foobar SYSTEM \"") |
| 241 | + .append(externalEntity.getPath()).append("\"> ]>\n") |
| 242 | + .append("<manifest>") |
| 243 | + .append("<remote name=\"remote1\" fetch=\".\" />") |
| 244 | + .append("<default revision=\"master\" remote=\"remote1\" />") |
| 245 | + .append("&foobar;") |
| 246 | + .append("<project path=\"foo\" name=\"foo\" groups=\"a,test\" />") |
| 247 | + .append("</manifest>"); |
| 248 | + |
| 249 | + IOException e = assertThrows(IOException.class, |
| 250 | + () -> new ManifestParser(null, null, "master", baseUrl, null, |
| 251 | + null) |
| 252 | + .read(new ByteArrayInputStream( |
| 253 | + xmlContent.toString().getBytes(UTF_8)))); |
| 254 | + assertTrue(e.getCause().getMessage().contains("DOCTYPE")); |
| 255 | + } |
| 256 | + |
224 | 257 | }
|
0 commit comments