Spike to see if we can enforce visible scope usage#22810
Draft
klaustopher wants to merge 3 commits intodevfrom
Draft
Spike to see if we can enforce visible scope usage#22810klaustopher wants to merge 3 commits intodevfrom
klaustopher wants to merge 3 commits intodevfrom
Conversation
Introduce an opt-in runtime safeguard that detects SELECT queries on
visibility-protected tables which were not scoped through `.visible(user)`.
The check mirrors the pedagogy of `authorization_check_required`: fail
fast with a message that points at the ways to satisfy it.
The enforcer subscribes to `sql.active_record` and verifies that every
protected table referenced in a query (via FROM or JOIN, including
subqueries) carries a matching `/* visibility_checked:<table> */` or
`/* skip_visibility_check:<table>:<reason> */` annotation. Annotations
are emitted by helpers on `VisibilityAnnotation`, exposed on
`ApplicationRecord` and `ActiveRecord::Relation`:
Model.visible(user) # visibility_checked
relation.skip_visibility_check(reason: "...") # self bypass
relation.skip_visibility_check_for(Other, # joined/subqueried
reason: "...")
Which tables are enforced is controlled by the
`OP_VISIBILITY_ENFORCED_TABLES` env var (comma-separated). With the var
unset, the subscriber is installed but a no-op, so this change is safe
to land ahead of the per-table rollout.
`VisibilityCheckEnforcer.bypass { }` suppresses enforcement on the
current thread, intended for factories and seeders.
Per-table annotations ensure that joined queries like
`Project.visible.joins(:meetings)` are flagged when the joined table is
not also visibility-checked, which a static check could not catch.
Append `.visibility_checked` (or `.annotate(\"visibility_checked:<table>\")`) to every `scope :visible` and `def self.visible` across app/models and module models, so that queries flowing through `.visible(user)` are marked for the runtime enforcer. Once a table is added to `OP_VISIBILITY_ENFORCED_TABLES`, any query against that table that did not go through its visible scope or an explicit `.skip_visibility_check` bypass will raise in dev and test.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Ticket
https://community.openproject.org/projects/openproject/work_packages/60757/activity
What are you trying to accomplish?
Screenshots
What approach did you choose and why?
Merge checklist