Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

fix(secret-sync): validate portainer stack environment and scope duplicate check per instance

Yeongjin Lee290a46c2661c
diff --git a/backend/src/services/secret-sync/portainer/portainer-sync-fns.ts b/backend/src/services/secret-sync/portainer/portainer-sync-fns.ts
index bee61f14df..5d677e7d2f 100644
--- a/backend/src/services/secret-sync/portainer/portainer-sync-fns.ts
+++ b/backend/src/services/secret-sync/portainer/portainer-sync-fns.ts
@@ -49,6 +49,13 @@ const writeStackEnv = async (
) => {
const { instanceUrl, headers, environmentId, stackId } = getPortainerClientDetails(secretSync);
+ if (stack.EndpointId !== environmentId) {
+ throw new SecretSyncError({
+ message: `Stack with ID '${stackId}' belongs to Portainer environment '${stack.EndpointId}', not the configured environment '${environmentId}'`,
+ shouldRetry: false
+ });
+ }
+
try {
if (stack.GitConfig?.URL) {
// The Git settings are replaced wholesale, so the existing reference and auto-update config are echoed back
diff --git a/backend/src/services/secret-sync/secret-sync-maps.ts b/backend/src/services/secret-sync/secret-sync-maps.ts
index b79a2c0a06..c0d02e81cb 100644
--- a/backend/src/services/secret-sync/secret-sync-maps.ts
+++ b/backend/src/services/secret-sync/secret-sync-maps.ts
@@ -2,6 +2,8 @@ import { AppConnection } from "@app/services/app-connection/app-connection-enums
import { buildAwsConnectionConfig, getAwsAccountId } from "@app/services/app-connection/aws/aws-connection-fns";
import { TAwsConnection } from "@app/services/app-connection/aws/aws-connection-types";
import { TDaytonaConnection } from "@app/services/app-connection/daytona";
+import { getPortainerInstanceUrl } from "@app/services/app-connection/portainer/portainer-connection-fns";
+import { TPortainerConnection } from "@app/services/app-connection/portainer/portainer-connection-types";
import { GcpSyncScope } from "@app/services/secret-sync/gcp/gcp-sync-enums";
import { SecretSync, SecretSyncPlanType } from "@app/services/secret-sync/secret-sync-enums";
import { DestinationDuplicateCheckFn } from "@app/services/secret-sync/secret-sync-types";
@@ -236,6 +238,28 @@ 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
+const portainerDuplicateCheck: DestinationDuplicateCheckFn = async ({ existingSync, newSync, decryptConnection }) => {
+ if (!newSync.connectionId) return true;
+
+ if (existingSync.connectionId === newSync.connectionId) {
+ return true;
+ }
+
+ if (!existingSync.connectionId) return false;
+
+ const [existingConn, newConn] = await Promise.all([
+ decryptConnection(existingSync.connectionId),
+ decryptConnection(newSync.connectionId)
+ ]);
+
+ return (
+ getPortainerInstanceUrl(existingConn as TPortainerConnection) ===
+ getPortainerInstanceUrl(newConn as TPortainerConnection)
+ );
+};
const awsDuplicateCheck: DestinationDuplicateCheckFn = async ({ existingSync, newSync, decryptConnection }) => {
if (!newSync.connectionId) return true;
@@ -352,7 +376,7 @@ export const DESTINATION_DUPLICATE_CHECK_MAP: Record<SecretSync, DestinationDupl
[SecretSync.CloudflareSecretsStore]: defaultDuplicateCheck,
[SecretSync.Supabase]: defaultDuplicateCheck,
[SecretSync.Rundeck]: defaultDuplicateCheck,
- [SecretSync.Portainer]: defaultDuplicateCheck,
+ [SecretSync.Portainer]: portainerDuplicateCheck,
[SecretSync.Zabbix]: defaultDuplicateCheck,
[SecretSync.Railway]: defaultDuplicateCheck,
[SecretSync.Checkly]: defaultDuplicateCheck,
diff --git a/docs/integrations/app-connections/portainer.mdx b/docs/integrations/app-connections/portainer.mdx
index 82b83b3788..87f75f768c 100644
--- a/docs/integrations/app-connections/portainer.mdx
+++ b/docs/integrations/app-connections/portainer.mdx
@@ -52,8 +52,7 @@ Infisical supports connecting to Portainer using an **API Token**.
![Portainer Connection Form](/images/app-connections/portainer/step-2.png)
<Note>
- The instance URL must be reachable from Infisical. For a self-hosted Portainer that is
- not exposed publicly, use Infisical's [Gateway](/documentation/platform/gateways/overview).
+ The instance URL must be reachable from your Infisical instance.
</Note>
</Step>
<Step title="Connection Created">