forked from itzmetanjim/coolton
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub_proxy.py
More file actions
411 lines (337 loc) · 14.4 KB
/
Copy pathgithub_proxy.py
File metadata and controls
411 lines (337 loc) · 14.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
#!/usr/bin/env python3
"""coolton GitHub proxy.
A tiny standalone HTTP forward proxy that lets the sandbox talk to GitHub as the
coolton-agent user WITHOUT the real PAT ever leaving this host.
How it works (no CA, no MITM, no CONNECT):
* It listens on localhost:29054. Caddy terminates TLS for ghproxy.tanjim.org and
reverse-proxies plain HTTP to it, so the sandbox connects directly to
https://ghproxy.tanjim.org (over normal HTTPS) - no proxy env, no CONNECT.
* The sandbox authenticates with a short-lived *sandbox token* (never the real PAT).
gh sends `Authorization: Bearer <sandbox_token>`; git sends
`Authorization: Basic <anything>:<sandbox_token>`.
* If the presented token is in the allowlist, the proxy rewrites it to the real
PAT (`Authorization: Basic <PAT>`) and forwards the request to github.com,
mapping ghproxy.tanjim.org hostnames back to github.com.
* Unknown tokens get 403.
Run:
python3 github_proxy.py
Importable:
from github_proxy import add_token, remove_token, start_proxy, stop_proxy
"""
import base64
import json
import logging
import os
import re
import threading
from http.server import BaseHTTPRequestHandler, HTTPServer
from socketserver import ThreadingMixIn
from urllib.parse import urlparse, urlunparse
import requests
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s")
logger = logging.getLogger("github_proxy")
LISTEN_HOST = "127.0.0.1"
LISTEN_PORT = 29054
# Admin endpoint (localhost only) for the agent to issue/revoke per-sandbox tokens.
ADMIN_PORT = 29055
PROXY_HOST_SUFFIX = "ghproxy.tanjim.org"
GITHUB_TOKEN = os.environ.get("COOLTON_GH_TOKEN", "")
# Token used to authenticate admin calls (set via GH_PROXY_ADMIN_TOKEN; defaults to the
# GitHub token so a single secret protects both). Never exposed to sandboxes.
ADMIN_TOKEN = os.environ.get("GH_PROXY_ADMIN_TOKEN", "") or GITHUB_TOKEN
def _real_auth(upstream: str) -> str:
# git smart-HTTP endpoints (github.com, uploads.github.com) require HTTP Basic auth
# with the PAT as the password; the REST/GraphQL API accepts "token <PAT>" (what gh
# sends). Use Basic for git, token form for the API.
if "api.github.com" in upstream or "uploads.github.com" in upstream:
return "token " + GITHUB_TOKEN
return "Basic " + base64.b64encode(f"{GITHUB_TOKEN}:".encode()).decode()
def translate_ghe_to_github(url: str, ghe_hostname: str = PROXY_HOST_SUFFIX) -> str:
"""Translate a GitHub Enterprise (ghproxy.tanjim.org) URL to its github.com equivalent.
gh treats a custom GH_HOST as GitHub Enterprise and emits GHE-style URLs:
REST under /api/v3, GraphQL at /api/graphql, and git/UI on the bare host. This maps
all of those (plus raw/gist/pages/upload variants) back to the public github.com
endpoints. See the shared reference implementation for the full matrix.
"""
if url.startswith(f"git@{ghe_hostname}:"):
return url.replace(f"git@{ghe_hostname}:", "git@github.com:")
parsed = urlparse(url)
if parsed.scheme == "ssh" and parsed.netloc == f"git@{ghe_hostname}":
return url.replace(f"git@{ghe_hostname}", "git@github.com")
netloc = parsed.netloc.lower()
path = parsed.path
host_only = netloc.split(":")[0]
# Case A: GitHub Pages subdomain (pages.host -> [owner].github.io)
if host_only == f"pages.{ghe_hostname}":
parts = [p for p in path.split("/") if p]
if parts:
owner = parts[0]
new_netloc = f"{owner}.github.io"
new_path = "/" + "/".join(parts[1:])
else:
new_netloc, new_path = "github.io", path
# Case B: GitHub Pages subpath (host/pages/owner -> [owner].github.io)
elif host_only == ghe_hostname and path.startswith("/pages/"):
parts = [p for p in path.split("/") if p][1:]
if parts:
owner = parts[0]
new_netloc = f"{owner}.github.io"
new_path = "/" + "/".join(parts[1:])
else:
new_netloc, new_path = "github.io", "/"
# Case C: Raw subdomain (raw.host -> raw.githubusercontent.com)
elif host_only == f"raw.{ghe_hostname}":
new_netloc, new_path = "raw.githubusercontent.com", path
# Case D: Gist subdomain (gist.host -> gist.github.com)
elif host_only == f"gist.{ghe_hostname}":
new_netloc, new_path = "gist.github.com", path
# Case E: Standard host endpoints
elif host_only == ghe_hostname:
if path.startswith("/api/v3/uploads"):
new_netloc = "uploads.github.com"
new_path = path.replace("/api/v3/uploads", "", 1)
elif path.startswith("/api/v3/"):
new_netloc = "api.github.com"
new_path = path.replace("/api/v3", "", 1)
elif path == "/api/v3":
new_netloc, new_path = "api.github.com", "/"
elif path.startswith("/api/graphql"):
new_netloc = "api.github.com"
new_path = path.replace("/api/graphql", "/graphql", 1)
elif re.match(r"^/[^/]+/[^/]+/raw/", path):
new_netloc = "raw.githubusercontent.com"
new_path = re.sub(r"^/([^/]+)/([^/]+)/raw/(.+)$", r"/\1/\2/\3", path)
else:
new_netloc, new_path = "github.com", path
else:
return url
if new_path.startswith("//"):
new_path = "/" + new_path.lstrip("/")
return urlunparse(parsed._replace(netloc=new_netloc, path=new_path))
def _rewrite_url(host: str, path: str) -> str:
"""Build the full upstream github.com URL for an incoming proxy request."""
return translate_ghe_to_github(f"https://{host}{path}")
class _Allowlist:
def __init__(self):
self._set = set()
self._lock = threading.Lock()
def add(self, tok: str):
if tok:
with self._lock:
self._set.add(tok)
def remove(self, tok: str):
with self._lock:
self._set.discard(tok)
def __contains__(self, tok: str) -> bool:
with self._lock:
return tok in self._set
allowlist = _Allowlist()
# A long-lived test token may be supplied via GH_PROXY_TOKEN (handy for manual testing);
# the agent issues short-lived per-sandbox tokens at runtime via add_token()/remove_token()
# (or the admin HTTP endpoint). If GH_PROXY_TOKEN is set it is always authorized.
_test_token = os.environ.get("GH_PROXY_TOKEN", "")
if _test_token:
allowlist.add(_test_token)
# Importable helpers (these are what the agent will call later).
def add_token(tok: str):
allowlist.add(tok)
def remove_token(tok: str):
allowlist.remove(tok)
class _AdminHandler(BaseHTTPRequestHandler):
"""localhost-only admin API for issuing/revoking per-sandbox tokens.
POST /tokens {token: "..."} -> authorize a sandbox token
DELETE /tokens {token: "..."} -> revoke a sandbox token
GET /health -> {"ok": true}
Authenticated with `Authorization: Bearer <GH_PROXY_ADMIN_TOKEN>`.
"""
protocol_version = "HTTP/1.1"
def _admin_ok(self) -> bool:
h = self.headers.get("Authorization", "")
if h.startswith("Bearer "):
return h[7:].strip() == ADMIN_TOKEN
return False
def _send_json(self, code, payload):
body = json.dumps(payload).encode()
self.send_response(code)
self.send_header("Content-Type", "application/json")
self.send_header("Content-Length", str(len(body)))
self.send_header("Connection", "close")
self.end_headers()
self.wfile.write(body)
self.close_connection = True
def _read_json(self) -> dict:
length = int(self.headers.get("Content-Length", 0) or 0)
if not length:
return {}
try:
return json.loads(self.rfile.read(length) or b"{}")
except Exception:
return {}
def do_GET(self):
if self.path == "/health" and self._admin_ok():
self._send_json(200, {"ok": True})
else:
self._send_json(403, {"error": "forbidden"})
def do_POST(self):
if self.path != "/tokens" or not self._admin_ok():
self._send_json(403, {"error": "forbidden"})
return
tok = (self._read_json() or {}).get("token", "")
if not tok:
self._send_json(400, {"error": "missing token"})
return
allowlist.add(tok)
self._send_json(200, {"ok": True})
def do_DELETE(self):
if self.path != "/tokens" or not self._admin_ok():
self._send_json(403, {"error": "forbidden"})
return
tok = (self._read_json() or {}).get("token", "")
if tok:
allowlist.remove(tok)
self._send_json(200, {"ok": True})
def log_message(self, *a): pass
def issue_token(tok: str):
"""Authorize a sandbox token (callable from the agent process directly OR via admin HTTP)."""
allowlist.add(tok)
def revoke_token(tok: str):
allowlist.remove(tok)
def _extract_token(headers: dict) -> str | None:
"""Return the sandbox token from the request, or None if not authorized.
Handles the ways clients send it:
* gh (github.com): Authorization: Bearer <tok>
* gh (custom host): Authorization: token <tok> (GH_ENTERPRISE_TOKEN)
* git (basic auth): Authorization: Basic <user>:<tok>
"""
auth = headers.get("Proxy-Authorization") or headers.get("Authorization", "")
if auth.startswith("Bearer "):
return auth[len("Bearer "):].strip() or None
if auth.startswith("token ") or auth.startswith("Token "):
return auth.split(" ", 1)[1].strip() or None
if auth.startswith("Basic "):
try:
decoded = base64.b64decode(auth[6:]).decode(errors="ignore")
except Exception:
return None
# git form is "user:token"; take the password part after the last colon
return decoded.split(":", 1)[-1].strip() or None
return None
class _Handler(BaseHTTPRequestHandler):
protocol_version = "HTTP/1.1"
def _deny(self, code=403, challenge=False):
self.send_response(code)
self.send_header("Content-Type", "text/plain")
if challenge:
# git sends an anonymous probe first; respond 401 (not 403) with a Basic
# challenge so git retries using its credential helper.
self.send_header("WWW-Authenticate", 'Basic realm="GitHub"')
self.send_header("Connection", "close")
self.end_headers()
self.wfile.write(b"forbidden")
self.close_connection = True
def _forward(self):
tok = _extract_token(self.headers)
if tok is None:
# No credential presented (e.g. git's first anonymous probe). Challenge so
# the client retries with auth instead of failing outright.
self._deny(401, challenge=True)
return
if tok not in allowlist:
logger.warning("deny: tok=%r in_allowlist=False auth=%r", tok, self.headers.get("Authorization", "")[:30])
self._deny(403)
return
target = self.path
if target.startswith("http://") or target.startswith("https://"):
parsed = urlparse(target)
host = parsed.netloc
else:
host = self.headers.get("Host", "")
parsed = None
# gh treats GH_HOST as GitHub Enterprise, so its URLs are GHE-formatted
# (/api/v3, /api/graphql, bare host for git/UI). Translate back to github.com.
upstream = _rewrite_url(host.split(":")[0], self.path)
body = None
if self.command in ("POST", "PUT", "PATCH"):
length = int(self.headers.get("Content-Length", 0) or 0)
body = self.rfile.read(length) if length else None
fwd = {
"Authorization": _real_auth(upstream),
"User-Agent": self.headers.get("User-Agent", "coolton-sandbox"),
"Accept": self.headers.get("Accept", "*/*"),
}
if "Content-Type" in self.headers:
fwd["Content-Type"] = self.headers["Content-Type"]
try:
req = requests.request(
self.command, upstream, data=body, headers=fwd,
stream=True, timeout=120, allow_redirects=False,
)
except Exception as e:
logger.warning("upstream error: %s", e)
self.send_response(502)
self.send_header("Connection", "close")
self.end_headers()
self.close_connection = True
return
self.send_response(req.status_code)
self.send_header("Connection", "close")
for k, v in req.headers.items():
if k.lower() in ("transfer-encoding", "connection", "content-encoding"):
continue
self.send_header(k, v)
self.end_headers()
try:
for chunk in req.iter_content(65536):
if chunk:
self.wfile.write(chunk)
self.wfile.flush()
except Exception as e:
logger.warning("stream error: %s", e)
finally:
req.close()
try:
self.wfile.flush()
self.connection.shutdown(1)
except Exception:
pass
self.close_connection = True
def do_GET(self): self._forward()
def do_POST(self): self._forward()
def do_PUT(self): self._forward()
def do_PATCH(self): self._forward()
def do_DELETE(self): self._forward()
def do_HEAD(self): self._forward()
def log_message(self, *a): pass
_server = None
def start_proxy(host=LISTEN_HOST, port=LISTEN_PORT):
global _server
if _server is not None:
return _server
if not GITHUB_TOKEN:
raise RuntimeError("COOLTON_GH_TOKEN not set")
class _S(ThreadingMixIn, HTTPServer):
daemon_threads = True
_server = _S((host, port), _Handler)
t = threading.Thread(target=_server.serve_forever, daemon=True)
t.start()
logger.info("github_proxy listening on %s:%s", host, port)
class _A(ThreadingMixIn, HTTPServer):
daemon_threads = True
_admin = _A(("127.0.0.1", ADMIN_PORT), _AdminHandler)
ta = threading.Thread(target=_admin.serve_forever, daemon=True)
ta.start()
logger.info("github_proxy admin listening on 127.0.0.1:%s", ADMIN_PORT)
return _server
def stop_proxy():
global _server
if _server:
_server.shutdown()
_server = None
if __name__ == "__main__":
start_proxy()
try:
import time
while True:
time.sleep(3600)
except KeyboardInterrupt:
stop_proxy()