ashb commented on code in PR #47577:
URL: https://github.com/apache/airflow/pull/47577#discussion_r1987597067


##########
airflow/api_fastapi/core_api/security.py:
##########
@@ -197,3 +199,26 @@ def _requires_access(
 ) -> None:
     if not is_authorized_callback():
         raise HTTPException(status.HTTP_403_FORBIDDEN, "Forbidden")
+
+
+def is_safe_url(target_url: str) -> bool:
+    """
+    Check that the URL is safe.
+
+    Needs to belong to the same domain as base_url, use HTTP or HTTPS (no 
JavaScript/data schemes),
+    is a valid normalized path.
+    """
+    base_url = conf.get("api", "base_url")
+    parsed_base = urlparse(base_url)
+    parsed_target = urlparse(urljoin(base_url, target_url))  # Resolves 
relative URLs
+
+    normalized_target_path = posixpath.normpath(parsed_target.path)
+
+    if (
+        normalized_target_path
+        and parsed_base.path
+        and not normalized_target_path.startswith(parsed_base.path)
+    ):

Review Comment:
   This is slightly tricky to follow. I think it's right, but I wonder if 
`pathlib.Path.is_realtive_to()` would be easier to follow?
   
   ```python
     target_path = Path(parsed_target.path).resolve()
   
       return parsed_target.scheme in {"http", "https"} and 
parsed_target.netloc == parsed_base.netloc and 
target_path.is_relative_to(parsed_base.path)
   ```
   
   (untested)



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to