Visitar URL original
Python: Port reflected XSS query by RasmusWL · Pull Request #4541 · github/codeql · GitHub
The Wayback Machine - https://web.archive.org/web/20260226164919/https://github.com/github/codeql/pull/4541
Skip to content

Python: Port reflected XSS query#4541

Merged
tausbn merged 37 commits intogithub:mainfrom
RasmusWL:python-port-reflected-xss
Oct 30, 2020
Merged

Python: Port reflected XSS query#4541
tausbn merged 37 commits intogithub:mainfrom
RasmusWL:python-port-reflected-xss

Conversation

@RasmusWL
Copy link
Member

Currently a draft PR as it builds on top of #4534


The commit history for this PR is not perfect, since there are some commits that could have been squashed with previous commits. I didn't do it, since I think it would have taken too long compared to what is gained from it.

To make things simple for the XSS query, I adopted the same approach we currently use, and treat the creation of a HTTP response a sink -- and don't try to take into account whether the HTTP response is actually send back to the client.

ConceptTests

Since I added quite a bit of tags for any HTTP response, all the other tests had to get quite a bit of annotations, that wasn't really too useful. The interesting bits of how we model HTTP responses happen in the newly added response_test.py file. So I came up with a neat little trick in 16cdfd6 to allow us to specify we only care about some subset of tags for certain files. I think it worked out pretty well 😊

Modeling flask responses

In a route handler in flask, you are allowed to do return a string, and it will be wrapped into a response, like so:

@app.route("/hello")
def hello(): 
    return "<h1>hello</h1>"

However, I was a bit sad that for the following snippet, I had to mark both places as a creation of a HTTP response -- in reality it is only happening with make_response(). More details in commit messages, but basically I gave up in the end, and just model both things as HTTP response creation (and I admit that I spend a little too much time pondering this problem).

resp = make_response(...)
return resp

We might need to rework this a bit when we also start to handle redirects. I
could see a world where we simply allow http redirects to be subclasses of http
responses, and need to manually exclude them from queries (or create
HttpContentResponse to model the HttpResponses that will contain a body). Let us
see where the wind will take us.

I looked through JS and Go libraries, but I didn't feel their modeling would map
very well to Python.
Which is runnable, if you have flask installed locally
The fact that we need to add routeSetup and routeHandler annotations is sort of
annoying :|
This seems really nice to me, but you might disagree
I'm not even trying to model it properly right now, and don't have a specific
use-case for it RIGHT NOW. I think we could want this in the future, but I think
it's probably better to model it when we know what we want to use it for.
I kept `Response::instance()` predicate even though we don't need it for
anything right now, I thought it could be nice to keep for the future.
I think I'll rework how we model content-type, since what we _actually_ want to
know is the mimetype
Since that's really what we're after (at least for now)
When dealing with

```
resp = make_response(...)
return resp
```

ideally we don't want to mark the return as a creation of a HTTP response. I'll
deal with this in a second commit, to show off how annoying it looks in the
tests right now :D
In these cases the `return` might end up creating a new HTTP response, so they
need to be modeled as such.

Initially I created a very naive solution that didn't handle either
tricky_return1 or tricky_return2.

The interaction in tricky_return2/helper highlighted for me that to handle this
properly, due to the fact that the flow is across functions, we either need to
use a global dataflow/taint-tracking configuration, or some clever use of
type-trackers.

In the end, this extra effort for not modeling all returns in a flask route
handler as a creation of a HTTP response doesn't really seem to be worth it (at
least not right now). Sicne we use it with taint-tracking for the Reflected XSS
query, and use a HTTP response _creation_ as the sink (without propagating taint
to the HTTP response), we won't get into trouble where we report a path to BOTH
`make_response(...)` and the `return`

```
resp = make_response(...)
return resp
```

If we change this setup in the future, we will probably need to do something to
avoid this double-path reporting.
Also did autoformatting, but the important part is the change to the .expected file
@RasmusWL RasmusWL force-pushed the python-port-reflected-xss branch from ad0d151 to d295c64 Compare October 23, 2020 12:32
@RasmusWL RasmusWL marked this pull request as ready for review October 23, 2020 12:32
@RasmusWL RasmusWL requested a review from a team as a code owner October 23, 2020 12:32
class HttpServerHttpResponseTest extends InlineExpectationsTest {
File file;

HttpServerHttpResponseTest() { this = "HttpServerHttpResponseTest: " + file }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we restrict file further here? Say to Python-files within the test directory?

*
* Use `Response::instance()` predicate to get references to instances of `flask.Response`.
*/
abstract class InstanceSource extends HTTP::Server::HttpResponse::Range, DataFlow::Node { }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess the concrete InstanceSources are yet to be written.

@yoff yoff dismissed a stale review via 96e79a2 October 29, 2020 14:06
@RasmusWL RasmusWL requested a review from felicitymay as a code owner October 29, 2020 14:06
Copy link
Contributor

@tausbn tausbn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think overall this looks good and close to mergeable. I think we can reuse the subclass type tracking pattern a few more places, to extend the modelling.

result = http_attr("HttpResponse")
or
// subclass
result.asExpr().(ClassExpr).getABase() = classRef(t.continue()).asExpr()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like we should just include this in all of the models of classes that extend HttpResponse. Would this not do what we want?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think you are right, and it is not a lot of code to duplicate...

// TODO: remove/expand this part of the template as needed
// Handle `http.HttpResponseRedirect` alias
t.start() and
result = http_attr("HttpResponseRedirect")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... so, here, for instance, to capture subclasses of HttpResponseRedirect.

@yoff yoff requested a review from tausbn October 30, 2020 16:39
tausbn
tausbn previously approved these changes Oct 30, 2020
Copy link
Contributor

@tausbn tausbn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome!

@tausbn tausbn merged commit ecc52a1 into github:main Oct 30, 2020
@RasmusWL RasmusWL deleted the python-port-reflected-xss branch November 2, 2020 09:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants