Skip to content

Commit 8dc40cd

Browse files
authored
fix: issue reported by CodeQL (#14936)
Error from CodeQL: Resolving XML external entity in user-controlled data XML parsing depends on a without guarding against external entity expansion.
1 parent 37d411e commit 8dc40cd

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

grails-wrapper/src/main/java/grails/init/GrailsUpdater.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -285,12 +285,10 @@ private static InputStream retrieveMavenMetadata(GrailsWrapperRepo repo, String
285285
}
286286

287287
private GrailsVersion getRootVersion(GrailsWrapperRepo repo) throws IOException, SAXException, ParserConfigurationException {
288-
SAXParserFactory factory = SAXParserFactory.newInstance();
289-
SAXParser saxParser = factory.newSAXParser();
290288
RootMetadataHandler findLastReleaseHandler = new RootMetadataHandler(grailsWrapperHome.allowedReleaseTypes);
291289

292290
try (InputStream stream = retrieveMavenMetadata(repo, repo.getRootMetadataUrl())) {
293-
saxParser.parse(stream, findLastReleaseHandler);
291+
createSAXParser().parse(stream, findLastReleaseHandler);
294292
List<GrailsVersion> foundVersions = findLastReleaseHandler.getVersions();
295293
if (foundVersions.isEmpty()) {
296294
throw new IllegalStateException("No Grails Releases were found for the allowed types: " + grailsWrapperHome.allowedReleaseTypes.stream().map(Enum::name).collect(Collectors.joining(", ")));
@@ -306,12 +304,10 @@ private GrailsVersion getRootVersion(GrailsWrapperRepo repo) throws IOException,
306304
private String fetchSnapshotForVersion(GrailsWrapperRepo repo, GrailsVersion baseVersion) throws IOException, SAXException, ParserConfigurationException {
307305
System.out.println("...A Grails snapshot version has been detected. Downloading latest snapshot.");
308306

309-
SAXParserFactory factory = SAXParserFactory.newInstance();
310-
SAXParser saxParser = factory.newSAXParser();
311307
FindLastSnapshotHandler findVersionHandler = new FindLastSnapshotHandler();
312308

313309
try (InputStream stream = retrieveMavenMetadata(repo, repo.getMetadataUrl(baseVersion))) {
314-
saxParser.parse(stream, findVersionHandler);
310+
createSAXParser().parse(stream, findVersionHandler);
315311
return findVersionHandler.getVersion();
316312
}
317313
}
@@ -323,4 +319,15 @@ private static HttpURLConnection createHttpURLConnection(String mavenMetadataFil
323319
conn.setInstanceFollowRedirects(true);
324320
return conn;
325321
}
322+
323+
private static SAXParser createSAXParser() throws ParserConfigurationException, SAXException {
324+
SAXParserFactory factory = SAXParserFactory.newInstance();
325+
factory.setFeature("http://javax.xml.XMLConstants/feature/secure-processing", true);
326+
factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
327+
factory.setFeature("http://xml.org/sax/features/external-general-entities", false);
328+
factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
329+
factory.setXIncludeAware(false);
330+
factory.setNamespaceAware(true);
331+
return factory.newSAXParser();
332+
}
326333
}

0 commit comments

Comments
 (0)