This repository was archived by the owner on Jan 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 128
CWE-643 XPathInjection on Go #66
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d6ff6b7
CWE-643 XPathInjection on Go
intrigus ec40cf0
Apply suggestions from review
intrigus-lgtm c7ead88
Restructure query, add default sanitizer
intrigus 948b79d
Update xpath example, use goxpath package
intrigus d81c9b1
Update query help to use goxpath
intrigus 9187bac
Apply suggestion from code review
intrigus-lgtm 1f63580
Fix copy-paste errors, remove debugging code
intrigus 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "net/http" | ||
|
|
||
| "github.com/ChrisTrenkamp/goxpath" | ||
| "github.com/ChrisTrenkamp/goxpath/tree" | ||
| ) | ||
|
|
||
| func main() {} | ||
|
|
||
| func processRequest(r *http.Request, doc tree.Node) { | ||
| r.ParseForm() | ||
| username := r.Form.Get("username") | ||
| password := r.Form.Get("password") | ||
|
|
||
| // BAD: User input used directly in an XPath expression | ||
| xPath := goxpath.MustParse("//users/user[login/text()='" + username + "' and password/text() = '" + password + "']/home_dir/text()") | ||
| unsafeRes, _ := xPath.ExecBool(doc) | ||
| fmt.Println(unsafeRes) | ||
|
|
||
| // GOOD: Value of parameters is defined here instead of directly in the query | ||
| opt := func(o *goxpath.Opts) { | ||
| o.Vars["username"] = tree.String(username) | ||
| o.Vars["password"] = tree.String(password) | ||
| } | ||
| // GOOD: Uses parameters to avoid including user input directly in XPath expression | ||
| xPath = goxpath.MustParse("//users/user[login/text()=$username and password/text() = $password]/home_dir/text()") | ||
| safeRes, _ := xPath.ExecBool(doc, opt) | ||
| fmt.Println(safeRes) | ||
| } | ||
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,44 @@ | ||
| <!DOCTYPE qhelp PUBLIC | ||
| "-//Semmle//qhelp//EN" | ||
| "qhelp.dtd"> | ||
| <qhelp> | ||
| <overview> | ||
| <p> | ||
| If an XPath expression is built using string concatenation, and the components of the concatenation | ||
| include user input, a user is likely to be able to create a malicious XPath expression. | ||
| </p> | ||
| </overview> | ||
|
|
||
| <recommendation> | ||
| <p> | ||
| If user input must be included in an XPath expression, pre-compile the query and use variable | ||
| references to include the user input. | ||
| </p> | ||
| <p> | ||
| For example, when using the <code>github.com/ChrisTrenkamp/goxpath</code> API, this can be done by creating a function that takes an <code>*goxpath.Opts</code> structure. | ||
| In this structure you can then set the values of the variable references. | ||
| This function can then be specified when calling <code>Exec()</code>, <code>Exec{Bool|Num|Node}()</code>, <code>ParseExec()</code> or <code>MustExec()</code>. | ||
| </p> | ||
|
|
||
| </recommendation> | ||
|
|
||
| <example> | ||
| <p> | ||
| In the first example, the code accepts a user name specified by the user, and uses this | ||
| unvalidated and unsanitized value in an XPath expression. This is vulnerable to the user providing | ||
| special characters or string sequences that change the meaning of the XPath expression to search | ||
| for different values. | ||
| </p> | ||
|
|
||
| <p> | ||
| In the second example, the XPath expression is a hard-coded string that specifies some variables, | ||
| which are safely resolved at runtime using the <code>goxpath.Opts</code> structure. | ||
| </p> | ||
| <sample src="XPathInjection.go" /> | ||
| </example> | ||
|
|
||
| <references> | ||
| <li>OWASP: <a href="https://www.owasp.org/index.php?title=Testing_for_XPath_Injection_(OTG-INPVAL-010)">Testing for XPath Injection</a>.</li> | ||
| <li>OWASP: <a href="https://www.owasp.org/index.php/XPATH_Injection">XPath Injection</a>.</li> | ||
| </references> | ||
| </qhelp> |
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,188 @@ | ||
| /** | ||
| * @name XPath injection | ||
| * @description Building an XPath expression from user-controlled sources is vulnerable to insertion of | ||
| * malicious code by the user. | ||
| * @kind path-problem | ||
| * @problem.severity error | ||
| * @id go/xml/xpath-injection | ||
| * @tags security | ||
| * external/cwe/cwe-643 | ||
| */ | ||
|
|
||
| import go | ||
| import DataFlow::PathGraph | ||
|
|
||
| class ByteSliceType extends SliceType { | ||
| ByteSliceType() { this.getElementType() instanceof Uint8Type } | ||
| } | ||
|
|
||
| class XPathInjectionConfiguration extends TaintTracking::Configuration { | ||
| XPathInjectionConfiguration() { this = "XPathInjectionConfiguration" } | ||
|
|
||
| override predicate isSource(DataFlow::Node source) { source instanceof UntrustedFlowSource } | ||
|
|
||
| override predicate isSink(DataFlow::Node sink) { sink instanceof XPathInjectionSink } | ||
|
|
||
| override predicate isSanitizer(DataFlow::Node node) { | ||
| exists(Type t | t = node.getType().getUnderlyingType() | | ||
| not t instanceof StringType or not t instanceof ByteSliceType | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| abstract class XPathInjectionSink extends DataFlow::Node { } | ||
|
|
||
| // https://github.com/antchfx/xpath | ||
| class XPathSink extends XPathInjectionSink { | ||
| XPathSink() { | ||
| exists(Function f, string name | name.matches("Compile%") | | ||
| f.hasQualifiedName("github.com/antchfx/xpath", name) and | ||
| this = f.getACall().getArgument(0) | ||
| ) | ||
| or | ||
| exists(Function f, string name | name.matches("MustCompile%") | | ||
| f.hasQualifiedName("github.com/antchfx/xpath", name) and | ||
| this = f.getACall().getArgument(0) | ||
| ) | ||
| or | ||
| exists(Function f, string name | name.matches("Select%") | | ||
| f.hasQualifiedName("github.com/antchfx/xpath", name) and | ||
| this = f.getACall().getArgument(1) | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| // https://github.com/antchfx/htmlquery | ||
| class HtmlQuerySink extends XPathInjectionSink { | ||
| HtmlQuerySink() { | ||
| exists(Function f, string name | name.matches("Find%") | | ||
| f.hasQualifiedName("github.com/antchfx/htmlquery", name) and | ||
| this = f.getACall().getArgument(1) | ||
| ) | ||
| or | ||
| exists(Function f, string name | name.matches("Query%") | | ||
| f.hasQualifiedName("github.com/antchfx/htmlquery", name) and | ||
| this = f.getACall().getArgument(1) | ||
| ) | ||
| or | ||
| exists(Function f | | ||
| f.hasQualifiedName("github.com/antchfx/htmlquery", "getQuery") and | ||
| this = f.getACall().getArgument(0) | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| // https://github.com/antchfx/xmlquery | ||
| class XmlQuerySink extends XPathInjectionSink { | ||
| XmlQuerySink() { | ||
| exists(Function f, string name | name.matches("Find%") | | ||
| f.hasQualifiedName("github.com/antchfx/xmlquery", name) and | ||
| this = f.getACall().getArgument(1) | ||
| ) | ||
| or | ||
| exists(Function f, string name | name.matches("Query%") | | ||
| f.hasQualifiedName("github.com/antchfx/xmlquery", name) and | ||
| this = f.getACall().getArgument(1) | ||
| ) | ||
| or | ||
| exists(Function f, string name | name.matches("Select%") | | ||
| f.hasQualifiedName("github.com/antchfx/xmlquery", name) and | ||
| this = f.getACall().getArgument(0) | ||
| ) | ||
| or | ||
| exists(Function f | | ||
| f.hasQualifiedName("github.com/antchfx/xmlquery", "getQuery") and | ||
| this = f.getACall().getArgument(0) | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| // https://github.com/antchfx/jsonquery | ||
| class JsonQuerySink extends XPathInjectionSink { | ||
| JsonQuerySink() { | ||
| exists(Function f, string name | name.matches("Find%") | | ||
| f.hasQualifiedName("github.com/antchfx/jsonquery", name) and | ||
| this = f.getACall().getArgument(1) | ||
| ) | ||
| or | ||
| exists(Function f, string name | name.matches("Query%") | | ||
| f.hasQualifiedName("github.com/antchfx/jsonquery", name) and | ||
| this = f.getACall().getArgument(1) | ||
| ) | ||
| or | ||
| exists(Function f | | ||
| f.hasQualifiedName("github.com/antchfx/jsonquery", "getQuery") and | ||
| this = f.getACall().getArgument(0) | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| // https://github.com/go-xmlpath/xmlpath | ||
| class XmlPathSink extends XPathInjectionSink { | ||
| XmlPathSink() { | ||
| exists(Function f, string name | name.matches("Compile%") | | ||
| f.hasQualifiedName("github.com/go-xmlpath/xmlpath", name) and | ||
| this = f.getACall().getArgument(0) | ||
| ) | ||
| or | ||
| exists(Function f, string name | name.matches("MustCompile%") | | ||
| f.hasQualifiedName("github.com/go-xmlpath/xmlpath", name) and | ||
| this = f.getACall().getArgument(0) | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| // https://github.com/ChrisTrenkamp/goxpath | ||
| class GoXPathSink extends XPathInjectionSink { | ||
| GoXPathSink() { | ||
| exists(Function f, string name | name.matches("Parse%") | | ||
| f.hasQualifiedName("github.com/ChrisTrenkamp/goxpath", name) and | ||
| this = f.getACall().getArgument(0) | ||
| ) | ||
| or | ||
| exists(Function f, string name | name.matches("MustParse%") | | ||
| f.hasQualifiedName("github.com/ChrisTrenkamp/goxpath", name) and | ||
| this = f.getACall().getArgument(0) | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| // https://github.com/santhosh-tekuri/xpathparser | ||
| class XPathParserSink extends XPathInjectionSink { | ||
| XPathParserSink() { | ||
| exists(Function f, string name | name.matches("Parse%") | | ||
| f.hasQualifiedName("github.com/santhosh-tekuri/xpathparser", name) and | ||
| this = f.getACall().getArgument(0) | ||
| ) | ||
| or | ||
| exists(Function f, string name | name.matches("MustParse%") | | ||
| f.hasQualifiedName("github.com/santhosh-tekuri/xpathparser", name) and | ||
| this = f.getACall().getArgument(0) | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| // https://github.com/moovweb/gokogiri | ||
| class GokogiriSink extends XPathInjectionSink { | ||
| GokogiriSink() { | ||
| exists(Function f, string name | name.matches("Compile%") | | ||
| f.hasQualifiedName("github.com/moovweb/gokogiri/xpath", name) and | ||
| this = f.getACall().getArgument(0) | ||
| ) | ||
| or | ||
| exists(Function f, string name | name.matches("Search%") | | ||
| f.hasQualifiedName("github.com/moovweb/gokogiri/xml", name) and | ||
| this = f.getACall().getArgument(0) | ||
| ) | ||
| or | ||
| exists(Function f, string name | name.matches("EvalXPath%") | | ||
| f.hasQualifiedName("github.com/moovweb/gokogiri/xml", name) and | ||
| this = f.getACall().getArgument(0) | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| from DataFlow::PathNode source, DataFlow::PathNode sink, XPathInjectionConfiguration c | ||
| where c.hasFlowPath(source, sink) | ||
| select sink.getNode(), source, sink, "$@ flows here and is used in an XPath expression.", | ||
| source.getNode(), "A user-provided value" |
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.