Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

fix(security): require storage secrets again when the bucket or endpoint changes (#19566)

Steffen Reutherc02df04f314b
diff --git a/web/src/__tests__/server/blob-storage-integration-trpc.servertest.ts b/web/src/__tests__/server/blob-storage-integration-trpc.servertest.ts
index 056657901c..c1440da5e8 100644
--- a/web/src/__tests__/server/blob-storage-integration-trpc.servertest.ts
+++ b/web/src/__tests__/server/blob-storage-integration-trpc.servertest.ts
@@ -509,6 +509,36 @@ describe("Blob Storage Integration tRPC Router", () => {
);
});
+ it("requires the key again when the bucket changes", async () => {
+ const { caller, project } = await prepare({
+ plan: "self-hosted:enterprise",
+ });
+ await caller.blobStorageIntegration.update({
+ projectId: project.id,
+ ...gcsKeyConfig,
+ });
+
+ await expect(
+ caller.blobStorageIntegration.update({
+ projectId: project.id,
+ ...gcsKeyConfig,
+ bucketName: "allowed-bucket",
+ secretAccessKey: null,
+ }),
+ ).rejects.toMatchObject({
+ code: "BAD_REQUEST",
+ message: expect.stringContaining(
+ "Secret access key is required when changing",
+ ),
+ });
+
+ const integration = await findIntegration(project.id);
+ expect(integration?.bucketName).toBe("customer-bucket");
+ expect(decrypt(integration!.secretAccessKey!)).toBe(
+ fakeServiceAccountKey,
+ );
+ });
+
it("drops the stored key when switched to default credentials", async () => {
const { caller, project } = await prepare({
plan: "self-hosted:enterprise",
@@ -622,6 +652,92 @@ describe("Blob Storage Integration tRPC Router", () => {
}),
).rejects.toThrow(/Secret access key is required/);
});
+
+ const storedS3 = {
+ type: "S3",
+ endpoint: null,
+ forcePathStyle: false,
+ } as const;
+ const storedS3Compatible = {
+ type: "S3_COMPATIBLE",
+ endpoint: "https://storage.example.com",
+ forcePathStyle: true,
+ } as const;
+
+ const createStoredIntegration = async (
+ projectId: string,
+ destination: typeof storedS3 | typeof storedS3Compatible,
+ ) => {
+ await createIntegration({ projectId });
+ await prisma.blobStorageIntegration.update({
+ where: { projectId },
+ data: destination,
+ });
+ };
+
+ const updateWithoutSecret = {
+ ...baseConfig,
+ bucketName: "test-bucket",
+ accessKeyId: "test-access-key",
+ secretAccessKey: null,
+ };
+
+ it.each([
+ ["bucket", storedS3, { bucketName: "other-bucket" }],
+ ["access key ID", storedS3, { accessKeyId: "other-access-key" }],
+ [
+ "endpoint",
+ storedS3Compatible,
+ { endpoint: "https://other-storage.example.com" },
+ ],
+ ["storage provider", storedS3, storedS3Compatible],
+ ])(
+ "requires the secret again when the %s changes",
+ async (_case, stored, change) => {
+ const { caller, project } = await prepare();
+ await createStoredIntegration(project.id, stored);
+
+ await expect(
+ caller.blobStorageIntegration.update({
+ projectId: project.id,
+ ...updateWithoutSecret,
+ ...stored,
+ ...change,
+ }),
+ ).rejects.toMatchObject({
+ code: "BAD_REQUEST",
+ message:
+ "Secret access key is required when changing the storage provider, bucket, endpoint, or access key ID",
+ });
+
+ const integration = await prisma.blobStorageIntegration.findUnique({
+ where: { projectId: project.id },
+ });
+ expect(integration).toMatchObject({
+ bucketName: "test-bucket",
+ accessKeyId: "test-access-key",
+ ...stored,
+ });
+ expect(decrypt(integration!.secretAccessKey!)).toBe("test-secret-key");
+ },
+ );
+
+ it("keeps the stored secret for an equivalent endpoint URL", async () => {
+ const { caller, project } = await prepare();
+ await createStoredIntegration(project.id, storedS3Compatible);
+
+ await caller.blobStorageIntegration.update({
+ projectId: project.id,
+ ...updateWithoutSecret,
+ ...storedS3Compatible,
+ endpoint: "https://STORAGE.example.com/",
+ });
+
+ const integration = await prisma.blobStorageIntegration.findUnique({
+ where: { projectId: project.id },
+ });
+ expect(decrypt(integration!.secretAccessKey!)).toBe("test-secret-key");
+ });
});
describe("region normalization", () => {
diff --git a/web/src/__tests__/server/external-media-storage-service.servertest.ts b/web/src/__tests__/server/external-media-storage-service.servertest.ts
index 5281c02d7d..c1c92454a9 100644
--- a/web/src/__tests__/server/external-media-storage-service.servertest.ts
+++ b/web/src/__tests__/server/external-media-storage-service.servertest.ts
@@ -1,8 +1,9 @@
+import dns from "node:dns/promises";
import { randomUUID } from "crypto";
import type { Mock } from "vitest";
import { InvalidRequestError } from "@langfuse/shared";
-import { encrypt } from "@langfuse/shared/encryption";
+import { decrypt, encrypt } from "@langfuse/shared/encryption";
import { prisma } from "@langfuse/shared/src/db";
import {
createOrgProjectAndApiKey,
@@ -114,7 +115,7 @@ describe("external media storage service", () => {
endpoint: "https://stale.example.com",
region: "us-east-1",
accessKeyId: "access-key",
- secretAccessKey: "",
+ secretAccessKey: "new-secret-key",
prefix: "",
forcePathStyle: true,
},
@@ -128,6 +129,96 @@ describe("external media storage service", () => {
).resolves.toEqual({ endpoint: null, forcePathStyle: false });
});
+ describe("stored secret reuse", () => {
+ const savedValues = {
+ type: "S3_COMPATIBLE" as const,
+ bucketName: "media-bucket",
+ endpoint: "https://storage.example.com",
+ region: "us-east-1",
+ accessKeyId: "access-key",
+ secretAccessKey: "",
+ prefix: "",
+ forcePathStyle: true,
+ };
+
+ beforeEach(() => {
+ vi.spyOn(dns, "resolve4").mockResolvedValue(["1.1.1.1"]);
+ vi.spyOn(dns, "resolve6").mockRejectedValue(new Error("ENODATA"));
+ vi.spyOn(dns, "lookup").mockResolvedValue([
+ { address: "1.1.1.1", family: 4 },
+ ] as never);
+ });
+
+ afterEach(() => {
+ vi.restoreAllMocks();
+ });
+
+ async function prepareS3CompatibleIntegration() {
+ const project = await prepareIntegration({ prefix: null });
+ await prisma.externalMediaStorageIntegration.update({
+ where: { projectId: project.id },
+ data: {
+ type: savedValues.type,
+ endpoint: savedValues.endpoint,
+ accessKeyId: savedValues.accessKeyId,
+ forcePathStyle: true,
+ },
+ });
+ return project;
+ }
+
+ const findStored = (projectId: string) =>
+ prisma.externalMediaStorageIntegration.findUniqueOrThrow({
+ where: { projectId },
+ select: {
+ type: true,
+ bucketName: true,
+ endpoint: true,
+ accessKeyId: true,
+ secretAccessKey: true,
+ },
+ });
+
+ it("keeps the stored secret when the destination is unchanged", async () => {
+ const project = await prepareS3CompatibleIntegration();
+
+ await createExternalMediaStorageService(prisma).saveConfiguration({
+ actor: { apiKeyId: "test-api-key", orgId: project.orgId },
+ projectId: project.id,
+ values: { ...savedValues, prefix: "customer/" },
+ });
+
+ const stored = await findStored(project.id);
+ expect(decrypt(stored.secretAccessKey!)).toBe("test-secret-key");
+ });
+
+ it.each([
+ ["endpoint", { endpoint: "https://other-storage.example.com" }],
+ ["bucket", { bucketName: "other-bucket" }],
+ ["access key ID", { accessKeyId: "other-access-key" }],
+ ["storage type", { type: "S3" as const, endpoint: null }],
+ ])(
+ "requires the secret again when the %s changes",
+ async (_case, change) => {
+ const project = await prepareS3CompatibleIntegration();
+ const before = await findStored(project.id);
+
+ const rejection = createExternalMediaStorageService(
+ prisma,
+ ).saveConfiguration({
+ actor: { apiKeyId: "test-api-key", orgId: project.orgId },
+ projectId: project.id,
+ values: { ...savedValues, ...change },
+ });
+ await expect(rejection).rejects.toBeInstanceOf(InvalidRequestError);
+ await expect(rejection).rejects.toThrow(
+ "Secret Access Key is required when changing the storage provider, bucket, endpoint, or Access Key ID",
+ );
+ await expect(findStored(project.id)).resolves.toEqual(before);
+ },
+ );
+ });
+
it("signs an object within the configured bucket and prefix", async () => {
const getSignedUrl = vi.fn().mockResolvedValue("https://signed.example");
(StorageServiceFactory.getInstance as Mock).mockReturnValue({
diff --git a/web/src/__tests__/server/external-media-storage-trpc.servertest.ts b/web/src/__tests__/server/external-media-storage-trpc.servertest.ts
index e0b1eaf5f7..3cc23d85d5 100644
--- a/web/src/__tests__/server/external-media-storage-trpc.servertest.ts
+++ b/web/src/__tests__/server/external-media-storage-trpc.servertest.ts
@@ -244,6 +244,38 @@ describe("externalMediaStorage tRPC", () => {
});
});
+ describe("stored secret reuse", () => {
+ it("rejects a bucket change without the secret as a bad request", async () => {
+ const { project, caller } = await prepare();
+ const integration = await createIntegration({ projectId: project.id });
+
+ await expect(
+ caller.externalMediaStorage.update({
+ projectId: project.id,
+ type: "S3",
+ bucketName: "other-bucket",
+ endpoint: null,
+ region: "us-east-1",
+ accessKeyId: "test-access-key",
+ secretAccessKey: "",
+ prefix: "",
+ forcePathStyle: false,
+ }),
+ ).rejects.toMatchObject({
+ code: "BAD_REQUEST",
+ message: expect.stringContaining(
+ "Secret Access Key is required when changing",
+ ),
+ });
+ await expect(
+ prisma.externalMediaStorageIntegration.findUniqueOrThrow({
+ where: { projectId: project.id },
+ select: { bucketName: true },
+ }),
+ ).resolves.toEqual({ bucketName: integration.bucketName });
+ });
+ });
+
describe("access control", () => {
it("forbids a VIEWER from reading, saving, deleting, or testing the configuration", async () => {
const { project, caller } = await prepare({ projectRole: "VIEWER" });
diff --git a/web/src/features/automations/destinationUrl.ts b/web/src/features/automations/destinationUrl.ts
index cee1be6b41..ef8283ec6f 100644
--- a/web/src/features/automations/destinationUrl.ts
+++ b/web/src/features/automations/destinationUrl.ts
@@ -8,3 +8,29 @@ export function areDestinationUrlsEquivalent(
return firstUrl === secondUrl;
}
}
+
+type StorageDestination = {
+ type: string;
+ bucketName: string;
+ endpoint: string | null;
+ accessKeyId: string | null;
+};
+
+// A stored storage secret may only be reused for the destination it was
+// entered for.
+export function isSameStorageDestination(
+ existing: StorageDestination,
+ next: StorageDestination,
+): boolean {
+ const isSameEndpoint =
+ existing.endpoint === null || next.endpoint === null
+ ? existing.endpoint === next.endpoint
+ : areDestinationUrlsEquivalent(existing.endpoint, next.endpoint);
+
+ return (
+ isSameEndpoint &&
+ existing.type === next.type &&
+ existing.bucketName === next.bucketName &&
+ existing.accessKeyId === next.accessKeyId
+ );
+}
diff --git a/web/src/features/blobstorage-integration/components/StorageProviderFields.tsx b/web/src/features/blobstorage-integration/components/StorageProviderFields.tsx
index 3c63cf240b..d4336afe8a 100644
--- a/web/src/features/blobstorage-integration/components/StorageProviderFields.tsx
+++ b/web/src/features/blobstorage-integration/components/StorageProviderFields.tsx
@@ -230,8 +230,8 @@ export const StorageProviderFields = ({
/>
</FormControl>
<FormDescription>
- Stored encrypted. Leave empty to keep the saved key. The key
- needs write access to the bucket.
+ Stored encrypted. Leave empty to keep the saved key, unless you
+ change the bucket. The key needs write access to the bucket.
</FormDescription>
<FormMessage />
</FormItem>
diff --git a/web/src/features/blobstorage-integration/service.ts b/web/src/features/blobstorage-integration/service.ts
index 45e29580ed..a217fbfbc1 100644
--- a/web/src/features/blobstorage-integration/service.ts
+++ b/web/src/features/blobstorage-integration/service.ts
@@ -12,6 +12,7 @@ import {
GCS_USE_DEFAULT_CREDENTIALS,
} from "@langfuse/shared";
import { assertPersistedExportSourceAllowed } from "@/src/features/analytics-integrations/server";
+import { isSameStorageDestination } from "@/src/features/automations/destinationUrl";
import { encrypt } from "@langfuse/shared/encryption";
import { env } from "@/src/env.mjs";
import {
@@ -184,8 +185,9 @@ export async function upsertBlobStorageIntegration(params: {
return prisma.$transaction(async (tx) => {
const existing = await tx.blobStorageIntegration.findUnique({
where: { projectId },
- // createdAt/exportSource feed the post-upsert backstop below;
- // type/secretAccessKey decide whether an update keeps a stored secret.
+ // createdAt/exportSource feed the post-upsert backstop below; the
+ // destination fields and secretAccessKey decide whether an update keeps
+ // a stored secret.
select: {
enabled: true,
exportMode: true,
@@ -194,14 +196,40 @@ export async function upsertBlobStorageIntegration(params: {
createdAt: true,
exportSource: true,
type: true,
+ bucketName: true,
+ endpoint: true,
+ accessKeyId: true,
secretAccessKey: true,
},
});
- // A stored secret is only reusable within the same credential family.
- const canKeepStoredSecret =
+ // A stored secret is only reusable within the same credential family, and
+ // only for the destination it was entered for.
+ const hasStoredSecretOfFamily =
!!existing?.secretAccessKey &&
secretFamily(existing.type) === secretFamily(data.type);
+ const canKeepStoredSecret =
+ hasStoredSecretOfFamily &&
+ isSameStorageDestination(existing, {
+ type: data.type,
+ bucketName: data.bucketName,
+ endpoint,
+ accessKeyId,
+ });
+ // Switching a self-hosted S3 integration to host credentials drops the
+ // stored secret instead of asking for it again.
+ const isSwitchingToHostCredentials = canUseHostCredentials && !accessKeyId;
+ if (
+ hasStoredSecretOfFamily &&
+ !canKeepStoredSecret &&
+ !secretAccessKey &&
+ !isKeylessGcs &&
+ !isSwitchingToHostCredentials
+ ) {
+ throw new InvalidRequestError(
+ "Secret access key is required when changing the storage provider, bucket, endpoint, or access key ID",
+ );
+ }
// A GCS integration that will run keyless: explicitly requested, or an
// update that sends no secret and has no stored GCS key to keep. Keyless
// runs as the deployment identity, so it is gated by the bucket allowlist.
@@ -239,16 +267,16 @@ export async function upsertBlobStorageIntegration(params: {
const modeChanged = existing && existing.exportMode !== data.exportMode;
const justEnabled = data.enabled && !existing?.enabled;
const encryptedSecret = secretAccessKey ? encrypt(secretAccessKey) : null;
- // Only overwrite secretAccessKey when a new value is provided, so partial
- // updates don't wipe the existing encrypted secret. Keyless GCS, or a switch
- // to another credential family, clears it, so a secret is never reused by
- // the wrong provider.
- let secretAccessKeyUpdate: { secretAccessKey: string | null } | object = {};
- if (encryptedSecret) {
- secretAccessKeyUpdate = { secretAccessKey: encryptedSecret };
- } else if (willRunKeyless || !canKeepStoredSecret) {
- secretAccessKeyUpdate = { secretAccessKey: null };
- }
+ // An update without a new secret writes back the stored secret checked
+ // above, never omits the column: a concurrent save that changed the secret
+ // in between must not end up paired with this request's destination.
+ // Keyless GCS, or a destination the stored secret was not entered for,
+ // clears it.
+ const nextSecretAccessKey =
+ encryptedSecret ??
+ (canKeepStoredSecret && !willRunKeyless
+ ? existing.secretAccessKey
+ : null);
// The CREATE payload always carries a concrete source, resolved by the
// caller through resolveExportSource. Applying it unconditionally (rather
@@ -274,7 +302,7 @@ export async function upsertBlobStorageIntegration(params: {
},
update: {
...writeData,
- ...secretAccessKeyUpdate,
+ secretAccessKey: nextSecretAccessKey,
// Schedule an immediate retry when saving an errored integration
// so the scheduler picks it up via the nextSyncAt clause.
...(existing?.lastError && data.enabled && !modeChanged
diff --git a/web/src/features/external-media-storage/components/ExternalMediaStorageForm/ExternalMediaStorageForm.tsx b/web/src/features/external-media-storage/components/ExternalMediaStorageForm/ExternalMediaStorageForm.tsx
index 267ebe2054..ad15136702 100644
--- a/web/src/features/external-media-storage/components/ExternalMediaStorageForm/ExternalMediaStorageForm.tsx
+++ b/web/src/features/external-media-storage/components/ExternalMediaStorageForm/ExternalMediaStorageForm.tsx
@@ -191,7 +191,8 @@ export function ExternalMediaStorageForm({
/>
</FormControl>
<FormDescription>
- Leave empty to keep the saved secret.
+ Leave empty to keep the saved secret, unless you change the
+ storage provider, bucket, endpoint, or Access Key ID.
</FormDescription>
<FormMessage />
</FormItem>
diff --git a/web/src/features/external-media-storage/server/service.ts b/web/src/features/external-media-storage/server/service.ts
index a0934863ad..bc6c459231 100644
--- a/web/src/features/external-media-storage/server/service.ts
+++ b/web/src/features/external-media-storage/server/service.ts
@@ -18,6 +18,7 @@ import {
import { getDisplayCredential } from "@/src/features/analytics-integrations/server";
import { auditLog } from "@/src/features/audit-logs/server";
+import { isSameStorageDestination } from "@/src/features/automations/destinationUrl";
import { type ExternalMediaStorageFormValues } from "@/src/features/external-media-storage/types";
import {
createExternalMediaStorageRepository,
@@ -161,8 +162,26 @@ export function createExternalMediaStorageService(prisma: PrismaClient) {
}
const existing = await repository.findByProjectId(projectId);
+ const bucketName = values.bucketName.trim();
const accessKeyId = values.accessKeyId?.trim() || null;
const secretAccessKey = values.secretAccessKey?.trim() || null;
+
+ // A stored secret is only used with the storage it was entered for.
+ if (
+ !secretAccessKey &&
+ existing?.secretAccessKey &&
+ !isSameStorageDestination(existing, {
+ type: values.type,
+ endpoint,
+ bucketName,
+ accessKeyId,
+ })
+ ) {
+ throw new InvalidRequestError(
+ "Secret Access Key is required when changing the storage provider, bucket, endpoint, or Access Key ID",
+ );
+ }
+
const encryptedSecretAccessKey = secretAccessKey
? encrypt(secretAccessKey)
: existing?.secretAccessKey;
@@ -177,7 +196,7 @@ export function createExternalMediaStorageService(prisma: PrismaClient) {
projectId,
data: {
type: values.type,
- bucketName: values.bucketName.trim(),
+ bucketName,
prefix: values.prefix?.trim() || null,
accessKeyId,
secretAccessKey: encryptedSecretAccessKey ?? null,
diff --git a/web/src/features/mcp/server/externalMediaStorage/tools.ts b/web/src/features/mcp/server/externalMediaStorage/tools.ts
index efc6ad6031..bde5811504 100644
--- a/web/src/features/mcp/server/externalMediaStorage/tools.ts
+++ b/web/src/features/mcp/server/externalMediaStorage/tools.ts
@@ -59,7 +59,7 @@ export const [
] = defineTool({
name: "configureExternalMediaStorage",
description:
- "Create or update the S3 external media storage configuration for the current project. Leave secretAccessKey empty to retain the saved secret.",
+ "Create or update the S3 external media storage configuration for the current project. Leave secretAccessKey empty to retain the saved secret; it is required again when type, endpoint, bucketName, or accessKeyId changes.",
action: "integrations:CRUD",
baseSchema: ExternalMediaStorageBaseSchema,
inputSchema: externalMediaStorageFormSchema,