Skip to content

Commit

Permalink
MM-9973: ignore dispatched events against unregistered suggestion box…
Browse files Browse the repository at this point in the history
…es (mattermost#1052)

In a slow network environment, typing a channel name, then erasing it,
then quickly typing again and hitting enter to unmount the suggestion
box could result in a previous network call eventually dispatching an
event for the now unregistered suggestion box. If the dispatched event
was for an empty string, it would incorrectly try to handle that,
ultimately attempting to dereference `undefined.push`.
  • Loading branch information
lieut-data authored and GoldUniform committed Apr 6, 2018
1 parent d0b92ba commit b1c8b07
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
8 changes: 8 additions & 0 deletions stores/suggestion_store.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ class SuggestionStore extends EventEmitter {
suggestion.selection = '';
}

suggestionBoxExists(id) {
return this.suggestions.has(id);
}

hasSuggestions(id) {
return this.getSuggestions(id).terms.length > 0;
}
Expand Down Expand Up @@ -250,6 +254,10 @@ class SuggestionStore extends EventEmitter {
handleEventPayload(payload) {
const {type, id, ...other} = payload.action;

if (id && !this.suggestionBoxExists(id)) {
return;
}

switch (type) {
case ActionTypes.SUGGESTION_PRETEXT_CHANGED:
// Clear the suggestions if the pretext is empty or ends with whitespace
Expand Down
52 changes: 52 additions & 0 deletions tests/stores/suggestion_store.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

import assert from 'assert';

import {ActionTypes} from 'utils/constants.jsx';
import SuggestionStore from 'stores/suggestion_store.jsx';

describe('stores/SuggestionStore', () => {
test('should ignore events from unknown suggestion boxes', () => {
const id = 'unknown';
const pretext = 'pretext';
SuggestionStore.handleEventPayload({
action: {
id,
type: ActionTypes.SUGGESTION_PRETEXT_CHANGED,
pretext,
},
});
assert.equal(SuggestionStore.suggestions.has(id), false);
});

test('should process events from registered suggestion boxes', () => {
const id = 'id1';
const pretext = 'pretext';
SuggestionStore.registerSuggestionBox(id);
SuggestionStore.handleEventPayload({
action: {
id,
type: ActionTypes.SUGGESTION_PRETEXT_CHANGED,
pretext,
},
});
assert.equal(SuggestionStore.suggestions.get(id).pretext, pretext);
});

test('should ignore events from unregistered suggestion boxes', () => {
const id = 'id1';
const pretext = 'pretext';
SuggestionStore.registerSuggestionBox(id);
SuggestionStore.unregisterSuggestionBox(id);

SuggestionStore.handleEventPayload({
action: {
id,
type: ActionTypes.SUGGESTION_PRETEXT_CHANGED,
pretext,
},
});
assert.equal(SuggestionStore.suggestions.has(id), false);
});
});

0 comments on commit b1c8b07

Please sign in to comment.