HarnessRouter Community Edition: the self-hosted, Apache-2.0 edition of the unified interface for agent harnesses. Run Codex, Claude Code, Hermes, PI, DSH, and more through one API, with sessions, streaming, files, cancellation, and failure handling. Implements the Unified Harness Protocol (UHP), an open standard. Your keys, your infrastructure.
Python agent-apiagent-frameworkagent-harnessai-agentsai-infrastructurecoding-agentconformance-testingdeepseek-harnessharness-engineeringhermesllmopen-protocolopen-sourceopen-standardpi-coding-agentself-hosteduhpunified-apiunified-harness-protocolunified-interface
feat(sql): a read-only SQL data plane, and secrets that refuse to sit in plaintext
richard-epsillabd22cd6e78fb
diff --git a/gateway/backing.py b/gateway/backing.pyindex cfdd49f7..39143c07 100644--- a/gateway/backing.py+++ b/gateway/backing.py@@ -24,6 +24,10 @@ relational table, or anything else that can look up records by label + propertyfrom __future__ import annotationsimport asyncio+import base64+import contextlib+import hashlib+import secrets as secrets_modimport ioimport jsonimport os@@ -247,13 +251,64 @@ class FileBlobStore:# ── local: env/file secrets ("bring your own provider keys") ──────────────────────+class SecretsNotConfigured(RuntimeError):+ """Raised when a secret must be written but there is nowhere safe to write it.++ Deliberately an error rather than a fallback. A provider key injected read-only through the+ environment is one risk; a customer's production database connection string written to a file+ in plaintext is another entirely, and the difference is not something to decide silently on+ the operator's behalf."""++class FileSecretStore:"""Reads HR_SECRET_{TENANT}_{NAME} from the environment first (read-only injection — the- open-repo way to supply provider credentials without any vault), then falls back to plain- files under {root}/{tenant}/{name}. Writes (e.g. per-org MCP tokens) always go to files."""+ open-repo way to supply provider credentials without any vault), then falls back to files+ under {root}/{tenant}/{name}.++ Files are ENCRYPTED when HR_SECRET_KEY is set: AES-256-GCM under a key derived from it, with+ a random nonce per write, stored as `hrenc1:<b64 nonce||ciphertext>`. Reads accept both forms,+ so a store written before this existed keeps working and re-encrypts on its next write.++ Without HR_SECRET_KEY, `put(..., require_encryption=True)` REFUSES. That is the whole point:+ the caller that stores a database credential says so, and on an instance with no key it gets+ an error telling the operator what to set, instead of a plaintext file nobody knew about."""++ _MAGIC = "hrenc1:"def __init__(self, root: str):self._root = Path(root)+ self._key = self._derive(os.environ.get("HR_SECRET_KEY", ""))++ @property+ def encrypts(self) -> bool:+ return self._key is not None++ @staticmethod+ def _derive(passphrase: str) -> bytes | None:+ """A 32-byte key from the operator's passphrase. Scrypt with a fixed salt: the salt's job+ is to stop cross-deployment rainbow tables, and there is nowhere to keep a random one that+ would not sit beside the ciphertext anyway."""+ if not passphrase:+ return None+ return hashlib.scrypt(passphrase.encode(), salt=b"harnessrouter.secret.v1",+ n=2 ** 14, r=8, p=1, dklen=32)++ def _seal(self, value: str) -> str:+ from cryptography.hazmat.primitives.ciphers.aead import AESGCM+ nonce = secrets_mod.token_bytes(12)+ blob = nonce + AESGCM(self._key).encrypt(nonce, value.encode(), None)+ return self._MAGIC + base64.b64encode(blob).decode()++ def _open(self, raw: str) -> str:+ if not raw.startswith(self._MAGIC):+ return raw # written before encryption existed+ if self._key is None:+ raise SecretsNotConfigured(+ "This secret is encrypted but HR_SECRET_KEY is not set. Set it to the passphrase "+ "used when the secret was stored.")+ from cryptography.hazmat.primitives.ciphers.aead import AESGCM+ blob = base64.b64decode(raw[len(self._MAGIC):])+ return AESGCM(self._key).decrypt(blob[:12], blob[12:], None).decode()@staticmethoddef _env_key(tenant: str, name: str) -> str:@@ -273,13 +328,22 @@ class FileSecretStore:return self._p(tenant, name).read_text()except OSError:return None- return await asyncio.to_thread(_do)+ 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- async def put(self, tenant: str, name: str, value: str) -> None:def _do():p = self._p(tenant, name)p.parent.mkdir(parents=True, exist_ok=True)- p.write_text(value)+ p.write_text(body)+ with contextlib.suppress(OSError):+ p.chmod(0o600) # belt to the encryption's bracesawait asyncio.to_thread(_do)diff --git a/gateway/requirements.txt b/gateway/requirements.txtindex 4a3b9985..aec1a449 100644--- a/gateway/requirements.txt+++ b/gateway/requirements.txt@@ -8,3 +8,6 @@ azure-cosmos==4.9.0aiohttp==3.11.11redis==5.2.1PyJWT==2.10.1+cryptography==44.0.0+asyncpg==0.30.0+aiomysql==0.2.0diff --git a/gateway/sql_plane.py b/gateway/sql_plane.pynew file mode 100644index 00000000..c94ac795--- /dev/null+++ b/gateway/sql_plane.py@@ -0,0 +1,308 @@+"""Running one SELECT against a customer database, safely.++Two callers, one engine, one place the credential is resolved:++ * the `sql` MCP tool, so an agent can explore a schema and test a query while it designs a+ dashboard;+ * `POST /v1/sql/query`, which a dashboard app calls to refresh its panels when someone opens it.++The second is why this is not simply an MCP server. Opening a dashboard must re-run its queries+without spending an agent turn — a turn per panel per page-load is both slow and billed. Both+paths land here, so the read-only gate and the row cap cannot be true of one and not the other.++WHAT NEVER LEAVES THIS PROCESS. The connection string is resolved from the secret store inside+the gateway. The runner never receives it (an agent gets a `datasource` name, never a DSN), and+neither does the browser. That is the same shape MCP bearer tokens already use: config stores+`vault:<ref>`, the live value is fetched at the moment of use.++THE READ-ONLY GATE IS NOT THE ONLY DEFENCE, and the documentation says so. It is a parser, and a+parser is a thing that can be wrong. Point the kit at a database account that only has SELECT and+the two defences are independent.+"""+from __future__ import annotations++import asyncio+import re++# A query that has not returned by now is not going to make a dashboard feel alive, and it is+# holding a connection while it fails to.+DEFAULT_TIMEOUT_S = 30.0+# Rows beyond this are of no use to a chart and considerable use to an out-of-memory error. The+# caller is TOLD when this truncates rather than silently receiving a partial answer.+DEFAULT_MAX_ROWS = 5000++ENGINES = ("postgres", "mysql")+++class SqlError(Exception):+ """A failure with a sentence a person can act on. The database's own message is included when+ it is safe to: 'column "revenu" does not exist' is exactly what the agent needs to fix it."""+++class SqlRefused(SqlError):+ """The statement was refused before it reached the database."""+++# ── the read-only gate ────────────────────────────────────────────────────────────++_LINE_COMMENT = re.compile(r"--[^\n]*")+_BLOCK_COMMENT = re.compile(r"/\*.*?\*/", re.S)+_STRING = re.compile(r"'(?:[^']|'')*'|\"(?:[^\"]|\"\")*\"|`[^`]*`")++# Anywhere in the statement, not just at its start. Postgres allows a data-modifying CTE —+# `WITH x AS (DELETE FROM t RETURNING *) SELECT * FROM x` starts with WITH and deletes your rows —+# so checking only the leading keyword is not a check.+_FORBIDDEN = (+ "insert", "update", "delete", "merge", "upsert", "replace",+ "drop", "create", "alter", "truncate", "rename", "comment",+ "grant", "revoke", "vacuum", "analyze", "reindex", "cluster",+ "copy", "call", "do", "execute", "prepare", "deallocate",+ "set", "reset", "begin", "commit", "rollback", "savepoint",+ "lock", "listen", "notify", "load", "handler", "attach", "detach",+ "into", # SELECT … INTO creates a table+)+++def _strip(sql: str) -> str:+ """Comments and string/identifier literals removed, so keyword matching cannot be fooled by a+ column called `update_time` or a literal 'please delete me'."""+ s = _BLOCK_COMMENT.sub(" ", sql)+ s = _LINE_COMMENT.sub(" ", s)+ s = _STRING.sub(" '' ", s)+ return s+++def check_readonly(sql: str) -> str:+ """The statement, or SqlRefused naming what was wrong. Returns the trimmed statement."""+ raw = (sql or "").strip().rstrip(";").strip()+ if not raw:+ raise SqlRefused("The query is empty.")++ bare = _strip(raw)++ # One statement. `SELECT 1; DROP TABLE users` is two, and the second is the interesting one.+ if ";" in bare.strip().rstrip(";"):+ raise SqlRefused("Send one statement at a time — this contains more than one.")++ words = re.findall(r"[a-zA-Z_][a-zA-Z0-9_]*", bare.lower())+ if not words:+ raise SqlRefused("This does not look like a SQL statement.")+ if words[0] not in ("select", "with"):+ raise SqlRefused(+ f"Only SELECT is allowed here; this starts with {words[0].upper()}. "+ "This connection is for reading data to chart it.")++ hit = next((w for w in words if w in _FORBIDDEN), None)+ if hit:+ raise SqlRefused(+ f"Only SELECT is allowed here, and this contains {hit.upper()}. "+ "If that word is a column or table name, alias it — the check cannot tell them apart.")+ return raw+++_LIMIT_TAIL = re.compile(r"\blimit\s+\d+\s*(?:offset\s+\d+\s*)?$", re.I)+++def with_limit(sql: str, max_rows: int) -> tuple[str, bool]:+ """The statement with a row cap, and whether one was added.++ Wrapping in a subquery would be the robust way and it breaks ORDER BY semantics in enough+ engines to be worse. A trailing LIMIT is appended only when the statement does not already end+ in one; a query with its own smaller LIMIT keeps it.+ """+ bare = _strip(sql).strip().rstrip(";").strip()+ if _LIMIT_TAIL.search(bare):+ return sql, False+ return f"{sql}\nLIMIT {int(max_rows)}", True+++# ── engines ───────────────────────────────────────────────────────────────────────++def _rows_out(columns: list[str], records: list, max_rows: int) -> dict:+ truncated = len(records) > max_rows+ rows = [[_jsonable(v) for v in rec] for rec in records[:max_rows]]+ return {"columns": columns, "rows": rows, "row_count": len(rows), "truncated": truncated}+++def _jsonable(v):+ """Whatever the driver handed back, as something JSON can carry. Decimals, dates, UUIDs and+ memoryviews all arrive from real schemas and none of them survive json.dumps."""+ import datetime+ import decimal+ import uuid+ if v is None or isinstance(v, (str, int, float, bool)):+ return v+ if isinstance(v, decimal.Decimal):+ # A float would quietly lose precision on money. A string keeps what the database said,+ # and ECharts parses it.+ return str(v)+ if isinstance(v, (datetime.datetime, datetime.date, datetime.time)):+ return v.isoformat()+ if isinstance(v, datetime.timedelta):+ return v.total_seconds()+ if isinstance(v, uuid.UUID):+ return str(v)+ if isinstance(v, (bytes, bytearray, memoryview)):+ return f"<{len(bytes(v))} bytes>"+ return str(v)+++async def _pg_query(dsn: str, sql: str, timeout: float, max_rows: int) -> dict:+ try:+ import asyncpg+ except ModuleNotFoundError as e: # pragma: no cover+ raise SqlError("PostgreSQL support is not installed in this build.") from e+ conn = None+ try:+ conn = await asyncio.wait_for(asyncpg.connect(dsn), timeout=timeout)+ # A read-only transaction: the gate above is a parser, this is the database's own opinion.+ # Belt and braces, and this one cannot be fooled by clever text.+ async with conn.transaction(readonly=True):+ recs = await asyncio.wait_for(conn.fetch(sql), timeout=timeout)+ cols = list(recs[0].keys()) if recs else []+ return _rows_out(cols, [tuple(r.values()) for r in recs], max_rows)+ except asyncio.TimeoutError as e:+ raise SqlError(f"The query did not finish within {int(timeout)}s.") from e+ except SqlError:+ raise+ except Exception as e:+ raise SqlError(_clean_db_error(e)) from e+ finally:+ if conn is not None:+ await conn.close()+++async def _mysql_query(dsn: str, sql: str, timeout: float, max_rows: int) -> dict:+ try:+ import aiomysql+ except ModuleNotFoundError as e: # pragma: no cover+ raise SqlError("MySQL support is not installed in this build.") from e+ parsed = _parse_mysql_dsn(dsn)+ conn = None+ try:+ conn = await asyncio.wait_for(aiomysql.connect(**parsed), timeout=timeout)+ async with conn.cursor() as cur:+ # MySQL has no read-only transaction that survives a plain connection cleanly, so the+ # session is set read-only where the server supports it and the parser carries the+ # rest. Documented, not pretended.+ with_suppress = "SET SESSION TRANSACTION READ ONLY"+ try:+ await cur.execute(with_suppress)+ except Exception:+ pass+ await asyncio.wait_for(cur.execute(sql), timeout=timeout)+ recs = await cur.fetchall()+ cols = [d[0] for d in (cur.description or [])]+ return _rows_out(cols, list(recs), max_rows)+ except asyncio.TimeoutError as e:+ raise SqlError(f"The query did not finish within {int(timeout)}s.") from e+ except SqlError:+ raise+ except Exception as e:+ raise SqlError(_clean_db_error(e)) from e+ finally:+ if conn is not None:+ conn.close()+++def _parse_mysql_dsn(dsn: str) -> dict:+ from urllib.parse import unquote, urlparse+ u = urlparse(dsn)+ if u.scheme not in ("mysql", "mariadb", "mysql+aiomysql"):+ raise SqlError("A MySQL connection string looks like mysql://user:password@host:3306/database")+ if not u.hostname:+ raise SqlError("That connection string has no host.")+ return {+ "host": u.hostname, "port": u.port or 3306,+ "user": unquote(u.username or ""), "password": unquote(u.password or ""),+ "db": (u.path or "/").lstrip("/") or None,+ "autocommit": True,+ }+++_CRED = re.compile(r"://[^@/\s]*@")+++def _clean_db_error(e: Exception) -> str:+ """The database's message, with any credential the driver echoed back removed.++ Drivers put the DSN in connection errors, and this message is shown to a person and handed to+ an agent — both places a password must not appear."""+ msg = str(e).strip() or e.__class__.__name__+ return _CRED.sub("://***@", msg)[:600]+++async def run_query(engine: str, dsn: str, sql: str, *, timeout: float = DEFAULT_TIMEOUT_S,+ max_rows: int = DEFAULT_MAX_ROWS) -> dict:+ """Check, cap, execute. The only way a statement reaches a customer database from here."""+ if engine not in ENGINES:+ raise SqlRefused(f"Unsupported database: {engine}. This build supports {', '.join(ENGINES)}.")+ checked = check_readonly(sql)+ capped, added = with_limit(checked, max_rows)+ out = await (_pg_query if engine == "postgres" else _mysql_query)(dsn, capped, timeout, max_rows)+ out["limit_applied"] = added and max_rows or None+ return out+++# ── schema introspection ──────────────────────────────────────────────────────────++_PG_SCHEMA = """+SELECT c.table_schema, c.table_name, c.column_name, c.data_type, c.is_nullable+FROM information_schema.columns c+JOIN information_schema.tables t+ ON t.table_schema = c.table_schema AND t.table_name = c.table_name+WHERE c.table_schema NOT IN ('pg_catalog', 'information_schema')+ AND t.table_type IN ('BASE TABLE', 'VIEW')+ORDER BY c.table_schema, c.table_name, c.ordinal_position+"""++_MYSQL_SCHEMA = """+SELECT c.table_schema, c.table_name, c.column_name, c.data_type, c.is_nullable+FROM information_schema.columns c+JOIN information_schema.tables t+ ON t.table_schema = c.table_schema AND t.table_name = c.table_name+WHERE c.table_schema NOT IN ('mysql', 'information_schema', 'performance_schema', 'sys')+ORDER BY c.table_schema, c.table_name, c.ordinal_position+"""+++async def introspect(engine: str, dsn: str, *, sample_rows: int = 0,+ max_tables: int = 200, timeout: float = DEFAULT_TIMEOUT_S) -> dict:+ """The shape of the database, and optionally a few real rows per table.++ `sample_rows` is 0 unless the person turned sampling on when they launched the kit. Column+ names are one thing; the values in them are the customer's data, and sending them to a model+ provider is a decision only the person who owns them can make.+ """+ sql = _PG_SCHEMA if engine == "postgres" else _MYSQL_SCHEMA+ raw = await run_query(engine, dsn, sql, timeout=timeout, max_rows=20000)++ tables: dict[tuple[str, str], dict] = {}+ for schema, table, column, dtype, nullable in raw["rows"]:+ key = (schema, table)+ t = tables.setdefault(key, {"schema": schema, "name": table, "columns": []})+ t["columns"].append({"name": column, "type": dtype, "nullable": nullable == "YES"})++ out = list(tables.values())[:max_tables]+ truncated_tables = len(tables) > len(out)++ if sample_rows > 0:+ for t in out:+ ident = _quote_ident(engine, t["schema"], t["name"])+ try:+ s = await run_query(engine, dsn, f"SELECT * FROM {ident}",+ timeout=min(timeout, 10.0), max_rows=int(sample_rows))+ t["sample"] = {"columns": s["columns"], "rows": s["rows"]}+ except SqlError as e:+ # One unreadable table must not cost the whole schema — a view over a missing+ # foreign server is common and says nothing about the other 40 tables.+ t["sample_error"] = str(e)[:160]++ return {"engine": engine, "tables": out, "table_count": len(tables),+ "truncated": truncated_tables, "sampled": sample_rows > 0}+++def _quote_ident(engine: str, schema: str, table: str) -> str:+ if engine == "postgres":+ return f'"{schema}"."{table}"'+ return f"`{schema}`.`{table}`"diff --git a/gateway/tests/test_sql_plane.py b/gateway/tests/test_sql_plane.pynew file mode 100644index 00000000..7ae6e788--- /dev/null+++ b/gateway/tests/test_sql_plane.py@@ -0,0 +1,126 @@+"""The read-only gate, pinned.++This is the file that decides whether an agent can write to a customer's production database, so+the cases worth testing are the ones an attacker or a confused model would actually produce: a+second statement hidden behind a semicolon, a data-modifying CTE that starts with the word WITH,+a keyword buried in a comment, and a column legitimately called `update_time` that must NOT be+refused.+"""+import sys+from pathlib import Path++import pytest++sys.path.insert(0, str(Path(__file__).resolve().parents[1]))+from sql_plane import SqlRefused, check_readonly, with_limit # noqa: E402+++def refused(sql):+ with pytest.raises(SqlRefused) as e:+ check_readonly(sql)+ return str(e.value)+++# ── what must be allowed ─────────────────────────────────────────────────────────+[email protected]("sql", [+ "SELECT 1",+ "select * from orders",+ " SELECT count(*) FROM users; ",+ "WITH recent AS (SELECT * FROM orders WHERE created_at > now() - interval '7 days')"+ " SELECT count(*) FROM recent",+ # A column whose NAME contains a forbidden word. Refusing this would make the kit useless on+ # half the schemas in the world.+ "SELECT id, updated_at, created_at FROM users",+ "SELECT deleted_flag FROM accounts WHERE deleted_flag = false",+ # The word only appears inside a string literal.+ "SELECT * FROM logs WHERE message = 'please delete this row'",+ "SELECT * FROM t WHERE note = 'drop table users'",+])+def test_allowed(sql):+ assert check_readonly(sql)+++# ── what must be refused ─────────────────────────────────────────────────────────+[email protected]("sql", [+ "DELETE FROM users",+ "UPDATE users SET admin = true",+ "INSERT INTO users VALUES (1)",+ "DROP TABLE users",+ "TRUNCATE users",+ "ALTER TABLE users ADD COLUMN x int",+ "GRANT ALL ON users TO public",+ "CREATE TABLE t (id int)",+])+def test_refuses_plain_writes(sql):+ assert "only select" in refused(sql).lower()+++def test_refuses_a_second_statement():+ # The classic. The first statement is innocent and the second is the point.+ assert "one statement" in refused("SELECT 1; DROP TABLE users").lower()+ assert "one statement" in refused("SELECT 1;DELETE FROM users;").lower()+++def test_refuses_data_modifying_cte():+ """Starts with WITH, deletes your rows. Checking only the leading keyword is not a check —+ Postgres runs this and it is the single most likely way a write would have slipped through."""+ sql = "WITH gone AS (DELETE FROM orders WHERE id < 100 RETURNING *) SELECT count(*) FROM gone"+ assert "delete" in refused(sql).lower()+++def test_refuses_select_into():+ """SELECT … INTO creates a table in Postgres. It starts with SELECT and it is not a read."""+ assert "into" in refused("SELECT * INTO backup_users FROM users").lower()+++def test_keyword_hidden_in_a_comment_does_not_sneak_through_or_falsely_trip():+ # Commented-out text is not executed, so it must not refuse the query…+ assert check_readonly("SELECT id FROM users -- delete this later")+ assert check_readonly("SELECT id /* drop table users */ FROM users")+ # …but a real statement after a comment is still a real statement.+ assert "one statement" in refused("SELECT 1 -- ok\n; DROP TABLE users").lower()+++def test_refuses_empty_and_nonsense():+ assert "empty" in refused(" ").lower()+ assert "starts with" in refused("EXPLAIN ANALYZE SELECT 1").lower()+++def test_trailing_semicolon_is_fine_but_stripped():+ assert check_readonly("SELECT 1;") == "SELECT 1"+++# ── the row cap ──────────────────────────────────────────────────────────────────++def test_limit_is_added_when_absent():+ out, added = with_limit("SELECT * FROM t", 500)+ assert added and out.endswith("LIMIT 500")+++def test_existing_limit_is_respected():+ """A query that asked for 10 rows means 10 — appending another LIMIT would be a syntax error+ in some engines and a surprise in the rest."""+ out, added = with_limit("SELECT * FROM t ORDER BY x LIMIT 10", 500)+ assert not added and out.count("LIMIT") == 1+++def test_limit_with_offset_is_respected():+ out, added = with_limit("SELECT * FROM t LIMIT 10 OFFSET 20", 500)+ assert not added+++def test_limit_inside_a_subquery_does_not_count_as_the_outer_one():+ """The outer query is unbounded and must still be capped."""+ out, added = with_limit("SELECT * FROM (SELECT * FROM t LIMIT 10) s", 500)+ assert added and out.rstrip().endswith("LIMIT 500")+++# ── credentials must never appear in an error ────────────────────────────────────++def test_connection_errors_do_not_echo_the_password():+ from sql_plane import _clean_db_error+ e = Exception('could not connect to postgresql://app:[email protected]:5432/analytics')+ cleaned = _clean_db_error(e)+ assert "hunter2" not in cleaned and "://***@" in cleaned