Skip to content

Spike to see if we can enforce visible scope usage#22810

Draft
klaustopher wants to merge 3 commits intodevfrom
visibility-check-enforcement
Draft

Spike to see if we can enforce visible scope usage#22810
klaustopher wants to merge 3 commits intodevfrom
visibility-check-enforcement

Conversation

@klaustopher
Copy link
Copy Markdown
Contributor

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

  • Added/updated tests
  • Added/updated documentation in Lookbook (patterns, previews, etc)
  • Tested major browsers (Chrome, Firefox, Edge, ...)

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

1 participant