jason810496 commented on code in PR #55262:
URL: https://github.com/apache/airflow/pull/55262#discussion_r2354383750


##########
airflow-core/src/airflow/api_fastapi/app.py:
##########
@@ -185,6 +188,13 @@ def init_plugins(app: FastAPI) -> None:
         if url_prefix is None:
             log.error("'url_prefix' key is missing for the fastapi app: %s", 
name)
             continue
+        if url_prefix == "":
+            log.error("'url_prefix' key is empty string for the fastapi app: 
%s", name)
+            continue
+        for prefix in RESERVED_URL_PREFIXES:
+            if url_prefix.startswith(prefix):
+                log.error("Plugin %s attempted to use reserved url_prefix 
'%s'", name, url_prefix)
+                continue

Review Comment:
   ```suggestion
           if any(url_prefix.startswith(prefix) for prefix in 
RESERVED_URL_PREFIXES):
               log.error("Plugin %s attempted to use reserved url_prefix '%s'", 
name, url_prefix)
               continue
   ```



##########
airflow-core/tests/unit/api_fastapi/test_app.py:
##########
@@ -90,3 +94,20 @@ def test_catch_all_route_last(client):
     """
     test_app = client(apps="all").app
     assert test_app.routes[-1].path == "/{rest_of_path:path}"
+
+def test_plugin_with_empty_url_prefix(caplog):
+    app = FastAPI()
+    with mock.patch.object(plugins_manager, "fastapi_apps", [{"name": "test", 
"app": FastAPI(), "url_prefix": ""}]):
+        app_module.init_plugins(app)
+
+    assert any("'url_prefix' key is empty string" in rec.message for rec in 
caplog.records)
+    assert not any(r.path == "" for r in app.routes)
+
+def test_plugin_with_reserved_url_prefix(caplog):
+    app = FastAPI()
+    reserved = next(iter(app_module.RESERVED_URL_PREFIXES))
+    with mock.patch.object(plugins_manager, "fastapi_apps", [{"name": "test", 
"app": FastAPI(), "url_prefix": reserved}]):
+        app_module.init_plugins(app)
+
+    assert any("attempted to use reserved url_prefix" in rec.message for rec 
in caplog.records)
+    assert not any(r.path == reserved for r in app.routes)

Review Comment:
   It seems the tests are almost the same and we can consolidate the tests with 
`parameterizes`



##########
airflow-core/src/airflow/api_fastapi/app.py:
##########
@@ -49,6 +49,9 @@
 # Define the full path on which the potential auth manager fastapi is mounted
 AUTH_MANAGER_FASTAPI_APP_PREFIX = f"{API_ROOT_PATH}auth"
 
+# Fast API apps mounted under these prefixes are not allowed
+RESERVED_URL_PREFIXES = {"/api/v2", "/ui", "/execution"}

Review Comment:
   Since we will only iterate though the element, we can simply define as list 
instead of set.
   ```suggestion
   RESERVED_URL_PREFIXES = ["/api/v2", "/ui", "/execution"]
   ```



-- 
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