Skip to content

Scope and paranoid conditions are merged into options.where, so nothing downstream can tell them from the caller's own filter #18323

Open

Description

@WikiRik

Status

This is open for discussion rather than a settled proposal. It is filed mainly as a
reminder: the workaround described below is deliberate and works, but the underlying
shape is something we want to re-architect later, and that intent should not live only
in a PR review thread. Nothing here is urgent, and the sketch is a starting point, not
a design.

Problem

Model.destroy, Model.update and Model.increment all merge Sequelize's own
conditions into the caller's options.where before the statement is built:

  • _injectScope(options) merges the model's scope (model.js:3178)
  • _paranoidClause(model, options) adds the deletedAt condition (model.js:272)
  • paranoid destroy merges again with Object.assign(where, options.where) (model.js:2798)

After that point options.where is a single object, and no code downstream can
answer "did the caller actually restrict this statement?" — the caller's
conditions and Sequelize's are indistinguishable.

That question matters, and it is about to matter more. The write-path guard added
in #18307 has to answer exactly it: a where built from a list that turned out
empty imposes no restriction, and the statement then rewrites the whole table.
Judged after the merge, a paranoid model's deletedAt IS NULL looks like a real
filter and the guard stays silent:

// paranoid model
await User.destroy({ where: { id: { [Op.notIn]: [] } } });
// -> DELETE ... WHERE "deletedAt" IS NULL
// every visible row deleted; the caller's only condition imposed nothing

The workaround in that PR is to run the check in the three Model methods, in the
window between cloneDeep(options) and _injectScope(options), which is the last
moment options.where is purely the caller's. It works, but it is a window rather
than a property: it costs an extra compilation of the where, it has to be
repeated at each entry point, and it silently stops working if anything is ever
merged in earlier.

Proposal

Carry Sequelize-injected conditions separately from the caller's where, and AND
them together at SQL-generation time rather than by mutating the caller's object.

Sketch — the naming is up for discussion:

// instead of merging into options.where
options.where          // stays exactly what the caller passed
options.internalWhere  // scope, paranoid clause, association scope

whereQuery (or bulkDeleteQuery / updateQuery) combines the two. Anything
that needs to reason about caller intent then has a first-class thing to look at,
at any layer, without a timing window.

What this would enable or simplify

  • The write-path guard becomes a property of the compiled statement rather than a
    pre-check at three call sites, and cannot be bypassed by a model configuration.
  • Model.destroy's "a where is required" safeguard could distinguish "the caller
    passed no filter" from "a scope happens to supply one".
  • Removes a class of surprise where a scope silently changes what a where means.

Scope

This touches _injectScope, _paranoidClause, the paranoid branch of
Model.destroy, and every consumer that reads options.where expecting the merged
value — including association mixins and include handling. It is a breaking
change for anyone reading options.where in a hook and expecting the scope to be
present.

Not urgent, and deliberately out of scope for #18307: raising it so the workaround
there is recorded as a workaround rather than mistaken for the design.

Alternatives considered

  • Judge the caller's where before injection (what fix(core): make empty Op.in compose correctly under Op.not #18307 does). Correct today,
    but depends on the ordering inside three methods and re-compiles the where.
  • Detect the absorption during compilation — throw when an AND swallows a
    derived always-true. Tried in fix(core): make empty Op.in compose correctly under Op.not #18307 and reverted: it cannot tell a scope from a
    second, legitimate caller filter, so { tenantId: 5, id: { [Op.notIn]: [] } }
    throws when it should not.
  • Mark injected conditions with a sentinel in the where tree. Less invasive than
    separating them, but the marker leaks into every consumer and into user-visible
    where objects.

Created by Opus 5 with Claude Code, supervised by @WikiRik.

Activity

  1. added
    pending-approval
    on Aug 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    pending-approval

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions