Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.
TypeScript acmeagentsai-agentscertificate-managementdevsecopsenvironment-variableskubernetesopen-sourcepampkipostgresprivate-caprivileged-access-managementsecret-managementsecret-managersecretssecrets-managementsecuritysecurity-toolsvault
fix(secret-sync): quote every dotenv-sensitive portainer value and key duplicates by instance and route
Yeongjin Lee8cd6ef7cc89a
diff --git a/backend/src/lib/api-docs/constants.ts b/backend/src/lib/api-docs/constants.tsindex f4e49c8f04..beaa6cacff 100644--- a/backend/src/lib/api-docs/constants.ts+++ b/backend/src/lib/api-docs/constants.ts@@ -3587,6 +3587,10 @@ export const SecretSyncs = {"The file path for the mounted file relative to /mnt/workspace/. Required when configType is 'file-mount'. When fileMountFormat is 'secret-per-file', this is the directory path. Example: 'secrets.env' or 'secrets/'.",fileMountFormat:"The format for file mount config elements. Either 'dot-env' (default) to store all secrets in a single .env file, or 'secret-per-file' to create a separate file mount per secret under the mount path directory."+ },+ PORTAINER: {+ environmentId: "The ID of the Portainer environment (endpoint) that hosts the stack.",+ stackId: "The ID of the Portainer stack to sync secrets to."}}};diff --git a/backend/src/services/secret-sync/portainer/portainer-sync-fns.test.ts b/backend/src/services/secret-sync/portainer/portainer-sync-fns.test.tsindex 3256450c15..3839892b08 100644--- a/backend/src/services/secret-sync/portainer/portainer-sync-fns.test.ts+++ b/backend/src/services/secret-sync/portainer/portainer-sync-fns.test.ts@@ -111,8 +111,20 @@ describe("encodePortainerEnvValue / decodePortainerEnvValue", () => {expect(decodePortainerEnvValue(encoded, PortainerStackType.Compose)).toBe('line1\nline2 "quoted" back\\slash\r\n');});+ it("quotes values the dotenv parser would trim or cut short", () => {+ for (const value of [" leading", "trailing ", "abc #def", '"quoted"', "'single'"]) {+ const encoded = encodePortainerEnvValue(value, PortainerStackType.Compose);++ expect(encoded).not.toBe(value);+ expect(decodePortainerEnvValue(encoded, PortainerStackType.Compose)).toBe(value);+ }+ expect(encodePortainerEnvValue("abc #def", PortainerStackType.Compose)).toBe('"abc #def"');+ });+it("leaves single-line values and Swarm stacks untouched", () => {expect(encodePortainerEnvValue("plain", PortainerStackType.Compose)).toBe("plain");+ expect(encodePortainerEnvValue("no#space", PortainerStackType.Compose)).toBe("no#space");+ expect(encodePortainerEnvValue("abc #def", PortainerStackType.Swarm)).toBe("abc #def");expect(encodePortainerEnvValue("line1\nline2", PortainerStackType.Swarm)).toBe("line1\nline2");expect(decodePortainerEnvValue('"line1\\nline2"', PortainerStackType.Swarm)).toBe('"line1\\nline2"');});diff --git a/backend/src/services/secret-sync/portainer/portainer-sync-fns.ts b/backend/src/services/secret-sync/portainer/portainer-sync-fns.tsindex bf4bf5ae77..38d7e317d9 100644--- a/backend/src/services/secret-sync/portainer/portainer-sync-fns.ts+++ b/backend/src/services/secret-sync/portainer/portainer-sync-fns.ts@@ -20,20 +20,24 @@ import { TSecretMap } from "../secret-sync-types";import { TPortainerStackFileResponse, TPortainerSyncWithCredentials } from "./portainer-sync-types";const PORTAINER_DEPLOYMENT_POLL_INTERVAL_MS = 5_000;-const PORTAINER_DEPLOYMENT_TIMEOUT_MS = 10 * 60_000;+// The sync job holds a five-minute lock per sync, so the wait has to give up inside that window+const PORTAINER_DEPLOYMENT_TIMEOUT_MS = 4 * 60_000;// Portainer releases before the background deployer run the redeploy inside the update request itselfconst PORTAINER_STACK_UPDATE_TIMEOUT_MS = PORTAINER_DEPLOYMENT_TIMEOUT_MS;const DOTENV_ESCAPES: Record<string, string> = { '"': '"', "\\": "\\", n: "\n", r: "\r" };+// Unquoted dotenv values lose surrounding whitespace, end at " #", and must not start with a quote+const needsDotenvQuoting = (value: string) => /[\r\n]|^\s|\s$|\s#|^["']/.test(value);+/*** Portainer writes a Compose stack's variables to a `stack.env` file one `NAME=VALUE` line at a time,- * so a value with line breaks has to use the dotenv double-quoted form or the file stops parsing and- * the redeploy fails. Swarm stacks receive their variables through the process environment, where- * line breaks are fine as-is.+ * so a value that the dotenv parser would cut or trim has to use the double-quoted form or it reaches+ * the container changed (or, with line breaks, stops the file from parsing at all). Swarm stacks+ * receive their variables through the process environment, where every value is fine as-is.*/export const encodePortainerEnvValue = (value: string, stackType: PortainerStackType) => {- if (stackType !== PortainerStackType.Compose || !/[\r\n]/.test(value)) return value;+ if (stackType !== PortainerStackType.Compose || !needsDotenvQuoting(value)) return value;const escaped = value.replace(/\\/g, "\\\\").replace(/"/g, '\\"').replace(/\r/g, "\\r").replace(/\n/g, "\\n");@@ -46,8 +50,8 @@ export const decodePortainerEnvValue = (value: string, stackType: PortainerStackconst decoded = value.slice(1, -1).replace(/\\(["\\nr])/g, (_, escape: string) => DOTENV_ESCAPES[escape]);- // Only unwrap what the encoder above produces; a single-line value a user quoted by hand is left alone- return /[\r\n]/.test(decoded) ? decoded : value;+ // Only unwrap what the encoder above produces; a value a user quoted by hand is left alone+ return encodePortainerEnvValue(decoded, stackType) === value ? decoded : value;};const toSortedPairs = (env: TPortainerEnvVar[]) =>diff --git a/backend/src/services/secret-sync/portainer/portainer-sync-schemas.ts b/backend/src/services/secret-sync/portainer/portainer-sync-schemas.tsindex 8d572a7617..44a169107a 100644--- a/backend/src/services/secret-sync/portainer/portainer-sync-schemas.ts+++ b/backend/src/services/secret-sync/portainer/portainer-sync-schemas.ts@@ -1,5 +1,6 @@import { z } from "zod";+import { SecretSyncs } from "@app/lib/api-docs";import { AppConnection } from "@app/services/app-connection/app-connection-enums";import { SecretSync } from "@app/services/secret-sync/secret-sync-enums";import {@@ -12,8 +13,12 @@ import { TSyncOptionsConfig } from "@app/services/secret-sync/secret-sync-types"import { SECRET_SYNC_NAME_MAP } from "../secret-sync-maps";const PortainerSyncDestinationConfigSchema = z.object({- environmentId: z.number().int().positive("Environment is required"),- stackId: z.number().int().positive("Stack is required")+ environmentId: z+ .number()+ .int()+ .positive("Environment is required")+ .describe(SecretSyncs.DESTINATION_CONFIG.PORTAINER.environmentId),+ stackId: z.number().int().positive("Stack is required").describe(SecretSyncs.DESTINATION_CONFIG.PORTAINER.stackId)});const PortainerSyncOptionsConfig: TSyncOptionsConfig = { canImportSecrets: true };diff --git a/backend/src/services/secret-sync/secret-sync-maps.ts b/backend/src/services/secret-sync/secret-sync-maps.tsindex c0d02e81cb..b43641973c 100644--- a/backend/src/services/secret-sync/secret-sync-maps.ts+++ b/backend/src/services/secret-sync/secret-sync-maps.ts@@ -240,7 +240,15 @@ export const SECRET_SYNC_SKIP_FIELDS_MAP: Record<SecretSync, string[]> = {const defaultDuplicateCheck: DestinationDuplicateCheckFn = async () => true;// Portainer environment and stack IDs are local to an instance, so identical IDs on-// different instances are not the same destination+// different instances are not the same destination. The same address behind two+// gateways is two instances too, while `https://host` and `https://host:443` are one.+const getPortainerInstanceKey = (connection: TPortainerConnection) => {+ const url = new URL(getPortainerInstanceUrl(connection));+ const route = connection.gatewayPoolId ?? connection.gatewayId ?? "direct";++ return `${url.origin}${url.pathname.replace(/\/+$/, "")}@${route}`;+};+const portainerDuplicateCheck: DestinationDuplicateCheckFn = async ({ existingSync, newSync, decryptConnection }) => {if (!newSync.connectionId) return true;@@ -256,8 +264,8 @@ const portainerDuplicateCheck: DestinationDuplicateCheckFn = async ({ existingSy]);return (- getPortainerInstanceUrl(existingConn as TPortainerConnection) ===- getPortainerInstanceUrl(newConn as TPortainerConnection)+ getPortainerInstanceKey(existingConn as TPortainerConnection) ===+ getPortainerInstanceKey(newConn as TPortainerConnection));};const awsDuplicateCheck: DestinationDuplicateCheckFn = async ({ existingSync, newSync, decryptConnection }) => {