Files
headroom/tests/test_sql_anon_access.py
Tejas ChopraandClaude Opus 5.5 b324efb1d4 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>
2026-11-13 20:31:41 -06:00

36 lines
1.5 KiB
Python

"""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)