Skip to content

Commit be4ef10

Browse files
committed
Change next message functionality to use localStorage to eliminate possibility of XSS vulnerability
1 parent 245ac7f commit be4ef10

File tree

4 files changed

+14
-8
lines changed

4 files changed

+14
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
## 0.5.4
2-
- When reconnecting to an existing chat session, the bot will send any message contained in the `?send=<message>` URL parameter once it has restored the socket.io connection
2+
- When reconnecting to an existing chat session, the bot will send a message contained in the localStorage key specified by the `NEXT_MESSAGE` constant. The message should be stringified JSON with a `message` property describing the message and an `expiry` property set to a UNIX timestamp in milliseconds after which this message should not be sent.
33

44
## 0.5.3
55
- Added the parameter hideWhenNotConnected to not display the widget when the server is not connected (defaults to true)

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ emit('bot_uttered', message, room=socket_id)
210210

211211
## Sending a message on page load
212212

213-
When reconnecting to an existing chat session, the bot will send any message contained in the `?send=<message>` URL parameter once it has restored the socket.io connection. This is useful if you would like your bot to be able to offer your user to navigate around the site.
213+
When reconnecting to an existing chat session, the bot will send a message contained in the localStorage key specified by the `NEXT_MESSAGE` constant. The message should be stringified JSON with a `message` property describing the message and an `expiry` property set to a UNIX timestamp in milliseconds after which this message should not be sent. This is useful if you would like your bot to be able to offer your user to navigate around the site.
214214

215215

216216
## API

src/components/Widget/index.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import {
2222
import { isSnippet, isVideo, isImage, isQR, isText } from './msgProcessor';
2323
import WidgetLayout from './layout';
2424
import { storeLocalSession, getLocalSession } from '../../store/reducers/helper';
25-
import { SESSION_NAME } from 'constants';
25+
import { SESSION_NAME, NEXT_MESSAGE } from 'constants';
2626

2727
class Widget extends Component {
2828

@@ -74,12 +74,16 @@ class Widget extends Component {
7474
} else {
7575
// If this is an existing session, it's possible we changed pages and want to send a
7676
// user message when we land.
77-
const params = (new URL(document.location)).searchParams;
78-
const send = params.get('send');
77+
const nextMessage = window.localStorage.getItem(NEXT_MESSAGE);
7978

80-
if (send) {
81-
this.props.dispatch(addUserMessage(send));
82-
this.props.dispatch(emitUserMessage(send));
79+
if (nextMessage !== null) {
80+
const { message, expiry } = JSON.parse(nextMessage);
81+
window.localStorage.removeItem(NEXT_MESSAGE);
82+
83+
if (expiry === 0 || expiry > Date.now()) {
84+
this.props.dispatch(addUserMessage(message));
85+
this.props.dispatch(emitUserMessage(message));
86+
}
8387
}
8488
}
8589
});

src/constants.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ export const MESSAGES_TYPES = {
2323
CUSTOM_COMPONENT: 'component'
2424
};
2525

26+
export const NEXT_MESSAGE = 'mrbot_next_message';
27+
2628
export const PROP_TYPES = {
2729

2830
MESSAGE: ImmutablePropTypes.contains({

0 commit comments

Comments
 (0)