fix: use $ne operator in findCustomRoles to prevent false negatives#41031
fix: use $ne operator in findCustomRoles to prevent false negatives#41031aegisforensicsmonk wants to merge 4 commits into
Conversation
The query protected:false missed roles where the protected field was absent, causing a false negative. Changed to protected:{$ne:true} which correctly matches both protected:false and missing protected field. Applied the same fix to countCustomRoles.
🦋 Changeset detectedLatest commit: 58dbc6c The changes in this PR will be included in the next version bump. This PR includes changesets to release 4 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
Looks like this PR is not ready to merge, because of the following issues:
Please fix the issues and try again If you have any trouble, please check the PR guidelines |
|
|
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (1)
📜 Recent review details⏰ Context from checks skipped due to timeout. (1)
|
| Layer / File(s) | Summary |
|---|---|
Custom roles query filter update packages/models/src/models/Roles.ts, .changeset/fix-missing-protected-roles.md |
findCustomRoles and countCustomRoles now use protected: { $ne: true }; a changeset documents the fix and patch releases. |
Estimated code review effort: 2 (Simple) | ~10 minutes
Possibly related issues
- Issue
#40798— Directly addresses custom roles not being found when theprotectedfield is missing.
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
| Check name | Status | Explanation |
|---|---|---|
| Description Check | ✅ Passed | Check skipped - CodeRabbit’s high-level summary is enabled. |
| Title check | ✅ Passed | The title clearly reflects the core fix to custom role queries using $ne to avoid missing roles. |
| Docstring Coverage | ✅ Passed | No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. |
| Linked Issues check | ✅ Passed | Check skipped because no linked issues were found for this pull request. |
| Out of Scope Changes check | ✅ Passed | Check skipped because no linked issues were found for this pull request. |
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Comment @coderabbitai help to get the list of available commands.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: f6f86080df
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| findCustomRoles(options?: FindOptions<IRole>): FindCursor<IRole> { | ||
| const query: Filter<IRole> = { | ||
| protected: false, | ||
| protected: { $ne: true }, |
There was a problem hiding this comment.
Don't classify unmarked default roles as custom
On upgraded workspaces where default roles were created before protected existed, those roles can still be missing the field: startup runs createOrUpdateProtectedRoleAsync, but its existing-role path only calls Roles.updateById(...) and never writes protected: true. This predicate therefore makes built-in roles like admin, moderator, and user match findCustomRoles/countCustomRoles, so Apps-Engine custom-role reads and custom-role statistics include protected default roles instead of only real custom roles. The fix should distinguish known protected roles or migrate/default their protected flag, rather than treating every missing value as custom.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Thanks for pointing this out. My intention was to include custom roles where the protected field is missing, since the previous query only matched roles with protected: false.
However, I see the issue with legacy workspaces where built-in roles such as admin, moderator, and user may also be missing the protected field. In those cases, using protected: { $ne: true } would incorrectly classify those default roles as custom roles.
I'll revisit the approach and look into a solution that correctly handles legacy protected roles, either by ensuring their protected flag is properly set through a migration or by explicitly distinguishing them from custom roles.
Since legacy workspaces might have built-in roles missing the protected flag, this ensures that the protected flag is added to them on startup.
Shevilll
left a comment
There was a problem hiding this comment.
The core fix ({ protected: false } → { protected: { $ne: true } }) is correct — equality on false skips documents where protected is missing, so legacy custom roles get dropped.
A few things before it's mergeable:
- This duplicates #41033, which makes the same change to
Roles.ts, links the tracking issue (#40798), and includes a changeset — worth coordinating so the fix isn't reviewed twice. - No
.changeset/entry, but this touches@rocket.chat/models(andapps/meteor), so the changeset check will fail — needs ayarn changeset. - The added
Roles.updateOne(...)increateOrUpdateProtectedRole.tsisn't in the description and is a separate concern — it backfillsprotected: trueonto legacy system roles (extra DB write per protected role). Might be reasonable, but it deserves its own PR + rationale rather than riding along with the query fix. - Mind linking #40798 in the description for traceability?
Proposed changes
Fixed an issue where custom roles with a missing
protectedfield were incorrectly excluded from query results.fix: include roles with missing protected field in custom role queries
Previously, the query used:
This only matched documents where
protectedwas explicitly set tofalse, causing a false negative for legacy roles where the field was absent.The query has been updated to:
This correctly includes both:
protected: falseprotectedfield is missingThe same fix was applied to
countCustomRolesto ensure consistency between role retrieval and counting operations.Issue(s)
Fixes incorrect custom role filtering when the
protectedfield is absent.Steps to test or reproduce
protectedfield in the database.protected: false).protected: trueremain excluded.countCustomRolesreturns the correct count.Further comments
This change preserves the intended behavior of excluding protected roles while maintaining compatibility with legacy documents that do not contain the
protectedfield.Summary by CodeRabbit