text
stringlengths
0
1.47k
blob = base64.b64decode(raw[len(self._MAGIC):])
return AESGCM(self._key).decrypt(blob[:12], blob[12:], None).decode()
@staticmethod
def _env_key(tenant: str, name: str) -> str:
return "HR_SECRET_" + re.sub(r"[^A-Za-z0-9]", "_", f"{tenant}_{name}").upper()
def _p(self, tenant: str, name: str) -> Path:
safe = re.sub(r"[^A-Za-z0-9._-]", "_", name)
return self._root / re.sub(r"[^A-Za-z0-9._-]", "_", tenant) / safe
async def get(self, tenant: str, name: str) -> str | None:
v = os.environ.get(self._env_key(tenant, name))
if v:
return v
def _do():
try:
return self._p(tenant, name).read_text()
except OSError:
return None
raw = await asyncio.to_thread(_do)
return self._open(raw) if raw is not None else None
async def put(self, tenant: str, name: str, value: str, *, require_encryption: bool = False) -> None:
if require_encryption and self._key is None:
raise SecretsNotConfigured(
"Refusing to store this credential: HR_SECRET_KEY is not set, so it could only be "
"written to disk in plaintext. Set HR_SECRET_KEY to a passphrase and restart.")
body = self._seal(value) if self._key is not None else value
def _do():
p = self._p(tenant, name)
p.parent.mkdir(parents=True, exist_ok=True)
p.write_text(body)
with contextlib.suppress(OSError):
p.chmod(0o600) # belt to the encryption's braces
await asyncio.to_thread(_do)
async def delete(self, tenant: str, name: str) -> None:
def _do():
with contextlib.suppress(OSError):
self._p(tenant, name).unlink()
await asyncio.to_thread(_do)
async def blank(self) -> list[tuple[str, str]]:
"""Every stored secret whose value is empty, as (tenant, name). Before 0.32.0 a removed
credential was overwritten with nothing rather than deleted; the retention sweep removes
those files. A value that cannot be opened (encrypted, and no key) is not reported."""
def _do():
out: list[tuple[str, str]] = []
if not self._root.is_dir():
return out
for tdir in sorted(self._root.iterdir()):
if not tdir.is_dir():
continue
for f in sorted(tdir.iterdir()):
if not f.is_file():
continue
try:
if self._open(f.read_text()) == "":
out.append((tdir.name, f.name))
except Exception: # noqa: BLE001 — unreadable is not empty
continue
return out
return await asyncio.to_thread(_do)
# ── selection ─────────────────────────────────────────────────────────────────────
# ── workspace files ───────────────────────────────────────────────────────────────
def _safe_member(path: str) -> str | None:
"""A workspace-relative path, or None if it tries to leave the workspace.
Rejected rather than sanitised: a path that needed cleaning was not the path the caller meant,
and silently writing somewhere else is worse than refusing."""
p = (path or "").strip().lstrip("/")
if not p or p != posixpath.normpath(p) or p.startswith("../") or "\\" in p:
return None
return p
class RunnerWorkspaceFiles:
"""Self-hosted: a session's workspace is a live directory on the data volume, and the RUNNER is
the only principal that touches it. Behind the write-wall each session directory belongs to that
session's own uid and the product deliberately cannot read or write there (see _isolate_session
in the runner), so a live read or write goes through the runner, which acts as the session,
rather than through this process's own filesystem access. One door, whether the wall is up or
not: the runner is beside the gateway in every self-hosted deployment.
Live, unlike the checkpoint the listing route reads: an app sees a file the moment a tool call
creates it, rather than when the turn ends."""
def __init__(self, endpoint: str, key: str, blob=None, kb: str = ""):
self.endpoint = (endpoint or "").rstrip("/")
self.key = key
self.blob, self.kb = blob, kb
self._client: httpx.AsyncClient | None = None
def _c(self) -> httpx.AsyncClient: