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(erp): honour X-Forwarded-Proto when validating the Referer (#1685)
Naveen Kashyapb075d3ebe103
diff --git a/apps/erp/app/utils/path.test.ts b/apps/erp/app/utils/path.test.tsnew file mode 100644index 0000000000..05d8c698d1--- /dev/null+++ b/apps/erp/app/utils/path.test.ts@@ -0,0 +1,90 @@+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()+}));++const request = (url: string, headers: Record<string, string>) =>+ new Request(url, { headers });++describe("requestReferrer", () => {+ it("returns the same-origin referer as a relative path", () => {+ expect(+ requestReferrer(+ request("https://app.example.com/x/action", {+ referer: "https://app.example.com/x/part/1/details?tab=a#b"+ })+ )+ ).toBe("/x/part/1/details?tab=a#b");+ });++ it("returns null without a referer", () => {+ expect(requestReferrer(request("https://app.example.com/x", {}))).toBe(+ null+ );+ });++ it("rejects a cross-origin referer", () => {+ expect(+ requestReferrer(+ request("https://app.example.com/x/action", {+ referer: "https://evil.example.net/x/part/1"+ })+ )+ ).toBe(null);+ });++ it("accepts an https referer behind a TLS-terminating proxy", () => {+ // The load balancer ends TLS and forwards over http, so request.url is+ // http while the browser's Referer is https.+ expect(+ requestReferrer(+ request("http://app.example.com/x/action", {+ referer: "https://app.example.com/x/part/1/details",+ "x-forwarded-proto": "https"+ })+ )+ ).toBe("/x/part/1/details");+ });++ it("uses the first hop of a multi-proxy x-forwarded-proto", () => {+ expect(+ requestReferrer(+ request("http://app.example.com/x/action", {+ referer: "https://app.example.com/x/part/1",+ "x-forwarded-proto": "https, http"+ })+ )+ ).toBe("/x/part/1");+ });++ it("still rejects a cross-origin referer when x-forwarded-proto is set", () => {+ expect(+ requestReferrer(+ request("http://app.example.com/x/action", {+ referer: "https://evil.example.net/x/part/1",+ "x-forwarded-proto": "https"+ })+ )+ ).toBe(null);+ });++ it("ignores an x-forwarded-proto that is not http or https", () => {+ expect(+ requestReferrer(+ request("http://app.example.com/x/action", {+ referer: "javascript://app.example.com/x",+ "x-forwarded-proto": "javascript"+ })+ )+ ).toBe(null);+ });+});diff --git a/apps/erp/app/utils/path.ts b/apps/erp/app/utils/path.tsindex 6930eaa221..0f1b90e0ef 100644--- a/apps/erp/app/utils/path.ts+++ b/apps/erp/app/utils/path.ts@@ -2391,12 +2391,25 @@ export const getStoragePath = (bucket: string, path: string) => {* 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.+ *+ * Behind a TLS-terminating load balancer `request.url` is `http://` (the+ * server does not trust proxy headers) while the browser's Referer is+ * `https://`, so the scheme comes from `X-Forwarded-Proto` when it names one.+ * Only the scheme is taken from it; the host still has to match.*/export const requestReferrer = (request: Request, withParams = true) => {const referer = request.headers.get("referer");if (!referer) return null;try {const requestUrl = new URL(request.url);+ const forwardedProto = request.headers+ .get("x-forwarded-proto")+ ?.split(",")[0]+ ?.trim()+ .toLowerCase();+ if (forwardedProto === "http" || forwardedProto === "https") {+ requestUrl.protocol = `${forwardedProto}:`;+ }const url = new URL(referer, requestUrl.origin);if (url.origin !== requestUrl.origin) return null;return url.pathname + url.search + url.hash;