failed NO_PATCH NO_PATCH UNSUBMITTED empty_patch(budget) · 21 tool calls · 305 s · fastapi/fastapi
🐛 Fix hierarchical security scope propagation see defect #5623
| # | Tool | Arguments | Result |
|---|---|---|---|
| 1 | run_command | {
"command": "grep -rn \"SecurityScopes\" . | head -20"
} | {"status": "ok", "stdout": "./tests/test_dependency_security_overrides.py:4:from fastapi.security import SecurityScopes\n./tests/test_dependency_security_overrifull{"status": "ok", "stdout": "./tests/test_dependency_security_overrides.py:4:from fastapi.security import SecurityScopes\n./tests/test_dependency_security_overrides.py:10:def get_user(required_scopes: SecurityScopes):\n./tests/test_dependency_security_overrides.py:14:def get_user_override(required_scopes: SecurityScopes):\n./tests/test_dependency_paramless.py:6: SecurityScopes,\n./tests/test_dependency_paramless.py:18: security_scopes: SecurityScopes,\n./docs/em/docs/advanced/security/oauth2-scopes.md:139:## \u2699\ufe0f `SecurityScopes`\n./docs/em/docs/advanced/security/oauth2-scopes.md:149:\ud83d\udc65 \ud83d\udce3 \ud83c\udf81 \ud83d\udd22 \ud83c\udd8e `SecurityScopes`, \ud83d\uddc4 \u26aa\ufe0f\u27a1\ufe0f `fastapi.security`.\n./docs/em/docs/advanced/security/oauth2-scopes.md:151:\ud83d\udc49 `SecurityScopes` \ud83c\udf93 \ud83c\udf8f `Request` (`Request` \u2699\ufe0f \ud83e\udd1a \ud83d\udce8 \ud83c\udf9a \ud83d\udd17).\n./docs/em/docs/advanced/security/oauth2-scopes.md:157:\ud83d\udd22 `security_scopes` \ud83d\udd1c \ud83c\udd8e `SecurityScopes`.\n./docs/em/docs/advanced/security/oauth2-scopes.md:161:`security_scopes` \ud83c\udf9a (\ud83c\udf93 `SecurityScopes`) \ud83d\ude9a `scope_str` \ud83d\udd22 \u23ee\ufe0f \ud83d\udc41 \ud83c\udfbb, \ud83d\udd0c \ud83d\udc48 \u2194 \ud83d\udc7d \ud83d\ude80 (\ud83d\udc65 \ud83d\udd1c \u2699\ufe0f \u26ab\ufe0f).\n./docs/em/docs/advanced/security/oauth2-scopes.md:212: * `security_scopes` \ud83d\udd22 \ud83c\udd8e `SecurityScopes`:\n./docs/em/docs/advanced/security/oauth2-scopes.md:226:## \ud83c\udf16 \u2139 \ud83d\udd03 `SecurityScopes`\n./docs/em/docs/advanced/security/oauth2-scopes.md:228:\ud83d\udc46 \ud83d\udcaa \u2699\ufe0f `SecurityScopes` \ud83d\ude46 \u261d, & \ud83d\udc97 \ud83e\udd49, \u26ab\ufe0f \ud83d\udeab \u2714\ufe0f \"\ud83c\udf31\" \ud83d\udd17.\n./docs/em/docs/advanced/security/oauth2-scopes.md:232:\u21a9\ufe0f `SecurityScopes` \ud83d\udd1c \u2714\ufe0f \ud83c\udf10 \u2194 \u |
| 2 | run_command | {
"command": "grep -rn \"class SecurityScopes\" ."
} | {"status": "ok", "stdout": "./fastapi/security/oauth2.py:626:class SecurityScopes:\n", "stderr": "", "exit_code": 0}full{"status": "ok", "stdout": "./fastapi/security/oauth2.py:626:class SecurityScopes:\n", "stderr": "", "exit_code": 0} |
| 3 | run_command | {
"command": "sed -n '626,650p' fastapi/security/oauth2.py"
} | {"status": "ok", "stdout": "class SecurityScopes:\n \"\"\"\n This is a special class that you can define in a parameter in a dependency to\n obtain the OAuth2 sfull{"status": "ok", "stdout": "class SecurityScopes:\n \"\"\"\n This is a special class that you can define in a parameter in a dependency to\n obtain the OAuth2 scopes required by all the dependencies in the same chain.\n\n This way, multiple dependencies can have different scopes, even when used in the\n same *path operation*. And with this, you can access all the scopes required in\n all those dependencies in a single place.\n\n Read more about it in the\n [FastAPI docs for OAuth2 scopes](https://fastapi.tiangolo.com/advanced/security/oauth2-scopes/).\n \"\"\"\n\n def __init__(\n self,\n scopes: Annotated[\n Optional[List[str]],\n Doc(\n \"\"\"\n This will be filled by FastAPI.\n \"\"\"\n ),\n ] = None,\n ):\n self.scopes: Annotated[\n", "stderr": "", "exit_code": 0} |
| 4 | run_command | {
"command": "grep -rn \"SecurityScopes\" fastapi/dependencies.py"
} | {"status": "error", "error_type": "CommandError", "error_message": "grep: fastapi/dependencies.py: No such file or directory\n", "details": {"stdout": "", "stdefull{"status": "error", "error_type": "CommandError", "error_message": "grep: fastapi/dependencies.py: No such file or directory\n", "details": {"stdout": "", "stderr": "grep: fastapi/dependencies.py: No such file or directory\n", "exit_code": 2}} |
| 5 | run_command | {
"command": "find fastapi -name \"*.py\" | xargs grep -l \"SecurityScopes\""
} | {"status": "ok", "stdout": "fastapi/security/oauth2.py\nfastapi/security/__init__.py\nfastapi/dependencies/utils.py\n", "stderr": "", "exit_code": 0}full{"status": "ok", "stdout": "fastapi/security/oauth2.py\nfastapi/security/__init__.py\nfastapi/dependencies/utils.py\n", "stderr": "", "exit_code": 0} |
| 6 | run_command | {
"command": "grep -n \"SecurityScopes\" fastapi/dependencies/utils.py"
} | {"status": "ok", "stdout": "61:from fastapi.security.oauth2 import OAuth2, SecurityScopes\n329: elif lenient_issubclass(type_annotation, SecurityScopes):\n443: full{"status": "ok", "stdout": "61:from fastapi.security.oauth2 import OAuth2, SecurityScopes\n329: elif lenient_issubclass(type_annotation, SecurityScopes):\n443: SecurityScopes,\n693: values[dependant.security_scopes_param_name] = SecurityScopes(\n", "stderr": "", "exit_code": 0} |
| 7 | run_command | {
"command": "sed -n '680,710p' fastapi/dependencies/utils.py"
} | {"status": "ok", "stdout": " if dependant.http_connection_param_name:\n values[dependant.http_connection_param_name] = request\n if dependant.request_param_namefull{"status": "ok", "stdout": " if dependant.http_connection_param_name:\n values[dependant.http_connection_param_name] = request\n if dependant.request_param_name and isinstance(request, Request):\n values[dependant.request_param_name] = request\n elif dependant.websocket_param_name and isinstance(request, WebSocket):\n values[dependant.websocket_param_name] = request\n if dependant.background_tasks_param_name:\n if background_tasks is None:\n background_tasks = BackgroundTasks()\n values[dependant.background_tasks_param_name] = background_tasks\n if dependant.response_param_name:\n values[dependant.response_param_name] = response\n if dependant.security_scopes_param_name:\n values[dependant.security_scopes_param_name] = SecurityScopes(\n scopes=dependant.security_scopes\n )\n return SolvedDependency(\n values=values,\n errors=errors,\n background_tasks=background_tasks,\n response=response,\n dependency_cache=dependency_cache,\n )\n\n\ndef _validate_value_with_model_field(\n *, field: ModelField, value: Any, values: Dict[str, Any], loc: Tuple[str, ...]\n) -> Tuple[Any, List[Any]]:\n if value is None:\n if field.required:\n return None, [get_missing_field_error(loc=loc)]\n", "stderr": "", "exit_code": 0} |
| 8 | run_command | {
"command": "grep -rn \"solve_dependencies\" fastapi/"
} | {"status": "ok", "stdout": "fastapi/dependencies/utils.py:565:async def solve_dependencies(\nfastapi/dependencies/utils.py:616: solved_result = await solve_depefull{"status": "ok", "stdout": "fastapi/dependencies/utils.py:565:async def solve_dependencies(\nfastapi/dependencies/utils.py:616: solved_result = await solve_dependencies(\nfastapi/routing.py:46: solve_dependencies,\nfastapi/routing.py:381: solved_result = await solve_dependencies(\nfastapi/routing.py:451: solved_result = await solve_dependencies(\n", "stderr": "", "exit_code": 0} |
| 9 | run_command | {
"command": "sed -n '565,695p' fastapi/dependencies/utils.py"
} | {"status": "ok", "stdout": "async def solve_dependencies(\n *,\n request: Union[Request, WebSocket],\n dependant: Dependant,\n body: Optional[Union[Dict[str, Anfull{"status": "ok", "stdout": "async def solve_dependencies(\n *,\n request: Union[Request, WebSocket],\n dependant: Dependant,\n body: Optional[Union[Dict[str, Any], FormData]] = None,\n background_tasks: Optional[StarletteBackgroundTasks] = None,\n response: Optional[Response] = None,\n dependency_overrides_provider: Optional[Any] = None,\n dependency_cache: Optional[Dict[DependencyCacheKey, Any]] = None,\n # TODO: remove this parameter later, no longer used, not removing it yet as some\n # people might be monkey patching this function (although that's not supported)\n async_exit_stack: AsyncExitStack,\n embed_body_fields: bool,\n) -> SolvedDependency:\n request_astack = request.scope.get(\"fastapi_inner_astack\")\n assert isinstance(request_astack, AsyncExitStack), (\n \"fastapi_inner_astack not found in request scope\"\n )\n function_astack = request.scope.get(\"fastapi_function_astack\")\n assert isinstance(function_astack, AsyncExitStack), (\n \"fastapi_function_astack not found in request scope\"\n )\n values: Dict[str, Any] = {}\n errors: List[Any] = []\n if response is None:\n response = Response()\n del response.headers[\"content-length\"]\n response.status_code = None # type: ignore\n if dependency_cache is None:\n dependency_cache = {}\n for sub_dependant in dependant.dependencies:\n sub_dependant.call = cast(Callable[..., Any], sub_dependant.call)\n call = sub_dependant.call\n use_sub_dependant = sub_dependant\n if (\n dependency_overrides_provider\n and dependency_overrides_provider.dependency_overrides\n ):\n original_call = sub_dependant.call\n call = getattr(\n dependency_overrides_provider, \"dependency_overrides\", {}\n ).get(original_call, original_call)\n use_path: str = sub_dependant.path # type: ignore\n use_su |
| 10 | run_command | {
"command": "cat > /tmp/repro.py <<'PY'\nfrom fastapi import FastAPI, Depends, Security\nfrom fastapi.security import OAuth2PasswordBearer, SecurityScopes\nfrom fastapi.testclient import TestClient\nfrom typing import List\n\napp = FastAPI()\noauth2_scheme = OAuth2PasswordBearer(tokenUrl=\"token\")\n\ndef get_scopes(security_scopes: SecurityScopes):\n return security_scopes.scopes\n\ndef get_user(scopes: List[str] = Depends(get_scopes)):\n return scopes\n\n@app.get(\"/test\")\ndef test_route(user: List[str] = Security(get_user, scopes=[\"read\", \"write\"])):\n return {\"user_scopes\": user}\n\nclient = TestClient(app)\n\ndef test_scopes():\n # We need to mock the token to avoid 401\n # But we are testing what get_scopes receives.\n # Since we are using Security(get_user, scopes=[\"read\", \"write\"]),\n # get_user depends on get_scopes.\n # get_scopes should receive SecurityScopes(scopes=[\"read\", \"write\"]).\n # Then get_user should receive [\"read\", \"write\"].\n \n # To avoid 401, we can override get_user or just provide a token.\n # Actually, let's override the oauth2_scheme to just return a token.\n app.dependency_overrides[oauth2_scheme] = lambda: \"fake-token\"\n \n response = client.get(\"/test\")\n print(f\"Response: {response.json()}\")\n assert response.status_code == 200\n assert response.json()[\"user_scopes\"] == [\"read\", \"write\"]\n\nif __name__ == \"__main__\":\n test_scopes()\nPY\npython3 /tmp/repro.py\n"
} | {"status": "ok", "stdout": "Response: {'user_scopes': ['read', 'write']}\n", "stderr": "", "exit_code": 0}full{"status": "ok", "stdout": "Response: {'user_scopes': ['read', 'write']}\n", "stderr": "", "exit_code": 0} |
| 11 | run_command | {
"command": "cat > /tmp/repro.py <<'PY'\nfrom fastapi import FastAPI, Depends, Security\nfrom fastapi.security import OAuth2PasswordBearer, SecurityScopes\nfrom fastapi.testclient import TestClient\nfrom typing import List\n\napp = FastAPI()\noauth2_scheme = OAuth2PasswordBearer(tokenUrl=\"token\")\n\ndef get_scopes(security_scopes: SecurityScopes):\n return security_scopes.scopes\n\ndef get_user(scopes: List[str] = Depends(get_scopes)):\n return scopes\n\ndef get_admin(user_scopes: List[str] = Depends(get_user)):\n if \"admin\" not in user_scopes:\n return \"not admin\"\n return \"admin\"\n\n@app.get(\"/test\")\ndef test_route(admin: str = Security(get_admin, scopes=[\"admin\"])):\n return {\"admin\": admin}\n\nclient = TestClient(app)\n\ndef test_scopes():\n app.dependency_overrides[oauth2_scheme] = lambda: \"fake-token\"\n \n response = client.get(\"/test\")\n print(f\"Response: {response.json()}\")\n # If propagation works, get_scopes should receive [\"admin\"], \n # then get_user receives [\"admin\"], then get_admin receives [\"admin\"]\n # and returns \"admin\".\n assert response.status_code == 200\n assert response.json()[\"admin\"] == \"admin\"\n\nif __name__ == \"__main__\":\n test_scopes()\nPY\npython3 /tmp/repro.py\n"
} | {"status": "ok", "stdout": "Response: {'admin': 'admin'}\n", "stderr": "", "exit_code": 0}full{"status": "ok", "stdout": "Response: {'admin': 'admin'}\n", "stderr": "", "exit_code": 0} |
| 12 | run_command | {
"command": "cat > /tmp/repro.py <<'PY'\nfrom fastapi import FastAPI, Depends, Security\nfrom fastapi.security import OAuth2PasswordBearer, SecurityScopes\nfrom fastapi.testclient import TestClient\nfrom typing import List\n\napp = FastAPI()\noauth2_scheme = OAuth2PasswordBearer(tokenUrl=\"token\")\n\ndef get_scopes(security_scopes: SecurityScopes):\n return security_scopes.scopes\n\ndef get_user(scopes: List[str] = Depends(get_scopes)):\n return scopes\n\ndef get_admin(user_scopes: List[str] = Depends(get_user)):\n return user_scopes\n\n@app.get(\"/test\")\ndef test_route(\n user_scopes_1: List[str] = Security(get_user, scopes=[\"read\"]),\n user_scopes_2: List[str] = Security(get_admin, scopes=[\"admin\"]),\n):\n return {\"user_scopes_1\": user_scopes_1, \"user_scopes_2\": user_scopes_2}\n\nclient = TestClient(app)\n\ndef test_scopes():\n app.dependency_overrides[oauth2_scheme] = lambda: \"fake-token\"\n \n response = client.get(\"/test\")\n print(f\"Response: {response.json()}\")\n # get_user should receive [\"read\"]\n # get_admin should receive [\"admin\"]\n assert response.status_code == 200\n assert response.json()[\"user_scopes_1\"] == [\"read\"]\n assert response.json()[\"user_scopes_2\"] == [\"admin\"]\n\nif __name__ == \"__main__\":\n test_scopes()\nPY\npython3 /tmp/repro.py\n"
} | {"status": "ok", "stdout": "Response: {'user_scopes_1': ['read'], 'user_scopes_2': ['admin']}\n", "stderr": "", "exit_code": 0}full{"status": "ok", "stdout": "Response: {'user_scopes_1': ['read'], 'user_scopes_2': ['admin']}\n", "stderr": "", "exit_code": 0} |
| 13 | run_command | {
"command": "cat > /tmp/repro.py <<'PY'\nfrom fastapi import FastAPI, Depends, Security\nfrom fastapi.security import OAuth2PasswordBearer, SecurityScopes\nfrom fastapi.testclient import TestClient\nfrom typing import List\n\napp = FastAPI()\noauth2_scheme = OAuth2PasswordBearer(tokenUrl=\"token\")\n\ndef get_scopes(security_scopes: SecurityScopes):\n return security_scopes.scopes\n\ndef get_user(scopes: List[str] = Depends(get_scopes)):\n return scopes\n\ndef get_admin(user: List[str] = Depends(get_user)):\n return user\n\n@app.get(\"/test\")\ndef test_route(admin: List[str] = Security(get_admin, scopes=[\"admin\"])):\n return {\"admin\": admin}\n\nclient = TestClient(app)\n\ndef test_scopes():\n app.dependency_overrides[oauth2_scheme] = lambda: \"fake-token\"\n \n response = client.get(\"/test\")\n print(f\"Response: {response.json()}\")\n assert response.status_code == 200\n assert response.json()[\"admin\"] == [\"admin\"]\n\nif __name__ == \"__main__\":\n test_scopes()\nPY\npython3 /tmp/repro.py\n"
} | {"status": "ok", "stdout": "Response: {'admin': ['admin']}\n", "stderr": "", "exit_code": 0}full{"status": "ok", "stdout": "Response: {'admin': ['admin']}\n", "stderr": "", "exit_code": 0} |
| 14 | run_command | {
"command": "cat > /tmp/repro.py <<'PY'\nfrom fastapi import FastAPI, Depends, Security\nfrom fastapi.security import OAuth2PasswordBearer, SecurityScopes\nfrom fastapi.testclient import TestClient\nfrom typing import List\n\napp = FastAPI()\noauth2_scheme = OAuth2PasswordBearer(tokenUrl=\"token\")\n\ndef get_scopes(security_scopes: SecurityScopes):\n return security_scopes.scopes\n\ndef get_user(scopes: List[str] = Depends(get_scopes)):\n return scopes\n\ndef get_admin(user: List[str] = Depends(get_user)):\n return user\n\n@app.get(\"/test\")\ndef test_route(\n user: List[str] = Security(get_user, scopes=[\"read\"]),\n admin: List[str] = Security(get_admin, scopes=[\"admin\"]),\n):\n return {\"user\": user, \"admin\": admin}\n\nclient = TestClient(app)\n\ndef test_scopes():\n app.dependency_overrides[oauth2_scheme] = lambda: \"fake-token\"\n \n response = client.get(\"/test\")\n print(f\"Response: {response.json()}\")\n # get_user is called twice. Once for 'user' param (scopes=[\"read\"])\n # and once as a dependency of 'admin' param (scopes=[\"admin\"]).\n # But FastAPI caches dependencies.\n # The first call to get_user (for 'user' param) will be cached.\n # Then when get_admin is called, it depends on get_user, which is already cached.\n # So get_admin will receive the cached result of get_user (which was called with [\"read\"]).\n # This is the bug.\n assert response.status_code == 200\n assert response.json()[\"user\"] == [\"read\"]\n assert response.json()[\"admin\"] == [\"admin\"]\n\nif __name__ == \"__main__\":\n test_scopes()\nPY\npython3 /tmp/repro.py\n"
} | {"status": "ok", "stdout": "Response: {'user': ['read'], 'admin': ['admin']}\n", "stderr": "", "exit_code": 0}full{"status": "ok", "stdout": "Response: {'user': ['read'], 'admin': ['admin']}\n", "stderr": "", "exit_code": 0} |
| 15 | run_command | {
"command": "cat > /tmp/repro.py <<'PY'\nfrom fastapi import FastAPI, Depends, Security\nfrom fastapi.security import OAuth2PasswordBearer, SecurityScopes\nfrom fastapi.testclient import TestClient\nfrom typing import List\n\napp = FastAPI()\noauth2_scheme = OAuth2PasswordBearer(tokenUrl=\"token\")\n\ndef get_scopes(security_scopes: SecurityScopes):\n return security_scopes.scopes\n\ndef get_user(scopes: List[str] = Depends(get_scopes)):\n return scopes\n\ndef get_admin(user: List[str] = Depends(get_user)):\n return user\n\n@app.get(\"/test\")\ndef test_route(\n admin: List[str] = Security(get_admin, scopes=[\"admin\"]),\n user: List[str] = Security(get_user, scopes=[\"read\"]),\n):\n return {\"user\": user, \"admin\": admin}\n\nclient = TestClient(app)\n\ndef test_scopes():\n app.dependency_overrides[oauth2_scheme] = lambda: \"fake-token\"\n \n response = client.get(\"/test\")\n print(f\"Response: {response.json()}\")\n # admin is called first with scopes=[\"admin\"]. get_user is called as its dependency.\n # get_user is cached with result [\"admin\"].\n # then user is called with scopes=[\"read\"]. get_user is already cached.\n # so user will receive [\"admin\"] instead of [\"read\"].\n assert response.status_code == 200\n assert response.json()[\"user\"] == [\"read\"]\n assert response.json()[\"admin\"] == [\"admin\"]\n\nif __name__ == \"__main__\":\n test_scopes()\nPY\npython3 /tmp/repro.py\n"
} | {"status": "ok", "stdout": "Response: {'user': ['read'], 'admin': ['admin']}\n", "stderr": "", "exit_code": 0}full{"status": "ok", "stdout": "Response: {'user': ['read'], 'admin': ['admin']}\n", "stderr": "", "exit_code": 0} |
| 16 | run_command | {
"command": "cat > /tmp/repro.py <<'PY'\nfrom fastapi import FastAPI, Depends, Security\nfrom fastapi.security import OAuth2PasswordBearer, SecurityScopes\nfrom fastapi.testclient import TestClient\nfrom typing import List\n\napp = FastAPI()\noauth2_scheme = OAuth2PasswordBearer(tokenUrl=\"token\")\n\ndef get_scopes(security_scopes: SecurityScopes):\n return security_scopes.scopes\n\ndef get_user(scopes: List[str] = Depends(get_scopes)):\n return scopes\n\ndef get_admin(user: List[str] = Depends(get_user)):\n return user\n\n@app.get(\"/test\")\ndef test_route(\n admin: List[str] = Security(get_admin, scopes=[\"admin\"]),\n user: List[str] = Security(get_user, scopes=[\"read\"]),\n):\n return {\"user\": user, \"admin\": admin}\n\nclient = TestClient(app)\n\ndef test_scopes():\n app.dependency_overrides[oauth2_scheme] = lambda: \"fake-token\"\n \n response = client.get(\"/test\")\n print(f\"Response: {response.json()}\")\n assert response.status_code == 200\n assert response.json()[\"user\"] == [\"read\"]\n assert response.json()[\"admin\"] == [\"admin\"]\n\nif __name__ == \"__main__\":\n test_scopes()\nPY\npython3 /tmp/repro.py\n"
} | {"status": "ok", "stdout": "Response: {'user': ['read'], 'admin': ['admin']}\n", "stderr": "", "exit_code": 0}full{"status": "ok", "stdout": "Response: {'user': ['read'], 'admin': ['admin']}\n", "stderr": "", "exit_code": 0} |
| 17 | run_command | {
"command": "cat > /tmp/repro.py <<'PY'\nfrom fastapi import FastAPI, Depends, Security\nfrom fastapi.security import OAuth2PasswordBearer, SecurityScopes\nfrom fastapi.testclient import TestClient\nfrom typing import List\n\napp = FastAPI()\noauth2_scheme = OAuth2PasswordBearer(tokenUrl=\"token\")\n\ndef get_scopes(security_scopes: SecurityScopes):\n return security_scopes.scopes\n\ndef get_user(scopes: List[str] = Depends(get_scopes)):\n return scopes\n\ndef get_admin(user: List[str] = Depends(get_user)):\n return user\n\n@app.get(\"/test\")\ndef test_route(\n admin: List[str] = Security(get_admin, scopes=[\"admin\"]),\n user: List[str] = Security(get_user, scopes=[\"read\"]),\n):\n return {\"user\": user, \"admin\": admin}\n\nclient = TestClient(app)\n\ndef test_scopes():\n app.dependency_overrides[oauth2_scheme] = lambda: \"fake-token\"\n \n response = client.get(\"/test\")\n print(f\"Response: {response.json()}\")\n # If caching is based on the function and not the scopes, \n # then get_user will be cached the first time it's called.\n # In this case, get_admin is called first, which calls get_user with scopes=[\"admin\"].\n # Then get_user is called directly with scopes=[\"read\"], but it should use the cache.\n assert response.status_code == 200\n assert response.json()[\"user\"] == [\"read\"]\n assert response.json()[\"admin\"] == [\"admin\"]\n\nif __name__ == \"__main__\":\n test_scopes()\nPY\npython3 /tmp/repro.py\n"
} | {"status": "ok", "stdout": "Response: {'user': ['read'], 'admin': ['admin']}\n", "stderr": "", "exit_code": 0}full{"status": "ok", "stdout": "Response: {'user': ['read'], 'admin': ['admin']}\n", "stderr": "", "exit_code": 0} |
| 18 | run_command | {
"command": "cat > /tmp/repro.py <<'PY'\nfrom fastapi import FastAPI, Depends, Security\nfrom fastapi.security import OAuth2PasswordBearer, SecurityScopes\nfrom fastapi.testclient import TestClient\nfrom typing import List\n\napp = FastAPI()\noauth2_scheme = OAuth2PasswordBearer(tokenUrl=\"token\")\n\ndef get_scopes(security_scopes: SecurityScopes):\n return security_scopes.scopes\n\ndef get_user(scopes: List[str] = Depends(get_scopes)):\n return scopes\n\ndef get_admin(user: List[str] = Depends(get_user)):\n return user\n\n@app.get(\"/test\")\ndef test_route(\n admin: List[str] = Security(get_admin, scopes=[\"admin\"]),\n user: List[str] = Security(get_user, scopes=[\"read\"]),\n):\n return {\"user\": user, \"admin\": admin}\n\nclient = TestClient(app)\n\ndef test_scopes():\n app.dependency_overrides[oauth2_scheme] = lambda: \"fake-token\"\n \n response = client.get(\"/test\")\n print(f\"Response: {response.json()}\")\n # We want to see if get_user is called with different scopes.\n # Let's use a global to track calls.\n global_scopes = []\n def get_scopes_tracked(security_scopes: SecurityScopes):\n global_scopes.append(security_scopes.scopes)\n return security_scopes.scopes\n \n app.dependency_overrides[get_scopes] = get_scopes_tracked\n \n response = client.get(\"/test\")\n print(f\"Global scopes: {global_scopes}\")\n assert response.status_code == 200\n assert global_scopes == [[\"admin\"], [\"read\"]]\n\nif __name__ == \"__main__\":\n test_scopes()\nPY\npython3 /tmp/repro.py\n"
} | {"status": "ok", "stdout": "Response: {'user': ['read'], 'admin': ['admin']}\nGlobal scopes: [['admin'], ['read']]\n", "stderr": "", "exit_code": 0}full{"status": "ok", "stdout": "Response: {'user': ['read'], 'admin': ['admin']}\nGlobal scopes: [['admin'], ['read']]\n", "stderr": "", "exit_code": 0} |
| 19 | run_command | {
"command": "cat > /tmp/repro.py <<'PY'\nfrom fastapi import FastAPI, Depends, Security\nfrom fastapi.security import OAuth2PasswordBearer, SecurityScopes\nfrom fastapi.testclient import TestClient\nfrom typing import List\n\napp = FastAPI()\noauth2_scheme = OAuth2PasswordBearer(tokenUrl=\"token\")\n\ndef get_scopes(security_scopes: SecurityScopes):\n return security_scopes.scopes\n\ndef get_user(scopes: List[str] = Depends(get_scopes)):\n return scopes\n\ndef get_admin(user: List[str] = Depends(get_user)):\n return user\n\n@app.get(\"/test\")\ndef test_route(\n admin: List[str] = Security(get_admin, scopes=[\"admin\"]),\n user: List[str] = Security(get_user, scopes=[\"read\"]),\n):\n return {\"user\": user, \"admin\": admin}\n\nclient = TestClient(app)\n\ndef test_scopes():\n app.dependency_overrides[oauth2_scheme] = lambda: \"fake-token\"\n \n # We want to see if get_user is called twice.\n # If it's cached, it should be called once.\n # But it should be called once for each set of scopes.\n \n call_count = 0\n def get_user_tracked(scopes: List[str] = Depends(get_scopes)):\n nonlocal call_count\n call_count += 1\n return scopes\n \n app.dependency_overrides[get_user] = get_user_tracked\n \n response = client.get(\"/test\")\n print(f\"Call count: {call_count}\")\n assert response.status_code == 200\n assert call_count == 2\n\nif __name__ == \"__main__\":\n test_scopes()\nPY\npython3 /tmp/repro.py\n"
} | {"status": "ok", "stdout": "Call count: 2\n", "stderr": "", "exit_code": 0}full{"status": "ok", "stdout": "Call count: 2\n", "stderr": "", "exit_code": 0} |
| 20 | run_command | {
"command": "cat > /tmp/repro.py <<'PY'\nfrom fastapi import FastAPI, Depends, Security\nfrom fastapi.security import OAuth2PasswordBearer, SecurityScopes\nfrom fastapi.testclient import TestClient\nfrom typing import List\n\napp = FastAPI()\noauth2_scheme = OAuth2PasswordBearer(tokenUrl=\"token\")\n\ndef get_scopes(security_scopes: SecurityScopes):\n return security_scopes.scopes\n\ndef get_user(scopes: List[str] = Depends(get_scopes)):\n return scopes\n\ndef get_admin(user: List[str] = Depends(get_user)):\n return user\n\n@app.get(\"/test\")\ndef test_route(\n admin: List[str] = Security(get_admin, scopes=[\"admin\"]),\n user: List[str] = Security(get_user, scopes=[\"read\"]),\n):\n return {\"user\": user, \"admin\": admin}\n\nclient = TestClient(app)\n\ndef test_scopes():\n app.dependency_overrides[oauth2_scheme] = lambda: \"fake-token\"\n \n # If caching is working, get_user should be called once if scopes are the same.\n # But here they are different.\n \n call_count = 0\n def get_scopes_tracked(security_scopes: SecurityScopes):\n nonlocal call_count\n call_count += 1\n return security_scopes.scopes\n \n app.dependency_overrides[get_scopes] = get_scopes_tracked\n \n response = client.get(\"/test\")\n print(f\"Call count: {call_count}\")\n assert response.status_code == 200\n assert call_count == 2\n\nif __name__ == \"__main__\":\n test_scopes()\nPY\npython3 /tmp/repro.py\n"
} | {"status": "ok", "stdout": "Call count: 2\n", "stderr": "", "exit_code": 0}full{"status": "ok", "stdout": "Call count: 2\n", "stderr": "", "exit_code": 0} |
| 21 | run_command | {
"command": "cat > /tmp/repro.py <<'PY'\nfrom fastapi import FastAPI, Depends, Security\nfrom fastapi.security import OAuth2PasswordBearer, SecurityScopes\nfrom fastapi.testclient import TestClient\nfrom typing import List\n\napp = FastAPI()\noauth2_scheme = OAuth2PasswordBearer(tokenUrl=\"token\")\n\ndef get_scopes(security_scopes: SecurityScopes):\n return security_scopes.scopes\n\ndef get_user(scopes: List[str] = Depends(get_scopes)):\n return scopes\n\ndef get_admin(user: List[str] = Depends(get_user)):\n return user\n\n@app.get(\"/test\")\ndef test_route(\n admin: List[str] = Security(get_admin, scopes=[\"admin\"]),\n user: List[str] = Security(get_user, scopes=[\"read\"]),\n):\n return {\"user\": user, \"admin\": admin}\n\nclient = TestClient(app)\n\ndef test_scopes():\n app.dependency_overrides[oauth2_scheme] = lambda: \"fake-token\"\n \n # If caching is working, get_user should be called once if scopes are the same.\n # Let's make them the same.\n \n call_count = 0\n def get_scopes_tracked(security_scopes: SecurityScopes):\n nonlocal call_count\n call_count += 1\n return security_scopes.scopes\n \n app.dependency_overrides[get_scopes] = get_scopes_tracked\n \n # Change scopes to be the same\n @app.get(\"/test_same\")\n def test_same(\n admin: List[str] = Security(get_admin, scopes=[\"read\"]),\n user: List[str] = Security(get_user, scopes=[\"read\"]),\n ):\n return {\"user\": user, \"admin\": admin}\n \n response = client.get(\"/test_same\")\n print(f\"Call count: {call_count}\")\n assert response.status_code == 200\n assert call_count == 1\n\nif __name__ == \"__main__\":\n test_scopes()\nPY\npython3 /tmp/repro.py\n"
} | {"status": "ok", "stdout": "Call count: 1\n", "stderr": "", "exit_code": 0}full{"status": "ok", "stdout": "Call count: 1\n", "stderr": "", "exit_code": 0} |