fix(sql): stop granting anon read access to raw proxy telemetry (#4867)

> Replaces #4864: same change as one commit, because a commit message
there failed commitlint and fixing it would need a force-push.

## Description

`sql/create_proxy_telemetry_v2.sql` created an `anon_select` policy and
`GRANT SELECT ... TO anon` on the raw telemetry table. The anon key is
meant to be public, so anyone holding it could read every raw row. The
Supabase beacon was removed in #1526, so the script is now marked legacy
and keeps only the INSERT path that older releases still use. The SELECT
policy and grant are gone. The script now runs `DROP POLICY IF EXISTS
"anon_select"` and `REVOKE SELECT ... FROM anon`, and drops
`anon_insert` before creating it, so re-running the whole script on a
project created with the old version removes the read access.

`run-all-plugins.sh` (dev launcher) silently exported
`HEADROOM_LICENSE_DEV=1`, turning off licence verification, whenever
`~/.headroom/plugins.env` was missing. It now keeps a `HEADROOM_LICENSE`
already in the environment and otherwise exits with a message.

Closes #

## Type of Change

- [x] Bug fix (non-breaking change that fixes an issue)
- [ ] New feature (non-breaking change that adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to change)
- [ ] Documentation update
- [ ] Performance improvement
- [ ] Code refactoring (no functional changes)

## Changes Made

- `sql/create_proxy_telemetry_v2.sql`: legacy header; anon INSERT only;
idempotent policy section that drops the old read policy and revokes
SELECT.
- `run-all-plugins.sh`: no `HEADROOM_LICENSE_DEV` fallback.
- `tests/test_sql_anon_access.py`: static checks (there is no SQL
harness in the repo) that no policy or grant gives anon SELECT/ALL on
the raw table, INSERT is still granted, the DROP POLICY / REVOKE run as
live statements, and the dev script does not export
`HEADROOM_LICENSE_DEV`.

## Testing

- [x] Unit tests pass (`pytest`)
- [x] Linting passes (`ruff check .`)
- [x] Type checking passes (`mypy headroom`)
- [x] New tests added for new functionality
- [ ] Manual testing performed

### Test Output

```text
$ pytest -q tests/test_sql_anon_access.py
2 passed

# same tests against origin/main files
2 failed
```

## Real Behavior Proof

- Not run against Postgres: a local Postgres container run was attempted
but Docker was unavailable on the test machine. Not run against the live
Supabase project; if it still exists, someone with access needs to
re-run this script (or the DROP POLICY / REVOKE pair) once. This PR does
not change any deployed database.

## Runtime Rollout Safety

- Rollout-managed feature(s): none
- Minimum rollout channel: n/a
- Stable/default behavior changed: no runtime change; setup SQL and a
dev script only.
- Kill switch / disable path: n/a
- Unsafe override required: no
- Qualification impact: none
- Rollback path: revert this commit.

## Review Readiness

- [x] I have performed a self-review
- [x] This PR is ready for human review

## Checklist

- [x] My code follows the project's style guidelines
- [x] I have performed a self-review of my code
- [x] I have commented my code, particularly in hard-to-understand areas
- [x] I have made corresponding changes to the documentation
- [x] My changes generate no new warnings
- [x] I have added tests that prove my fix is effective or that my
feature works
- [x] New and existing unit tests pass locally with my changes
- [x] I did **not** edit `CHANGELOG.md` — it is generated by
release-please from my Conventional Commit PR title (a CI guard enforces
this)

🤖 Generated with [Claude Code](https://claude.com/claude-code)


<!-- devin-review-badge-begin -->

---

<a href="https://app.devin.ai/review/headroomlabs-ai/headroom/pull/4867"
target="_blank"><picture><source media="(prefers-color-scheme: dark)"
srcset="https://static.devin.ai/assets/gh-devin-review-dark.svg?v=4"><img
src="https://static.devin.ai/assets/gh-devin-review-light.svg?v=4"
alt="Devin Review"></picture></a>
<!-- devin-review-badge-end -->

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Tejas Chopra
2026-11-13 20:31:41 -06:00
committed by GitHub
co-authored by Claude Opus 5.5
parent f0bdcfc9ca
commit b324efb1d4
3 changed files with 55 additions and 12 deletions
+5 -4
View File
@@ -71,13 +71,14 @@ source "$ENV_TXT"; set +a
# ── 4. plugin license (Ed25519, offline, wildcard) ────────────────────────────
# HEADROOM_LICENSE + HEADROOM_LICENSE_PUBKEY. Core and every plugin read the same
# HEADROOM_LICENSE; the banner says "LICENSED (usage reporting off)" unless
# HEADROOM_USAGE_REPORTING=1 is also set. Fallback: skip verification entirely.
# HEADROOM_USAGE_REPORTING=1 is also set. Licence variables already exported in
# the calling shell are kept.
if [ -f "$PLUGINS_ENV" ]; then
set -a; # shellcheck disable=SC1090
source "$PLUGINS_ENV"; set +a
else
echo "▶ $PLUGINS_ENV missing — using dev license bypass"
export HEADROOM_LICENSE_DEV=1
elif [ -z "${HEADROOM_LICENSE:-}" ]; then
echo "!! $PLUGINS_ENV not found and HEADROOM_LICENSE is not set: the plugins need a licence"
exit 1
fi
# ── 5. Kompress ML offload → Modal ────────────────────────────────────────────
+15 -8
View File
@@ -1,6 +1,8 @@
-- Supabase SQL: Create proxy_telemetry_v2 table
-- LEGACY: current releases no longer send to Supabase (removed in #1526); only
-- older releases still insert rows here. Kept for reference and for the
-- dashboard_summary refresh in create_dashboard_summary.sql.
-- Run this in the Supabase SQL Editor (https://supabase.com/dashboard → SQL Editor)
-- This table matches every field the beacon code writes in headroom/telemetry/beacon.py
CREATE TABLE IF NOT EXISTS proxy_telemetry_v2 (
id uuid DEFAULT gen_random_uuid() PRIMARY KEY,
@@ -60,14 +62,19 @@ CREATE INDEX IF NOT EXISTS idx_ptv2_version ON proxy_telemetry_v2(headroom_versi
-- Enable Row Level Security
ALTER TABLE proxy_telemetry_v2 ENABLE ROW LEVEL SECURITY;
-- RLS policies: anon can INSERT (telemetry beacon) and SELECT (for debugging)
-- RLS policy: anon can INSERT only (the beacon of older releases). anon has no
-- SELECT: read raw rows with the service role. The public dashboard reads the
-- aggregated dashboard_summary table (create_dashboard_summary.sql).
--
-- Safe to re-run on an existing project, and re-running is how a project set
-- up with an earlier version of this script (which also let anon read every
-- row) is fixed: the old read policy and grant are removed here.
DROP POLICY IF EXISTS "anon_select" ON proxy_telemetry_v2;
REVOKE SELECT ON proxy_telemetry_v2 FROM anon;
DROP POLICY IF EXISTS "anon_insert" ON proxy_telemetry_v2;
CREATE POLICY "anon_insert" ON proxy_telemetry_v2
FOR INSERT TO anon
WITH CHECK (true);
CREATE POLICY "anon_select" ON proxy_telemetry_v2
FOR SELECT TO anon
USING (true);
-- Grant permissions to anon role
GRANT INSERT, SELECT ON proxy_telemetry_v2 TO anon;
GRANT INSERT ON proxy_telemetry_v2 TO anon;
+35
View File
@@ -0,0 +1,35 @@
"""Static checks on the Supabase setup scripts and the all-plugins dev script.
There is no SQL harness in the repo, so these read the script text.
"""
from __future__ import annotations
import re
from pathlib import Path
ROOT = Path(__file__).resolve().parent.parent
def _statements(sql: str) -> list[str]:
sql = re.sub(r"--[^\n]*", "", sql) # comments may quote the statements to undo
return [" ".join(s.split()) for s in sql.split(";") if s.strip()]
def test_raw_telemetry_table_is_not_readable_by_anon() -> None:
sql = (ROOT / "sql" / "create_proxy_telemetry_v2.sql").read_text(encoding="utf-8")
statements = _statements(sql)
for stmt in statements:
assert not re.match(r"CREATE POLICY .* FOR (SELECT|ALL)\b.* TO anon\b", stmt, re.I), stmt
grant = re.match(r"GRANT (.*?) ON .* TO .*\banon\b", stmt, re.I)
assert not (grant and re.search(r"\b(SELECT|ALL)\b", grant.group(1), re.I)), stmt
assert any(re.match(r"GRANT INSERT ON proxy_telemetry_v2 TO anon", s) for s in statements)
# Re-running the script is how an existing project drops the old read access,
# so the cleanup must be live statements, not comments.
assert 'DROP POLICY IF EXISTS "anon_select" ON proxy_telemetry_v2' in statements
assert "REVOKE SELECT ON proxy_telemetry_v2 FROM anon" in statements
def test_all_plugins_script_does_not_turn_off_licence_checks() -> None:
text = (ROOT / "run-all-plugins.sh").read_text(encoding="utf-8")
assert not re.search(r"^\s*export\s+HEADROOM_LICENSE_DEV", text, re.M)