Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Verify that the external_id is somewhat secure/random before using #26

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
15 changes: 9 additions & 6 deletions components/ChatWindow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
shorten,
shouldActivateGameMode,
setupPostMessageHandlers,
isSecureExternalId,
} from '../helpers/utils';
import {isDev, getWebsocketUrl} from '../helpers/config';
import Logger from '../helpers/logger';
Expand Down Expand Up @@ -323,7 +324,11 @@ class ChatWindow extends React.Component<Props, State> {
metadata?: API.CustomerMetadata,
defaultCustomerId?: string | null
): Promise<string | null> => {
if (!metadata || !metadata?.external_id) {
if (
!metadata ||
!metadata?.external_id ||
!isSecureExternalId(metadata?.external_id)
) {
this.setState({customerId: defaultCustomerId});

return defaultCustomerId;
Expand All @@ -343,11 +348,9 @@ class ChatWindow extends React.Component<Props, State> {
baseUrl
);

if (!matchingCustomerId) {
this.setState({customerId: null});

return null;
} else if (matchingCustomerId === defaultCustomerId) {
// TODO: should we just set customerId to null if no match is found?
// We currently just fall back to the cached value if no match is found
if (!matchingCustomerId || matchingCustomerId === defaultCustomerId) {
this.setState({customerId: defaultCustomerId});

return defaultCustomerId;
Expand Down
14 changes: 14 additions & 0 deletions helpers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,20 @@ export function shouldActivateGameMode(message: string) {
);
}

export function isSecureExternalId(id?: any) {
if (!id) {
return false;
} else if (typeof id !== 'string') {
return false;
}

const isLongEnough = id.length >= 8;
const isOnlyNumbers = /^\d+$/.test(id);
const isOnlyLetters = /^[a-zA-Z]+$/.test(id);

return isLongEnough && !isOnlyNumbers && !isOnlyLetters;
}

export function setupPostMessageHandlers(w: any, handler: (msg: any) => void) {
const cb = (msg: any) => {
handler(msg);
Expand Down