This is an automated email from the ASF dual-hosted git repository.

pierrejeambrun pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/airflow.git


The following commit(s) were added to refs/heads/main by this push:
     new 7468c9ddff5 Check login redirect targets the way a browser reads them 
(#77797)
7468c9ddff5 is described below

commit 7468c9ddff5a36b3331dd03af49d699896138f8e
Author: Pierre Jeambrun <[email protected]>
AuthorDate: Tue Dec 1 15:52:09 2026 +0100

    Check login redirect targets the way a browser reads them (#77797)
    
    * Check login redirect targets the way a browser reads them
    
    The next URL was checked after unquoting and with backslashes turned into
    slashes, but callers redirect to it as given, and a browser skips every
    slash after an http or https scheme. Both forms are now checked, and a
    scheme without a host is refused.
    
    * Replace a redirect test case that also passed before the fix
    
    * Parse each redirect target once
---
 .../src/airflow/api_fastapi/core_api/security.py       | 18 +++++++++++++++---
 .../tests/unit/api_fastapi/core_api/test_security.py   |  8 ++++++++
 2 files changed, 23 insertions(+), 3 deletions(-)

diff --git a/airflow-core/src/airflow/api_fastapi/core_api/security.py 
b/airflow-core/src/airflow/api_fastapi/core_api/security.py
index f249f6e6442..da20a350c46 100644
--- a/airflow-core/src/airflow/api_fastapi/core_api/security.py
+++ b/airflow-core/src/airflow/api_fastapi/core_api/security.py
@@ -1524,9 +1524,17 @@ def is_safe_url(target_url: str, request: Request | None 
= None) -> bool:
     # this leads to an inconsistency where python returns a target url with 
/// as a valid url
     # The same thing also happens with \ where under WHATWG \ are translated 
to /, including
     # after a scheme, so "https:\\host" is an authority for a browser but a 
path for urllib.
+    # Callers redirect to the URL as given, which is sent with "\\" quoted and 
which a browser does
+    # not unquote: in "https://host%5C@other"; the authority is "other", so it 
is checked too.
+    given_url = target_url.strip()
     target_url = unquote(target_url).strip().replace("\\", "/")
-    if target_url.startswith("//"):
-        return False
+    for url in (given_url, target_url):
+        # A browser skips every slash after "http:" or "https:", so 
"http:///other"; names "other",
+        # while urllib reads no authority and urljoin resolves it onto the 
base.
+        parsed_url = urlparse(url)
+        if url.startswith("//") or (parsed_url.scheme and not 
parsed_url.netloc):
+            return False
+    given_netloc = urlparse(given_url).netloc
     for base_url, parsed_base in parsed_bases:
         parsed_target = urlparse(urljoin(base_url, target_url))  # Resolves 
relative URLs
 
@@ -1542,7 +1550,11 @@ def is_safe_url(target_url: str, request: Request | None 
= None) -> bool:
             if norm_target != norm_base and not 
norm_target.startswith(norm_base_with_slash):
                 continue
 
-        if parsed_target.scheme in {"http", "https"} and parsed_target.netloc 
== parsed_base.netloc:
+        if (
+            parsed_target.scheme in {"http", "https"}
+            and parsed_target.netloc == parsed_base.netloc
+            and given_netloc in ("", parsed_base.netloc)
+        ):
             return True
     return False
 
diff --git a/airflow-core/tests/unit/api_fastapi/core_api/test_security.py 
b/airflow-core/tests/unit/api_fastapi/core_api/test_security.py
index e4fdae5c06d..9b27f4d67cb 100644
--- a/airflow-core/tests/unit/api_fastapi/core_api/test_security.py
+++ b/airflow-core/tests/unit/api_fastapi/core_api/test_security.py
@@ -914,6 +914,14 @@ class TestFastApiSecurity:
             ("https:/\\some_netlock.com", False),
             ("https:\\/some_netlock.com", False),
             ("https%3A%5C%5Csome_netlock.com", False),
+            # slashes after the scheme, which a browser skips to reach the 
authority
+            ("https:///some_netlock.com";, False),
+            ("https:/some_netlock.com", False),
+            ("https%3A%2F%2F%2Fsome_netlock.com", False),
+            # a separator encoded in the authority, which the browser 
following the redirect does not decode
+            ("https://requesting_server_base_url.com%5C@some_netlock.com";, 
False),
+            ("https://requesting_server_base_url.com\\@some_netlock.com";, 
False),
+            ("https://requesting_server_base_url.com%2F@some_netlock.com";, 
False),
             # a single leading \ still resolves to a same-origin path
             ("\\some_page", True),
             ("/some_page", True),

Reply via email to