-
Notifications
You must be signed in to change notification settings - Fork 162
feat: add xml parser guidance hook #999
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
184 changes: 184 additions & 0 deletions
184
sanitizers/src/main/java/com/code_intelligence/jazzer/sanitizers/XmlParserSsrfGuidance.kt
This file contains hidden or 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,184 @@ | ||
| /* | ||
| * Copyright 2025 Code Intelligence GmbH | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.code_intelligence.jazzer.sanitizers | ||
|
|
||
| import com.code_intelligence.jazzer.api.HookType | ||
| import com.code_intelligence.jazzer.api.Jazzer | ||
| import com.code_intelligence.jazzer.api.MethodHook | ||
| import com.code_intelligence.jazzer.api.MethodHooks | ||
| import org.xml.sax.InputSource | ||
| import java.io.InputStream | ||
| import java.lang.invoke.MethodHandle | ||
|
|
||
| /** | ||
| * Guides XML parser entry points towards patterns that can trigger external resource fetching. | ||
| * | ||
| * This does not report findings directly; it steers inputs so that existing SSRF detection | ||
| * (e.g. Socket/SocketChannel hooks) can observe network connections initiated by XML parsers | ||
| * resolving external entities, schemas, or includes. | ||
| */ | ||
| @Suppress("unused") | ||
| object XmlParserSsrfGuidance { | ||
| private val EXTERNAL_DOCTYPE = "<!DOCTYPE x PUBLIC \"\" \"http://foo\">" | ||
| private val EXTERNAL_DOCTYPE_SIZE = EXTERNAL_DOCTYPE.toByteArray().size | ||
|
|
||
| init { | ||
| require(EXTERNAL_DOCTYPE_SIZE <= 64) { | ||
| "XML exploit must fit in a table of recent compares entry (64 bytes)" | ||
| } | ||
| } | ||
|
|
||
| // Top-level URI fetch guidance when a systemId is provided as a String. | ||
| private const val HTTP_PREFIX = "http://" | ||
| private const val HTTPS_PREFIX = "https://" | ||
|
|
||
| private fun guidePossibleXmlStream( | ||
| arg: Any?, | ||
| hookId: Int, | ||
| ) { | ||
| when (arg) { | ||
| is InputStream -> { | ||
| runCatching { | ||
| Jazzer.guideTowardsContainment( | ||
| String(peekMarkableInputStream(arg, EXTERNAL_DOCTYPE_SIZE)), | ||
| EXTERNAL_DOCTYPE, | ||
| hookId, | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| is InputSource -> { | ||
| arg.byteStream?.let { stream -> | ||
| runCatching { | ||
| Jazzer.guideTowardsContainment( | ||
| String(peekMarkableInputStream(stream, EXTERNAL_DOCTYPE_SIZE)), | ||
| EXTERNAL_DOCTYPE, | ||
| hookId, | ||
| ) | ||
| } | ||
| } | ||
| arg.characterStream?.let { reader -> | ||
| runCatching { | ||
| Jazzer.guideTowardsContainment( | ||
| peekMarkableReader(reader, EXTERNAL_DOCTYPE_SIZE), | ||
| EXTERNAL_DOCTYPE, | ||
| hookId, | ||
| ) | ||
| } | ||
| } | ||
| // If only a systemId is provided, guide it to be a URL. | ||
| arg.systemId?.let { guidePossibleUrlString(it, hookId) } | ||
| } | ||
|
|
||
| is String -> { | ||
| // Some parse APIs accept a systemId/URI as a String. | ||
| guidePossibleUrlString(arg, hookId) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private fun guidePossibleUrlString( | ||
| s: String, | ||
| hookId: Int, | ||
| ) { | ||
| Jazzer.guideTowardsContainment(s, HTTP_PREFIX, hookId) | ||
| Jazzer.guideTowardsContainment(s, HTTPS_PREFIX, 31 * hookId) | ||
| } | ||
|
|
||
| // javax.xml.parsers.DocumentBuilder.parse(...) | ||
| @MethodHook( | ||
| type = HookType.BEFORE, | ||
| targetClassName = "javax.xml.parsers.DocumentBuilder", | ||
| targetMethod = "parse", | ||
| ) | ||
| @JvmStatic | ||
| fun guideDocumentBuilderParse( | ||
| method: MethodHandle, | ||
| thisObject: Any?, | ||
| arguments: Array<Any>, | ||
| hookId: Int, | ||
| ) { | ||
| if (arguments.isNotEmpty()) { | ||
| guidePossibleXmlStream(arguments[0], hookId) | ||
| if (arguments.size >= 2) guidePossibleXmlStream(arguments[1], 13 * hookId) | ||
| } | ||
| } | ||
|
|
||
| @MethodHook( | ||
| type = HookType.BEFORE, | ||
| targetClassName = "org.xml.sax.XMLReader", | ||
| targetMethod = "parse", | ||
| ) | ||
| @JvmStatic | ||
| fun guideXmlReaderParse( | ||
| method: MethodHandle, | ||
| thisObject: Any?, | ||
| arguments: Array<Any>, | ||
| hookId: Int, | ||
| ) { | ||
| if (arguments.isNotEmpty()) { | ||
| guidePossibleXmlStream(arguments[0], hookId) | ||
| } | ||
| } | ||
|
|
||
| @MethodHook( | ||
| type = HookType.BEFORE, | ||
| targetClassName = "javax.xml.parsers.SAXParser", | ||
| targetMethod = "parse", | ||
| ) | ||
| @JvmStatic | ||
| fun guideSaxParserParse( | ||
| method: MethodHandle, | ||
| thisObject: Any?, | ||
| arguments: Array<Any>, | ||
| hookId: Int, | ||
| ) { | ||
| if (arguments.isNotEmpty()) { | ||
| // First arg is usually the source (InputStream, InputSource, File, or String systemId). | ||
| guidePossibleXmlStream(arguments[0], hookId) | ||
| if (arguments.size >= 3) { | ||
| // There is an overload parse(InputStream, HandlerBase/DefaultHandler, String systemId) | ||
| guidePossibleXmlStream(arguments[2], 17 * hookId) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @MethodHooks( | ||
| MethodHook( | ||
| type = HookType.BEFORE, | ||
| targetClassName = "javax.xml.stream.XMLInputFactory", | ||
| targetMethod = "createXMLStreamReader", | ||
| ), | ||
| MethodHook( | ||
| type = HookType.BEFORE, | ||
| targetClassName = "javax.xml.stream.XMLInputFactory", | ||
| targetMethod = "createXMLEventReader", | ||
| ), | ||
| ) | ||
| @JvmStatic | ||
| fun guideStaxCreateReader( | ||
| method: MethodHandle, | ||
| thisObject: Any?, | ||
| arguments: Array<Any>, | ||
| hookId: Int, | ||
| ) { | ||
| if (arguments.isNotEmpty()) { | ||
| guidePossibleXmlStream(arguments[0], hookId) | ||
| if (arguments.size >= 2) guidePossibleXmlStream(arguments[1], 19 * hookId) | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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,39 @@ | ||
| /* | ||
| * Copyright 2025 Code Intelligence GmbH | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.example; | ||
|
|
||
| import java.io.StringReader; | ||
| import javax.xml.parsers.SAXParserFactory; | ||
| import org.xml.sax.InputSource; | ||
| import org.xml.sax.XMLReader; | ||
| import org.xml.sax.helpers.DefaultHandler; | ||
|
|
||
| public class SsrfXmlParser { | ||
|
|
||
| public static void fuzzerTestOneInput(byte[] data) throws Exception { | ||
| SAXParserFactory factory = SAXParserFactory.newInstance(); | ||
| // Default XML parser supports looking up external entities. | ||
| // Disallow all doctype declarations if XML is not trusted: | ||
| // factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true); | ||
| XMLReader parser = factory.newSAXParser().getXMLReader(); | ||
| parser.setErrorHandler(new DefaultHandler() {}); | ||
| try { | ||
| parser.parse(new InputSource(new StringReader(new String(data)))); | ||
| } catch (Exception ignored) { | ||
| } | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
Really minor: Shouldn't this use
sizeinstead ofreadlimitfor consistency, sincesizeis the parameter of this local function (and it just happens to be called withreadlimitas value)?Also, would it make sense to switch the branches? Due to Copilot's suggestion to use
>=, this statement looks a bit confusing now. What about this?(Sorry if this comment is distruptive; please let me know in that case)
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.
Not disruptive at all. I completely agree. :)
I created a small PR: #1011