Skip to content

fix(security): sanitize the user document in org-switch and /me responses - #3968

Merged
PierreBrisorgueil merged 1 commit into
masterfrom
fix/3963-sanitize-user-response
Jul 16, 2026
Merged

fix(security): sanitize the user document in org-switch and /me responses#3968
PierreBrisorgueil merged 1 commit into
masterfrom
fix/3963-sanitize-user-response

Conversation

@PierreBrisorgueil

Copy link
Copy Markdown
Contributor

Summary

  • What changed: POST /organizations/switch now routes the response user through the existing UserService.removeSensitive() allow-list sanitizer instead of serializing the raw, unselected Mongoose document. GET /users/me no longer forwards providerData verbatim in the response payload.
  • Why: both endpoints were returning the caller's full account record — password hash, OAuth access/refresh tokens, password-reset and email-verification tokens — straight to the client. A single-use reset/verification token or OAuth token captured off these responses (log pipeline, error tracker, browser history) enables account takeover; the password hash enables offline cracking. Internal use of the raw document (ability building via CASL, JWT claims) is untouched — only the response boundary is sanitized.
  • Related issues: Closes 🔒 Sanitize user document in org-switch and /me responses #3963

Scope

  • Module(s) impacted: organizations (controller), users (controller)
  • Cross-module impact: none
  • Risk level: low — response-shape change only (fields removed, not renamed/added); no schema, route, or contract changes

Validation

  • npm run lint
  • npm test
  • Manual checks done (if applicable)

Guardrails check

  • No secrets or credentials introduced (.env*, secrets/**, keys, tokens)
  • No risky rename/move of core stack paths
  • Changes remain merge-friendly for downstream projects
  • Tests added or updated when behavior changed

Notes for reviewers

  • Security considerations: red-green tests seed real OAuth access/refresh tokens and a password-reset token on the fixture user, then assert their absence (and the absence of password) in the actual JSON response body for both /organizations/switch and /users/me — not just presence of a sanitizer call. Full suite green (2662 tests). No secret values are restated in code, tests, or this description.
  • Mergeability considerations: none — additive test coverage, minimal controller diff.
  • Follow-up tasks (optional): none.

https://claude.ai/code/session_01WfNC8bt1TgL4AsiYgCEGup

…nses

What: POST /api/organizations/:id/switch serialized the raw updatedUser
straight from findByIdAndUpdatePopulated().populate() with no .select(),
leaking password hash, providerData/additionalProvidersData (OAuth
access+refresh tokens), reset/verification tokens, and lockout fields.
GET /api/users/me separately forwarded req.user.providerData verbatim
in its manually-whitelisted response.

Why: a captured OAuth token or single-use reset/verification token off
these responses (log pipeline, error tracker, browser history) enables
account takeover; the password hash enables offline cracking.

Fix: route the org-switch response through the existing
UserService.removeSensitive whitelist sanitizer (same pattern already
used by users.admin.controller.js and the local-signin flow) — jwt
signing and ability-building still use the raw doc, only the response
payload is sanitized. Drop providerData from the /me response entirely
(not whitelisted, no known consumer needs it).

Tests: RED-then-GREEN integration coverage for both endpoints
(seeds a fake linked-OAuth token + reset token so the assertion proves
the leak is actually closed, not just absent by fixture luck) plus a
controller-level unit test for switchOrganization using the real
sanitizeUser.removeSensitive.

Closes #3963

Claude-Session: https://claude.ai/code/session_01WfNC8bt1TgL4AsiYgCEGup
@coderabbitai

coderabbitai Bot commented Jul 16, 2026

Copy link
Copy Markdown

Warning

Review limit reached

@PierreBrisorgueil, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 54 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: f1f66bf2-4f5b-4ef9-85c7-0baf10860311

📥 Commits

Reviewing files that changed from the base of the PR and between 8c3b208 and 9f4e9d6.

📒 Files selected for processing (6)
  • ERRORS.md
  • modules/organizations/controllers/organizations.controller.js
  • modules/organizations/tests/organizations.controller.unit.tests.js
  • modules/organizations/tests/organizations.integration.tests.js
  • modules/users/controllers/users.account.controller.js
  • modules/users/tests/user.account.integration.tests.js
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/3963-sanitize-user-response

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@codecov

codecov Bot commented Jul 16, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.04%. Comparing base (8c3b208) to head (9f4e9d6).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #3968      +/-   ##
==========================================
+ Coverage   92.72%   93.04%   +0.32%     
==========================================
  Files         169      169              
  Lines        5580     5580              
  Branches     1793     1793              
==========================================
+ Hits         5174     5192      +18     
+ Misses        326      313      -13     
+ Partials       80       75       -5     
Flag Coverage Δ
integration 61.30% <ø> (+0.28%) ⬆️
unit 74.76% <ø> (+0.28%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.


Continue to review full report in Codecov by Harness.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 8c3b208...9f4e9d6. Read the comment docs.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@PierreBrisorgueil
PierreBrisorgueil marked this pull request as ready for review July 16, 2026 21:17
@PierreBrisorgueil

Copy link
Copy Markdown
Contributor Author

@coderabbitai full review

@coderabbitai

coderabbitai Bot commented Jul 16, 2026

Copy link
Copy Markdown
✅ Action performed

Full review finished.


Your plan includes PR reviews subject to rate limits. More reviews will be available in 54 minutes.

@PierreBrisorgueil
PierreBrisorgueil merged commit 86def2c into master Jul 16, 2026
8 checks passed
@PierreBrisorgueil
PierreBrisorgueil deleted the fix/3963-sanitize-user-response branch July 16, 2026 21:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

🔒 Sanitize user document in org-switch and /me responses

1 participant