Excluded Projects Resurface Through a Serve Without the Env
A serve without the exclude env brings a hidden project back, stale rows included. The lesson: enforce the visibility policy at every read path.
TL;DR
An excluded project resurfaced because the ban only lived in a single env var applied at the write layer, while a serve run without it happily read stale rows from the database. The fix enforces exclusion on every read path: filtered lists, and detail routes returning 404 before querying. Visibility is treated as a query-time property, checked per request.
Certain projects are deliberately kept out of the census of an internal knowledge tool: not indexed, not counted, not expected to appear. The ban lives in a single environment variable, KP_PROJECTS_EXCLUDE, and the census honoring it was considered enough. This time a serve ran ad-hoc without that variable for a quick look at the database. A long-excluded project immediately looked alive again. The projects list endpoint named it, the web dashboard displayed it, the project switcher in the UI too, and its detail route handed over old rows as-is.
The working assumption before the fix: excluding a project at the write layer meant it would never appear anywhere. The logic sounded reasonable. Data that was never indexed has no path to the UI. What the reasoning missed: the project's rows had long settled in a database table, and the write-layer filter never touches read paths at all.
An audit made the gap obvious. The policy was enforced in one layer only, while serving happened in another. A serve started without the env broke no rule because no rule existed on the path it was using. It simply read the table and served what was already there, including what should have stayed hidden.
Closing the Read Paths at Three Points
The fix enforces the same policy on every read path. The projects list endpoint filters out excluded slugs before returning results. The detail route rejects early: once a slug is on the exclusion list, the request stops with a 404 before touching the database. The dashboard and the project switcher in the UI apply the same filter to their lists, so users never see an entry that another door already refuses. The core pattern is compact.
def excluded_slugs() -> set[str]:
# one env, two layers: census writes, reads honor it too
raw = os.environ.get("KP_PROJECTS_EXCLUDE", "")
return {s.strip() for s in re.split(r"[,\s]+", raw) if s.strip()}
def pid_or_404(conn, slug):
if slug in excluded_slugs():
raise HTTPException(status_code=404, detail="unknown project")
return conn.execute(
"SELECT id FROM kp_projects WHERE slug = ?", (slug,)
).fetchone()
One small detail decides long-term behavior: the env is read per call, not once at startup. The cost is nearly zero, since reading an environment variable is a cheap operation. The reward is a policy that stays current without a restart, and tests can change the env value through monkeypatching, then verify read-layer behavior without starting a new process. One policy source serves both layers: the census on the write side, and every read path on the serving side.
Visibility Is a Query-Time Property
The view that makes the pattern click comes from three directions. The PostgreSQL documentation on row security policies states that without any policy, all rows in a table are equally available to queries that hold the privilege [2]. Visibility is a property evaluated when the query runs, not a stamp applied once in the past. The manual filter on the list endpoint is essentially a simple version of the same principle.
The early-rejection mechanism is nothing exotic either. The FastAPI documentation explains that an HTTPException raised from a utility function stops the request immediately and sends the HTTP error to the client [1]. Rejecting before the query runs saves database work and keeps the outgoing message uniform, unknown project, without revealing that the project exists.
The security principle is old and settled. The OWASP Authorization Cheat Sheet requires permissions to be validated correctly on every request, because a single missed access control check already endangers the resource [3]. The same sheet recommends defense in depth: never rest enforcement on a single control. An exclusion that only lives in the census is exactly that single control.
The decision baked into the fix: one env for two layers, read per call, no cache at startup. The next serve that starts without the env brings nothing back to life. The policy sticks to every read path instead of riding on one write layer.