-
Notifications
You must be signed in to change notification settings - Fork 0
๐ก๏ธ Sentinel: [MEDIUM] API ํค ๊ฒ์ฆ ์ ์ฒ๋ฆฌ๋์ง ์์ ์์ธ ์์ #490
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -114,7 +114,7 @@ async def require_api_key(request: Request, call_next): | |
| if configured_keys and not (request.method == "GET" and request.url.path == "/"): | ||
| provided_key = request.headers.get("x-api-key", "") | ||
| if not any( | ||
| hmac.compare_digest(provided_key, key) for key in configured_keys | ||
| hmac.compare_digest(provided_key.encode("utf-8"), key.encode("utf-8")) for key in configured_keys | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ๐ Info: UTF-8 encoding fix resolves the TypeError
Was this helpful? React with ๐ or ๐ to provide feedback. |
||
| ): | ||
| return JSONResponse( | ||
| status_code=401, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| from fastapi import FastAPI, Request | ||
| from fastapi.testclient import TestClient | ||
| from fastapi.responses import JSONResponse | ||
| import hmac | ||
| import traceback | ||
| import uvicorn | ||
| import httpx | ||
| import asyncio | ||
|
|
||
| app = FastAPI() | ||
|
|
||
| configured_keys = ["validkey"] | ||
|
|
||
| @app.middleware("http") | ||
| async def require_api_key(request: Request, call_next): | ||
| provided_key = request.headers.get("x-api-key", "") | ||
| try: | ||
| if not any(hmac.compare_digest(provided_key, key) for key in configured_keys): | ||
| return JSONResponse(status_code=401, content={"error": "Invalid or missing API key"}) | ||
| except Exception as e: | ||
| print("Exception:", e) | ||
| traceback.print_exc() | ||
| return JSONResponse(status_code=500, content={"error": "Server error"}) | ||
| return await call_next(request) | ||
|
|
||
| @app.get("/test") | ||
| def test(): | ||
| return {"status": "ok"} | ||
|
|
||
| async def run_test(): | ||
| config = uvicorn.Config(app, port=8888, log_level="info") | ||
| server = uvicorn.Server(config) | ||
| task = asyncio.create_task(server.serve()) | ||
| await asyncio.sleep(1) # wait for server to start | ||
|
|
||
| # Use raw socket to bypass httpx ascii check | ||
| import socket | ||
| s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | ||
| s.connect(("127.0.0.1", 8888)) | ||
|
|
||
| # send raw bytes | ||
| req = b"GET /test HTTP/1.1\r\nHost: 127.0.0.1:8888\r\nx-api-key: invalid\xc3\xb1\r\n\r\n" | ||
| s.sendall(req) | ||
|
|
||
| resp = s.recv(4096) | ||
| print("Response:\n", resp.decode('latin1')) | ||
| s.close() | ||
|
|
||
| server.should_exit = True | ||
| await task | ||
|
|
||
| if __name__ == "__main__": | ||
| asyncio.run(run_test()) | ||
|
Comment on lines
+1
to
+53
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ๐ก Debug scratch files break the docstring-coverage gate Three new root-level scripts ( Prompt for agentsWas this helpful? React with ๐ or ๐ to provide feedback. |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| import hmac | ||
|
|
||
| provided_key = "invalid\xc3\xb1" # simulated latin-1 decode from ASGI | ||
| key = "validkey" | ||
|
|
||
| try: | ||
| hmac.compare_digest(provided_key, key) | ||
| except Exception as e: | ||
| print(e) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import hmac | ||
|
|
||
| provided_key = "invalid\xc3\xb1" # simulated latin-1 decode from ASGI | ||
| key = "validkey" | ||
|
|
||
| try: | ||
| hmac.compare_digest(provided_key.encode('utf-8'), key.encode('utf-8')) | ||
| print("Success") | ||
| except Exception as e: | ||
| print(e) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๐ Info: API keys still sourced from environment
get_configured_api_keysreadsCODEC_CARVER_API_KEYSfromos.environ, the anti-pattern AGENTS.md marks for migration to the credential registry. Unchanged by this PR and outside the diff, but noted since adjacent auth code is being edited.(Refers to this code)
Was this helpful? React with ๐ or ๐ to provide feedback.