Skip to content
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

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft

Import cornice tests #3506

wants to merge 4 commits into from

Conversation

leplatrem
Copy link
Contributor

No description provided.

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

The string
mozilla.org
may be at an arbitrary position in the sanitized URL.

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.
Suggested changeset 1
tests/core/cornice/test_service.py

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/tests/core/cornice/test_service.py b/tests/core/cornice/test_service.py
--- a/tests/core/cornice/test_service.py
+++ b/tests/core/cornice/test_service.py
@@ -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")
 
EOF
@@ -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")

Copilot is powered by AI and may make mistakes. Always verify output.
Positive Feedback
Negative Feedback

Provide additional feedback

Please help us improve GitHub Copilot by sharing more details about this comment.

Please select one or more of the options
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

The string
lolnet.org
may be at an arbitrary position in the sanitized URL.

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.
Suggested changeset 1
tests/core/cornice/test_service.py

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/tests/core/cornice/test_service.py b/tests/core/cornice/test_service.py
--- a/tests/core/cornice/test_service.py
+++ b/tests/core/cornice/test_service.py
@@ -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))
 
EOF
@@ -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))

Copilot is powered by AI and may make mistakes. Always verify output.
Positive Feedback
Negative Feedback

Provide additional feedback

Please help us improve GitHub Copilot by sharing more details about this comment.

Please select one or more of the options
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

The string
lolnet.org
may be at an arbitrary position in the sanitized URL.

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.
Suggested changeset 1
tests/core/cornice/test_service.py

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/tests/core/cornice/test_service.py b/tests/core/cornice/test_service.py
--- a/tests/core/cornice/test_service.py
+++ b/tests/core/cornice/test_service.py
@@ -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")))
 
EOF
@@ -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")))

Copilot is powered by AI and may make mistakes. Always verify output.
Positive Feedback
Negative Feedback

Provide additional feedback

Please help us improve GitHub Copilot by sharing more details about this comment.

Please select one or more of the options
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant