-
Notifications
You must be signed in to change notification settings - Fork 422
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
Import cornice tests #3506
base: main
Are you sure you want to change the base?
Import cornice tests #3506
Conversation
foo = Service(name="foo", path="/foo", cors_origins=("mozilla.org",)) | ||
foo.add_view("GET", _stub, cors_origins=("lolnet.org",)) | ||
|
||
self.assertTrue("mozilla.org" in foo.cors_origins_for("GET")) |
Check failure
Code scanning / CodeQL
Incomplete URL substring sanitization High test
mozilla.org
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix AI 3 days ago
To fix the problem, we need to ensure that the origin check is performed on the parsed hostname of the URL rather than using a substring match. This can be achieved by using the urlparse
function from the urllib.parse
module to extract the hostname and then checking if it matches the expected origin.
- Parse the URL to extract the hostname.
- Check if the hostname matches the expected origin.
- Update the test cases to use this new method of checking origins.
-
Copy modified lines R402-R406
@@ -401,4 +401,7 @@ | ||
|
||
self.assertTrue("mozilla.org" in foo.cors_origins_for("GET")) | ||
self.assertTrue("lolnet.org" in foo.cors_origins_for("GET")) | ||
from urllib.parse import urlparse | ||
def get_hostname(url): | ||
return urlparse(url).hostname | ||
self.assertTrue(get_hostname(foo.cors_origins_for("GET")) == "mozilla.org") | ||
self.assertTrue(get_hostname(foo.cors_origins_for("GET")) == "lolnet.org") | ||
|
foo.add_view("GET", _stub, cors_origins=("lolnet.org",)) | ||
|
||
self.assertTrue("mozilla.org" in foo.cors_origins_for("GET")) | ||
self.assertTrue("lolnet.org" in foo.cors_origins_for("GET")) |
Check failure
Code scanning / CodeQL
Incomplete URL substring sanitization High test
lolnet.org
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix AI 3 days ago
To fix the problem, we need to ensure that the origins are checked correctly by parsing the URL and verifying the hostname. We should replace the substring check with a more robust method that ensures the origin matches exactly or ends with the allowed domain.
- Parse the URL to extract the hostname.
- Check if the hostname matches the allowed origin or ends with the allowed domain.
- Update the test cases to use this new method.
-
Copy modified lines R402-R405
@@ -401,4 +401,6 @@ | ||
|
||
self.assertTrue("mozilla.org" in foo.cors_origins_for("GET")) | ||
self.assertTrue("lolnet.org" in foo.cors_origins_for("GET")) | ||
from urllib.parse import urlparse | ||
origins = foo.cors_origins_for("GET") | ||
self.assertTrue(any(urlparse(origin).hostname == "mozilla.org" for origin in origins)) | ||
self.assertTrue(any(urlparse(origin).hostname == "lolnet.org" for origin in origins)) | ||
|
self.assertTrue("lolnet.org" in foo.cors_origins_for("GET")) | ||
|
||
foo.add_view("POST", _stub) | ||
self.assertFalse("lolnet.org" in foo.cors_origins_for("POST")) |
Check failure
Code scanning / CodeQL
Incomplete URL substring sanitization High test
lolnet.org
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix AI 3 days ago
To fix the problem, we need to ensure that the URL is parsed and the hostname is checked correctly. This can be done using the urlparse
function from the urllib.parse
module. We will extract the hostname from the URL and then check if it matches the allowed origins.
- Parse the URL using
urlparse
. - Extract the hostname from the parsed URL.
- Check if the hostname matches the allowed origins.
-
Copy modified lines R402-R409 -
Copy modified line R412
@@ -401,7 +401,13 @@ | ||
|
||
self.assertTrue("mozilla.org" in foo.cors_origins_for("GET")) | ||
self.assertTrue("lolnet.org" in foo.cors_origins_for("GET")) | ||
from urllib.parse import urlparse | ||
|
||
def is_allowed_origin(url, allowed_origins): | ||
hostname = urlparse(url).hostname | ||
return hostname in allowed_origins | ||
|
||
self.assertTrue(is_allowed_origin("http://mozilla.org", foo.cors_origins_for("GET"))) | ||
self.assertTrue(is_allowed_origin("http://lolnet.org", foo.cors_origins_for("GET"))) | ||
|
||
foo.add_view("POST", _stub) | ||
self.assertFalse("lolnet.org" in foo.cors_origins_for("POST")) | ||
self.assertFalse(is_allowed_origin("http://lolnet.org", foo.cors_origins_for("POST"))) | ||
|
No description provided.