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 code scanning alert no. 41: Resolving XML external entity in user-controlled data #1205

Merged
merged 2 commits into from
Dec 19, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,11 @@ public String descriptionRetrieval(String patentNumber) throws IOException,
spf.setFeature("http://xml.org/sax/features/validation", false);
spf.setFeature("http://xml.org/sax/features/external-general-entities", false);
spf.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
spf.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
//get a new instance of parser
XMLReader reader = spf.newSAXParser().getXMLReader();
reader.setFeature("http://xml.org/sax/features/external-general-entities", false);
reader.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
reader.setEntityResolver(new EntityResolver() {
public InputSource resolveEntity(String publicId, String systemId) {
return new InputSource(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,11 @@ public void run() {
public Response resultTraining(String token) {
Response response = null;
try {
// Validate the token to prevent directory traversal
if (token.contains("..") || token.contains("/") || token.contains("\\")) {
throw new GrobidServiceException("Invalid token", Status.BAD_REQUEST);
}

// access report file under token subdirectory
File home = GrobidProperties.getInstance().getGrobidHomePath();
String tokenPath = home.getAbsolutePath() + "/training-history/" + token;
Expand Down