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.
TypeScript ai-agentsbill-of-materialserpinventory-managementleanmanufacturingmanufacturing-execution-systemmcp-servermesmrpopen-source-erppostgresqlproduction-planningqmsquality-management-systemreact-routerself-hostedsupabasetraceabilitytypescript
fix(mes): going back after an action returns to the page, not the schedule
Sidwebworksb9a63d463366
diff --git a/apps/erp/app/utils/path.ts b/apps/erp/app/utils/path.tsindex 531fdbb308..93bdda4192 100644--- a/apps/erp/app/utils/path.ts+++ b/apps/erp/app/utils/path.ts@@ -8,7 +8,7 @@ import {getMESUrl,SUPABASE_URL} from "@carbon/auth";-import { getRequestOrigin } from "@carbon/utils";+import { requestReferrer } from "@carbon/utils";import { generatePath } from "react-router";const x = "/x"; // from ~/routes/x+ folder@@ -2436,29 +2436,7 @@ export const getStoragePath = (bucket: string, path: string) => {return `${SUPABASE_URL}/storage/v1/object/public/${bucket}/${path}`;};-/**- * The Referer header, reduced to a SAME-ORIGIN relative path (or null). Many- * actions redirect back here — returning the raw header would let a crafted- * request bounce the user to an attacker origin (CWE-601 open redirect), so a- * cross-origin or unparsable referer yields null and callers fall back to- * their fixed route.- *- * Compared with the origin the client addressed (`getRequestOrigin`), not- * `request.url`'s: behind the proxy that is the internal scheme and host, which- * never matches a real Referer.- */-export const requestReferrer = (request: Request, withParams = true) => {- const referer = request.headers.get("referer");- if (!referer) return null;- try {- const origin = getRequestOrigin(request) ?? new URL(request.url).origin;- const url = new URL(referer, origin);- if (url.origin !== origin) return null;- return url.pathname + url.search + url.hash;- } catch {- return null;- }-};+export { requestReferrer };export const getParams = (request: Request) => {const url = new URL(requestReferrer(request) ?? "/", "http://relative.local");diff --git a/apps/mes/app/routes/x+/_layout.tsx b/apps/mes/app/routes/x+/_layout.tsxindex 6f6755a968..86c3d6fef0 100644--- a/apps/mes/app/routes/x+/_layout.tsx+++ b/apps/mes/app/routes/x+/_layout.tsx@@ -43,6 +43,7 @@ import { getStripeCustomerByCompanyId } from "@carbon/stripe/stripe.server";import {Edition,redirect,+ redirectExternal,requiresItarEntityCertification,SHELL_MAX_AGE_MS} from "@carbon/utils";@@ -247,7 +248,8 @@ export async function loader({ request, context }: LoaderFunctionArgs) {: { entityCertified: true, userCertified: true, entityRequired: false };if (!companyPlan && CarbonEdition === Edition.Cloud) {- throw redirect(path.to.onboarding);+ // Onboarding lives in the ERP: another origin.+ throw redirectExternal(path.to.onboarding);}if (!locations.data || locations.data.length === 0) {diff --git a/apps/mes/app/utils/path.ts b/apps/mes/app/utils/path.tsindex 738f691cae..58172d1074 100644--- a/apps/mes/app/utils/path.ts+++ b/apps/mes/app/utils/path.ts@@ -3,6 +3,7 @@// including ports, remain AGPLv3; serving them over a network requires releasing their source.import { getAppUrl, getMESUrl, SUPABASE_URL } from "@carbon/auth";+import { requestReferrer } from "@carbon/utils";import { generatePath } from "react-router";export const ERP_URL = getAppUrl();@@ -238,12 +239,10 @@ export const getStoragePath = (bucket: string, path: string) => {return `${SUPABASE_URL}/storage/v1/object/public/${bucket}/${path}`;};-export const requestReferrer = (request: Request) => {- return request.headers.get("referer");-};+export { requestReferrer };export const getParams = (request: Request) => {- const url = new URL(requestReferrer(request) ?? "");+ const url = new URL(requestReferrer(request) ?? "/", "http://relative.local");const searchParams = new URLSearchParams(url.search);return searchParams.toString();};diff --git a/apps/starter/app/utils/path.ts b/apps/starter/app/utils/path.tsindex 25886879d4..aa319deab1 100644--- a/apps/starter/app/utils/path.ts+++ b/apps/starter/app/utils/path.ts@@ -3,6 +3,7 @@// including ports, remain AGPLv3; serving them over a network requires releasing their source.import { getAppUrl, SUPABASE_URL } from "@carbon/auth";+import { requestReferrer } from "@carbon/utils";import { generatePath } from "react-router";const x = "/x"; // from ~/routes/x+ folder@@ -42,12 +43,10 @@ export const getStoragePath = (bucket: string, path: string) => {return `${SUPABASE_URL}/storage/v1/object/public/${bucket}/${path}`;};-export const requestReferrer = (request: Request) => {- return request.headers.get("referer");-};+export { requestReferrer };export const getParams = (request: Request) => {- const url = new URL(requestReferrer(request) ?? "");+ const url = new URL(requestReferrer(request) ?? "/", "http://relative.local");const searchParams = new URLSearchParams(url.search);return searchParams.toString();};diff --git a/packages/utils/src/headers.ts b/packages/utils/src/headers.tsindex b2e9cb1159..d232e04746 100644--- a/packages/utils/src/headers.ts+++ b/packages/utils/src/headers.ts@@ -71,6 +71,30 @@ export function getRequestOrigin(input: RequestLike): string | null {return host ? `${getRequestProtocol(input)}://${host}` : null;}+/**+ * The Referer header, reduced to a SAME-ORIGIN relative path (or null). Many+ * actions redirect back here — returning the raw header would let a crafted+ * request bounce the user to an attacker origin (CWE-601 open redirect), so a+ * cross-origin or unparsable referer yields null and callers fall back to+ * their fixed route.+ *+ * Compared with the origin the client addressed (`getRequestOrigin`), not+ * `request.url`'s: behind the proxy that is the internal scheme and host, which+ * never matches a real Referer.+ */+export function requestReferrer(request: Request): string | null {+ const referer = request.headers.get("referer");+ if (!referer) return null;+ try {+ const origin = getRequestOrigin(request) ?? new URL(request.url).origin;+ const url = new URL(referer, origin);+ if (url.origin !== origin) return null;+ return url.pathname + url.search + url.hash;+ } catch {+ return null;+ }+}+export const getPreferenceHeaders = (request: Request) => {const acceptLanguage = request.headers.get("accept-language");const cookieHeader = request.headers.get("cookie");diff --git a/apps/erp/app/utils/path.test.ts b/packages/utils/src/request-referrer.test.tssimilarity index 85%rename from apps/erp/app/utils/path.test.tsrename to packages/utils/src/request-referrer.test.tsindex 3524a84ec7..54c9a0623d 100644--- a/apps/erp/app/utils/path.test.ts+++ b/packages/utils/src/request-referrer.test.ts@@ -2,19 +2,8 @@// 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 { describe, expect, it, vi } from "vitest";-import { requestReferrer } from "./path";--vi.mock("@carbon/auth", () => ({- CARBON_API_URL: "https://api.example.com",- getAppUrl: () => "https://app.example.com",- getMESUrl: () => "https://mes.example.com",- SUPABASE_URL: "https://supabase.example.com"-}));-vi.mock("@carbon/files/media", () => ({- getPrivateUrl: vi.fn(),- getRawModelUrl: vi.fn()-}));+import { describe, expect, it } from "vitest";+import { requestReferrer } from "./headers";const request = (url: string, headers: Record<string, string>) =>new Request(url, { headers });