Skip to content

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

chore(secret-sharing): keep share and request ids out of telemetry

Vlad Ligaidf6b9e2cafc3
diff --git a/backend/src/server/routes/v1/secret-requests-router.test.ts b/backend/src/server/routes/v1/secret-requests-router.test.ts
new file mode 100644
index 0000000000..cb4a042778
--- /dev/null
+++ b/backend/src/server/routes/v1/secret-requests-router.test.ts
@@ -0,0 +1,56 @@
+import Fastify, { FastifyInstance } from "fastify";
+import { vi } from "vitest";
+
+import { serializerCompiler, validatorCompiler, ZodTypeProvider } from "@app/server/plugins/fastify-zod";
+import { AuthMode } from "@app/services/auth/auth-type";
+
+import { registerSecretRequestsRouter } from "./secret-requests-router";
+
+const URL = "/api/v1/shared-secrets/requests";
+const REQUEST_ID = "6f1c2c1e-6c55-4b39-9a39-2f3c8a1d9b7e";
+
+const buildServer = async () => {
+ const sendPostHogEvents = vi.fn().mockResolvedValue(undefined);
+
+ const app = Fastify({ logger: false }).withTypeProvider<ZodTypeProvider>();
+ app.setValidatorCompiler(validatorCompiler);
+ app.setSerializerCompiler(serializerCompiler);
+
+ app.decorate("services", {
+ secretSharing: { createSecretRequest: vi.fn().mockResolvedValue({ id: REQUEST_ID }) },
+ auditLog: { createAuditLog: vi.fn().mockResolvedValue(undefined) },
+ telemetry: { sendPostHogEvents }
+ } as never);
+ app.addHook("onRequest", async (req) => {
+ Object.assign(req, {
+ auth: { authMode: AuthMode.JWT },
+ permission: { orgId: "org-id", id: "user-id" },
+ auditLogInfo: {}
+ });
+ });
+
+ await app.register(registerSecretRequestsRouter as never, { prefix: URL });
+ await app.ready();
+
+ return { app, sendPostHogEvents };
+};
+
+describe("secret request routes", () => {
+ let app: FastifyInstance | undefined;
+
+ afterEach(async () => {
+ await app?.close();
+ app = undefined;
+ });
+
+ test("a new secret request's id is not sent to analytics", async () => {
+ const server = await buildServer();
+ app = server.app;
+
+ const res = await app.inject({ method: "POST", url: URL, body: { expiresIn: "1d" } });
+
+ expect(res.statusCode).toBe(200);
+ expect(server.sendPostHogEvents).toHaveBeenCalledTimes(1);
+ expect(JSON.stringify(server.sendPostHogEvents.mock.calls)).not.toContain(REQUEST_ID);
+ });
+});
diff --git a/backend/src/server/routes/v1/secret-requests-router.ts b/backend/src/server/routes/v1/secret-requests-router.ts
index dbf89e4c91..62a84cfbe1 100644
--- a/backend/src/server/routes/v1/secret-requests-router.ts
+++ b/backend/src/server/routes/v1/secret-requests-router.ts
@@ -233,7 +233,6 @@ export const registerSecretRequestsRouter = async (server: FastifyZodProvider) =
distinctId: getTelemetryDistinctId(req),
organizationId: req.permission.orgId,
properties: {
- secretRequestId: req.params.id,
organizationId: req.permission.orgId,
...req.auditLogInfo
}
@@ -355,7 +354,6 @@ export const registerSecretRequestsRouter = async (server: FastifyZodProvider) =
distinctId: getTelemetryDistinctId(req),
organizationId: req.permission.orgId,
properties: {
- secretRequestId: shareRequest.id,
organizationId: req.permission.orgId,
secretRequestName: req.body.name,
...req.auditLogInfo
diff --git a/backend/src/server/routes/v1/secret-sharing-router.test.ts b/backend/src/server/routes/v1/secret-sharing-router.test.ts
new file mode 100644
index 0000000000..05f8e8ac0a
--- /dev/null
+++ b/backend/src/server/routes/v1/secret-sharing-router.test.ts
@@ -0,0 +1,79 @@
+import Fastify, { FastifyInstance } from "fastify";
+import { vi } from "vitest";
+
+import { serializerCompiler, validatorCompiler, ZodTypeProvider } from "@app/server/plugins/fastify-zod";
+
+import { registerSecretSharingRouter } from "./secret-sharing-router";
+
+vi.mock("@app/lib/config/env", async (importOriginal) => ({
+ ...(await importOriginal<typeof import("@app/lib/config/env")>()),
+ getConfig: () => ({ DISABLE_PUBLIC_SECRET_SHARING: false })
+}));
+
+const URL = "/api/v1/shared-secrets";
+const SHARE_ID = "q1w2e3r4t5y6u7i8o9p0a1s2d3f4g5h6j7k8l9z0x1c";
+
+const buildServer = async () => {
+ const sendPostHogEvents = vi.fn().mockResolvedValue(undefined);
+ const share = {
+ id: SHARE_ID,
+ orgId: null,
+ accessType: "anyone",
+ type: "share",
+ expiresAt: new Date(Date.now() + 3_600_000),
+ createdAt: new Date(),
+ updatedAt: new Date()
+ };
+
+ const app = Fastify({ logger: false }).withTypeProvider<ZodTypeProvider>();
+ app.setValidatorCompiler(validatorCompiler);
+ app.setSerializerCompiler(serializerCompiler);
+
+ app.decorate("services", {
+ secretSharing: {
+ createPublicSharedSecret: vi.fn().mockResolvedValue(share),
+ accessSharedSecret: vi.fn().mockResolvedValue({ ...share, secretValue: "value" })
+ },
+ telemetry: { sendPostHogEvents }
+ } as never);
+
+ await app.register(registerSecretSharingRouter as never, { prefix: URL });
+ await app.ready();
+
+ return { app, sendPostHogEvents };
+};
+
+describe("secret sharing routes", () => {
+ let app: FastifyInstance | undefined;
+
+ afterEach(async () => {
+ await app?.close();
+ app = undefined;
+ });
+
+ test("a new public share's id is not sent to analytics", async () => {
+ const server = await buildServer();
+ app = server.app;
+
+ const res = await app.inject({
+ method: "POST",
+ url: `${URL}/public`,
+ body: { secretValue: "value", expiresIn: "1h" }
+ });
+
+ expect(res.statusCode).toBe(200);
+ expect(server.sendPostHogEvents).toHaveBeenCalledTimes(1);
+ expect(JSON.stringify(server.sendPostHogEvents.mock.calls)).not.toContain(SHARE_ID);
+ });
+
+ test("a viewed share's id is not sent to analytics", async () => {
+ const server = await buildServer();
+ app = server.app;
+
+ const res = await app.inject({ method: "POST", url: `${URL}/${SHARE_ID}/access`, body: {} });
+
+ expect(res.statusCode).toBe(200);
+ expect(server.sendPostHogEvents).toHaveBeenCalledTimes(1);
+ expect(JSON.stringify(server.sendPostHogEvents.mock.calls)).not.toContain(SHARE_ID);
+ });
+});
diff --git a/backend/src/server/routes/v1/secret-sharing-router.ts b/backend/src/server/routes/v1/secret-sharing-router.ts
index f71243ee95..2bf3396bec 100644
--- a/backend/src/server/routes/v1/secret-sharing-router.ts
+++ b/backend/src/server/routes/v1/secret-sharing-router.ts
@@ -5,6 +5,7 @@ import { z } from "zod";
import { EventType } from "@app/ee/services/audit-log/audit-log-types";
import { ApiDocsTags, SECRET_SHARING } from "@app/lib/api-docs";
import { getConfig } from "@app/lib/config/env";
+import { crypto } from "@app/lib/crypto/cryptography";
import { BadRequestError, NotFoundError } from "@app/lib/errors";
import { unique } from "@app/lib/fn";
import { ms } from "@app/lib/ms";
@@ -27,6 +28,10 @@ import { SanitizedSecretSharingSchema } from "../sanitizedSchemas";
const ALLOWED_IMAGE_CONTENT_TYPES = ["image/png", "image/jpeg"];
const MAX_IMAGE_SIZE = 1 * 1024 * 1024; // 1MB
+// Keeps resource ids out of analytics (ANALYTICS.md) while a share's events still correlate.
+const getAnonymousShareDistinctId = (sharedSecretId: string) =>
+ `anonymous-${crypto.nativeCrypto.createHash("sha256").update(sharedSecretId).digest("hex")}`;
+
export const registerSecretSharingRouter = async (server: FastifyZodProvider) => {
// Allow multipart file uploads
await server.register(fastifyMultipart, {
@@ -167,16 +172,15 @@ export const registerSecretSharingRouter = async (server: FastifyZodProvider) =>
await server.services.telemetry.sendPostHogEvents({
event: PostHogEventTypes.SharedSecretViewed,
- distinctId: req.permission?.id ? getTelemetryDistinctId(req) : `anonymous-${req.params.id}`,
+ distinctId: req.permission?.id ? getTelemetryDistinctId(req) : getAnonymousShareDistinctId(sharedSecret.id),
// Suppress person-record creation for unauthenticated viewers — the
- // `anonymous-<shareId>` distinctId is synthesised per share and has
+ // `anonymous-<share hash>` distinctId is synthesised per share and has
// no continuity with any real user or identity, so creating a
// PostHog person for it inflates the person count without adding
// any analytical value.
anonymous: !req.permission?.id,
organizationId: sharedSecret.orgId ?? undefined,
properties: {
- sharedSecretId: req.params.id,
accessType: sharedSecret.accessType as SecretSharingAccessType
}
});
@@ -247,7 +251,7 @@ export const registerSecretSharingRouter = async (server: FastifyZodProvider) =>
await server.services.telemetry.sendPostHogEvents({
event: PostHogEventTypes.SecretShared,
- distinctId: `anonymous-${sharedSecret.id}`,
+ distinctId: getAnonymousShareDistinctId(sharedSecret.id),
// The public share endpoint is unauthenticated and the distinctId
// is unique per share, so creating a PostHog person for each share
// would inflate the person count by one record per share forever.
diff --git a/backend/src/services/telemetry/telemetry-service.ts b/backend/src/services/telemetry/telemetry-service.ts
index f4587db971..51957d26ff 100644
--- a/backend/src/services/telemetry/telemetry-service.ts
+++ b/backend/src/services/telemetry/telemetry-service.ts
@@ -371,7 +371,7 @@ To opt into telemetry, you can set "TELEMETRY_ENABLED=true" within the environme
// posthog-node implements `groupIdentify` as a regular capture of a
// `$groupidentify` event keyed by the supplied `distinctId`. For an
// anonymous-share viewer of an org-scoped share, that `distinctId`
- // is the synthesised `anonymous-<shareId>` value, so calling
+ // is the synthesised `anonymous-<share hash>` value, so calling
// groupIdentify here would create exactly the per-share person
// record we are trying to suppress — defeating the
// `$process_person_profile: false` flag we set on the explicit
@@ -407,7 +407,7 @@ To opt into telemetry, you can set "TELEMETRY_ENABLED=true" within the environme
// When `anonymous` is set, we still record the event but instruct
// PostHog not to create or update a person record for the synthesised
// distinctId. This prevents per-request distinctIds (e.g. the
- // `anonymous-<shareId>` keys used by unauthenticated public secret
+ // `anonymous-<share hash>` keys used by unauthenticated public secret
// shares) from inflating the person count while preserving event
// counts, funnels, and breakdowns.
const instanceId = await getInstanceId();
diff --git a/backend/src/services/telemetry/telemetry-types.ts b/backend/src/services/telemetry/telemetry-types.ts
index c9435c897b..4242cf250c 100644
--- a/backend/src/services/telemetry/telemetry-types.ts
+++ b/backend/src/services/telemetry/telemetry-types.ts
@@ -705,7 +705,6 @@ export type TTelemetryInstanceStatsEvent = {
export type TSecretRequestCreatedEvent = {
event: PostHogEventTypes.SecretRequestCreated;
properties: {
- secretRequestId: string;
organizationId: string;
secretRequestName?: string;
};
@@ -714,7 +713,6 @@ export type TSecretRequestCreatedEvent = {
export type TSecretRequestDeletedEvent = {
event: PostHogEventTypes.SecretRequestDeleted;
properties: {
- secretRequestId: string;
organizationId: string;
};
};
@@ -796,7 +794,6 @@ export type TSecretSharedEvent = {
export type TSharedSecretViewedEvent = {
event: PostHogEventTypes.SharedSecretViewed;
properties: {
- sharedSecretId: string;
accessType: SecretSharingAccessType;
};
};