Files
upstream-mirror 30810eecd7
Some checks failed
Create issues from TODOs / todos (push) Failing after 3s
Close inactive issues / close-issues (push) Has been cancelled
Deploy GitHub Pages / deploy-preview (push) Has been cancelled
CI / ⚙️ Variables Setup (push) Has been cancelled
CI / ⚙️ Test Guard (push) Has been cancelled
CI / 🚀 Notify external services - draft (push) Has been cancelled
CI / 📦 Build Packages (push) Has been cancelled
CI / 📦 Meteor Build (coverage) (push) Has been cancelled
CI / 📦 Meteor Build (production) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], fips) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Publish Docker Images (ghcr.io) (push) Has been cancelled
CI / 📦 Track Image Sizes (push) Has been cancelled
CI / 🔎 Code Check - Action Lint (push) Has been cancelled
CI / 🔎 Code Check (push) Has been cancelled
CI / 🔨 Test Storybook (push) Has been cancelled
CI / 🔨 Test Unit (push) Has been cancelled
CI / 🔨 Test API (CE) (push) Has been cancelled
CI / 🔨 Test API Livechat (CE) (push) Has been cancelled
CI / 🔨 Test UI (CE) (push) Has been cancelled
CI / 🔨 Test API (EE) (push) Has been cancelled
CI / 🔨 Test API Livechat (EE) (push) Has been cancelled
CI / 🔨 Test API Apps (deno-runtime - EE) (push) Has been cancelled
CI / 🔨 Test UI (EE) (push) Has been cancelled
CI / 🔨 Test API (FIPS) (push) Has been cancelled
CI / 🔨 Test API Livechat (FIPS) (push) Has been cancelled
CI / 🔨 Test UI (FIPS) (push) Has been cancelled
CI / 🔨 Test Federation Matrix (push) Has been cancelled
CI / 📊 Report Coverage (push) Has been cancelled
CI / ✅ Tests Done (push) Has been cancelled
CI / 🚀 Publish build assets (push) Has been cancelled
CI / 🚀 Publish Docker Images (DockerHub) (push) Has been cancelled
CI / 🚀 Notify external services (push) Has been cancelled
CI / Update Version Durability (push) Has been cancelled
Release / Release (push) Has been cancelled
Auto-close duplicate issues / auto-close-duplicates (push) Failing after 1s
Code scanning - action / CodeQL-Build (push) Failing after 2s
snapshot of https://github.com/RocketChat/Rocket.Chat.git (develop, 2026-09-16; blobs > 131072B omitted)
2026-09-16 01:21:02 +05:00

93 lines
3.0 KiB
TypeScript

import { Base64 } from '@rocket.chat/base64';
import type { EncryptedContent } from '@rocket.chat/core-typings';
type DecodedContent = {
kid: string;
iv: Uint8Array<ArrayBuffer>;
ciphertext: Uint8Array<ArrayBuffer>;
};
interface ISlice {
slice(start: number, end?: number): this;
}
/**
* Splits a slice of data into two parts at the specified index.
* @param data The data to split.
* @param index The index at which to split the data.
* @returns A tuple containing the two parts of the split data.
*/
const split = <T extends ISlice>(data: T, index: number): [T, T] => [data.slice(0, index), data.slice(index)];
/**
* Decodes an encrypted content string in the format "kid + base64(iv + ciphertext)".
* @param payload The encrypted content string.
* @returns An object containing the key ID (kid), initialization vector (iv), and ciphertext.
*/
const decodeV1EncryptedContent = (
payload: Omit<Extract<EncryptedContent, { algorithm: 'rc.v1.aes-sha2' }>, 'algorithm'>,
): DecodedContent => {
if (payload.ciphertext.length < 12) {
throw new Error('Invalid v1 ciphertext: too short for kid extraction');
}
const [kid, base64] = split(payload.ciphertext, 12);
const decoded = Base64.decode(base64);
if (decoded.length < 16) {
throw new Error('Invalid v1 ciphertext: too short for iv extraction');
}
const [iv, ciphertext] = split(decoded, 16);
return {
kid,
iv,
ciphertext,
};
};
/**
* Decodes an encrypted content object with separate fields for kid, iv, and ciphertext.
* @param payload The encrypted content object.
* @returns An object containing the key ID (kid), initialization vector (iv), and ciphertext.
*/
const decodeV2EncryptedContent = (payload: Extract<EncryptedContent, { algorithm: 'rc.v2.aes-sha2' }>): DecodedContent => {
if (!payload.kid || !payload.iv || !payload.ciphertext) {
throw new Error('Invalid v2 payload: kid, iv, and ciphertext must be non-empty');
}
const iv = Base64.decode(payload.iv);
const ciphertext = Base64.decode(payload.ciphertext);
return { kid: payload.kid, iv, ciphertext };
};
const normalizePayload = (payload: string | EncryptedContent): EncryptedContent => {
if (typeof payload === 'string') {
return { algorithm: 'rc.v1.aes-sha2', ciphertext: payload } as const;
}
return payload;
};
/**
* Parses encrypted content from either a string or an object and decodes it into its components.
* @param payload The encrypted content, either as a string or an object.
* @returns An object containing the key ID (kid), initialization vector (iv), and ciphertext.
* @throws Will throw an error if the encryption algorithm is unsupported.
*/
export const decodeEncryptedContent = (payload: string | EncryptedContent): DecodedContent => {
payload = normalizePayload(payload);
const { algorithm } = payload;
switch (algorithm) {
case 'rc.v1.aes-sha2':
return decodeV1EncryptedContent(payload);
case 'rc.v2.aes-sha2':
return decodeV2EncryptedContent(payload);
default:
throw new Error(`Unsupported encryption algorithm: ${algorithm}`);
}
};