From 814b9874d17c3a79170633bbf0d96ae842886ee9 Mon Sep 17 00:00:00 2001 From: Evan Hahn <69474926+EvanHahn-Signal@users.noreply.github.com> Date: Thu, 12 Nov 2020 17:29:54 -0600 Subject: [PATCH] Add OS to User-Agent --- ts/test/util/getUserAgent_test.ts | 37 +++++++++++++++++++++++++++++++ ts/util/getUserAgent.ts | 16 ++++++++++++- 2 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 ts/test/util/getUserAgent_test.ts diff --git a/ts/test/util/getUserAgent_test.ts b/ts/test/util/getUserAgent_test.ts new file mode 100644 index 0000000000..45527d600d --- /dev/null +++ b/ts/test/util/getUserAgent_test.ts @@ -0,0 +1,37 @@ +// Copyright 2020 Signal Messenger, LLC +// SPDX-License-Identifier: AGPL-3.0-only + +import { assert } from 'chai'; +import * as sinon from 'sinon'; + +import { getUserAgent } from '../../util/getUserAgent'; + +describe('getUserAgent', () => { + beforeEach(function beforeEach() { + this.sandbox = sinon.createSandbox(); + }); + + afterEach(function afterEach() { + this.sandbox.restore(); + }); + + it('returns the right User-Agent on Windows', function test() { + this.sandbox.stub(process, 'platform').get(() => 'win32'); + assert.strictEqual(getUserAgent('1.2.3'), 'Signal-Desktop/1.2.3 Windows'); + }); + + it('returns the right User-Agent on macOS', function test() { + this.sandbox.stub(process, 'platform').get(() => 'darwin'); + assert.strictEqual(getUserAgent('1.2.3'), 'Signal-Desktop/1.2.3 macOS'); + }); + + it('returns the right User-Agent on Linux', function test() { + this.sandbox.stub(process, 'platform').get(() => 'linux'); + assert.strictEqual(getUserAgent('1.2.3'), 'Signal-Desktop/1.2.3 Linux'); + }); + + it('omits the platform on unsupported platforms', function test() { + this.sandbox.stub(process, 'platform').get(() => 'freebsd'); + assert.strictEqual(getUserAgent('1.2.3'), 'Signal-Desktop/1.2.3'); + }); +}); diff --git a/ts/util/getUserAgent.ts b/ts/util/getUserAgent.ts index fdc2942178..55b4e07dab 100644 --- a/ts/util/getUserAgent.ts +++ b/ts/util/getUserAgent.ts @@ -1,6 +1,20 @@ // Copyright 2020 Signal Messenger, LLC // SPDX-License-Identifier: AGPL-3.0-only +import { getOwn } from './getOwn'; + +const PLATFORM_STRINGS: { [platform: string]: string } = { + win32: 'Windows', + darwin: 'macOS', + linux: 'Linux', +}; + export function getUserAgent(appVersion: string): string { - return `Signal-Desktop/${appVersion}`; + // `process.platform` could be missing if someone figures out how to compile Signal on + // an unsupported OS and forgets to update this file. We'd rather send nothing than + // crash. + const platformString = getOwn(PLATFORM_STRINGS, process.platform); + const platformStringWithSpace = platformString ? ` ${platformString}` : ''; + + return `Signal-Desktop/${appVersion}${platformStringWithSpace}`; }