vincbeck commented on code in PR #77209:
URL: https://github.com/apache/airflow/pull/77209#discussion_r4615661622
##########
providers/amazon/tests/unit/amazon/aws/auth_manager/routes/test_login.py:
##########
@@ -149,13 +160,14 @@ def get_login_callback_response(
relay_state: str,
*,
base_url: str = "http://testserver",
- pending: list[tuple[str, str, str]] | None = None,
+ pending: list[tuple] | None = None,
raw_login_state: str | None = None,
expires_in: float = 600,
in_response_to: str | None = None,
is_authenticated: bool = True,
extra_conf: dict | None = None,
return_auth_mock: bool = False,
+ cookies: dict | None = None,
Review Comment:
This parameter is not used by any test, leftover from the cookie version?
##########
providers/amazon/src/airflow/providers/amazon/aws/auth_manager/routes/login.py:
##########
@@ -81,6 +82,10 @@
# invalidate the first. The cap bounds the cookie; the oldest pending login is
dropped.
MAX_PENDING_LOGINS = 5
+# Cap the length of the redirect URL stored in the pending entry so that 5
pending entries
+# do not push the base64-encoded cookie past the ~4 KB browser limit.
+MAX_NEXT_URL_LENGTH = 500
Review Comment:
The 500-character cap doesn't keep the cookie under 4 KB. Five pending
entries with a 500-character `next` give a `_awsam_login_state` cookie of about
4480 bytes. A single entry with 500 non-Latin-1 characters is about 4260 bytes,
because `json.dumps` escapes each one to `\uXXXX` and the check counts
characters. The browser then drops the cookie, the callback finds no pending
login and returns a 401, so the user can't log in at all. Fix: check the
encoded length (`len(json.dumps(next_url))`) and lower the cap to about 400.
##########
providers/amazon/src/airflow/providers/amazon/aws/auth_manager/aws_auth_manager.py:
##########
@@ -538,7 +538,12 @@ def filter_authorized_variables(
}
def get_url_login(self, **kwargs) -> str:
- return urljoin(self.apiserver_endpoint,
f"{AUTH_MANAGER_FASTAPI_APP_PREFIX}/login")
+ """Return the login page url."""
+ login_url = urljoin(self.apiserver_endpoint,
f"{AUTH_MANAGER_FASTAPI_APP_PREFIX}/login")
+ next_url = kwargs.get("next_url")
Review Comment:
Where is `next_url` passed? Core's `/auth/login` calls `get_url_login()`
without arguments and appends `?next=` itself.
##########
providers/amazon/tests/unit/amazon/aws/auth_manager/routes/test_login.py:
##########
@@ -226,6 +241,107 @@ def test_login(self, test_client, url):
"https://portal.sso.us-east-1.amazonaws.com/saml/assertion/"
)
+ @pytest.mark.parametrize(
+ "next_url",
+ [
+ "/dags/my_dag",
+ "/dags/my_dag/runs/run_1/tasks/task_1",
+ ],
+ )
+ def test_login_stores_safe_next_url(self, test_client, next_url):
Review Comment:
nit: this test and the 3 below only differ by `next` and the expected stored
value, can you merge them into one parametrized test?
##########
providers/amazon/tests/unit/amazon/aws/auth_manager/routes/test_login.py:
##########
@@ -226,6 +241,107 @@ def test_login(self, test_client, url):
"https://portal.sso.us-east-1.amazonaws.com/saml/assertion/"
)
+ @pytest.mark.parametrize(
+ "next_url",
+ [
+ "/dags/my_dag",
+ "/dags/my_dag/runs/run_1/tasks/task_1",
+ ],
+ )
+ def test_login_stores_safe_next_url(self, test_client, next_url):
+ response = test_client.get(
+ AUTH_MANAGER_FASTAPI_APP_PREFIX + "/login",
+ params={"next": next_url},
+ follow_redirects=False,
+ )
+ assert response.status_code == 307
+ entries = parse_login_state_cookie(response)
+ assert len(entries) == 1
+ assert entries[0].get("next") == next_url
+
+ @pytest.mark.parametrize(
+ "next_url",
+ [
+ "https://attacker.example.net/dags",
+ "//attacker.example.net/dags",
+ "javascript:alert(1)",
+ ],
+ )
+ def test_login_does_not_store_unsafe_next_url(self, test_client, next_url):
+ response = test_client.get(
+ AUTH_MANAGER_FASTAPI_APP_PREFIX + "/login",
+ params={"next": next_url},
+ follow_redirects=False,
+ )
+ assert response.status_code == 307
+ entries = parse_login_state_cookie(response)
+ assert len(entries) == 1
+ assert "next" not in entries[0]
+
+ def test_login_does_not_store_overly_long_next_url(self, test_client):
+ long_url = "/dags/" + "a" * (MAX_NEXT_URL_LENGTH + 1)
+ response = test_client.get(
+ AUTH_MANAGER_FASTAPI_APP_PREFIX + "/login",
+ params={"next": long_url},
+ follow_redirects=False,
+ )
+ assert response.status_code == 307
+ entries = parse_login_state_cookie(response)
+ assert len(entries) == 1
+ assert "next" not in entries[0]
+
+ def test_login_stores_non_latin1_next_url(self, test_client):
+ """Starlette Set-Cookie is latin-1; storing next in base64 state
cookie avoids 500."""
+ non_latin1_url = "/dags?search=日本"
+ response = test_client.get(
+ AUTH_MANAGER_FASTAPI_APP_PREFIX + "/login",
+ params={"next": non_latin1_url},
+ follow_redirects=False,
+ )
+ assert response.status_code == 307
+ entries = parse_login_state_cookie(response)
+ assert len(entries) == 1
+ assert entries[0].get("next") == non_latin1_url
+
+ def test_login_token_does_not_store_next_url(self, test_client):
+ response = test_client.get(
+ AUTH_MANAGER_FASTAPI_APP_PREFIX + "/login/token",
+ params={"next": "/dags/my_dag"},
+ follow_redirects=False,
+ )
+ assert response.status_code == 307
+ entries = parse_login_state_cookie(response)
+ assert len(entries) == 1
+ assert "next" not in entries[0]
+
+ def test_login_preserves_per_tab_next_url(self):
Review Comment:
This builds both entries by hand and only calls the callback, so it doesn't
check that a second `/login` keeps the first tab's `next`. Can you call
`/login` twice on the same client instead?
--
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]