Require client API key on /v1/* endpoints
Browse files- Proxy was an open relay: anyone could spend the upstream Kimi quota
- Clients must send PROXY_KEY (space secret) as Bearer or x-api-key
- / and /health stay public for status checks; no upstream key material
is ever exposed in responses, headers, or error bodies
README.md
CHANGED
|
@@ -10,4 +10,4 @@ pinned: true
|
|
| 10 |
# WirelessAudit Proxy
|
| 11 |
|
| 12 |
OpenAI-compatible proxy → Kimi K3 (`https://api.kimi.com/coding`).
|
| 13 |
-
|
|
|
|
| 10 |
# WirelessAudit Proxy
|
| 11 |
|
| 12 |
OpenAI-compatible proxy → Kimi K3 (`https://api.kimi.com/coding`).
|
| 13 |
+
Space secrets: `KIMI_API_KEY` (upstream), `PROXY_KEY` (clients must send it as Bearer on `/v1/*`). Client base URL: `/v1`.
|
app.py
CHANGED
|
@@ -14,6 +14,17 @@ def get_key():
|
|
| 14 |
raise ValueError("KIMI_API_KEY not set")
|
| 15 |
return key
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
def auth_headers():
|
| 18 |
return {"Authorization": f"Bearer {get_key()}", "Content-Type": "application/json"}
|
| 19 |
|
|
@@ -42,7 +53,7 @@ async def root():
|
|
| 42 |
<p>Backend: <code>Kimi K3</code> — <code>{KIMI_BASE}</code></p>
|
| 43 |
<p>Status: <b style="color:{color}">{status}</b></p>
|
| 44 |
<p>Models: <code>{models}</code></p>
|
| 45 |
-
<p>Cline Base URL: <code>/v1</code></p>
|
| 46 |
</body></html>""")
|
| 47 |
|
| 48 |
@app.get("/health")
|
|
@@ -57,7 +68,9 @@ async def health():
|
|
| 57 |
return JSONResponse({"status": "offline", "error": info}, status_code=503)
|
| 58 |
|
| 59 |
@app.get("/v1/models")
|
| 60 |
-
async def models():
|
|
|
|
|
|
|
| 61 |
try:
|
| 62 |
async with httpx.AsyncClient(timeout=10) as c:
|
| 63 |
r = await c.get(f"{KIMI_BASE}/v1/models", headers=auth_headers())
|
|
@@ -69,6 +82,8 @@ async def models():
|
|
| 69 |
|
| 70 |
@app.post("/v1/chat/completions")
|
| 71 |
async def chat(request: Request):
|
|
|
|
|
|
|
| 72 |
try:
|
| 73 |
headers = auth_headers()
|
| 74 |
except ValueError as e:
|
|
|
|
| 14 |
raise ValueError("KIMI_API_KEY not set")
|
| 15 |
return key
|
| 16 |
|
| 17 |
+
def check_client_auth(request: Request):
|
| 18 |
+
"""Clients must present the proxy's own key. Returns a 401/503 response or None."""
|
| 19 |
+
proxy_key = os.environ.get("PROXY_KEY", "")
|
| 20 |
+
if not proxy_key:
|
| 21 |
+
return JSONResponse({"error": {"message": "PROXY_KEY not configured", "code": 503}}, status_code=503)
|
| 22 |
+
auth = request.headers.get("Authorization", "")
|
| 23 |
+
x_key = request.headers.get("x-api-key", "")
|
| 24 |
+
if auth != f"Bearer {proxy_key}" and x_key != proxy_key:
|
| 25 |
+
return JSONResponse({"error": {"message": "Invalid or missing API key", "code": 401}}, status_code=401)
|
| 26 |
+
return None
|
| 27 |
+
|
| 28 |
def auth_headers():
|
| 29 |
return {"Authorization": f"Bearer {get_key()}", "Content-Type": "application/json"}
|
| 30 |
|
|
|
|
| 53 |
<p>Backend: <code>Kimi K3</code> — <code>{KIMI_BASE}</code></p>
|
| 54 |
<p>Status: <b style="color:{color}">{status}</b></p>
|
| 55 |
<p>Models: <code>{models}</code></p>
|
| 56 |
+
<p>Cline Base URL: <code>/v1</code> (requires API key)</p>
|
| 57 |
</body></html>""")
|
| 58 |
|
| 59 |
@app.get("/health")
|
|
|
|
| 68 |
return JSONResponse({"status": "offline", "error": info}, status_code=503)
|
| 69 |
|
| 70 |
@app.get("/v1/models")
|
| 71 |
+
async def models(request: Request):
|
| 72 |
+
if (deny := check_client_auth(request)) is not None:
|
| 73 |
+
return deny
|
| 74 |
try:
|
| 75 |
async with httpx.AsyncClient(timeout=10) as c:
|
| 76 |
r = await c.get(f"{KIMI_BASE}/v1/models", headers=auth_headers())
|
|
|
|
| 82 |
|
| 83 |
@app.post("/v1/chat/completions")
|
| 84 |
async def chat(request: Request):
|
| 85 |
+
if (deny := check_client_auth(request)) is not None:
|
| 86 |
+
return deny
|
| 87 |
try:
|
| 88 |
headers = auth_headers()
|
| 89 |
except ValueError as e:
|