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
secure xml #1
Merged
Merged
secure xml #1
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
00409c8
ignore intellij idea files
viqueen d2ab4d5
secure-xml: welcome test to highlight the xxe vulnerability
viqueen 67283fb
secure-xml: welcome secure xml interface providing a safe parser
viqueen fb93399
secure-xml: increase test coverage for MethodBase that shows the same…
viqueen cd51e67
secure-xml: update method base to rely on secure xml parser
viqueen ae4469f
Revert "ignore intellij idea files"
viqueen 0ce2007
format: follow the repo code style, and welcome mvnvm props to manage…
viqueen File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
mvn_version=3.5.4 | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
src/main/java/org/bedework/webdav/servlet/common/SecureXml.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package org.bedework.webdav.servlet.common; | ||
|
||
import org.bedework.webdav.servlet.shared.WebdavBadRequest; | ||
import org.bedework.webdav.servlet.shared.WebdavException; | ||
import org.w3c.dom.Document; | ||
import org.xml.sax.InputSource; | ||
import org.xml.sax.SAXException; | ||
|
||
import javax.xml.parsers.DocumentBuilder; | ||
import javax.xml.parsers.DocumentBuilderFactory; | ||
import java.io.Reader; | ||
|
||
public interface SecureXml { | ||
|
||
default Document parseXmlSafely(final int contentLength, final Reader reader) throws WebdavException { | ||
if (contentLength == 0) { | ||
return null; | ||
} | ||
|
||
if (reader == null) { | ||
// No content? | ||
return null; | ||
} | ||
|
||
try { | ||
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); | ||
factory.setNamespaceAware(false); | ||
factory.setFeature("http://javax.xml.XMLConstants/feature/secure-processing", true); | ||
factory.setFeature("http://xml.org/sax/features/external-general-entities", false); | ||
factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false); | ||
factory.setAttribute("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); | ||
|
||
DocumentBuilder builder = factory.newDocumentBuilder(); | ||
|
||
return builder.parse(new InputSource(reader)); | ||
} catch (SAXException exception) { | ||
throw new WebdavBadRequest(exception.getMessage()); | ||
} catch (Throwable t) { | ||
throw new WebdavException(t); | ||
} | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
src/test/java/org/bedework/webdav/servlet/common/TestSecureXmlTypes.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package org.bedework.webdav.servlet.common; | ||
|
||
import org.bedework.webdav.servlet.shared.WebdavException; | ||
import org.bedework.webdav.servlet.shared.WebdavNsIntf; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.mockito.Answers; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.MockitoJUnit; | ||
import org.mockito.junit.MockitoRule; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.util.UUID; | ||
|
||
import static org.junit.Assert.assertNotNull; | ||
import static org.junit.Assert.assertTrue; | ||
import static org.mockito.Mockito.when; | ||
|
||
public class TestSecureXmlTypes { | ||
|
||
@Rule | ||
public MockitoRule mockitoRule = MockitoJUnit.rule(); | ||
|
||
@Mock | ||
private HttpServletRequest request; | ||
@Mock | ||
private HttpServletResponse response; | ||
|
||
@Mock(answer = Answers.CALLS_REAL_METHODS) | ||
private WebdavNsIntf webdavNsIntf; | ||
|
||
@Mock(answer = Answers.CALLS_REAL_METHODS) | ||
private MethodBase methodBase; | ||
|
||
private PostRequestPars requestPars; | ||
|
||
@Before | ||
public void setup() throws WebdavException, IOException { | ||
when(request.getContentType()).thenReturn("application/xml"); | ||
requestPars = new PostRequestPars(request, webdavNsIntf, UUID.randomUUID().toString()); | ||
|
||
when(methodBase.getNsIntf()).thenReturn(webdavNsIntf); | ||
|
||
when(request.getContentLength()).thenReturn(1); | ||
when(request.getReader()).thenReturn(getResource("/malicious-request.xml")); | ||
} | ||
|
||
@Test | ||
public void testPostRequestParsWithMaliciousRequest() throws WebdavException { | ||
assertTrue(requestPars.processXml()); | ||
} | ||
|
||
@Test | ||
public void testMethodBaseWithMaliciousRequest() throws WebdavException { | ||
assertNotNull(methodBase.parseContent(request, response)); | ||
} | ||
|
||
private BufferedReader getResource(final String name) { | ||
return new BufferedReader( | ||
new InputStreamReader(this.getClass().getResourceAsStream(name)) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!DOCTYPE invite-reply [ | ||
<!ENTITY xxe SYSTEM "file:///xxe"> | ||
]> | ||
<A:invite-reply xmlns:A="http://calendarserver.org/ns/" xmlns:B="DAV:"> | ||
<A:invite-accepted>&xxe;</A:invite-accepted> | ||
<B:href>/some/url</B:href> | ||
<A:in-reply-to>something</A:in-reply-to> | ||
<A:hosturl> | ||
<B:href>/</B:href> | ||
</A:hosturl> | ||
</A:invite-reply> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we use the following to manage maven versions across developers http://mvnvm.org/
thought it might be useful here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks - I'll take a look