[deploy] Encode domain name in pusher channels (#11136)

Co-authored-by: Nick Taylor <nick@iamdeveloper.com>
This commit is contained in:
Mac Siri 2020-10-28 14:52:38 -04:00 committed by GitHub
parent 034b049e3d
commit 8ba2c03360
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 29 additions and 16 deletions

View file

@ -64,7 +64,7 @@ export default class Chat extends Component {
);
this.state = {
appName: document.body.dataset.appName,
appDomain: document.body.dataset.appDomain,
messages: [],
scrolled: false,
showAlert: false,
@ -116,7 +116,7 @@ export default class Chat extends Component {
isMobileDevice,
channelPaginationNum,
currentUserId,
appName,
appDomain,
messageOffset,
} = this.state;
@ -127,13 +127,13 @@ export default class Chat extends Component {
);
this.subscribeChannelsToPusher(
channelsForPusherSub,
(channel) => `open-channel--${appName}-${channel.chat_channel_id}`,
(channel) => `open-channel--${appDomain}-${channel.chat_channel_id}`,
);
setupObserver(this.observerCallback);
this.subscribePusher(
`private-message-notifications--${appName}-${currentUserId}`,
`private-message-notifications--${appDomain}-${currentUserId}`,
);
if (activeChannelId) {
@ -236,7 +236,7 @@ export default class Chat extends Component {
messageOpened = () => {};
loadChannels = (channels, query) => {
const { activeChannelId, appName } = this.state;
const { activeChannelId, appDomain } = this.state;
const activeChannel =
this.state.activeChannel ||
channels.filter(
@ -285,11 +285,11 @@ export default class Chat extends Component {
}
this.subscribeChannelsToPusher(
channels.filter(this.channelTypeFilterFn('open')),
(channel) => `open-channel--${appName}-${channel.chat_channel_id}`,
(channel) => `open-channel--${appDomain}-${channel.chat_channel_id}`,
);
this.subscribeChannelsToPusher(
channels.filter(this.channelTypeFilterFn('invite_only')),
(channel) => `private-channel--${appName}-${channel.chat_channel_id}`,
(channel) => `private-channel--${appDomain}-${channel.chat_channel_id}`,
);
const chatChannelsList = document.getElementById(
'chatchannels__channelslist',
@ -346,7 +346,7 @@ export default class Chat extends Component {
};
setupChannel = (channelId) => {
const { messages, messageOffset, activeChannel, appName } = this.state;
const { messages, messageOffset, activeChannel, appDomain } = this.state;
if (
!messages[channelId] ||
messages[channelId].length === 0 ||
@ -361,9 +361,9 @@ export default class Chat extends Component {
null,
);
if (activeChannel.channel_type === 'open')
this.subscribePusher(`open-channel--${appName}-${channelId}`);
this.subscribePusher(`open-channel--${appDomain}-${channelId}`);
}
this.subscribePusher(`private-channel--${appName}-${channelId}`);
this.subscribePusher(`private-channel--${appDomain}-${channelId}`);
};
setOpenChannelUsers = (res) => {

View file

@ -23,9 +23,9 @@ class UnopenedChannelNotice extends Component {
componentDidMount() {
const { pusherKey } = this.props;
const appName = document.body.dataset.appName;
const { appDomain } = document.body.dataset;
setupPusher(pusherKey, {
channelId: `private-message-notifications--${appName}-${window.currentUser.id}`,
channelId: `private-message-notifications--${appDomain}-${window.currentUser.id}`,
messageCreated: this.receiveNewMessage,
messageDeleted: this.removeMessage,
messageEdited: this.updateMessage,

View file

@ -114,9 +114,9 @@ class ChatChannel < ApplicationRecord
def pusher_channels
# TODO: use something more unique here (uuid?) rather than just id.
if invite_only?
"private-channel--#{ApplicationConfig['APP_DOMAIN']}-#{id}"
"private-channel--#{ChatChannel.urlsafe_encoded_app_domain}-#{id}"
elsif open?
"open-channel--#{ApplicationConfig['APP_DOMAIN']}-#{id}"
"open-channel--#{ChatChannel.urlsafe_encoded_app_domain}-#{id}"
else
chat_channel_memberships.pluck(:user_id).map { |id| ChatChannel.pm_notifications_channel(id) }
end
@ -166,7 +166,11 @@ class ChatChannel < ApplicationRecord
end
def self.pm_notifications_channel(user_id)
"private-message-notifications--#{ApplicationConfig['APP_DOMAIN']}-#{user_id}"
"private-message-notifications--#{urlsafe_encoded_app_domain}-#{user_id}"
end
def self.urlsafe_encoded_app_domain
Base64.urlsafe_encode64(ApplicationConfig["APP_DOMAIN"])
end
private

View file

@ -58,7 +58,7 @@
data-user-status="<%= user_logged_in_status %>"
class="<%= SiteConfig.default_font.tr("_", "-") %>-article-body default-header"
data-pusher-key="<%= ApplicationConfig["PUSHER_KEY"] %>"
data-app-name="<%= ApplicationConfig["APP_DOMAIN"] %>"
data-app-domain="<%= ChatChannel.urlsafe_encoded_app_domain %>"
data-honeybadger-key="<%= ApplicationConfig["HONEYBADGER_JS_API_KEY"] %>"
data-release-footprint="<%= ApplicationConfig["RELEASE_FOOTPRINT"] %>"
data-ga-tracking="<%= SiteConfig.ga_tracking_id %>">

View file

@ -80,4 +80,13 @@ RSpec.describe ChatChannel, type: :model do
expect(chat_channel.private_org_channel?).to be(false)
end
end
describe "::urlsafe_encoded_app_domain" do
it "uses urlsafe base64 encoding" do
domain_name = "community.snyk.io"
allow(ApplicationConfig).to receive(:[]).with("APP_DOMAIN").and_return(domain_name)
encoded_domain_name = described_class.urlsafe_encoded_app_domain
expect(Base64.urlsafe_decode64(encoded_domain_name)).to eq(domain_name)
end
end
end