Skip to content

fix(realtime): keep a presence key named "__proto__" in presenceState() - #2890

Open
tejgokani wants to merge 1 commit into
supabase:masterfrom
tejgokani:fix/realtime-presence-proto-key
Open

tejgokani wants to merge 1 commit into
supabase:masterfrom
tejgokani:fix/realtime-presence-proto-key

Conversation

@tejgokani

Copy link
Copy Markdown

Summary

A presence key named __proto__ is silently missing from channel.presenceState(), even though the join and sync events fire for it. PresenceAdapter.transformState() built its result with newState[key] = ..., which for that key calls the __proto__ setter instead of creating an entry. This defines each key as an own property instead.

Problem and reproduction

  • Contract: every key in the presence state the server sends appears in presenceState(). The same file already handles __proto__ for presence metas (it copies property descriptors because plain assignment treats __proto__ as a setter); the top-level keys were missed.
  • Root cause: transformState in src/phoenix/presenceAdapter.ts reduced into {} with newState[key] = transformState(presences).
  • Before the patch: a presence_state frame containing {"alice": {...}, "__proto__": {...}} (presence keys are chosen by clients via config.presence.key) yields Object.keys(channel.presenceState()) of ['alice'], no __proto__ entry, and the returned object's prototype is the transformed presences array. The raw phoenix state does contain the key.
  • Environments: all.

Solution

  • Build the result with Object.defineProperty per key (enumerable, writable, configurable, same as a normal property), so the key is kept as an own property and the object keeps Object.prototype.
  • No public API or type changes; output for every other key is identical. Alternatives considered: Object.fromEntries (works, but its multi-line call form conflicts with this package's comma-dangle lint rule), Object.create(null) (would change the prototype of the returned object for everyone).

Verification

  • New test/presence.protoKey.test.ts: on master it fails (['alice'] instead of ['__proto__', 'alice']); with the patch it passes. It also checks the entry's value and that the returned object keeps Object.prototype.
  • realtime-js suite (vitest, no Docker): master 26 files / 509 tests pass (1 skipped); patch 27 files / 510 tests pass (1 skipped), i.e. only the new test is added.
  • prettier --check, eslint on the changed files, and nx build @supabase/realtime-js are clean.
  • Not run locally: the Playwright e2e and cross-runtime CI suites.

Prior-art and overlap check

Risks

  • None expected: the returned object is built the same way for ordinary keys; only the previously dropped __proto__ key now appears.

PresenceAdapter.transformState() built the returned state with
newState[key] = ..., so a presence key named "__proto__" invoked the
__proto__ setter instead of becoming a key. The entry was missing from
presenceState() while join and sync events still fired for it, and the
returned object's prototype was replaced by the transformed presences.

Define each key as an own property instead, as is already done for the
presence metas.
@tejgokani
tejgokani requested review from a team as code owners November 23, 2026 17:53
@coderabbitai

coderabbitai Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Summary

Summary by CodeRabbit

  • Bug Fixes
    • Presence state now correctly preserves keys such as "__proto__" and their associated data.

PresenceAdapter.transformState now defines presence keys as own properties. The added test checks that presenceState() retains the "__proto__" key and keeps the result’s prototype unchanged.

Suggested labels: realtime-js

Change: Bug fix

Merge Risk: Minimal · up to b1e8d

This fixes presenceState() dropping a presence key named "proto" and leaves ordinary keys unchanged. Production impact is small and the change is easy to revert. It looks ready to merge.

- [ ] Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: b984cdf9-f291-49b7-9783-6093ff2e85c3
Commits

Reviewing files that changed from the base of the PR and between 840843d and b1e8d01.

Files selected for processing: 2

  • packages/core/realtime-js/src/phoenix/presenceAdapter.ts
  • packages/core/realtime-js/test/presence.protoKey.test.ts

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 5 remain after this review.

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.

1 participant