security: require timestamped HMAC on the key feed when configured
ci/woodpecker/push/woodpecker Pipeline was canceled
ci/woodpecker/push/woodpecker Pipeline was canceled
This commit is contained in:
1 parent
f24f0c32f4
commit
637a8aed13
1 file changed
+33
-3
+33
-3
@@ -18,8 +18,10 @@ import inspect
|
|||||||
import html
|
import html
|
||||||
from html import escape as _html_escape
|
from html import escape as _html_escape
|
||||||
import hashlib
|
import hashlib
|
||||||
|
import hmac
|
||||||
import ipaddress
|
import ipaddress
|
||||||
import json
|
import json
|
||||||
|
import time
|
||||||
import urllib.parse
|
import urllib.parse
|
||||||
import logging
|
import logging
|
||||||
import re
|
import re
|
||||||
@@ -10135,6 +10137,31 @@ def _is_ip(value: str) -> bool:
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def _verify_sync_signature(request: Request) -> None:
|
||||||
|
"""Require a timestamped HMAC in addition to the sync token.
|
||||||
|
|
||||||
|
The token alone cannot distinguish the sandbox from a stolen-token caller
|
||||||
|
(both arrive through the Cloudron proxy), and an IP check is not enough if
|
||||||
|
the token is used from inside the network. The sandbox holds a secret the
|
||||||
|
token alone does not reveal. Fail closed when the secret is configured.
|
||||||
|
"""
|
||||||
|
secret = os.environ.get("KEY_FEED_HMAC_SECRET", "")
|
||||||
|
if not secret:
|
||||||
|
return # rollout compatibility; production sets it
|
||||||
|
ts = (request.headers.get("x-sync-timestamp") or "").strip()
|
||||||
|
sig = (request.headers.get("x-sync-signature") or "").strip()
|
||||||
|
try:
|
||||||
|
ts_int = int(ts)
|
||||||
|
except ValueError:
|
||||||
|
raise HTTPException(401, "Invalid sync timestamp")
|
||||||
|
if abs(time.time() - ts_int) > 60:
|
||||||
|
raise HTTPException(401, "Sync timestamp outside the allowed window")
|
||||||
|
msg = f"{ts}\n{request.url.path}\n{request.url.query or ''}"
|
||||||
|
expected = hmac.new(secret.encode("utf-8"), msg.encode("utf-8"), hashlib.sha256).hexdigest()
|
||||||
|
if not secrets.compare_digest(sig, expected):
|
||||||
|
raise HTTPException(401, "Invalid sync signature")
|
||||||
|
|
||||||
|
|
||||||
def _verify_sync_token(request: Request) -> None:
|
def _verify_sync_token(request: Request) -> None:
|
||||||
"""Authenticate the LibreChat key-sync feed via X-Sync-Token.
|
"""Authenticate the LibreChat key-sync feed via X-Sync-Token.
|
||||||
|
|
||||||
@@ -10163,15 +10190,18 @@ async def admin_librechat_keys(request: Request):
|
|||||||
DELIBERATE DESIGN (audit M2, 2026-11-05): one static token returns every
|
DELIBERATE DESIGN (audit M2, 2026-11-05): one static token returns every
|
||||||
member's decrypted chat key — a total-member-key blast radius. Kept (not
|
member's decrypted chat key — a total-member-key blast radius. Kept (not
|
||||||
per-member fetch) because the only consumer is the LibreChat sandbox's
|
per-member fetch) because the only consumer is the LibreChat sandbox's
|
||||||
in-app key-sync, and scoping the feed per member would require changing
|
the sandbox's in-app key-sync, and scoping the feed per member would require changing
|
||||||
the sandbox's sync identity model; a wrong change there stops key seeding
|
the sandbox's sync identity model; a wrong change there stops key seeding
|
||||||
for every member at once. Recorded in ops-docs
|
for every member at once. Recorded in ops-docs
|
||||||
(maintenance/librechat-key-feed.md). Monitoring: every fetch is logged
|
(maintenance/librechat-key-feed.md). Monitoring: every fetch is logged
|
||||||
(count only, never key material) and the daily digest watches the rate —
|
(count only, never key material) and the daily digest watches the rate —
|
||||||
the sandbox polls ~every 10 min, so anything far above that cadence (or a
|
the sandbox polls every minute, so anything far above that cadence (or a
|
||||||
401 burst on this route) is surfaced as an anomaly.
|
401 burst on this route) is surfaced as an anomaly. A timestamped HMAC
|
||||||
|
(`X-Sync-Timestamp` + `X-Sync-Signature`) is required in addition to the
|
||||||
|
token when KEY_FEED_HMAC_SECRET is configured.
|
||||||
"""
|
"""
|
||||||
_verify_sync_token(request)
|
_verify_sync_token(request)
|
||||||
|
_verify_sync_signature(request)
|
||||||
|
|
||||||
# SECURITY (2026-11-19): the token alone cannot distinguish the sandbox
|
# SECURITY (2026-11-19): the token alone cannot distinguish the sandbox
|
||||||
# from a stolen-token caller because both arrive through the Cloudron
|
# from a stolen-token caller because both arrive through the Cloudron
|
||||||
|
|||||||
Reference in new issue
Block a user