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

Potential fix for code scanning alert no. 5: Incomplete URL substring sanitization #154

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

kiranscaria
Copy link
Collaborator

Potential fix for https://github.com/raga-ai-hub/RagaAI-Catalyst/security/code-scanning/5

To fix the problem, we need to ensure that the URL is properly parsed and validated. Instead of checking for the substring "blob.core.windows.net" within the URL, we should parse the URL and check the hostname specifically. This will prevent malicious URLs from bypassing the check by embedding the allowed host in an unexpected location.

The best way to fix this is to use the urlparse function from the urllib.parse module to parse the URL and then check the hostname. We will update the _put_presigned_url method to implement this change.

Suggested fixes powered by Copilot Autofix. Review carefully before merging.

… sanitization

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
if "blob.core.windows.net" in presignedUrl: # Azure
from urllib.parse import urlparse
parsed_url = urlparse(presignedUrl)
if parsed_url.hostname and parsed_url.hostname.endswith("blob.core.windows.net"): # Azure

Check failure

Code scanning / CodeQL

Incomplete URL substring sanitization High

The string
blob.core.windows.net
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 hostname check is robust and cannot be bypassed by embedding the allowed host in an unexpected location. We will use urlparse to parse the URL and then check the hostname to ensure it matches the allowed host correctly.

  • Parse the URL using urlparse.
  • Check if the hostname ends with the allowed host and ensure it is a valid subdomain by checking the preceding character.
  • Update the _put_presigned_url method to implement this check.
Suggested changeset 1
ragaai_catalyst/tracers/upload_traces.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/ragaai_catalyst/tracers/upload_traces.py b/ragaai_catalyst/tracers/upload_traces.py
--- a/ragaai_catalyst/tracers/upload_traces.py
+++ b/ragaai_catalyst/tracers/upload_traces.py
@@ -92,3 +92,4 @@
         parsed_url = urlparse(presignedUrl)
-        if parsed_url.hostname and parsed_url.hostname.endswith("blob.core.windows.net"):  # Azure
+        allowed_host = "blob.core.windows.net"
+        if parsed_url.hostname and (parsed_url.hostname == allowed_host or parsed_url.hostname.endswith("." + allowed_host)):  # Azure
             headers["x-ms-blob-type"] = "BlockBlob"
EOF
@@ -92,3 +92,4 @@
parsed_url = urlparse(presignedUrl)
if parsed_url.hostname and parsed_url.hostname.endswith("blob.core.windows.net"): # Azure
allowed_host = "blob.core.windows.net"
if parsed_url.hostname and (parsed_url.hostname == allowed_host or parsed_url.hostname.endswith("." + allowed_host)): # Azure
headers["x-ms-blob-type"] = "BlockBlob"
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