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 importer vulnerability #4240

Merged
merged 6 commits into from
Jul 30, 2018
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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `#
- We fixed an issue where the "Convert to BibTeX-Cleanup" moved the content of the `file` field to the `pdf` field [#4120](https://github.com/JabRef/jabref/issues/4120)
- We fixed an issue where the preview pane in entry preview in preferences wasn't showing the citation style selected [#3849](https://github.com/JabRef/jabref/issues/3849)
- We fixed an issue where the default entry preview style still contained the field `review`. The field `review` in the style is now replaced with comment to be consistent with the entry editor [#4098](https://github.com/JabRef/jabref/issues/4098)
- We fixed an issue where filles added via the "Attach file" contextmenu of an entry were not made relative. [#4201](https://github.com/JabRef/jabref/issues/4201)
- We fixed an issue where users were vulnerable to XXE attacks during parsing [#4229](https://github.com/JabRef/jabref/issues/4229)
- We fixed an issue where files added via the "Attach file" contextmenu of an entry were not made relative. [#4201](https://github.com/JabRef/jabref/issues/4201)



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.jabref.logic.importer.Importer;
import org.jabref.logic.importer.ParserResult;
import org.jabref.logic.msbib.MSBibDatabase;
import org.jabref.logic.util.StandardFileType;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
Expand All @@ -20,24 +23,27 @@

/**
* Importer for the MS Office 2007 XML bibliography format
* By S. M. Mahbub Murshed
*
* ...
*/
public class MsBibImporter extends Importer {

private static final Logger LOGGER = LoggerFactory.getLogger(MsBibImporter.class);
private static final String DISABLEDTD = "http://apache.org/xml/features/disallow-doctype-decl";
private static final String DISABLEEXTERNALDTD = "http://apache.org/xml/features/nonvalidating/load-external-dtd";

@Override
public boolean isRecognizedFormat(BufferedReader reader) throws IOException {
Objects.requireNonNull(reader);

/*
The correct behaviour is to return false if it is certain that the file is
The correct behavior is to return false if it is certain that the file is
not of the MsBib type, and true otherwise. Returning true is the safe choice
if not certain.
*/
Document docin;
try {
DocumentBuilder dbuild = DocumentBuilderFactory.newInstance().newDocumentBuilder();
DocumentBuilder dbuild = makeSafeDocBuilderFactory(DocumentBuilderFactory.newInstance()).newDocumentBuilder();
dbuild.setErrorHandler(new ErrorHandler() {

@Override
Expand All @@ -55,6 +61,7 @@ public void error(SAXParseException exception) throws SAXException {
throw exception;
}
});

docin = dbuild.parse(new InputSource(reader));
} catch (Exception e) {
return false;
Expand Down Expand Up @@ -85,4 +92,29 @@ public String getDescription() {
return "Importer for the MS Office 2007 XML bibliography format.";
}

/**
* DocumentBuilderFactory makes a XXE safe Builder factory from dBuild. If not supported by current
* XML then returns original builder given and logs error.
* @param dBuild | DocumentBuilderFactory to be made XXE safe.
* @return If supported, XXE safe DocumentBuilderFactory. Else, returns original builder given
*/
private DocumentBuilderFactory makeSafeDocBuilderFactory(DocumentBuilderFactory dBuild) {
String feature = null;

try {
feature = DISABLEDTD;
dBuild.setFeature(feature, true);

feature = DISABLEEXTERNALDTD;
dBuild.setFeature(feature, false);

dBuild.setXIncludeAware(false);
dBuild.setExpandEntityReferences(false);

} catch (ParserConfigurationException e) {
LOGGER.warn("Builder not fully configured. Feature:'{}' is probably not supported by current XML processor. {}", feature, e);
}

return dBuild;
}
}