pierrejeambrun commented on code in PR #47577:
URL: https://github.com/apache/airflow/pull/47577#discussion_r1988966065
##########
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:
I tried locally, I think I can make it work both in UI test and locally but
I will have to still get this kind of `if` to handle cases where the
`parsed_base.path` is empty, etc..
The question is whether we want to use `posixpath.normpath` or
`pathlib.Path.is_realtive_to()`
--
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]