Skip to content
This repository has been archived by the owner on Apr 14, 2023. It is now read-only.

Commit

Permalink
Don't throw in the server on certain unsub messages
Browse files Browse the repository at this point in the history
To use an object as a map with arbitrary keys, you should either use
`Object.create(null)` or the `Map` class.  Otherwise you can run into trouble
where keys like `toString` and `constructor` are in your app too.  If the server
received such a message, an exception would get thrown within
SubscriptionManager.unsubscribe.
  • Loading branch information
glasser committed Dec 22, 2016
1 parent a77c21f commit ac56986
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class Server {
}, keepAlive);
}

const connectionSubscriptions: ConnectionSubscriptions = {};
const connectionSubscriptions: ConnectionSubscriptions = Object.create(null);
connection.on('message', this.onMessage(connection, connectionSubscriptions, request));
connection.on('close', this.onClose(connection, connectionSubscriptions));
});
Expand Down
14 changes: 14 additions & 0 deletions src/test/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
SUBSCRIPTION_FAIL,
SUBSCRIPTION_DATA,
SUBSCRIPTION_KEEPALIVE,
SUBSCRIPTION_END,
} from '../messageTypes';

import {
Expand Down Expand Up @@ -725,6 +726,19 @@ describe('Server', function() {
};
});

it('does not crash on unsub for Object.prototype member', function(done) {
// Use websocket because Client.unsubscribe will only take a number.
const client = new W3CWebSocket(`ws://localhost:${TEST_PORT}/`,
GRAPHQL_SUBSCRIPTIONS);
client.onopen = () => {
client.send(JSON.stringify({type: SUBSCRIPTION_END, id: 'toString'}));
// Strangely we don't send any acknowledgement for unsubbing from an
// unknown sub, so we just set a timeout and implicitly assert that
// there's no uncaught exception within the server code.
setTimeout(done, 10);
};
});

it('sends back any type of error', function(done) {
const client = new Client(`ws://localhost:${TEST_PORT}/`);
client.subscribe({
Expand Down

0 comments on commit ac56986

Please sign in to comment.