Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

6.2 KB
Raw Blame
// SPDX-License-Identifier: AGPL-3.0-only
// Carbon (github.com/crbnos/carbon). Modified or adapted versions of this file,
// including ports, remain AGPLv3; serving them over a network requires releasing their source.

import { getCarbonServiceRole } from "@carbon/auth/client.server";
import {
  completeWebhookVerification,
  getRampIntegration,
  RampWebhookEventSchema,
  verifyRampWebhookSignature
} from "@carbon/ee/ramp.server";
import { trigger } from "@carbon/jobs";
import { getLogger } from "@carbon/logger";
import type { ActionFunctionArgs, LoaderFunctionArgs } from "react-router";
import { data } from "react-router";

// Needs node:crypto for the constant-time HMAC comparison in
// verifyRampWebhookSignature.
export const config = { runtime: "nodejs" };

const logger = getLogger("erp", "webhook-ramp");

/**
 * Receives Ramp webhook deliveries for one company. Ramp is registered against
 * `${origin}/api/webhook/ramp/${companyId}` by `ensureRampWebhook` (the install
 * hook). A delivery is only a *nudge*: it triggers the same `ramp-sync` job the
 * hourly `ramp-sweep` cron fires, so the sync body itself is the source of truth
 * and the webhook is pure latency (a lost delivery becomes ≤1h of staleness, not
 * lost data). We ack fast; the job drains the SYNC_READY queues.
 *
 * SIGNING / CHALLENGE (documented defaults, PENDING Task 1 sandbox verification):
 * Ramp signs the RAW body with HMAC-SHA256 keyed by the per-webhook secret,
 * base64, in `X-Ramp-Signature`. Webhook-ownership verification arrives as a
 * signed body carrying a `challenge` string, which we answer via
 * `completeWebhookVerification` and echo back. Query parameters are not signed
 * and cannot supply a challenge. If Task 1 shows a different header
 * name, encoding, or challenge shape, update the header read below and
 * `packages/ee/src/ramp/lib/webhook.ts`.
 */

/** Health-check for the URL pasted into / probed by Ramp (GET). */
export async function loader({ params }: LoaderFunctionArgs) {
  if (!params.companyId) {
    return data({ success: false }, { status: 400 });
  }
  return { success: true };
}

export async function action({ request, params }: ActionFunctionArgs) {
  const { companyId } = params;
  if (!companyId) {
    return data({ success: false }, { status: 400 });
  }

  // Raw body FIRST — the signature covers the exact bytes, not re-serialized
  // JSON.
  const body = await request.text();

  const serviceRole = getCarbonServiceRole();
  // Loads only an active `ramp` integration and resolves its vaulted secrets
  // (incl. `webhookSecret`); returns null when not installed/active.
  const integration = await getRampIntegration(serviceRole, companyId);
  if (!integration) {
    return data({ success: false }, { status: 404 });
  }
  const { metadata } = integration;

  // Every delivery, including an ownership challenge, requires a stored secret
  // and a valid signature before it can call Ramp or echo authenticated data.
  const signature = request.headers.get("x-ramp-signature");

  // Rejections log a BOUNDED, server-derived reason only. An earlier diagnostic
  // block echoed the full sorted list of request header names on every rejection,
  // before any authentication — so an anonymous caller could drive log volume and
  // log content by POSTing in a loop. Nothing attacker-controlled is recorded
  // here; `companyId` comes from the URL the install registered.
  if (!signature || !metadata.webhookSecret) {
    logger.warn("Ramp webhook rejected before signature check", {
      companyId,
      reason: !signature ? "no-signature-header" : "no-stored-secret"
    });
    return data({ success: false }, { status: 401 });
  }
  const verified = verifyRampWebhookSignature({
    signature,
    body,
    secret: metadata.webhookSecret
  });
  if (!verified) {
    logger.warn("Ramp webhook signature mismatch", { companyId });
    return data({ success: false }, { status: 401 });
  }

  const challenge = extractChallenge(body);
  if (challenge) {
    try {
      await completeWebhookVerification(serviceRole, companyId, challenge);
    } catch (error) {
      // Non-fatal: still echo the authenticated challenge for an echo handshake.
      logger.error("Ramp webhook verify callback failed", { companyId, error });
    }
    return { challenge };
  }

  // Parse leniently; unrecognized events are acknowledged, never rejected
  // (forward-compat). The event id/type only decide whether to nudge the sync —
  // the job re-derives everything from Ramp, so no payload threading is needed.
  const parsed = RampWebhookEventSchema.safeParse(safeJsonParse(body));
  if (!parsed.success) {
    logger.error("Invalid Ramp webhook payload", { companyId });
    return data({ success: false }, { status: 400 });
  }

  await trigger("ramp-sync", { companyId, reason: "webhook" });

  logger.info("Triggered Ramp sync from webhook", {
    companyId,
    eventId: parsed.data.id,
    eventType: parsed.data.type
  });

  return { success: true };
}

function safeJsonParse(body: string): unknown {
  try {
    return JSON.parse(body);
  } catch {
    return null;
  }
}

/**
 * Pull a webhook-verification challenge only from the authenticated body.
 * Ramp's exact challenge shape still requires sandbox verification.
 */
function extractChallenge(body: string): string | null {
  const parsed = safeJsonParse(body);
  if (!parsed || typeof parsed !== "object") return null;

  // Activation must complete WITHOUT anyone clicking a button in Ramp, so this
  // looks in both plausible places rather than betting on one. Ramp's
  // `webhooks.verification` event is documented as carrying `challenge` at the
  // top level, but its envelope is `{ id, type, data }` for every other event
  // type and we have never seen a real one. Missing the challenge leaves the
  // endpoint stuck on "Pending verification" forever with no deliveries and no
  // error — the most silent failure available.
  const root = parsed as { challenge?: unknown; data?: unknown };
  if (typeof root.challenge === "string" && root.challenge) {
    return root.challenge;
  }
  const nested = root.data as { challenge?: unknown } | undefined;
  if (
    nested &&
    typeof nested === "object" &&
    typeof nested.challenge === "string" &&
    nested.challenge
  ) {
    return nested.challenge;
  }
  return null;
}