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
119 lines
4.1 KiB
TypeScript
119 lines
4.1 KiB
TypeScript
import type { IUser } from '@rocket.chat/core-typings';
|
|
|
|
import type { ActionThis } from './definition';
|
|
import { hasAllPermissionAsync, hasAtLeastOnePermissionAsync } from '../lib/authorization/hasPermission';
|
|
import type { DeprecationLoggerNextPlannedVersion } from '../lib/deprecationWarningLogger';
|
|
import { apiDeprecationLogger } from '../lib/deprecationWarningLogger';
|
|
|
|
type RequestMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | '*';
|
|
export type PermissionsPayload = {
|
|
[key in RequestMethod]?: {
|
|
operation: 'hasAll' | 'hasAny';
|
|
permissions: string[];
|
|
};
|
|
};
|
|
|
|
type PermissionsPayloadLight = {
|
|
[key in RequestMethod]?: string[];
|
|
};
|
|
|
|
type PermissionsRequiredKey = string[] | PermissionsPayload | PermissionsPayloadLight;
|
|
|
|
const isLegacyPermissionsPayload = (permissionsPayload: PermissionsRequiredKey): permissionsPayload is string[] => {
|
|
return Array.isArray(permissionsPayload);
|
|
};
|
|
|
|
const isLightPermissionsPayload = (permissionsPayload: PermissionsRequiredKey): permissionsPayload is PermissionsPayloadLight => {
|
|
return (
|
|
typeof permissionsPayload === 'object' &&
|
|
Object.keys(permissionsPayload).some((key) => ['GET', 'POST', 'PUT', 'DELETE', '*'].includes(key.toUpperCase())) &&
|
|
Object.values(permissionsPayload).every((value) => Array.isArray(value))
|
|
);
|
|
};
|
|
|
|
const isPermissionsPayload = (permissionsPayload: PermissionsRequiredKey): permissionsPayload is PermissionsPayload => {
|
|
return (
|
|
typeof permissionsPayload === 'object' &&
|
|
Object.keys(permissionsPayload).some((key) => ['GET', 'POST', 'PUT', 'DELETE', '*'].includes(key.toUpperCase())) &&
|
|
Object.values(permissionsPayload).every((value) => typeof value === 'object' && value.operation && value.permissions)
|
|
);
|
|
};
|
|
|
|
export async function canBypassRateLimit(userId: IUser['_id'], bypassPermissions: string[] = []): Promise<boolean> {
|
|
return hasAtLeastOnePermissionAsync(userId, ['api-bypass-rate-limit', ...bypassPermissions]);
|
|
}
|
|
|
|
export async function checkPermissionsForInvocation(
|
|
userId: IUser['_id'],
|
|
permissionsPayload: PermissionsPayload,
|
|
requestMethod: RequestMethod,
|
|
): Promise<boolean> {
|
|
const permissions = permissionsPayload[requestMethod] || permissionsPayload['*'];
|
|
|
|
if (!permissions) {
|
|
// how we reached here in the first place?
|
|
return false;
|
|
}
|
|
|
|
if (permissions.permissions.length === 0) {
|
|
// You can pass an empty array of permissions to allow access to the method
|
|
return true;
|
|
}
|
|
|
|
if (permissions.operation === 'hasAll') {
|
|
return hasAllPermissionAsync(userId, permissions.permissions);
|
|
}
|
|
|
|
if (permissions.operation === 'hasAny') {
|
|
return hasAtLeastOnePermissionAsync(userId, permissions.permissions);
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
// We'll assume options only contains permissionsRequired, as we don't care of the other elements
|
|
export function checkPermissions(options: { permissionsRequired?: PermissionsRequiredKey }) {
|
|
if (!options.permissionsRequired) {
|
|
return false;
|
|
}
|
|
|
|
if (isPermissionsPayload(options.permissionsRequired)) {
|
|
// No modifications needed
|
|
return true;
|
|
}
|
|
|
|
if (isLegacyPermissionsPayload(options.permissionsRequired)) {
|
|
options.permissionsRequired = {
|
|
'*': {
|
|
operation: 'hasAll',
|
|
permissions: options.permissionsRequired,
|
|
},
|
|
};
|
|
return true;
|
|
}
|
|
|
|
if (isLightPermissionsPayload(options.permissionsRequired)) {
|
|
Object.keys(options.permissionsRequired).forEach((method) => {
|
|
const methodKey = method as RequestMethod;
|
|
// @ts-expect-error -- we know the type of the value but ts refuses to infer it
|
|
options.permissionsRequired[methodKey] = {
|
|
operation: 'hasAll',
|
|
// @ts-expect-error -- we know the type of the value but ts refuses to infer it
|
|
permissions: options.permissionsRequired[methodKey],
|
|
};
|
|
});
|
|
return true;
|
|
}
|
|
|
|
// If reached here, options.permissionsRequired contained an invalid payload
|
|
return false;
|
|
}
|
|
|
|
export function parseDeprecation(
|
|
methodThis: ActionThis<any, any, any>,
|
|
{ alternatives, version }: { version: DeprecationLoggerNextPlannedVersion; alternatives?: string[] },
|
|
): void {
|
|
const infoMessage = alternatives?.length ? ` Please use the alternative(s): ${alternatives.join(',')}` : '';
|
|
apiDeprecationLogger.endpoint(methodThis.route, version, methodThis.response, infoMessage);
|
|
}
|