-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14406 from amammad/amammad-python-FileSystemAccess
Python: New FileSystem Access
- Loading branch information
Showing
33 changed files
with
355 additions
and
1 deletion.
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,42 @@ | ||
/** | ||
* Provides classes modeling security-relevant aspects of the `aiofile` PyPI package. | ||
* | ||
* See https://pypi.org/project/aiofile. | ||
*/ | ||
|
||
private import python | ||
private import semmle.python.dataflow.new.DataFlow | ||
private import semmle.python.dataflow.new.RemoteFlowSources | ||
private import semmle.python.dataflow.new.TaintTracking | ||
private import semmle.python.Concepts | ||
private import semmle.python.ApiGraphs | ||
|
||
/** | ||
* Provides models for the `aiofile` PyPI package. | ||
* | ||
* See https://pypi.org/project/aiofile. | ||
*/ | ||
private module Aiofile { | ||
/** | ||
* A call to the `async_open` function or `AIOFile` constructor from `aiofile` as a sink for Filesystem access. | ||
*/ | ||
class FileResponseCall extends FileSystemAccess::Range, API::CallNode { | ||
string methodName; | ||
|
||
FileResponseCall() { | ||
this = API::moduleImport("aiofile").getMember("async_open").getACall() and | ||
methodName = "async_open" | ||
or | ||
this = API::moduleImport("aiofile").getMember("AIOFile").getACall() and | ||
methodName = "AIOFile" | ||
} | ||
|
||
override DataFlow::Node getAPathArgument() { | ||
result = this.getParameter(0, "file_specifier").asSink() and | ||
methodName = "async_open" | ||
or | ||
result = this.getParameter(0, "filename").asSink() and | ||
methodName = "AIOFile" | ||
} | ||
} | ||
} |
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,28 @@ | ||
/** | ||
* Provides classes modeling security-relevant aspects of the `aiofiles` PyPI package. | ||
* | ||
* See https://pypi.org/project/aiofiles. | ||
*/ | ||
|
||
private import python | ||
private import semmle.python.dataflow.new.DataFlow | ||
private import semmle.python.dataflow.new.RemoteFlowSources | ||
private import semmle.python.dataflow.new.TaintTracking | ||
private import semmle.python.Concepts | ||
private import semmle.python.ApiGraphs | ||
|
||
/** | ||
* Provides models for the `aiofiles` PyPI package. | ||
* | ||
* See https://pypi.org/project/aiofiles. | ||
*/ | ||
private module Aiofiles { | ||
/** | ||
* A call to the `open` function from `aiofiles` as a sink for Filesystem access. | ||
*/ | ||
class FileResponseCall extends FileSystemAccess::Range, API::CallNode { | ||
FileResponseCall() { this = API::moduleImport("aiofiles").getMember("open").getACall() } | ||
|
||
override DataFlow::Node getAPathArgument() { result = this.getParameter(0, "file").asSink() } | ||
} | ||
} |
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,54 @@ | ||
/** | ||
* Provides classes modeling security-relevant aspects of the `anyio` PyPI package. | ||
* | ||
* See https://pypi.org/project/anyio. | ||
*/ | ||
|
||
private import python | ||
private import semmle.python.dataflow.new.DataFlow | ||
private import semmle.python.dataflow.new.RemoteFlowSources | ||
private import semmle.python.dataflow.new.TaintTracking | ||
private import semmle.python.Concepts | ||
private import semmle.python.ApiGraphs | ||
|
||
/** | ||
* Provides models for the `anyio` PyPI package. | ||
* | ||
* See https://pypi.org/project/anyio. | ||
*/ | ||
private module Anyio { | ||
/** | ||
* A call to the `from_path` function from `FileReadStream` or `FileWriteStream` constructors of `anyio.streams.file` as a sink for Filesystem access. | ||
*/ | ||
class FileStreamCall extends FileSystemAccess::Range, API::CallNode { | ||
FileStreamCall() { | ||
this = | ||
API::moduleImport("anyio") | ||
.getMember("streams") | ||
.getMember("file") | ||
.getMember(["FileReadStream", "FileWriteStream"]) | ||
.getMember("from_path") | ||
.getACall() | ||
} | ||
|
||
override DataFlow::Node getAPathArgument() { result = this.getParameter(0, "path").asSink() } | ||
} | ||
|
||
/** | ||
* A call to the `Path` constructor from `anyio` as a sink for Filesystem access. | ||
*/ | ||
class PathCall extends FileSystemAccess::Range, API::CallNode { | ||
PathCall() { this = API::moduleImport("anyio").getMember("Path").getACall() } | ||
|
||
override DataFlow::Node getAPathArgument() { result = this.getParameter(0).asSink() } | ||
} | ||
|
||
/** | ||
* A call to the `open_file` function from `anyio` as a sink for Filesystem access. | ||
*/ | ||
class OpenFileCall extends FileSystemAccess::Range, API::CallNode { | ||
OpenFileCall() { this = API::moduleImport("anyio").getMember("open_file").getACall() } | ||
|
||
override DataFlow::Node getAPathArgument() { result = this.getParameter(0, "file").asSink() } | ||
} | ||
} |
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,35 @@ | ||
/** | ||
* Provides classes modeling security-relevant aspects of the `baize` PyPI package. | ||
* | ||
* See https://pypi.org/project/baize. | ||
*/ | ||
|
||
private import python | ||
private import semmle.python.dataflow.new.DataFlow | ||
private import semmle.python.dataflow.new.TaintTracking | ||
private import semmle.python.Concepts | ||
private import semmle.python.ApiGraphs | ||
private import semmle.python.frameworks.internal.InstanceTaintStepsHelper | ||
private import semmle.python.frameworks.Stdlib | ||
|
||
/** | ||
* Provides models for `baize` PyPI package. | ||
* | ||
* See https://pypi.org/project/baize. | ||
*/ | ||
module Baize { | ||
/** | ||
* A call to the `baize.asgi.FileResponse` constructor as a sink for Filesystem access. | ||
* | ||
* it is not contained to Starlette source code but it is mentioned in documents as an alternative to Starlette FileResponse | ||
*/ | ||
class BaizeFileResponseCall extends FileSystemAccess::Range, API::CallNode { | ||
BaizeFileResponseCall() { | ||
this = API::moduleImport("baize").getMember("asgi").getMember("FileResponse").getACall() | ||
} | ||
|
||
override DataFlow::Node getAPathArgument() { | ||
result = this.getParameter(0, "filepath").asSink() | ||
} | ||
} | ||
} |
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,48 @@ | ||
/** | ||
* Provides classes modeling security-relevant aspects of the `cherrypy` PyPI package. | ||
*/ | ||
|
||
private import python | ||
private import semmle.python.dataflow.new.DataFlow | ||
private import semmle.python.dataflow.new.RemoteFlowSources | ||
private import semmle.python.dataflow.new.TaintTracking | ||
private import semmle.python.Concepts | ||
private import semmle.python.ApiGraphs | ||
|
||
/** | ||
* Provides models for the `cherrypy` PyPI package. | ||
* See https://cherrypy.dev/. | ||
*/ | ||
private module Cherrypy { | ||
/** | ||
* Holds for an instance of `cherrypy.lib.static` | ||
*/ | ||
API::Node libStatic() { | ||
result = API::moduleImport("cherrypy").getMember("lib").getMember("static") | ||
} | ||
|
||
/** | ||
* A call to the `serve_file` or `serve_download`or `staticfile` functions of `cherrypy.lib.static` as a sink for Filesystem access. | ||
*/ | ||
class FileResponseCall extends FileSystemAccess::Range, API::CallNode { | ||
string funcName; | ||
|
||
FileResponseCall() { | ||
this = libStatic().getMember("staticfile").getACall() and | ||
funcName = "staticfile" | ||
or | ||
this = libStatic().getMember("serve_file").getACall() and | ||
funcName = "serve_file" | ||
or | ||
this = libStatic().getMember("serve_download").getACall() and | ||
funcName = "serve_download" | ||
} | ||
|
||
override DataFlow::Node getAPathArgument() { | ||
result = this.getParameter(0, "path").asSink() and funcName = ["serve_download", "serve_file"] | ||
or | ||
result = this.getParameter(0, "filename").asSink() and | ||
funcName = "staticfile" | ||
} | ||
} | ||
} |
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 @@ | ||
/** | ||
* Provides classes modeling security-relevant aspects of the `sanic` PyPI package. | ||
* See https://sanic.dev/. | ||
*/ | ||
|
||
private import python | ||
private import semmle.python.dataflow.new.DataFlow | ||
private import semmle.python.dataflow.new.RemoteFlowSources | ||
private import semmle.python.dataflow.new.TaintTracking | ||
private import semmle.python.Concepts | ||
private import semmle.python.ApiGraphs | ||
|
||
/** | ||
* Provides models for the `sanic` PyPI package. | ||
* See https://sanic.dev/. | ||
*/ | ||
private module Sanic { | ||
/** | ||
* Provides models for Sanic applications (an instance of `sanic.Sanic`). | ||
*/ | ||
module App { | ||
/** Gets a reference to a Sanic application (an instance of `sanic.Sanic`). */ | ||
API::Node instance() { result = API::moduleImport("sanic").getMember("Sanic").getReturn() } | ||
} | ||
|
||
/** | ||
* A call to the `file` or `file_stream` functions of `sanic.response` as a sink for Filesystem access. | ||
*/ | ||
class FileResponseCall extends FileSystemAccess::Range, API::CallNode { | ||
FileResponseCall() { | ||
this = | ||
API::moduleImport("sanic") | ||
.getMember("response") | ||
.getMember(["file", "file_stream"]) | ||
.getACall() | ||
} | ||
|
||
override DataFlow::Node getAPathArgument() { | ||
result = this.getParameter(0, "location").asSink() | ||
} | ||
} | ||
} |
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
4 changes: 4 additions & 0 deletions
4
python/ql/src/change-notes/2023-11-06-more-filesystem-modeling.md
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,4 @@ | ||
--- | ||
category: minorAnalysis | ||
--- | ||
* Added modeling of more `FileSystemAccess` in packages `cherrypy`, `aiofile`, `aiofiles`, `anyio`, `sanic`, `starlette`, `baize`, and `io`. This will mainly affect the _Uncontrolled data used in path expression_ (`py/path-injection`) query. |
2 changes: 2 additions & 0 deletions
2
python/ql/test/library-tests/frameworks/aiofile/ConceptsTest.expected
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,2 @@ | ||
testFailures | ||
failures |
2 changes: 2 additions & 0 deletions
2
python/ql/test/library-tests/frameworks/aiofile/ConceptsTest.ql
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,2 @@ | ||
import python | ||
import experimental.meta.ConceptsTest |
4 changes: 4 additions & 0 deletions
4
python/ql/test/library-tests/frameworks/aiofile/FileSystemAccess.py
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,4 @@ | ||
from aiofile import async_open, AIOFile | ||
|
||
AIOFile("file", 'r') # $ getAPathArgument="file" | ||
async_open("file", "r") # $ getAPathArgument="file" |
2 changes: 2 additions & 0 deletions
2
python/ql/test/library-tests/frameworks/aiofiles/ConceptsTest.expected
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,2 @@ | ||
testFailures | ||
failures |
2 changes: 2 additions & 0 deletions
2
python/ql/test/library-tests/frameworks/aiofiles/ConceptsTest.ql
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,2 @@ | ||
import python | ||
import experimental.meta.ConceptsTest |
3 changes: 3 additions & 0 deletions
3
python/ql/test/library-tests/frameworks/aiofiles/FileSystemAccess.py
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,3 @@ | ||
import aiofiles | ||
|
||
aiofiles.open("file", mode='r') # $ getAPathArgument="file" |
2 changes: 2 additions & 0 deletions
2
python/ql/test/library-tests/frameworks/anyio/ConceptsTest.expected
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,2 @@ | ||
testFailures | ||
failures |
2 changes: 2 additions & 0 deletions
2
python/ql/test/library-tests/frameworks/anyio/ConceptsTest.ql
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,2 @@ | ||
import python | ||
import experimental.meta.ConceptsTest |
8 changes: 8 additions & 0 deletions
8
python/ql/test/library-tests/frameworks/anyio/FileSystemAccess.py
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,8 @@ | ||
import anyio | ||
from anyio.streams.file import FileReadStream, FileWriteStream | ||
from anyio import Path | ||
|
||
anyio.open_file("file", 'r') # $ getAPathArgument="file" | ||
FileReadStream.from_path("file") # $ getAPathArgument="file" | ||
FileWriteStream.from_path("file") # $ getAPathArgument="file" | ||
Path("file") # $ getAPathArgument="file" |
2 changes: 2 additions & 0 deletions
2
python/ql/test/library-tests/frameworks/baize/ConceptsTest.expected
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,2 @@ | ||
testFailures | ||
failures |
2 changes: 2 additions & 0 deletions
2
python/ql/test/library-tests/frameworks/baize/ConceptsTest.ql
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,2 @@ | ||
import python | ||
import experimental.meta.ConceptsTest |
3 changes: 3 additions & 0 deletions
3
python/ql/test/library-tests/frameworks/baize/FileSystemAccess.py
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,3 @@ | ||
from baize.asgi import FileResponse as baizeFileResponse | ||
|
||
baizeFileResponse("file") # $ getAPathArgument="file" |
2 changes: 2 additions & 0 deletions
2
python/ql/test/library-tests/frameworks/cherrypy/ConceptsTest.expected
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,2 @@ | ||
testFailures | ||
failures |
2 changes: 2 additions & 0 deletions
2
python/ql/test/library-tests/frameworks/cherrypy/ConceptsTest.ql
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,2 @@ | ||
import python | ||
import experimental.meta.ConceptsTest |
8 changes: 8 additions & 0 deletions
8
python/ql/test/library-tests/frameworks/cherrypy/FileSystemAccess.py
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,8 @@ | ||
import cherrypy | ||
from cherrypy.lib.static import serve_file, serve_download, staticfile | ||
|
||
serve_file("file") # $ getAPathArgument="file" | ||
serve_download("file") # $ getAPathArgument="file" | ||
staticfile("file") # $ getAPathArgument="file" | ||
# root won't make this safe | ||
staticfile("file", root="/path/to/safe/dir") # $ getAPathArgument="file" |
2 changes: 2 additions & 0 deletions
2
python/ql/test/library-tests/frameworks/sanic/ConceptsTest.expected
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,2 @@ | ||
testFailures | ||
failures |
2 changes: 2 additions & 0 deletions
2
python/ql/test/library-tests/frameworks/sanic/ConceptsTest.ql
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,2 @@ | ||
import python | ||
import experimental.meta.ConceptsTest |
Oops, something went wrong.