import type { IUser } from '@rocket.chat/core-typings'; import { License } from '@rocket.chat/license'; import { Users } from '@rocket.chat/models'; import { serverFetch as fetch } from '@rocket.chat/server-fetch'; import { buildWorkspaceRegistrationData } from './buildRegistrationData'; import { settings } from '../../settings'; import { SystemLogger } from '../logger/system'; export async function registerPreIntentWorkspaceWizard(): Promise { if (License.hasOfflineLicense()) { return false; } const firstUser = (await Users.getOldest({ projection: { name: 1, emails: 1 } })) as IUser | undefined; const email = firstUser?.emails?.find((address) => address)?.address; if (!email) { return false; } const regInfo = await buildWorkspaceRegistrationData(email); // Re-validated at dispatch time: an offline license applied while the // registration data was being built must still suppress the request. if (License.hasOfflineLicense()) { return false; } try { const cloudUrl = settings.get('Cloud_Url'); const response = await fetch(`${cloudUrl}/api/v2/register/workspace/pre-intent`, { method: 'POST', body: regInfo, timeout: 3 * 1000, // SECURITY: the URL is a default hardcoded value or an envvar/setting set by an admin. It's safe to disable this check. ignoreSsrfValidation: true, }); if (!response.ok) { throw new Error((await response.json()).error); } return true; } catch (err: any) { SystemLogger.error({ msg: 'Failed to register workspace pre-intent with Rocket.Chat Cloud', url: '/api/v2/register/workspace/pre-intent', err, }); return false; } }