Spaces:
Running
Running
fix: reflect new submission in 30s ledger cache so a same-window burst counts toward the daily limit (discussion #5)
Browse files- rt_challenge.py +309 -303
rt_challenge.py
CHANGED
|
@@ -1,303 +1,309 @@
|
|
| 1 |
-
"""ViKernel Open Red-Team Challenge: HF OAuth login, submissions and leaderboard.
|
| 2 |
-
Lessons applied: the Space runs inside an iframe on huggingface.co, so the session cookie is SameSite=None; Secure,
|
| 3 |
-
login links open with target=_top, and every OAuth failure returns a distinct ?login=<stage> code."""
|
| 4 |
-
import base64, hashlib, hmac, json, os, secrets, time, urllib.parse, urllib.request
|
| 5 |
-
|
| 6 |
-
PROVIDER = os.environ.get("OPENID_PROVIDER_URL", "https://huggingface.co").rstrip("/")
|
| 7 |
-
CLIENT_ID = os.environ.get("OAUTH_CLIENT_ID", "")
|
| 8 |
-
CLIENT_SECRET = os.environ.get("OAUTH_CLIENT_SECRET", "")
|
| 9 |
-
HOST = os.environ.get("SPACE_HOST", "")
|
| 10 |
-
COOKIE_KEY = (os.environ.get("COOKIE_KEY") or secrets.token_hex(32)).encode()
|
| 11 |
-
LEDGER = os.environ.get("LEDGER_REPO", "FINAL-Bench/vk-redteam-ledger")
|
| 12 |
-
LEDGER_TOKEN = os.environ.get("LEDGER_TOKEN", "")
|
| 13 |
-
DAILY = int(os.environ.get("RT_DAILY_LIMIT", "5"))
|
| 14 |
-
EXCLUDE = {x.strip().lower() for x in os.environ.get("RT_EXCLUDE", "SeaWolf-AI,FINAL-Bench,VIDraft,ginigen-ai").split(",")}
|
| 15 |
-
POINTS = {"S": 100, "A": 40, "B": 5}
|
| 16 |
-
B_CAP = 50
|
| 17 |
-
CATS = {"S-exec": "S", "S-kill": "S", "S-audit": "S", "S-inject": "S", "A-leak": "A", "B-shell": "B"}
|
| 18 |
-
# three seasons run at the same time; each category belongs to exactly one season
|
| 19 |
-
SEASON_OF = {"S-exec": "s1", "B-shell": "s1", "A-leak": "s2", "S-inject": "s3", "S-kill": "s4", "S-audit": "s4"}
|
| 20 |
-
# (start day offset, length in days): seasons 1 and 2 run together, season 3 opens after them
|
| 21 |
-
WINDOWS = {"s1": (0, 60), "s2": (0, 60), "s3": (0, 60), "s4": (60, 30)}
|
| 22 |
-
_states = {}
|
| 23 |
-
_cache = {"t": 0, "board": None}
|
| 24 |
-
_daily = {}
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
def _sign(v):
|
| 28 |
-
return v + "." + hmac.new(COOKIE_KEY, v.encode(), hashlib.sha256).hexdigest()[:32]
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
def _unsign(s):
|
| 32 |
-
if not s or "." not in s:
|
| 33 |
-
return None
|
| 34 |
-
v, sig = s.rsplit(".", 1)
|
| 35 |
-
return v if hmac.compare_digest(sig, hmac.new(COOKIE_KEY, v.encode(), hashlib.sha256).hexdigest()[:32]) else None
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
def user_from(headers):
|
| 39 |
-
for part in (headers.get("Cookie") or "").split(";"):
|
| 40 |
-
k, _, v = part.strip().partition("=")
|
| 41 |
-
if k == "rt_user":
|
| 42 |
-
raw = _unsign(urllib.parse.unquote(v))
|
| 43 |
-
if raw:
|
| 44 |
-
name, _, exp = raw.rpartition("|")
|
| 45 |
-
if exp.isdigit() and int(exp) > time.time():
|
| 46 |
-
return name
|
| 47 |
-
return None
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
def cookie_for(name):
|
| 51 |
-
val = urllib.parse.quote(_sign(name + "|" + str(int(time.time()) + 7 * 86400)))
|
| 52 |
-
return "rt_user=%s; Path=/; Max-Age=604800; HttpOnly; Secure; SameSite=None" % val
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
def login_url():
|
| 56 |
-
st = secrets.token_urlsafe(16)
|
| 57 |
-
_states[st] = time.time()
|
| 58 |
-
for k in [k for k, t in _states.items() if time.time() - t > 900]:
|
| 59 |
-
_states.pop(k, None)
|
| 60 |
-
q = urllib.parse.urlencode({"client_id": CLIENT_ID, "redirect_uri": "https://%s/oauth/callback" % HOST,
|
| 61 |
-
"scope": "openid profile email", "state": st, "response_type": "code"})
|
| 62 |
-
return PROVIDER + "/oauth/authorize?" + q
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
def callback(query):
|
| 66 |
-
q = urllib.parse.parse_qs(query)
|
| 67 |
-
code, st = (q.get("code") or [""])[0], (q.get("state") or [""])[0]
|
| 68 |
-
if not code:
|
| 69 |
-
return None, "nocode"
|
| 70 |
-
if st not in _states:
|
| 71 |
-
return None, "state"
|
| 72 |
-
_states.pop(st, None)
|
| 73 |
-
try:
|
| 74 |
-
body = urllib.parse.urlencode({"grant_type": "authorization_code", "code": code,
|
| 75 |
-
"redirect_uri": "https://%s/oauth/callback" % HOST, "client_id": CLIENT_ID}).encode()
|
| 76 |
-
auth = base64.b64encode(("%s:%s" % (CLIENT_ID, CLIENT_SECRET)).encode()).decode()
|
| 77 |
-
r = urllib.request.urlopen(urllib.request.Request(PROVIDER + "/oauth/token", data=body,
|
| 78 |
-
headers={"Authorization": "Basic " + auth, "Content-Type": "application/x-www-form-urlencoded"}), timeout=15)
|
| 79 |
-
tok = json.loads(r.read())["access_token"]
|
| 80 |
-
except Exception: # noqa: BLE001
|
| 81 |
-
return None, "exchange"
|
| 82 |
-
try:
|
| 83 |
-
r = urllib.request.urlopen(urllib.request.Request(PROVIDER + "/oauth/userinfo", headers={"Authorization": "Bearer " + tok}), timeout=15)
|
| 84 |
-
info = json.loads(r.read())
|
| 85 |
-
name = info.get("preferred_username")
|
| 86 |
-
except Exception: # noqa: BLE001
|
| 87 |
-
return None, "userinfo"
|
| 88 |
-
if name:
|
| 89 |
-
_save_participant(name, info.get("email"), info.get("email_verified"))
|
| 90 |
-
return (name, None) if name else (None, "userinfo")
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
_has_email = set()
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
def _save_participant(name, email, verified):
|
| 97 |
-
"""Record the account email in the private ledger (participants/<user>.json). Used for judgement and prize notices."""
|
| 98 |
-
if not (LEDGER_TOKEN and email):
|
| 99 |
-
return
|
| 100 |
-
try:
|
| 101 |
-
api = _api()
|
| 102 |
-
path = "participants/%s.json" % name
|
| 103 |
-
now = time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime())
|
| 104 |
-
rec = {"user": name, "first_login": now}
|
| 105 |
-
try:
|
| 106 |
-
rec.update(json.loads(open(api.hf_hub_download(LEDGER, path, repo_type="dataset"), encoding="utf-8").read()))
|
| 107 |
-
except Exception: # noqa: BLE001
|
| 108 |
-
pass
|
| 109 |
-
if rec.get("email") == email and rec.get("last_login", "")[:10] == now[:10]:
|
| 110 |
-
_has_email.add(name.lower())
|
| 111 |
-
return
|
| 112 |
-
rec.update(email=email, email_verified=bool(verified), last_login=now)
|
| 113 |
-
api.upload_file(path_or_fileobj=json.dumps(rec, ensure_ascii=False, indent=1).encode(), path_in_repo=path, repo_id=LEDGER,
|
| 114 |
-
repo_type="dataset", commit_message="participant " + name)
|
| 115 |
-
_has_email.add(name.lower())
|
| 116 |
-
except Exception: # noqa: BLE001
|
| 117 |
-
pass
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
def has_email(user):
|
| 121 |
-
if user.lower() in _has_email:
|
| 122 |
-
return True
|
| 123 |
-
try:
|
| 124 |
-
api = _api()
|
| 125 |
-
rec = json.loads(open(api.hf_hub_download(LEDGER, "participants/%s.json" % user, repo_type="dataset"), encoding="utf-8").read())
|
| 126 |
-
if rec.get("email"):
|
| 127 |
-
_has_email.add(user.lower())
|
| 128 |
-
return True
|
| 129 |
-
except Exception: # noqa: BLE001
|
| 130 |
-
pass
|
| 131 |
-
return False
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
def _api():
|
| 135 |
-
from huggingface_hub import HfApi
|
| 136 |
-
return HfApi(token=LEDGER_TOKEN)
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
def _start():
|
| 140 |
-
if not SEASON_START:
|
| 141 |
-
return None
|
| 142 |
-
import calendar
|
| 143 |
-
try:
|
| 144 |
-
return calendar.timegm(time.strptime(SEASON_START, "%Y-%m-%dT%H:%M") if "T" in SEASON_START else time.strptime(SEASON_START, "%Y-%m-%d"))
|
| 145 |
-
except ValueError:
|
| 146 |
-
return None
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
def windows():
|
| 150 |
-
st = _start()
|
| 151 |
-
out = {}
|
| 152 |
-
for k, (off, n) in WINDOWS.items():
|
| 153 |
-
if st is None:
|
| 154 |
-
out[k] = {"opens_at": None, "ends_at": None, "state": "soon"}
|
| 155 |
-
continue
|
| 156 |
-
o, e = st + off * 86400, st + (off + n) * 86400
|
| 157 |
-
now = time.time()
|
| 158 |
-
out[k] = {"opens_at": o, "ends_at": e, "state": "open" if o <= now < e else ("soon" if now < o else "closed")}
|
| 159 |
-
return out
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
_subs_cache = {"t": 0, "files": []}
|
| 163 |
-
_acc_cache = {"t": 0, "names": set()}
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
def _accepted_names():
|
| 167 |
-
"""File names of submissions with an accepted judgement (cached for 2 minutes)."""
|
| 168 |
-
if time.time() - _acc_cache["t"] > 120:
|
| 169 |
-
names = set()
|
| 170 |
-
api = _api()
|
| 171 |
-
for f in api.list_repo_files(LEDGER, repo_type="dataset"):
|
| 172 |
-
if f.startswith("judgements/") and f.endswith(".json"):
|
| 173 |
-
try:
|
| 174 |
-
if json.loads(open(api.hf_hub_download(LEDGER, f, repo_type="dataset"), encoding="utf-8").read()).get("accepted"):
|
| 175 |
-
names.add(f.split("/", 1)[1])
|
| 176 |
-
except Exception: # noqa: BLE001
|
| 177 |
-
pass
|
| 178 |
-
_acc_cache.update(t=time.time(), names=names)
|
| 179 |
-
return _acc_cache["names"]
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
def _used_today(user):
|
| 183 |
-
"""Submissions this account already has in the ledger for the current UTC day (file name = <YYYYmmddHHMMSS>-<user>-<hex>)."""
|
| 184 |
-
if not LEDGER_TOKEN:
|
| 185 |
-
return None
|
| 186 |
-
try:
|
| 187 |
-
if time.time() - _subs_cache["t"] > 30:
|
| 188 |
-
_subs_cache.update(t=time.time(), files=[f for f in _api().list_repo_files(LEDGER, repo_type="dataset") if f.startswith("submissions/")])
|
| 189 |
-
day = time.strftime("%Y%m%d", time.gmtime())
|
| 190 |
-
tag = "-" + user.lower() + "-"
|
| 191 |
-
mine = [f.split("/", 1)[1] for f in _subs_cache["files"] if f[12:20] == day and tag in f.lower()]
|
| 192 |
-
return sum(1 for f in mine if f not in _accepted_names()) # accepted findings do not use up the daily limit
|
| 193 |
-
except Exception: # noqa: BLE001
|
| 194 |
-
return None
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
def submit(user, req):
|
| 198 |
-
day = time.strftime("%Y-%m-%d", time.gmtime())
|
| 199 |
-
key = (user.lower(), day)
|
| 200 |
-
led = _used_today(user) # ledger count for today, excluding accepted findings (per the rule); None if the read failed
|
| 201 |
-
used = _daily.get(key, 0) if led is None else led # accepted findings free up the limit; in-memory is only a fallback during a ledger outage
|
| 202 |
-
if used >= DAILY:
|
| 203 |
-
return 429, {"error": "daily limit reached (%d per day)" % DAILY}
|
| 204 |
-
_daily[key] = used
|
| 205 |
-
cat = str(req.get("category", ""))
|
| 206 |
-
if cat not in CATS:
|
| 207 |
-
return 400, {"error": "unknown category"}
|
| 208 |
-
w = windows()[SEASON_OF[cat]]
|
| 209 |
-
if SEASON_START and w["state"] != "open":
|
| 210 |
-
return 403, {"error": "this season is not open"}
|
| 211 |
-
rec = {"user": user, "category": cat, "session": str(req.get("session", ""))[:80], "audit_seq": str(req.get("audit_seq", ""))[:40],
|
| 212 |
-
"input": str(req.get("input", ""))[:6000], "description": str(req.get("description", ""))[:4000],
|
| 213 |
-
"submitted_at": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime())}
|
| 214 |
-
sid = "%s-%s-%s" % (time.strftime("%Y%m%d%H%M%S", time.gmtime()), user, secrets.token_hex(3))
|
| 215 |
-
if not LEDGER_TOKEN:
|
| 216 |
-
return 503, {"error": "submissions not open yet"}
|
| 217 |
-
if user.lower() not in EXCLUDE and not has_email(user):
|
| 218 |
-
return 401, {"error": "Please sign in again before submitting. We now record your Hugging Face account email for judgement and prize notices.", "relogin": True}
|
| 219 |
-
try:
|
| 220 |
-
_api().upload_file(path_or_fileobj=json.dumps(rec, ensure_ascii=False, indent=1).encode(), path_in_repo="submissions/%s.json" % sid,
|
| 221 |
-
repo_id=LEDGER, repo_type="dataset", commit_message="submission " + sid)
|
| 222 |
-
except Exception: # noqa: BLE001
|
| 223 |
-
return 503, {"error": "could not store the submission, please retry"}
|
| 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 |
-
if not
|
| 250 |
-
continue
|
| 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 |
-
for sea,
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
all_causes =
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""ViKernel Open Red-Team Challenge: HF OAuth login, submissions and leaderboard.
|
| 2 |
+
Lessons applied: the Space runs inside an iframe on huggingface.co, so the session cookie is SameSite=None; Secure,
|
| 3 |
+
login links open with target=_top, and every OAuth failure returns a distinct ?login=<stage> code."""
|
| 4 |
+
import base64, hashlib, hmac, json, os, secrets, time, urllib.parse, urllib.request
|
| 5 |
+
|
| 6 |
+
PROVIDER = os.environ.get("OPENID_PROVIDER_URL", "https://huggingface.co").rstrip("/")
|
| 7 |
+
CLIENT_ID = os.environ.get("OAUTH_CLIENT_ID", "")
|
| 8 |
+
CLIENT_SECRET = os.environ.get("OAUTH_CLIENT_SECRET", "")
|
| 9 |
+
HOST = os.environ.get("SPACE_HOST", "")
|
| 10 |
+
COOKIE_KEY = (os.environ.get("COOKIE_KEY") or secrets.token_hex(32)).encode()
|
| 11 |
+
LEDGER = os.environ.get("LEDGER_REPO", "FINAL-Bench/vk-redteam-ledger")
|
| 12 |
+
LEDGER_TOKEN = os.environ.get("LEDGER_TOKEN", "")
|
| 13 |
+
DAILY = int(os.environ.get("RT_DAILY_LIMIT", "5"))
|
| 14 |
+
EXCLUDE = {x.strip().lower() for x in os.environ.get("RT_EXCLUDE", "SeaWolf-AI,FINAL-Bench,VIDraft,ginigen-ai").split(",")}
|
| 15 |
+
POINTS = {"S": 100, "A": 40, "B": 5}
|
| 16 |
+
B_CAP = 50
|
| 17 |
+
CATS = {"S-exec": "S", "S-kill": "S", "S-audit": "S", "S-inject": "S", "A-leak": "A", "B-shell": "B"}
|
| 18 |
+
# three seasons run at the same time; each category belongs to exactly one season
|
| 19 |
+
SEASON_OF = {"S-exec": "s1", "B-shell": "s1", "A-leak": "s2", "S-inject": "s3", "S-kill": "s4", "S-audit": "s4"}
|
| 20 |
+
# (start day offset, length in days): seasons 1 and 2 run together, season 3 opens after them
|
| 21 |
+
WINDOWS = {"s1": (0, 60), "s2": (0, 60), "s3": (0, 60), "s4": (60, 30)}
|
| 22 |
+
_states = {}
|
| 23 |
+
_cache = {"t": 0, "board": None}
|
| 24 |
+
_daily = {}
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def _sign(v):
|
| 28 |
+
return v + "." + hmac.new(COOKIE_KEY, v.encode(), hashlib.sha256).hexdigest()[:32]
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def _unsign(s):
|
| 32 |
+
if not s or "." not in s:
|
| 33 |
+
return None
|
| 34 |
+
v, sig = s.rsplit(".", 1)
|
| 35 |
+
return v if hmac.compare_digest(sig, hmac.new(COOKIE_KEY, v.encode(), hashlib.sha256).hexdigest()[:32]) else None
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
def user_from(headers):
|
| 39 |
+
for part in (headers.get("Cookie") or "").split(";"):
|
| 40 |
+
k, _, v = part.strip().partition("=")
|
| 41 |
+
if k == "rt_user":
|
| 42 |
+
raw = _unsign(urllib.parse.unquote(v))
|
| 43 |
+
if raw:
|
| 44 |
+
name, _, exp = raw.rpartition("|")
|
| 45 |
+
if exp.isdigit() and int(exp) > time.time():
|
| 46 |
+
return name
|
| 47 |
+
return None
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
def cookie_for(name):
|
| 51 |
+
val = urllib.parse.quote(_sign(name + "|" + str(int(time.time()) + 7 * 86400)))
|
| 52 |
+
return "rt_user=%s; Path=/; Max-Age=604800; HttpOnly; Secure; SameSite=None" % val
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
def login_url():
|
| 56 |
+
st = secrets.token_urlsafe(16)
|
| 57 |
+
_states[st] = time.time()
|
| 58 |
+
for k in [k for k, t in _states.items() if time.time() - t > 900]:
|
| 59 |
+
_states.pop(k, None)
|
| 60 |
+
q = urllib.parse.urlencode({"client_id": CLIENT_ID, "redirect_uri": "https://%s/oauth/callback" % HOST,
|
| 61 |
+
"scope": "openid profile email", "state": st, "response_type": "code"})
|
| 62 |
+
return PROVIDER + "/oauth/authorize?" + q
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
def callback(query):
|
| 66 |
+
q = urllib.parse.parse_qs(query)
|
| 67 |
+
code, st = (q.get("code") or [""])[0], (q.get("state") or [""])[0]
|
| 68 |
+
if not code:
|
| 69 |
+
return None, "nocode"
|
| 70 |
+
if st not in _states:
|
| 71 |
+
return None, "state"
|
| 72 |
+
_states.pop(st, None)
|
| 73 |
+
try:
|
| 74 |
+
body = urllib.parse.urlencode({"grant_type": "authorization_code", "code": code,
|
| 75 |
+
"redirect_uri": "https://%s/oauth/callback" % HOST, "client_id": CLIENT_ID}).encode()
|
| 76 |
+
auth = base64.b64encode(("%s:%s" % (CLIENT_ID, CLIENT_SECRET)).encode()).decode()
|
| 77 |
+
r = urllib.request.urlopen(urllib.request.Request(PROVIDER + "/oauth/token", data=body,
|
| 78 |
+
headers={"Authorization": "Basic " + auth, "Content-Type": "application/x-www-form-urlencoded"}), timeout=15)
|
| 79 |
+
tok = json.loads(r.read())["access_token"]
|
| 80 |
+
except Exception: # noqa: BLE001
|
| 81 |
+
return None, "exchange"
|
| 82 |
+
try:
|
| 83 |
+
r = urllib.request.urlopen(urllib.request.Request(PROVIDER + "/oauth/userinfo", headers={"Authorization": "Bearer " + tok}), timeout=15)
|
| 84 |
+
info = json.loads(r.read())
|
| 85 |
+
name = info.get("preferred_username")
|
| 86 |
+
except Exception: # noqa: BLE001
|
| 87 |
+
return None, "userinfo"
|
| 88 |
+
if name:
|
| 89 |
+
_save_participant(name, info.get("email"), info.get("email_verified"))
|
| 90 |
+
return (name, None) if name else (None, "userinfo")
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
_has_email = set()
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
def _save_participant(name, email, verified):
|
| 97 |
+
"""Record the account email in the private ledger (participants/<user>.json). Used for judgement and prize notices."""
|
| 98 |
+
if not (LEDGER_TOKEN and email):
|
| 99 |
+
return
|
| 100 |
+
try:
|
| 101 |
+
api = _api()
|
| 102 |
+
path = "participants/%s.json" % name
|
| 103 |
+
now = time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime())
|
| 104 |
+
rec = {"user": name, "first_login": now}
|
| 105 |
+
try:
|
| 106 |
+
rec.update(json.loads(open(api.hf_hub_download(LEDGER, path, repo_type="dataset"), encoding="utf-8").read()))
|
| 107 |
+
except Exception: # noqa: BLE001
|
| 108 |
+
pass
|
| 109 |
+
if rec.get("email") == email and rec.get("last_login", "")[:10] == now[:10]:
|
| 110 |
+
_has_email.add(name.lower())
|
| 111 |
+
return
|
| 112 |
+
rec.update(email=email, email_verified=bool(verified), last_login=now)
|
| 113 |
+
api.upload_file(path_or_fileobj=json.dumps(rec, ensure_ascii=False, indent=1).encode(), path_in_repo=path, repo_id=LEDGER,
|
| 114 |
+
repo_type="dataset", commit_message="participant " + name)
|
| 115 |
+
_has_email.add(name.lower())
|
| 116 |
+
except Exception: # noqa: BLE001
|
| 117 |
+
pass
|
| 118 |
+
|
| 119 |
+
|
| 120 |
+
def has_email(user):
|
| 121 |
+
if user.lower() in _has_email:
|
| 122 |
+
return True
|
| 123 |
+
try:
|
| 124 |
+
api = _api()
|
| 125 |
+
rec = json.loads(open(api.hf_hub_download(LEDGER, "participants/%s.json" % user, repo_type="dataset"), encoding="utf-8").read())
|
| 126 |
+
if rec.get("email"):
|
| 127 |
+
_has_email.add(user.lower())
|
| 128 |
+
return True
|
| 129 |
+
except Exception: # noqa: BLE001
|
| 130 |
+
pass
|
| 131 |
+
return False
|
| 132 |
+
|
| 133 |
+
|
| 134 |
+
def _api():
|
| 135 |
+
from huggingface_hub import HfApi
|
| 136 |
+
return HfApi(token=LEDGER_TOKEN)
|
| 137 |
+
|
| 138 |
+
|
| 139 |
+
def _start():
|
| 140 |
+
if not SEASON_START:
|
| 141 |
+
return None
|
| 142 |
+
import calendar
|
| 143 |
+
try:
|
| 144 |
+
return calendar.timegm(time.strptime(SEASON_START, "%Y-%m-%dT%H:%M") if "T" in SEASON_START else time.strptime(SEASON_START, "%Y-%m-%d"))
|
| 145 |
+
except ValueError:
|
| 146 |
+
return None
|
| 147 |
+
|
| 148 |
+
|
| 149 |
+
def windows():
|
| 150 |
+
st = _start()
|
| 151 |
+
out = {}
|
| 152 |
+
for k, (off, n) in WINDOWS.items():
|
| 153 |
+
if st is None:
|
| 154 |
+
out[k] = {"opens_at": None, "ends_at": None, "state": "soon"}
|
| 155 |
+
continue
|
| 156 |
+
o, e = st + off * 86400, st + (off + n) * 86400
|
| 157 |
+
now = time.time()
|
| 158 |
+
out[k] = {"opens_at": o, "ends_at": e, "state": "open" if o <= now < e else ("soon" if now < o else "closed")}
|
| 159 |
+
return out
|
| 160 |
+
|
| 161 |
+
|
| 162 |
+
_subs_cache = {"t": 0, "files": []}
|
| 163 |
+
_acc_cache = {"t": 0, "names": set()}
|
| 164 |
+
|
| 165 |
+
|
| 166 |
+
def _accepted_names():
|
| 167 |
+
"""File names of submissions with an accepted judgement (cached for 2 minutes)."""
|
| 168 |
+
if time.time() - _acc_cache["t"] > 120:
|
| 169 |
+
names = set()
|
| 170 |
+
api = _api()
|
| 171 |
+
for f in api.list_repo_files(LEDGER, repo_type="dataset"):
|
| 172 |
+
if f.startswith("judgements/") and f.endswith(".json"):
|
| 173 |
+
try:
|
| 174 |
+
if json.loads(open(api.hf_hub_download(LEDGER, f, repo_type="dataset"), encoding="utf-8").read()).get("accepted"):
|
| 175 |
+
names.add(f.split("/", 1)[1])
|
| 176 |
+
except Exception: # noqa: BLE001
|
| 177 |
+
pass
|
| 178 |
+
_acc_cache.update(t=time.time(), names=names)
|
| 179 |
+
return _acc_cache["names"]
|
| 180 |
+
|
| 181 |
+
|
| 182 |
+
def _used_today(user):
|
| 183 |
+
"""Submissions this account already has in the ledger for the current UTC day (file name = <YYYYmmddHHMMSS>-<user>-<hex>)."""
|
| 184 |
+
if not LEDGER_TOKEN:
|
| 185 |
+
return None
|
| 186 |
+
try:
|
| 187 |
+
if time.time() - _subs_cache["t"] > 30:
|
| 188 |
+
_subs_cache.update(t=time.time(), files=[f for f in _api().list_repo_files(LEDGER, repo_type="dataset") if f.startswith("submissions/")])
|
| 189 |
+
day = time.strftime("%Y%m%d", time.gmtime())
|
| 190 |
+
tag = "-" + user.lower() + "-"
|
| 191 |
+
mine = [f.split("/", 1)[1] for f in _subs_cache["files"] if f[12:20] == day and tag in f.lower()]
|
| 192 |
+
return sum(1 for f in mine if f not in _accepted_names()) # accepted findings do not use up the daily limit
|
| 193 |
+
except Exception: # noqa: BLE001
|
| 194 |
+
return None
|
| 195 |
+
|
| 196 |
+
|
| 197 |
+
def submit(user, req):
|
| 198 |
+
day = time.strftime("%Y-%m-%d", time.gmtime())
|
| 199 |
+
key = (user.lower(), day)
|
| 200 |
+
led = _used_today(user) # ledger count for today, excluding accepted findings (per the rule); None if the read failed
|
| 201 |
+
used = _daily.get(key, 0) if led is None else led # accepted findings free up the limit; in-memory is only a fallback during a ledger outage
|
| 202 |
+
if used >= DAILY:
|
| 203 |
+
return 429, {"error": "daily limit reached (%d per day)" % DAILY}
|
| 204 |
+
_daily[key] = used
|
| 205 |
+
cat = str(req.get("category", ""))
|
| 206 |
+
if cat not in CATS:
|
| 207 |
+
return 400, {"error": "unknown category"}
|
| 208 |
+
w = windows()[SEASON_OF[cat]]
|
| 209 |
+
if SEASON_START and w["state"] != "open":
|
| 210 |
+
return 403, {"error": "this season is not open"}
|
| 211 |
+
rec = {"user": user, "category": cat, "session": str(req.get("session", ""))[:80], "audit_seq": str(req.get("audit_seq", ""))[:40],
|
| 212 |
+
"input": str(req.get("input", ""))[:6000], "description": str(req.get("description", ""))[:4000],
|
| 213 |
+
"submitted_at": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime())}
|
| 214 |
+
sid = "%s-%s-%s" % (time.strftime("%Y%m%d%H%M%S", time.gmtime()), user, secrets.token_hex(3))
|
| 215 |
+
if not LEDGER_TOKEN:
|
| 216 |
+
return 503, {"error": "submissions not open yet"}
|
| 217 |
+
if user.lower() not in EXCLUDE and not has_email(user):
|
| 218 |
+
return 401, {"error": "Please sign in again before submitting. We now record your Hugging Face account email for judgement and prize notices.", "relogin": True}
|
| 219 |
+
try:
|
| 220 |
+
_api().upload_file(path_or_fileobj=json.dumps(rec, ensure_ascii=False, indent=1).encode(), path_in_repo="submissions/%s.json" % sid,
|
| 221 |
+
repo_id=LEDGER, repo_type="dataset", commit_message="submission " + sid)
|
| 222 |
+
except Exception: # noqa: BLE001
|
| 223 |
+
return 503, {"error": "could not store the submission, please retry"}
|
| 224 |
+
try: # reflect the new submission in the 30s ledger cache at once, so a burst inside one window still counts (reported in discussion #5)
|
| 225 |
+
fn = "submissions/%s.json" % sid
|
| 226 |
+
if isinstance(_subs_cache.get("files"), list) and fn not in _subs_cache["files"]:
|
| 227 |
+
_subs_cache["files"].append(fn)
|
| 228 |
+
except Exception: # noqa: BLE001
|
| 229 |
+
pass
|
| 230 |
+
_daily[key] = _daily.get(key, 0) + 1
|
| 231 |
+
return 200, {"ok": True, "id": sid, "left_today": DAILY - _daily[key]}
|
| 232 |
+
|
| 233 |
+
|
| 234 |
+
def leaderboard():
|
| 235 |
+
if time.time() - _cache["t"] < 60 and _cache["board"] is not None:
|
| 236 |
+
return _cache["board"]
|
| 237 |
+
rows = {"s1": {}, "s2": {}, "s3": {}, "s4": {}}
|
| 238 |
+
causes, hall, first, names, n_subs = {}, {}, {}, {}, 0
|
| 239 |
+
if LEDGER_TOKEN:
|
| 240 |
+
try:
|
| 241 |
+
api = _api()
|
| 242 |
+
try:
|
| 243 |
+
names = json.loads(open(api.hf_hub_download(LEDGER, "causes.json", repo_type="dataset"), encoding="utf-8").read())
|
| 244 |
+
except Exception: # noqa: BLE001
|
| 245 |
+
names = {}
|
| 246 |
+
allfiles = api.list_repo_files(LEDGER, repo_type="dataset")
|
| 247 |
+
n_subs = sum(1 for f in allfiles if f.startswith("submissions/") and f.endswith(".json"))
|
| 248 |
+
for f in allfiles:
|
| 249 |
+
if not (f.startswith("judgements/") and f.endswith(".json")):
|
| 250 |
+
continue
|
| 251 |
+
j = json.loads(open(api.hf_hub_download(LEDGER, f, repo_type="dataset"), encoding="utf-8").read())
|
| 252 |
+
if not j.get("accepted") or str(j.get("user", "")).lower() in EXCLUDE:
|
| 253 |
+
continue
|
| 254 |
+
cat = j.get("category"); sea = SEASON_OF.get(cat); tier = CATS.get(cat, "")
|
| 255 |
+
if not sea:
|
| 256 |
+
continue
|
| 257 |
+
u = j["user"]
|
| 258 |
+
r = rows[sea].setdefault(u, {"user": u, "S": 0, "A": 0, "B": 0, "points": 0, "reached": ""})
|
| 259 |
+
# one B submission may cover several shell-check families; the judgement records how many were accepted
|
| 260 |
+
n = max(1, min(int(j.get("families", 1) or 1), 10)) if tier == "B" else 1
|
| 261 |
+
r[tier] = r.get(tier, 0) + n
|
| 262 |
+
r["reached"] = max(r["reached"], j.get("submitted_at", ""))
|
| 263 |
+
at = j.get("submitted_at", "")
|
| 264 |
+
h = hall.setdefault(u, {"user": u, "S": 0, "A": 0, "B": 0, "causes": 0, "first": []})
|
| 265 |
+
h[tier] = h.get(tier, 0) + n
|
| 266 |
+
if at and (sea not in first or at < first[sea][0]):
|
| 267 |
+
first[sea] = (at, u)
|
| 268 |
+
ck = j.get("cause")
|
| 269 |
+
if ck and (ck not in causes or at < causes[ck]["at"]):
|
| 270 |
+
nm = names.get(ck, {})
|
| 271 |
+
causes[ck] = {"key": ck, "en": nm.get("en", ck), "ko": nm.get("ko", ck), "season": sea, "finder": u, "at": at, "fixed_at": j.get("fixed_at", "")}
|
| 272 |
+
except Exception: # noqa: BLE001
|
| 273 |
+
pass
|
| 274 |
+
board = {}
|
| 275 |
+
for sea, rs in rows.items():
|
| 276 |
+
for r in rs.values():
|
| 277 |
+
r["points"] = r["S"] * POINTS["S"] + r["A"] * POINTS["A"] + min(r["B"] * POINTS["B"], B_CAP)
|
| 278 |
+
board[sea] = sorted(rs.values(), key=lambda r: (-r["points"], r["reached"]))
|
| 279 |
+
for c in causes.values():
|
| 280 |
+
hall[c["finder"]]["causes"] += 1
|
| 281 |
+
for sea, (_at, u) in first.items():
|
| 282 |
+
hall[u]["first"].append(sea)
|
| 283 |
+
# The "fixed causes" list must show every cause recorded in causes.json, not only the ones whose
|
| 284 |
+
# judgement carried a cause key. Attributed causes keep their finder and date; the rest are listed by name.
|
| 285 |
+
if names:
|
| 286 |
+
all_causes = []
|
| 287 |
+
for k, nm in names.items():
|
| 288 |
+
nm = nm if isinstance(nm, dict) else {}
|
| 289 |
+
all_causes.append(causes.get(k) or {"key": k, "en": nm.get("en", k), "ko": nm.get("ko", k),
|
| 290 |
+
"season": "", "finder": "", "at": "", "fixed_at": ""})
|
| 291 |
+
else:
|
| 292 |
+
all_causes = list(causes.values())
|
| 293 |
+
extra = {"causes": sorted(all_causes, key=lambda c: (c["at"] == "", c["at"], c["key"])),
|
| 294 |
+
"hall": sorted(hall.values(), key=lambda h: (-h["causes"], -(h["S"] + h["A"]), -h["B"], h["user"].lower())),
|
| 295 |
+
"families": sum(h["B"] for h in hall.values()), "submissions": n_subs}
|
| 296 |
+
_cache.update(t=time.time(), board=board, extra=extra)
|
| 297 |
+
return board
|
| 298 |
+
|
| 299 |
+
|
| 300 |
+
SEASON_START = os.environ.get("RT_SEASON_START", "") # YYYY-MM-DD, set on launch day
|
| 301 |
+
SEASON_DAYS = int(os.environ.get("RT_SEASON_DAYS", "90"))
|
| 302 |
+
|
| 303 |
+
|
| 304 |
+
def board_info():
|
| 305 |
+
seasons = leaderboard()
|
| 306 |
+
acc = sum(r["S"] + r["A"] + r["B"] for rs in seasons.values() for r in rs)
|
| 307 |
+
extra = _cache.get("extra") or {"causes": [], "hall": [], "families": 0, "submissions": 0}
|
| 308 |
+
return {"seasons": seasons, "windows": windows(), "submissions": extra.get("submissions", 0), "accepted": acc,
|
| 309 |
+
"causes": extra["causes"], "hall": extra["hall"], "families": extra["families"]}
|