-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Docs: add path query example to data flow docs #20622
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
base: main
Are you sure you want to change the base?
Changes from all commits
2930e79
1004635
3c80690
944e116
2e0915e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -411,6 +411,48 @@ Exercise 4 | |||||
GetenvToGethostbynameFlow::flow(source, sink) | ||||||
select getenv, fc | ||||||
|
||||||
Path Query Example | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We use sentence case:
Suggested change
Please update in all files |
||||||
~~~~~~~~~~~~~~~~~~ | ||||||
|
||||||
Here is the answer to exercise 4 above, converted into a path query: | ||||||
|
||||||
.. code-block:: ql | ||||||
|
||||||
/** | ||||||
* @kind path-problem | ||||||
* @problem.severity warning | ||||||
* @id getenv-to-gethostbyname | ||||||
*/ | ||||||
|
||||||
import cpp | ||||||
import semmle.code.cpp.dataflow.new.DataFlow | ||||||
|
||||||
class GetenvSource extends DataFlow::Node { | ||||||
GetenvSource() { this.asIndirectExpr(1).(FunctionCall).getTarget().hasGlobalName("getenv") } | ||||||
} | ||||||
|
||||||
module GetenvToGethostbynameConfiguration implements DataFlow::ConfigSig { | ||||||
predicate isSource(DataFlow::Node source) { source instanceof GetenvSource } | ||||||
|
||||||
predicate isSink(DataFlow::Node sink) { | ||||||
exists(FunctionCall fc | | ||||||
sink.asIndirectExpr(1) = fc.getArgument(0) and | ||||||
fc.getTarget().hasName("gethostbyname") | ||||||
) | ||||||
} | ||||||
} | ||||||
|
||||||
module GetenvToGethostbynameFlow = DataFlow::Global<GetenvToGethostbynameConfiguration>; | ||||||
|
||||||
import GetenvToGethostbynameFlow::PathGraph | ||||||
|
||||||
from GetenvToGethostbynameFlow::PathNode source, GetenvToGethostbynameFlow::PathNode sink | ||||||
where GetenvToGethostbynameFlow::flowPath(source, sink) | ||||||
select sink.getNode(), source, sink, "This file access uses data from $@.", | ||||||
source, "user-controllable input." | ||||||
|
||||||
For more information, see "`Creating path queries <https://codeql.github.com/docs/writing-codeql-queries/creating-path-queries/>`__". | ||||||
|
||||||
Further reading | ||||||
--------------- | ||||||
|
||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -354,11 +354,50 @@ This data flow configuration tracks data flow from environment variables to open | |||||
select fileOpen, "This call to 'os.open' uses data from $@.", | ||||||
environment, "call to 'os.getenv'" | ||||||
|
||||||
Path Query Example | ||||||
~~~~~~~~~~~~~~~~~~ | ||||||
|
||||||
Here is the first example above, converted into a path query: | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be good to be more explicit here, maybe:
Suggested change
Alternatively, you could give the two queries above a heading. |
||||||
|
||||||
.. code-block:: ql | ||||||
|
||||||
/** | ||||||
* @kind path-problem | ||||||
* @problem.severity warning | ||||||
* @id file-system-access-from-remote-input | ||||||
*/ | ||||||
|
||||||
import python | ||||||
import semmle.python.dataflow.new.DataFlow | ||||||
import semmle.python.dataflow.new.TaintTracking | ||||||
import semmle.python.dataflow.new.RemoteFlowSources | ||||||
import semmle.python.Concepts | ||||||
|
||||||
module RemoteToFileConfiguration implements DataFlow::ConfigSig { | ||||||
predicate isSource(DataFlow::Node source) { | ||||||
source instanceof RemoteFlowSource | ||||||
} | ||||||
|
||||||
predicate isSink(DataFlow::Node sink) { | ||||||
sink = any(FileSystemAccess fa).getAPathArgument() | ||||||
} | ||||||
} | ||||||
|
||||||
module RemoteToFileFlow = TaintTracking::Global<RemoteToFileConfiguration>; | ||||||
|
||||||
import RemoteToFileFlow::PathGraph | ||||||
|
||||||
from RemoteToFileFlow::PathNode input, RemoteToFileFlow::PathNode fileAccess | ||||||
where RemoteToFileFlow::flowPath(input, fileAccess) | ||||||
select fileAccess.getNode(), input, fileAccess, "This file access uses data from $@.", | ||||||
input, "user-controllable input." | ||||||
|
||||||
For more information, see "`Creating path queries <https://codeql.github.com/docs/writing-codeql-queries/creating-path-queries/>`__". | ||||||
|
||||||
Further reading | ||||||
--------------- | ||||||
|
||||||
- `Exploring data flow with path queries <https://docs.github.com/en/code-security/codeql-for-vs-code/getting-started-with-codeql-for-vs-code/exploring-data-flow-with-path-queries>`__ in the GitHub documentation. | ||||||
- `Creating path queries <https://codeql.github.com/docs/writing-codeql-queries/creating-path-queries/>`__ in the GitHub documentation. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We've changed docs sites from GitHub to CodeQL, so we can simplify this:
Suggested change
|
||||||
|
||||||
|
||||||
.. include:: ../reusables/python-further-reading.rst | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -372,6 +372,43 @@ The following global data-flow query finds calls to ``File.open`` where the file | |
select fileOpen, "This call to 'File.open' uses data from $@.", environment, | ||
"an environment variable" | ||
Path Query Example | ||
~~~~~~~~~~~~~~~~~~ | ||
|
||
Here is the first example above, converted into a path query: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The same comment on the Python description applies here and to the Ruby, Rust, and Swift examples. |
||
|
||
.. code-block:: ql | ||
/** | ||
* @kind path-problem | ||
* @problem.severity warning | ||
* @id file-system-access-from-remote-input | ||
*/ | ||
import codeql.ruby.DataFlow | ||
import codeql.ruby.TaintTracking | ||
import codeql.ruby.Concepts | ||
import codeql.ruby.dataflow.RemoteFlowSources | ||
module RemoteToFileConfiguration implements DataFlow::ConfigSig { | ||
predicate isSource(DataFlow::Node source) { source instanceof RemoteFlowSource } | ||
predicate isSink(DataFlow::Node sink) { | ||
sink = any(FileSystemAccess fa).getAPathArgument() | ||
} | ||
} | ||
module RemoteToFileFlow = TaintTracking::Global<RemoteToFileConfiguration>; | ||
import RemoteToFileFlow::PathGraph | ||
from RemoteToFileFlow::PathNode input, RemoteToFileFlow::PathNode fileAccess | ||
where RemoteToFileFlow::flowPath(input, fileAccess) | ||
select fileAccess.getNode(), input, fileAccess, "This file access uses data from $@.", | ||
input, "user-controllable input." | ||
For more information, see "`Creating path queries <https://codeql.github.com/docs/writing-codeql-queries/creating-path-queries/>`__". | ||
|
||
Further reading | ||
--------------- | ||
|
||
|
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.
I'd recommend adding a link to this example to line 318, after the description of Exercise 4, to make it easier for users to find this example.
I'd suggest a similar change to all articles that have exercise numbering like this - I think that's everything down to and including JavaScript/TypeScript.