codeql icon indicating copy to clipboard operation
codeql copied to clipboard

Python: open redirect protection example is still vulnerable

Open stsewd opened this issue 1 year ago • 2 comments

The open redirect protection for this example is still vulnerable

https://github.com/github/codeql/blob/dea922958b954416edb3505026133a9f24c37e6e/python/ql/src/Security/CWE-601/examples/redirect_good2.py#L8-L12

A target like https:/example.com (notice the single /) will be parsed as having no netloc, but browsers will redirect to https://example.com (tested on Firefox and Chrome using Fedora).

from urllib.parse import urlparse

print(urlparse('https:/example.com'))
# ParseResult(scheme='https', netloc='', path='/example.com', params='', query='', fragment='')

See Django for example

https://github.com/django/django/blob/f339c4c8e4870f23d3ba8bf8ee68c57628739592/django/utils/http.py#L356-L361

stsewd avatar Mar 25 '24 14:03 stsewd

Thanks :+1: We recently changed this example, thanks for letting us know about this 💪 I'll look into rewriting our example to account for this edge-case, unless you have a suggestion for a "safe" rewrite?

(I'll also look into ensuring that our modeling recognize this edge-case)

RasmusWL avatar Mar 26 '24 15:03 RasmusWL

unless you have a suggestion for a "safe" rewrite?

Hmm, good question. I was about to suggest to check for the protocol as well like Django does, but in the past I've solved this by just forcing the target to start with one / (check for \ as well) or by adding the hostname to it.

For A:

  • //example.com -> /example.com
  • https://example.com -> /https://example.com

For B:

  • //example.com -> https://mydomain.com//example.com
  • https://example.com -> https://mydomain.com/https://example.com

stsewd avatar Mar 27 '24 20:03 stsewd

This one got away from me, but I've created https://github.com/github/codeql/pull/16646 now to address the issue :+1:

RasmusWL avatar Jun 03 '24 08:06 RasmusWL

Even though the PR is merged, it will take a little time for the documentation page to show the new text+example (since that is only updated after we make a release that contains these changes)

RasmusWL avatar Jun 04 '24 08:06 RasmusWL