Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix: track subscribed channels in a null-prototype object
A channel named `__proto__` was never stored as an own key of the plain
object backing the subscription set, so `channels()` did not list it and
`isEmpty()` reported the set empty while the subscription was live. An
(S)UNSUBSCRIBE of another kind then cleared the subscriber state and the
connection dropped messages under RESP3 and misrouted them under RESP2.
  • Loading branch information
askalf committed Sep 14, 2026
commit 81a88594bc5826c83231b36a7da05490ae2aa093
18 changes: 14 additions & 4 deletions lib/SubscriptionSet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,24 @@ import { CommandNameFlags } from "./Command";
type AddSet = CommandNameFlags["ENTER_SUBSCRIBER_MODE"][number];
type DelSet = CommandNameFlags["EXIT_SUBSCRIBER_MODE"][number];

type ChannelSet = { [channel: string]: boolean };

// Channel names come from the server, so they can collide with the names
// inherited from `Object.prototype`. Assigning `__proto__` on a plain object
// hits the prototype setter instead of creating an own key, which would hide
// the channel from `channels()` and from `isEmpty()`.
function createChannelSet(): ChannelSet {
return Object.create(null) as ChannelSet;
}

/**
* Tiny class to simplify dealing with subscription set
*/
export default class SubscriptionSet {
private set: { [key: string]: { [channel: string]: boolean } } = {
subscribe: {},
psubscribe: {},
ssubscribe: {},
private set: { [key: string]: ChannelSet } = {
subscribe: createChannelSet(),
psubscribe: createChannelSet(),
ssubscribe: createChannelSet(),
};

add(set: AddSet, channel: string) {
Expand Down
45 changes: 45 additions & 0 deletions test/unit/subscriberMode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,51 @@ PROTOCOLS.forEach((protocol, index) => {
expectSubscriptionStateCleared(redis, protocol);
});

// Channel names are chosen by the application, so they can collide with
// the names inherited from `Object.prototype`. `__proto__` in particular
// is never stored as an own key on a plain object, so the subscription
// set would report itself empty while the channel is still subscribed.
it("keeps a channel named __proto__ subscribed after sunsubscribe", async () => {
redis = new Redis({ port, protocol });
await redis.subscribe("__proto__");
await redis.ssubscribe("shard");
await redis.sunsubscribe("shard");

const { subscriber } = redis.condition;
expect(subscriber).to.not.equal(false);
expect((subscriber as any).channels("subscribe")).to.eql(["__proto__"]);
expect((subscriber as any).channels("ssubscribe")).to.eql([]);
});

it("delivers messages on a channel named __proto__ after sunsubscribe", (done) => {
redis = new Redis({ port, protocol });
redis.on("message", (channel, message) => {
expect(channel).to.eql("__proto__");
expect(message).to.eql("hi");
done();
});
(async () => {
await redis.subscribe("__proto__");
await redis.ssubscribe("shard");
await redis.sunsubscribe("shard");
server.broadcast(pubSubReply(protocol, "message", "__proto__", "hi"));
})();
});

// The removal has to reach the stored key as well: the channel is tracked
// while subscribed, and unsubscribing it empties the set and leaves the
// connection out of subscriber mode.
it("tracks and then clears a subscription to __proto__", async () => {
redis = new Redis({ port, protocol });
await redis.subscribe("__proto__");
expect((redis.condition.subscriber as any).channels("subscribe")).to.eql([
"__proto__",
]);

await redis.unsubscribe("__proto__");
expectSubscriptionStateCleared(redis, protocol);
});

// Channel names are binary safe, but the subscription set keys them by
// their utf8 rendering, so two distinct names can collapse onto one key:
// `<80>` and `<81>` are both invalid utf8 and both render as U+FFFD.
Expand Down