Spaces:
Running
Running
add public no-auth gate preview endpoint (/api/rt/preview), read-only, rate-limited, CORS
Browse files
server.py
CHANGED
|
@@ -62,6 +62,45 @@ class H(SimpleHTTPRequestHandler):
|
|
| 62 |
self.send_response(code); self.send_header("Content-Type", "application/json")
|
| 63 |
self.send_header("Content-Length", str(len(b))); self.send_header("Cache-Control", "no-store"); self.end_headers(); self.wfile.write(b)
|
| 64 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
def _proxy(self, name, body=None, base=None, query=""):
|
| 66 |
if base and base == BACKEND2 and name == "health":
|
| 67 |
return self._health2(h=None)
|
|
@@ -159,6 +198,8 @@ class H(SimpleHTTPRequestHandler):
|
|
| 159 |
|
| 160 |
def do_POST(self):
|
| 161 |
p = self.path.split("?")[0]
|
|
|
|
|
|
|
| 162 |
if p == "/api/rt/submit":
|
| 163 |
u = RC.user_from(self.headers)
|
| 164 |
if not u:
|
|
@@ -199,6 +240,8 @@ class H(SimpleHTTPRequestHandler):
|
|
| 199 |
|
| 200 |
def end_headers(self):
|
| 201 |
self.send_header("X-Content-Type-Options", "nosniff")
|
|
|
|
|
|
|
| 202 |
if getattr(self, "_static", False): # pages and scripts: always check for a newer copy
|
| 203 |
self.send_header("Cache-Control", "no-cache")
|
| 204 |
super().end_headers()
|
|
|
|
| 62 |
self.send_response(code); self.send_header("Content-Type", "application/json")
|
| 63 |
self.send_header("Content-Length", str(len(b))); self.send_header("Cache-Control", "no-store"); self.end_headers(); self.wfile.write(b)
|
| 64 |
|
| 65 |
+
def _cors_json(self, code, obj):
|
| 66 |
+
self._cors = True
|
| 67 |
+
self._json(code, obj)
|
| 68 |
+
|
| 69 |
+
def do_OPTIONS(self):
|
| 70 |
+
self.send_response(204)
|
| 71 |
+
self.send_header("Access-Control-Allow-Origin", "*")
|
| 72 |
+
self.send_header("Access-Control-Allow-Methods", "POST, OPTIONS")
|
| 73 |
+
self.send_header("Access-Control-Allow-Headers", "Content-Type")
|
| 74 |
+
self.send_header("Content-Length", "0"); self.end_headers()
|
| 75 |
+
|
| 76 |
+
def _preview(self):
|
| 77 |
+
"""Public, no-auth, read-only gate preview: classify one tool call with the second-line policy.
|
| 78 |
+
No audit record, no backend, no state change. Shell and money calls are judged by the kernel, not here."""
|
| 79 |
+
ip = (self.headers.get("X-Forwarded-For") or self.client_address[0]).split(",")[0].strip()
|
| 80 |
+
if limited("preview:" + ip):
|
| 81 |
+
return self._cors_json(429, {"error": "rate limit, try again shortly"})
|
| 82 |
+
n = int(self.headers.get("Content-Length", 0) or 0)
|
| 83 |
+
if n > 4096:
|
| 84 |
+
return self._cors_json(413, {"error": "too large"})
|
| 85 |
+
try:
|
| 86 |
+
req = json.loads(self.rfile.read(n) or b"{}")
|
| 87 |
+
except ValueError:
|
| 88 |
+
return self._cors_json(400, {"error": "bad json"})
|
| 89 |
+
fn = str(req.get("fn", ""))[:64]
|
| 90 |
+
args = req.get("args", {})
|
| 91 |
+
if not isinstance(args, dict):
|
| 92 |
+
return self._cors_json(200, {"decision": "DENY", "reason": "arguments sent in an unexpected shape"})
|
| 93 |
+
if fn in ("run_shell", "transfer_funds"):
|
| 94 |
+
return self._cors_json(200, {"decision": "KERNEL", "reason": "shell and money calls are judged by the kernel; sign in on the hackathon to test these live"})
|
| 95 |
+
try:
|
| 96 |
+
import pre_policy_r8 as PP
|
| 97 |
+
reason = PP.check(fn, args)
|
| 98 |
+
except Exception: # noqa: BLE001
|
| 99 |
+
return self._cors_json(200, {"decision": "ERROR", "reason": "could not evaluate this call"})
|
| 100 |
+
if reason:
|
| 101 |
+
return self._cors_json(200, {"decision": "DENY", "reason": reason})
|
| 102 |
+
return self._cors_json(200, {"decision": "ALLOW", "reason": "no policy violation found"})
|
| 103 |
+
|
| 104 |
def _proxy(self, name, body=None, base=None, query=""):
|
| 105 |
if base and base == BACKEND2 and name == "health":
|
| 106 |
return self._health2(h=None)
|
|
|
|
| 198 |
|
| 199 |
def do_POST(self):
|
| 200 |
p = self.path.split("?")[0]
|
| 201 |
+
if p == "/api/rt/preview":
|
| 202 |
+
return self._preview()
|
| 203 |
if p == "/api/rt/submit":
|
| 204 |
u = RC.user_from(self.headers)
|
| 205 |
if not u:
|
|
|
|
| 240 |
|
| 241 |
def end_headers(self):
|
| 242 |
self.send_header("X-Content-Type-Options", "nosniff")
|
| 243 |
+
if getattr(self, "_cors", False):
|
| 244 |
+
self.send_header("Access-Control-Allow-Origin", "*")
|
| 245 |
if getattr(self, "_static", False): # pages and scripts: always check for a newer copy
|
| 246 |
self.send_header("Cache-Control", "no-cache")
|
| 247 |
super().end_headers()
|