From 04b29f2845cc9dddee492be7cc7aa0227d03474b Mon Sep 17 00:00:00 2001 From: "dionisio-bot[bot]" <117394943+dionisio-bot[bot]@users.noreply.github.com> Date: Wed, 19 Jun 2024 00:01:52 -0400 Subject: [PATCH] fix: Supported Versions misbehaving (#32630) --- .changeset/dry-shoes-tap.md | 5 ++++ .../supportedVersionsChooseLatest.ts | 2 +- .../supportedVersionsToken.ts | 26 +++++++++++++++++++ apps/meteor/jest.config.ts | 5 +++- .../plugin/compile-version.js | 23 ++++++++++++---- 5 files changed, 54 insertions(+), 7 deletions(-) create mode 100644 .changeset/dry-shoes-tap.md diff --git a/.changeset/dry-shoes-tap.md b/.changeset/dry-shoes-tap.md new file mode 100644 index 000000000000..f5abf51c0df0 --- /dev/null +++ b/.changeset/dry-shoes-tap.md @@ -0,0 +1,5 @@ +--- +"@rocket.chat/meteor": patch +--- + +Fixes the supported versions problem, where in most cases the data chosen was the oldest diff --git a/apps/meteor/app/cloud/server/functions/supportedVersionsToken/supportedVersionsChooseLatest.ts b/apps/meteor/app/cloud/server/functions/supportedVersionsToken/supportedVersionsChooseLatest.ts index 3493401144cf..32753ba00429 100644 --- a/apps/meteor/app/cloud/server/functions/supportedVersionsToken/supportedVersionsChooseLatest.ts +++ b/apps/meteor/app/cloud/server/functions/supportedVersionsToken/supportedVersionsChooseLatest.ts @@ -2,7 +2,7 @@ import type { SignedSupportedVersions } from '@rocket.chat/server-cloud-communic export const supportedVersionsChooseLatest = async (...tokens: (SignedSupportedVersions | undefined)[]) => { const [token] = (tokens.filter((r) => r?.timestamp != null) as SignedSupportedVersions[]).sort((a, b) => { - return new Date(a.timestamp).getTime() - new Date(b.timestamp).getTime(); + return new Date(b.timestamp).getTime() - new Date(a.timestamp).getTime(); }); return token; diff --git a/apps/meteor/app/cloud/server/functions/supportedVersionsToken/supportedVersionsToken.ts b/apps/meteor/app/cloud/server/functions/supportedVersionsToken/supportedVersionsToken.ts index dd933107d57c..c4cc506d9175 100644 --- a/apps/meteor/app/cloud/server/functions/supportedVersionsToken/supportedVersionsToken.ts +++ b/apps/meteor/app/cloud/server/functions/supportedVersionsToken/supportedVersionsToken.ts @@ -62,6 +62,7 @@ const cacheValueInSettings = ( reset: () => Promise; } => { const reset = async () => { + SystemLogger.debug(`Resetting cached value ${key} in settings`); const value = await fn(); await Settings.updateValueById(key, value); @@ -134,6 +135,31 @@ const getSupportedVersionsToken = async () => { (response.success && response.result) || undefined, ); + SystemLogger.debug({ + msg: 'Supported versions', + supportedVersionsFromBuild: supportedVersionsFromBuild.timestamp, + versionsFromLicense: versionsFromLicense?.supportedVersions?.timestamp, + response: response.success && response.result?.timestamp, + }); + + switch (supportedVersions) { + case supportedVersionsFromBuild: + SystemLogger.info({ + msg: 'Using supported versions from build', + }); + break; + case versionsFromLicense?.supportedVersions: + SystemLogger.info({ + msg: 'Using supported versions from license', + }); + break; + case response.success && response.result: + SystemLogger.info({ + msg: 'Using supported versions from cloud', + }); + break; + } + await buildVersionUpdateMessage(supportedVersions?.versions); return supportedVersions?.signed; diff --git a/apps/meteor/jest.config.ts b/apps/meteor/jest.config.ts index 81938441d722..edca9027a284 100644 --- a/apps/meteor/jest.config.ts +++ b/apps/meteor/jest.config.ts @@ -27,7 +27,10 @@ const config: Config = { { displayName: 'server', testEnvironment: 'node', - testMatch: ['/ee/app/authorization/server/validateUserRoles.spec.ts'], + testMatch: [ + '/ee/app/authorization/server/validateUserRoles.spec.ts', + '/app/cloud/server/functions/supportedVersionsToken/**.spec.ts', + ], transformIgnorePatterns: ['!/node_modules/jose'], errorOnDeprecated: true, diff --git a/apps/meteor/packages/rocketchat-version/plugin/compile-version.js b/apps/meteor/packages/rocketchat-version/plugin/compile-version.js index d22aa55437ff..0b7c85a15e2f 100644 --- a/apps/meteor/packages/rocketchat-version/plugin/compile-version.js +++ b/apps/meteor/packages/rocketchat-version/plugin/compile-version.js @@ -21,10 +21,10 @@ class VersionCompiler { function handleError(err) { console.error(err); // TODO remove this when we are ready to fail - // if (process.env.NODE_ENV !== 'development') { - // reject(err); - // return; - // } + if (process.env.NODE_ENV !== 'development') { + reject(err); + return; + } resolve({}); } @@ -34,11 +34,24 @@ class VersionCompiler { response.on('data', function (chunk) { data += chunk; }); - response.on('end', function () { + response.on('end', async function () { const supportedVersions = JSON.parse(data); if (!supportedVersions?.signed) { return handleError(new Error(`Invalid supportedVersions result:\n URL: ${url} \n RESULT: ${data}`)); } + + // check if timestamp is inside 1 hour within build + if (Math.abs(new Date().getTime() - new Date(supportedVersions.timestamp).getTime()) > 1000 * 60 * 60) { + return handleError(new Error(`Invalid supportedVersions timestamp:\n URL: ${url} \n RESULT: ${data}`)); + } + + for await (const version of supportedVersions.versions) { + // check if expiration is after the first rocket.chat release + if (new Date(version.expiration) < new Date('2019-04-01T00:00:00.000Z')) { + return handleError(new Error(`Invalid supportedVersions expiration:\n URL: ${url} \n RESULT: ${data}`)); + } + } + resolve(supportedVersions); }); response.on('error', function (err) {