Repository navigation
Python : Add Xpath injection query #3522
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 1 commit
Commits
Show all changes
4 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
include sugestions from review
- Loading branch information
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 |
|---|---|---|
| @@ -1,32 +1,30 @@ | ||
| <!DOCTYPE qhelp SYSTEM "qhelp.dtd"> | ||
| <qhelp> | ||
| <overview> | ||
| Using user-supplied information to construct an XPath query for XML data can | ||
| <p> | ||
| Using user-supplied information to construct an XPath query for XML data can | ||
| result in an XPath injection flaw. By sending intentionally malformed information, | ||
| an attacker can access data that he may not normally have access to. | ||
| He/She may even be able to elevate his privileges on the web site if the XML data | ||
| an attacker can access data that he may not normally have access to. | ||
| He/She may even be able to elevate his privileges on the web site if the XML data | ||
| is being used for authentication (such as an XML based user file). | ||
| </p> | ||
| </overview> | ||
| <recommendation> | ||
| <p> | ||
| XPath injection can be prevented using parameterized XPath interface or escaping the user input to make it safe to include in a dynamically constructed query. | ||
| If you are using quotes to terminate untrusted input in a dynamically constructed XPath query, then you need to escape that quote in the untrusted input to ensure the untrusted data can’t try to break out of that quoted context. | ||
| XPath injection can be prevented using parameterized XPath interface or escaping the user input to make it safe to include in a dynamically constructed query. | ||
| If you are using quotes to terminate untrusted input in a dynamically constructed XPath query, then you need to escape that quote in the untrusted input to ensure the untrusted data can’t try to break out of that quoted context. | ||
| </p> | ||
| <p> | ||
| Another better mitigation option is to use a precompiled XPath query. Precompiled XPath queries are already preset before the program executes, rather than created on the fly after the user’s input has been added to the string. This is a better route because you don’t have to worry about missing a character that should have been escaped. | ||
| </p> | ||
| <example> | ||
|
|
||
| <p>In the example below, the xpath query is controlled by the user and hence leads to a vulnerability.</p> | ||
|
|
||
| <sample src="xpath.py" /> | ||
| </example> | ||
| <references> | ||
| <li>OWASP XPath injection : <a href="https://owasp.org/www-community/attacks/XPATH_Injection"></a>/>> </li> | ||
| </references> | ||
|
|
||
|
|
||
| </recommendation> | ||
|
|
||
|
|
||
| <example> | ||
| <p>In the example below, the xpath query is controlled by the user and hence leads to a vulnerability.</p> | ||
| <sample src="xpathBad.py" /> | ||
| </example> | ||
| <p> This can be fixed by using a parameterized query as shown below.</p> | ||
| <sample src="xpathGood.py" /> | ||
| <references> | ||
| <li>OWASP XPath injection : <a href="https://owasp.org/www-community/attacks/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
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,18 @@ | ||
| from lxml import etree | ||
| from io import StringIO | ||
|
|
||
| from django.urls import path | ||
| from django.http import HttpResponse | ||
| from django.template import Template, Context, Engine, engines | ||
|
|
||
|
|
||
| def a(request): | ||
| value = request.GET['xpath'] | ||
| f = StringIO('<foo><bar></bar></foo>') | ||
| tree = etree.parse(f) | ||
| r = tree.xpath("/tag[@id=$tagid]", tagid=value) | ||
|
|
||
|
|
||
| urlpatterns = [ | ||
| path('a', a) | ||
| ] |
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 was deleted.
Oops, something went wrong.
4 changes: 0 additions & 4 deletions
4
python/ql/test/experimental/CWE-643/XpathLibTests/xpathSinks.expected
This file was deleted.
Oops, something went wrong.
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
File renamed without changes.
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,18 @@ | ||
| from lxml import etree | ||
| from io import StringIO | ||
|
|
||
| from django.urls import path | ||
| from django.http import HttpResponse | ||
| from django.template import Template, Context, Engine, engines | ||
|
|
||
|
|
||
| def a(request): | ||
| value = request.GET['xpath'] | ||
| f = StringIO('<foo><bar></bar></foo>') | ||
| tree = etree.parse(f) | ||
| r = tree.xpath("/tag[@id='%s']" % value) | ||
|
|
||
|
|
||
| urlpatterns = [ | ||
| path('a', a) | ||
| ] |
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,18 @@ | ||
| from lxml import etree | ||
| from io import StringIO | ||
|
|
||
| from django.urls import path | ||
| from django.http import HttpResponse | ||
| from django.template import Template, Context, Engine, engines | ||
|
|
||
|
|
||
| def a(request): | ||
| value = request.GET['xpath'] | ||
| f = StringIO('<foo><bar></bar></foo>') | ||
| tree = etree.parse(f) | ||
| r = tree.xpath("/tag[@id=$tagid]", tagid=value) | ||
|
|
||
|
|
||
| urlpatterns = [ | ||
| path('a', a) | ||
| ] |
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,10 @@ | ||
| | xpath.py:8:20:8:29 | lxml.etree.parse.xpath | externally controlled string | | ||
| | xpath.py:13:29:13:38 | lxml.etree.Xpath | externally controlled string | | ||
| | xpath.py:19:29:19:38 | lxml.etree.Xpath | externally controlled string | | ||
| | xpath.py:25:38:25:46 | lxml.etree.ETXpath | externally controlled string | | ||
| | xpathBad.py:13:20:13:43 | lxml.etree.parse.xpath | externally controlled string | | ||
| | xpathFlow.py:13:20:13:29 | lxml.etree.parse.xpath | externally controlled string | | ||
| | xpathFlow.py:21:29:21:38 | lxml.etree.Xpath | externally controlled string | | ||
| | xpathFlow.py:29:29:29:38 | lxml.etree.Xpath | externally controlled string | | ||
| | xpathFlow.py:37:31:37:40 | lxml.etree.ETXpath | externally controlled string | | ||
| | xpathGood.py:13:20:13:37 | lxml.etree.parse.xpath | externally controlled string | |
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
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.