fix: Livechat widget connecting to localhost instead of actual site url (#36332)

pull/36343/head^2
Kevin Aleman 6 months ago committed by GitHub
parent 8290892af5
commit 0d2df48042
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 5
      .changeset/late-donkeys-beg.md
  2. 4
      apps/meteor/app/livechat/server/livechat.ts
  3. 35
      apps/meteor/tests/unit/app/livechat/lib/assets.spec.ts

@ -0,0 +1,5 @@
---
"@rocket.chat/meteor": patch
---
Fixes livechat widget attempting to connect to localhost instead of actual site url

@ -7,7 +7,7 @@ import { WebApp } from 'meteor/webapp';
import { settings } from '../../settings/server';
import { addServerUrlToIndex } from '../lib/Assets';
const indexHtmlWithServerURL = addServerUrlToIndex((await Assets.getTextAsync('livechat/index.html')) || '');
const indexHtmlWithServerURL = await Assets.getTextAsync('livechat/index.html');
function parseExtraAttributes(widgetData: string): string {
const liveChatAdditionalScripts = settings.get<string>('Livechat_AdditionalWidgetScripts');
@ -66,6 +66,6 @@ WebApp.connectHandlers.use('/livechat', (req, res, next) => {
res.removeHeader('Content-Security-Policy');
}
res.write(memoizedParseExtraAttributes(indexHtmlWithServerURL));
res.write(memoizedParseExtraAttributes(addServerUrlToIndex(indexHtmlWithServerURL || '')));
res.end();
});

@ -0,0 +1,35 @@
import { expect } from 'chai';
import { describe, it, before, after } from 'mocha';
import { addServerUrlToIndex } from '../../../../../app/livechat/lib/Assets';
describe('addServerUrlToIndex', () => {
before(() => {
(global as any).__meteor_runtime_config__ = { ROOT_URL: 'http://localhost:3000' };
});
after(() => {
delete (global as any).__meteor_runtime_config__;
});
it('should do nothing if "file" has no <body> tag', () => {
const file = addServerUrlToIndex('file');
expect(file).to.equal('file');
});
it('should replace <body> with an script tag containing the SERVER_URL inside', () => {
const file = addServerUrlToIndex('<html><body></body></html>');
expect(file).to.equal("<html><body><script> SERVER_URL = 'http://localhost:3000'; </script></body></html>");
});
it('should remove any trailing / from the SERVER_URL', () => {
(global as any).__meteor_runtime_config__ = { ROOT_URL: 'http://localhost:3000/' };
const file = addServerUrlToIndex('<html><body></body></html>');
expect(file).to.equal("<html><body><script> SERVER_URL = 'http://localhost:3000'; </script></body></html>");
});
it('should use the value from the global variable always', () => {
const file = addServerUrlToIndex('<html><body></body></html>');
expect(file).to.equal("<html><body><script> SERVER_URL = 'http://localhost:3000'; </script></body></html>");
(global as any).__meteor_runtime_config__ = { ROOT_URL: 'http://kodiak.rocket.chat/' };
const file2 = addServerUrlToIndex('<html><body></body></html>');
expect(file2).to.equal("<html><body><script> SERVER_URL = 'http://kodiak.rocket.chat'; </script></body></html>");
});
});
Loading…
Cancel
Save