Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

5.8 KB
Raw Blame
import { httpClient, HttpMethod } from '@activepieces/pieces-common';
import { isNil, tryCatch } from '@activepieces/pieces-framework';
import crypto from 'crypto';

async function sendRow({
    webhookUrl,
    authToken,
    row,
}: {
    webhookUrl: string;
    authToken?: string;
    row: Record<string, unknown>;
}): Promise<ClayWebhookResult> {
    const url = normalizeSourceUrl(webhookUrl);
    const token = trimmedSecretOf(authToken);
    const result = await tryCatch(() =>
        httpClient.sendRequest<unknown>({
            method: HttpMethod.POST,
            url,
            headers: isNil(token)
                ? undefined
                : { [WEBHOOK_AUTH_HEADER]: token },
            body: row,
        }),
    );

    if (result.error !== null) {
        throw new Error(sendFailureMessage({ error: result.error, authToken: token }));
    }

    return { success: true, response: result.data.body };
}

function verifySignature({
    signingSecret,
    rawBody,
    signatureHeader,
}: {
    signingSecret: string;
    rawBody: unknown;
    signatureHeader: string | undefined;
}): boolean {
    const signedBytes = signedPayloadOf(rawBody);
    if (isNil(signatureHeader) || isNil(signedBytes)) {
        return false;
    }

    const provided = signatureHeader.startsWith(SIGNATURE_PREFIX)
        ? signatureHeader.slice(SIGNATURE_PREFIX.length)
        : signatureHeader;
    const expected = crypto
        .createHmac('sha256', signingSecret)
        .update(signedBytes)
        .digest('hex');

    const providedBytes = Buffer.from(provided, 'hex');
    const expectedBytes = Buffer.from(expected, 'hex');
    if (
        providedBytes.length === 0 ||
        providedBytes.length !== expectedBytes.length
    ) {
        return false;
    }

    return crypto.timingSafeEqual(providedBytes, expectedBytes);
}

function signatureHeaderOf(headers: Record<string, string> | undefined): string | undefined {
    if (isNil(headers)) {
        return undefined;
    }
    const match = Object.keys(headers).find(
        (name) => name.toLowerCase() === SIGNATURE_HEADER,
    );
    return isNil(match) ? undefined : headers[match];
}

function isVerificationPing(body: unknown): boolean {
    if (typeof body !== 'object' || isNil(body)) {
        return false;
    }
    if (isNil(Reflect.get(body, 'webhookId'))) {
        return false;
    }
    const row = Reflect.get(body, 'data');
    if (isNil(row)) {
        return true;
    }
    return typeof row === 'object' && Object.keys(row).length === 0;
}

function signedPayloadOf(rawBody: unknown): string | Buffer | undefined {
    if (typeof rawBody === 'string' || Buffer.isBuffer(rawBody)) {
        return rawBody;
    }
    return undefined;
}

function normalizeSourceUrl(webhookUrl: string): string {
    const trimmed = webhookUrl.trim();
    if (trimmed.length === 0) {
        throw new Error('Webhook URL is required');
    }

    const parsed = parsedUrlOf(trimmed);
    if (isNil(parsed)) {
        throw new Error(
            `Webhook URL is not a valid URL. ${COPY_FROM_CLAY}`,
        );
    }
    if (parsed.protocol !== 'https:') {
        throw new Error(
            `Webhook URL must use https, because the row and the source token are sent to it. ${COPY_FROM_CLAY}`,
        );
    }
    if (!isClayHost(parsed.hostname)) {
        throw new Error(
            `Webhook URL must point at Clay, but this one points at ${parsed.hostname}. The row and the source token are sent to this address, so anywhere else is refused. ${COPY_FROM_CLAY}`,
        );
    }

    return trimmed;
}

function trimmedSecretOf(secret: string | undefined): string | undefined {
    const trimmed = secret?.trim();
    return isNil(trimmed) || trimmed.length === 0 ? undefined : trimmed;
}

function parsedUrlOf(value: string): URL | undefined {
    try {
        return new URL(value);
    }
    catch {
        return undefined;
    }
}

function isClayHost(hostname: string): boolean {
    const host = hostname.toLowerCase();
    return host === CLAY_DOMAIN || host.endsWith(`.${CLAY_DOMAIN}`);
}

function sendFailureMessage({
    error,
    authToken,
}: {
    error: Error;
    authToken?: string;
}): string {
    const status = statusOf(error);

    if (status === 401) {
        return isNil(authToken)
            ? 'Clay rejected the row with 401 Unauthorized. This webhook source has an authentication token, so fill in Authentication Token with the value Clay showed when the source was created.'
            : 'Clay rejected the row with 401 Unauthorized. The authentication token does not match the one on this Clay webhook source. Note that a newly refreshed token can take up to a minute to become active.';
    }
    if (status === 404) {
        return 'Clay returned 404 for this webhook URL. Check the URL against the Webhook URL panel on the table source, and that the source has not been deleted.';
    }

    return `Clay rejected the row${isNil(status) ? '' : ` with ${status}`}: ${error.message}`;
}

function statusOf(error: unknown): number | undefined {
    if (typeof error !== 'object' || isNil(error)) {
        return undefined;
    }
    const response = Reflect.get(error, 'response');
    if (typeof response !== 'object' || isNil(response)) {
        return undefined;
    }
    const status = Reflect.get(response, 'status');
    return typeof status === 'number' ? status : undefined;
}

export const clayWebhook = {
    sendRow,
    verifySignature,
    signatureHeaderOf,
    isVerificationPing,
    normalizeSourceUrl,
    trimmedSecretOf,
};

export const WEBHOOK_AUTH_HEADER = 'x-clay-webhook-auth';
export const SIGNATURE_HEADER = 'x-clay-signature';
export const SIGNATURE_PREFIX = 'sha256=';
export const CLAY_DOMAIN = 'clay.com';
export const COPY_FROM_CLAY =
    'Copy it from the Webhook URL panel on your Clay table source.';

export type ClayWebhookResult = {
    success: boolean;
    response: unknown;
};