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
Security review — company-private buckets (feat/supabase-bucket-migration)
Three-track review (tenant isolation / data loss / attacker-controlled input) of the
uncommitted branch diff, 2026-09-17. Headline: no branch-introduced cross-tenant
access and no scenario that deletes or orphans a customer file today. The preview
route's new isPrivateBucket gate actually closes a HEAD hole (any URL-supplied
bucket id was previously accepted). Remaining findings are pre-existing exposures the
branch preserved, plus deploy-sequencing risks — listed worst-first.
Fix before merge (small, high-value)
- Assert the tenant prefix inside the fallback helpers —
downloadCompanyPrivateObjectandcreateCompanyPrivateSignedUrl(packages/utils/src/storage.ts:123-129, 152-166) fall back to legacyprivatewith the rawobjectPathunchecked againstcompanyId. Under a service-role client the path string is the only tenant boundary. AddinghasCompanyPrivateObjectPathPrefix(companyId, objectPath)inside both helpers closes findings 2 and 3 below in one place.- Caveat: audit archives write company-bucket keys as
audit-logs/${companyId}/...(packages/jobs/src/inngest/functions/scheduled/audit-archive.ts:69) — NOT companyId-prefixed, violating the module invariant (and making the legacy fallback inpackages/database/src/audit.ts:365-377fail RLS for user clients). Either re-key archives to${companyId}/audit-logs/...or exempt that caller explicitly.
- Caveat: audit archives write company-bucket keys as
download.$token.tsxcross-tenant legacy read (Medium, pre-existing) —apps/erp/app/routes/download.$token.tsx:50-54:document.pathcontent is never validated against the company prefix; a company-A user who can write apathofcompanyB/<key>gets B's bytes via the service-role legacy fallback. Fixed by #1 (or a localhasCompanyPrivateObjectPathPrefixcheck like the share route already does).import-csvedge fn cross-tenant legacy read (Medium, pre-existing) —packages/database/supabase/functions/import-csv/index.ts:973-976:filePathis client-supplied (z.string().min(1)only), client is service-role; the legacy fallback reads any<otherCo>/....csvat a known key. Addif (!filePath.startsWith(companyId + "/")) throwbefore the download.- Empty-companyId bucket (Medium, net-new edge) —
getCompanyPrivateBucket("")returns""(packages/utils/src/storage.ts:16); withpath.split("/")[0] ?? ""derivations,.from("")gets probed (harmless miss) but the legacy fallback still runs with the raw path. MakegetCompanyPrivateBucketthrow (or callers guard) on empty input. - Reserved bucket ids in the trigger (Low, defense-in-depth) —
20260917163108_company-bucket-provisioning.sql:22-34: if acompany.idever equaledprivate/public/avatars/feedback/temp-staging/company-templates, theON CONFLICT DO NOTHINGsilently no-ops while the "Company bucket access" RLS policy would grant that company's employees FOR ALL on the shared bucket. Not reachable today (ids are generated, no user-supplied company insert path), but exclude reserved ids in the trigger + backfill.
Separate urgent ticket (NOT this branch)
- Unauthenticated cross-tenant model read (High, pre-existing) —
apps/erp/app/routes/file+/model+/public.$.tsx:33-66: norequirePermissions, "companyId" is derived from the attacker-supplied path itself, only gate ispath.includes("models")+ extension; serves from legacyprivate(and now company buckets) withAccess-Control-Allow-Origin: *and long-lived caching. Anyone with a guessable key reads any tenant's model/PDF/image under amodelssegment. Same reach on HEAD — the branch didn't widen it materially — but it is the weakest point found. Fix: require auth (or a signed token) +path.startsWith(companyId + "/").
Deploy / operational risks (data-loss track)
- H1 — copy script coverage claim is false for non-companyId prefixes.
scripts/migrate-private-buckets.ts:101lists only<companyId>/prefixes; legacy audit archives ataudit-logs/<companyId>/...(and prefixes of deleted companies) are never copied. Safe today (readers fall back), but decommissioning the legacy bucket on the script's "copied everything" claim would permanently destroy pre-migration audit archives. Fix the docblock now; extend the script (or migrateaudit-logs/explicitly) before fallback removal. - H2 — deploy order. Companies created after 2025-08-27 have no bucket until
20260917163108applies. App code writes company-bucket-only → apply the migration before (or atomically with) the app deploy, else uploads for those tenants fail loudly ("Bucket not found") until it lands. - M1 — 50MB cap vs older legacy files. Legacy
privatewas uncapped until 2026-07-15; company buckets are capped at 52428800. Oversized legacy objects would fail to copy (loud, script exits 1) and stay pinned to legacy. One-time check of legacy object sizes before the prod copy run; raise/drop the cap if any exceed it. - M2 — backup listing silently incomplete (pre-existing).
listBucketFilesRecursive(packages/jobs/src/inngest/functions/tasks/company-export.ts:472-496) swallows list errors and does no pagination (>1000 direct children in a folder are dropped) while reporting success. Backup is the safety net for this exact migration — worth fixing alongside. - M3 — restore overwrite deletes before re-copy (
company-backup.ts:604-614): duplicate →removelive object → retry copy; a failed retry leaves the live object gone (backup copy survives). Narrow, user-initiated, pre-existing. - M4 —
.move()is company-bucket-only (x+/quote+/$quoteId.drag.tsx:309-320,x+/sales-rfq+/$rfqId.drag.tsx:165): a file present in both buckets moves only the company copy; the stale legacy copy is resurrected forever by the list union and no delete ever targets the old legacy path. - Low: several
removeCompanyPrivateObjectscallers ignore returnederrors(production.service.ts:6824/6891, drag routes, model-thumbnail.ts:132) — a failed legacy-side delete leaves a resurrectable copy silently. Files uploaded to legacy by old pods during the deploy window stay legacy-only — re-run the copy script before removing fallback.
Verified safe (with evidence)
- RLS: "Company bucket access" (
20250827181005_company-bucket-rls.sql:95-103) scopesbucket_id = ANY(get_companies_with_employee_role()); legacy policy prefix- scopesfoldername(name)[1].FOR ALL ... USINGdoubles as WITH CHECK — employee of A cannot touch B's bucket or B's legacy prefix via a user JWT. - Trigger: SECURITY DEFINER with pinned
search_path,NEW.idused only as a parameterized value (no dynamic SQL → no injection),RETURNS TRIGGERso not directly invocable; bucket creation bounded 1:1 with company inserts. - Preview routes: bucket allowlist (own bucket / legacy / public / temp-staging) +
ownsPath+ fallback reuses the same checked path; naming another company's bucket → 403. URL-decoding tricks don't bypass (decode happens before matching). - Share route: companyId from the share record, prefix guard + 3 further job/customer checks + 10/min rate limit.
- Uploads / MCP:
companyIdinjected from auth, keys always${companyId}/...-prefixed, signed upload URLs target the caller's own bucket only. - Copy script: zero delete calls;
copy()without upsert → existing destination 409s and is countedskipped(idempotent, can never clobber newer data); correct 1000-page offset pagination + folder recursion. - No missed readers/writers: repo-wide grep leaves exactly one
.from("private")— the import-csv read fallback. Every traced reader has company→legacy fallback; all private deletes go through the dual-bucket helper;cleanup.tsnever prunes a referenced object without a durable copy in either private bucket. - Backup/restore: export unions both buckets (company copy wins); restore writes to the company bucket, which every reader checks first.