Skip to content

📍 fix: Migrate Every Tenant's Legacy SHARED_GLOBAL Role Permissions - #16370

Draft
jtodaroii wants to merge 1 commit into
LibreChat-AI:devfrom
jtodaroii:fix/role-migration-tenant-scoping
Draft

jtodaroii wants to merge 1 commit into
LibreChat-AI:devfrom
jtodaroii:fix/role-migration-tenant-scoping

Conversation

@jtodaroii

Copy link
Copy Markdown
Contributor

Pull Request

Summary

initializeRoles's SHARED_GLOBAL → SHARE migration in packages/data-schemas/src/methods/role.ts reads and writes the Role collection through the raw driver, filtered on { name: roleName } alone. Role is tenant-scoped ({name, tenantId} unique index) and the migration runs under system context, so the name-only filter matches an arbitrary tenant's document on both the read and the write, leaving every other tenant sharing that role name permanently unmigrated since the migration is idempotent-by-absence. The same block also tests SHARE presence with 'SHARE' in block, which is true for a stored SHARE: null, so a document holding { SHARED_GLOBAL: true, SHARE: null } skips inheriting the legacy value while still $unsetting SHARED_GLOBAL, discarding the deployment's sharing intent — the per-field merge later in the same function treats null as missing, so the two checks disagreed about what null means. This PR reads every document matching the role name and migrates each one by _id, and switches the presence check to a nullish check so a stored null is treated the same as absent.

Fixes #16213

Type of change

  • Bug fix

Testing

Tested environments/configuration:

  • Database: MongoDB (in-memory via mongodb-memory-server)

Automated tests:

  • Added two tests to packages/data-schemas/src/methods/role.methods.spec.ts: one seeds two tenants' legacy documents and asserts both migrate, the other seeds a stored SHARE: null and asserts it inherits from SHARED_GLOBAL instead of being treated as present.
  • npx jest role.methods.spec.ts — 72 passed
  • npx tsc --noEmit — clean

Screenshots / recordings

N/A — server-side data migration, no UI change.

`initializeRoles`'s SHARED_GLOBAL -> SHARE migration reads and writes the
Role collection through the raw driver, filtered on `{ name: roleName }`
alone. Role is tenant-scoped (`{name, tenantId}` unique index) and this
runs under system context, so the name-only filter matches an arbitrary
tenant's document on both the read and the write. Every other tenant
sharing that role name keeps its legacy SHARED_GLOBAL field, and the
migration is idempotent-by-absence, so a later boot skips the tenant it
already cleared and still never reaches the rest.

The read now loads every document matching the role name and migrates
each one by _id, so no tenant is left behind.

The same block also tested SHARE presence with `'SHARE' in block`, which
is true for a stored `SHARE: null`. That skipped inheriting from
SHARED_GLOBAL while still $unsetting it, discarding the deployment's
sharing intent -- the per-field merge later in the same function treats
null as missing (`== null`), so the two checks disagreed about what null
means. The presence check is now nullish, matching the merge.

Fixes LibreChat-AI#16213
Copilot AI lite review requested due to automatic review settings September 25, 2026 17:52

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟢 Approval recommended

The migration fixes are covered by focused regression tests, with no unresolved review issues.

Review effort: Lite
Findings: None

What changed in this PR

Fixes tenant-scoped legacy role-permission migration and correctly handles SHARE: null.

Changes:

  • Migrates every matching role document by _id.
  • Treats nullish SHARE values as absent.
  • Adds regression tests for both cases.
File Description
packages/​data-schemas/​src/​methods/​role.ts Corrects migration filtering and null handling.
packages/​data-schemas/​src/​methods/​role.methods.spec.ts Adds regression coverage for both defects.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants