-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Python: Add models for socketio #20914
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
Open
joefarebrother
wants to merge
8
commits into
github:main
Choose a base branch
from
joefarebrother:python-socketio
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+229
−0
Open
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ba06990
Add socketio models
joefarebrother a83c70f
Add tests
joefarebrother b0be818
Add taint test
joefarebrother 83eadba
Add namespace models
joefarebrother eb7fe71
Fix namespace instances and update tests
joefarebrother 6207137
Add changenote
joefarebrother 8d313ff
qldoc fixes
joefarebrother 16018e9
Minor test fix
joefarebrother 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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| /** | ||
| * Provides definitions and modelling for the `python-socketio` PyPI package. | ||
| * See https://python-socketio.readthedocs.io/en/stable/. | ||
| */ | ||
|
|
||
| private import python | ||
| private import semmle.python.dataflow.new.DataFlow | ||
|
||
| private import semmle.python.dataflow.new.TaintTracking | ||
| private import semmle.python.dataflow.new.RemoteFlowSources | ||
| private import semmle.python.Concepts | ||
| private import semmle.python.ApiGraphs | ||
| private import semmle.python.frameworks.internal.PoorMansFunctionResolution | ||
|
|
||
| /** | ||
| * Provides models for the `python-socketio` PyPI package. | ||
| * See https://python-socketio.readthedocs.io/en/stable/. | ||
| */ | ||
| module SocketIO { | ||
| /** Provides models for socketio `Server` and `AsyncServer` classes. */ | ||
| module Server { | ||
| /** An instance of a socketio `Server` or `AsyncServer`. */ | ||
|
||
| API::Node server() { | ||
| result = API::moduleImport("socketio").getMember(["Server", "AsyncServer"]).getAnInstance() | ||
| } | ||
|
|
||
| /** A decorator that indicates a socketio event handler. */ | ||
| private API::Node serverEventAnnotation() { | ||
| result = server().getMember("event") | ||
| or | ||
| result = server().getMember("on").getReturn() | ||
| } | ||
|
|
||
| private class EventHandler extends Http::Server::RequestHandler::Range { | ||
| EventHandler() { | ||
| serverEventAnnotation().getAValueReachableFromSource().asExpr() = this.getADecorator() | ||
| or | ||
| exists(DataFlow::CallCfgNode c, DataFlow::Node arg | | ||
| c = server().getMember("on").getACall() | ||
| | | ||
| ( | ||
| arg = c.getArg(1) | ||
| or | ||
| arg = c.getArgByName("handler") | ||
| ) and | ||
| poorMansFunctionTracker(this) = arg | ||
| ) | ||
| } | ||
|
|
||
| override Parameter getARoutedParameter() { | ||
| result = this.getAnArg() and | ||
| not result = this.getArg(0) // First parameter is `sid`, which is not a remote flow source as it cannot be controlled by the client. | ||
| } | ||
|
|
||
| override string getFramework() { result = "socketio" } | ||
| } | ||
|
|
||
| private class CallbackArgument extends DataFlow::Node { | ||
| CallbackArgument() { | ||
| exists(DataFlow::CallCfgNode c | | ||
| c = [server(), Namespace::instance()].getMember(["emit", "send"]).getACall() | ||
| | | ||
| this = c.getArgByName("callback") | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| private class CallbackHandler extends Http::Server::RequestHandler::Range { | ||
| CallbackHandler() { any(CallbackArgument ca) = poorMansFunctionTracker(this) } | ||
|
|
||
| override Parameter getARoutedParameter() { result = this.getAnArg() } | ||
|
|
||
| override string getFramework() { result = "socketio" } | ||
| } | ||
|
|
||
| private class SocketIOCall extends RemoteFlowSource::Range { | ||
| SocketIOCall() { this = [server(), Namespace::instance()].getMember("call").getACall() } | ||
|
|
||
| override string getSourceType() { result = "socketio call" } | ||
| } | ||
| } | ||
|
|
||
| /** Provides modelling for socketio server Namespace/AsyncNamespace classes. */ | ||
|
||
| module Namespace { | ||
| /** Gets a reference to the `socketio.Namespace` or `socketio.AsyncNamespace` classes or any subclass. */ | ||
| API::Node subclassRef() { | ||
| result = | ||
| API::moduleImport("socketio").getMember(["Namespace", "AsyncNamespace"]).getASubclass*() | ||
| } | ||
|
|
||
| /** Gets a reference to an instance of a subclass of `socketio.Namespace` or `socketio.AsyncNamespace`. */ | ||
| API::Node instance() { result = subclassRef().getAnInstance() } | ||
|
|
||
| /** A socketio Namespace class. */ | ||
| class NamespaceClass extends Class { | ||
| NamespaceClass() { this.getABase() = subclassRef().asSource().asExpr() } | ||
|
|
||
| /** Gets a handler for socketio events. */ | ||
| Function getAnEventHandler() { | ||
| result = this.getAMethod() and | ||
| result.getName().matches("on_%") | ||
| } | ||
| } | ||
|
|
||
| private class NamespaceEventHandler extends Http::Server::RequestHandler::Range { | ||
| NamespaceEventHandler() { this = any(NamespaceClass nc).getAnEventHandler() } | ||
|
|
||
| override Parameter getARoutedParameter() { | ||
| result = this.getAnArg() and | ||
| not result = this.getArg(0) and | ||
| not result = this.getArg(1) // First 2 parameters are `self` and `sid`. | ||
| } | ||
|
|
||
| override string getFramework() { result = "socketio" } | ||
| } | ||
| } | ||
| } | ||
Empty file.
2 changes: 2 additions & 0 deletions
2
python/ql/test/library-tests/frameworks/socketio/ConceptsTest.ql
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,2 @@ | ||
| import python | ||
| import experimental.meta.ConceptsTest |
3 changes: 3 additions & 0 deletions
3
python/ql/test/library-tests/frameworks/socketio/InlineTaintTest.expected
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,3 @@ | ||
| argumentToEnsureNotTaintedNotMarkedAsSpurious | ||
| untaintedArgumentToEnsureTaintedNotMarkedAsMissing | ||
| testFailures |
2 changes: 2 additions & 0 deletions
2
python/ql/test/library-tests/frameworks/socketio/InlineTaintTest.ql
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,2 @@ | ||
| import experimental.meta.InlineTaintTest | ||
| import MakeInlineTaintTest<TestTaintTrackingConfig> |
59 changes: 59 additions & 0 deletions
59
python/ql/test/library-tests/frameworks/socketio/taint_test.py
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,59 @@ | ||
| import sys | ||
| import socketio | ||
| import sys | ||
|
|
||
joefarebrother marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| def ensure_tainted(*args): | ||
| print("tainted", args) | ||
|
|
||
| def ensure_not_tainted(*args): | ||
| print("not tainted", args) | ||
|
|
||
| sio = socketio.Server() | ||
|
|
||
| @sio.event | ||
| def connect(sid, environ, auth): # $ requestHandler routedParameter=environ routedParameter=auth | ||
| ensure_not_tainted(sid) | ||
| ensure_tainted(environ, # $ tainted | ||
| auth) # $ tainted | ||
|
|
||
| @sio.event | ||
| def event1(sid, data): # $ requestHandler routedParameter=data | ||
| ensure_not_tainted(sid) | ||
| ensure_tainted(data) # $ tainted | ||
| res = sio.call("e1", sid=sid) | ||
| ensure_tainted(res) # $ tainted | ||
| sio.emit("e2", "hi", to=sid, callback=lambda x: ensure_tainted(x)) # $ tainted | ||
| sio.send("hi", to=sid, callback=lambda x: ensure_tainted(x)) # $ tainted | ||
|
|
||
| class MyNamespace(socketio.Namespace): | ||
| def on_event2(self, sid, data): # $ requestHandler routedParameter=data | ||
| ensure_not_tainted(self, sid) | ||
| ensure_tainted(data) | ||
| res = self.call("e1", sid=sid) | ||
| ensure_tainted(res) # $ tainted | ||
| self.emit("e2", "hi", to=sid, callback=lambda x: ensure_tainted(x)) # $ tainted | ||
| self.send("hi", to=sid, callback=lambda x: ensure_tainted(x)) # $ tainted | ||
|
|
||
| sio.register_namespace(MyNamespace("/ns")) | ||
|
|
||
| asio = socketio.AsyncServer(async_mode='asgi') | ||
|
|
||
| @asio.event | ||
| async def event3(sid, data): # $ requestHandler routedParameter=sid routedParameter=data | ||
| ensure_not_tainted(sid) | ||
| ensure_tainted(data) # $ tainted | ||
| res = await asio.call("e1", sid=sid) | ||
| ensure_tainted(res) # $ tainted | ||
| await asio.emit("e2", "hi", to=sid, callback=lambda x: ensure_tainted(x)) # $ tainted | ||
| await asio.send("hi", to=sid, callback=lambda x: ensure_tainted(x)) # $ tainted | ||
|
|
||
| if __name__ == "__main__": | ||
|
|
||
| if "--async" in sys.argv: | ||
| import uvicorn | ||
| app = socketio.ASGIApp(asio) | ||
| uvicorn.run(app, host='127.0.0.1', port=8000) | ||
| else: | ||
| import eventlet | ||
| app = socketio.WSGIApp(sio) | ||
| eventlet.wsgi.server(eventlet.listen(('', 8000)), app) | ||
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,29 @@ | ||
| import socketio | ||
|
|
||
| sio = socketio.Server() | ||
|
|
||
| @sio.on("connect") | ||
| def connect(sid, environ, auth): # $ requestHandler routedParameter=environ routedParameter=auth | ||
| print("connect", sid, environ, auth) | ||
|
|
||
| @sio.on("event1") | ||
| def handle(sid, data): # $ requestHandler routedParameter=data | ||
| print("e1", sid, data) | ||
|
|
||
| @sio.event | ||
| def event2(sid, data): # $ requestHandler routedParameter=data | ||
| print("e2", sid, data) | ||
|
|
||
| def event3(sid, data): # $ requestHandler routedParameter=data | ||
| print("e3", sid, data) | ||
|
|
||
| sio.on("event3", handler=event3) | ||
|
|
||
| sio.on("event4", lambda sid,data: print("e4", sid, data)) # $ requestHandler routedParameter=data | ||
|
|
||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| app = socketio.WSGIApp(sio) | ||
| import eventlet | ||
| eventlet.wsgi.server(eventlet.listen(('', 8000)), app) |
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.