|
46 | 46 | import javax.xml.stream.XMLInputFactory; |
47 | 47 | import javax.xml.stream.XMLStreamReader; |
48 | 48 | import javax.xml.stream.events.XMLEvent; |
| 49 | +import javax.xml.validation.Schema; |
| 50 | +import javax.xml.validation.SchemaFactory; |
49 | 51 | import java.awt.*; |
50 | 52 | import java.awt.image.BufferedImage; |
51 | 53 | import java.io.*; |
@@ -1379,4 +1381,35 @@ public static UUID computeUUIDv7() { |
1379 | 1381 | UUID uuidv7 = new UUID(high, low); |
1380 | 1382 | return uuidv7; |
1381 | 1383 | } |
| 1384 | + |
| 1385 | + /** |
| 1386 | + * Ensure that an XSD file does not contain any include/import instruction (prevent exposure to SSRF). |
| 1387 | + * |
| 1388 | + * @param xsdFilePath Filename of the XSD file to check. |
| 1389 | + * @return True only if the file pass all validations. |
| 1390 | + * @see "https://portswigger.net/web-security/ssrf" |
| 1391 | + * @see "https://www.w3schools.com/Xml/el_import.asp" |
| 1392 | + * @see "https://www.w3schools.com/xml/el_include.asp" |
| 1393 | + * @see "https://www.linkedin.com/posts/righettod_appsec-appsecurity-java-activity-7344048434326188053-6Ru9" |
| 1394 | + * @see "https://docs.oracle.com/en/java/javase/21/docs/api/java.xml/javax/xml/validation/SchemaFactory.html#setProperty(java.lang.String,java.lang.Object)" |
| 1395 | + */ |
| 1396 | + public static boolean isXSDSafe(String xsdFilePath) { |
| 1397 | + boolean isSafe = false; |
| 1398 | + try { |
| 1399 | + File xsdFile = new File(xsdFilePath); |
| 1400 | + if (xsdFile.exists() && xsdFile.canRead() && xsdFile.isFile()) { |
| 1401 | + //Parse the XSD file, if an exception occur then it's imply that the XSD specified is not a valid ones |
| 1402 | + //Create an schema factory throwing Exception if a external schema is specified |
| 1403 | + SchemaFactory schemaFactory = SchemaFactory.newDefaultInstance(); |
| 1404 | + schemaFactory.setProperty(XMLConstants.ACCESS_EXTERNAL_DTD, ""); |
| 1405 | + schemaFactory.setProperty(XMLConstants.ACCESS_EXTERNAL_SCHEMA, ""); |
| 1406 | + //Parse the schema |
| 1407 | + Schema schema = schemaFactory.newSchema(xsdFile); |
| 1408 | + isSafe = (schema != null); |
| 1409 | + } |
| 1410 | + } catch (Exception e) { |
| 1411 | + isSafe = false; |
| 1412 | + } |
| 1413 | + return isSafe; |
| 1414 | + } |
1382 | 1415 | } |
0 commit comments