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.

fix: fix Paperless Parts webhook HMAC signature computation

Rob Carrington79ba9053c19f
diff --git a/apps/erp/app/modules/settings/settings.service.ts b/apps/erp/app/modules/settings/settings.service.ts
index 5fec6316aa..6b9b26cc12 100644
--- a/apps/erp/app/modules/settings/settings.service.ts
+++ b/apps/erp/app/modules/settings/settings.service.ts
@@ -237,6 +237,7 @@ export async function getIntegration(
return client
.from("companyIntegration")
.select("*")
+ .eq("active", true)
.eq("id", id)
.eq("companyId", companyId)
.maybeSingle();
diff --git a/apps/erp/app/routes/api+/webhook.paperless-parts.$companyId.ts b/apps/erp/app/routes/api+/webhook.paperless-parts.$companyId.ts
index 62d5c84e67..e671b8c134 100644
--- a/apps/erp/app/routes/api+/webhook.paperless-parts.$companyId.ts
+++ b/apps/erp/app/routes/api+/webhook.paperless-parts.$companyId.ts
@@ -39,8 +39,8 @@ export async function action({ request, params }: ActionFunctionArgs) {
const serviceRole = await getCarbonServiceRole();
const paperlessPartsIntegration = await getIntegration(
serviceRole,
- companyId,
- "paperless-parts"
+ "paperless-parts",
+ companyId
);
if (paperlessPartsIntegration.error || !paperlessPartsIntegration.data) {
@@ -49,12 +49,23 @@ export async function action({ request, params }: ActionFunctionArgs) {
try {
const { apiKey, secretKey } = integrationValidator.parse(
- paperlessPartsIntegration.data
+ paperlessPartsIntegration.data.metadata
);
- const payloadText = await request.text();
- const signatureHeader = request.headers.get("Paperless-Parts-Signature");
-
+ // The signature provided by Paperless Parts is computed using the Python json.dumps() function,
+ // which formats the JSON string with newlines and whitespace.
+ // We need to remove the newlines and whitespace to match the signature.
+ const payloadText = JSON.stringify(await request.json(), null, 1)
+ .replace(/^ +/gm, " ")
+ .replace(/\n/g, "")
+ .replace(/{ /g, "{")
+ .replace(/ }/g, "}")
+ .replace(/\[ /g, "[")
+ .replace(/ \]/g, "]");
+
+ const signatureHeader =
+ request.headers.get("paperless-parts-signature") ||
+ request.headers.get("Paperless-Parts-Signature");
if (!signatureHeader) {
return json({ success: false }, { status: 401 });
}