Skip to content

Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

fix(cli): tie analytics identity to the active credential; harden logout + alias

Kshitij Jhunjhunwala2208ed65415f
diff --git a/ts/packages/cli/src/analytics/dispatch.ts b/ts/packages/cli/src/analytics/dispatch.ts
index 747ed1086..d0f387c2d 100644
--- a/ts/packages/cli/src/analytics/dispatch.ts
+++ b/ts/packages/cli/src/analytics/dispatch.ts
@@ -175,6 +175,10 @@ type AnalyticsState = {
// Tracked separately from apollo_user_id so an alias that was never enqueued
// (no project key, telemetry off) is retried instead of blocked forever.
readonly aliased_apollo_user_id?: string;
+ // Fingerprint of the credential that apollo_user_id was resolved from. The id
+ // is only trusted while the active credential still matches, so an env-var key
+ // override can never attribute one user's events to another.
+ readonly api_key_fingerprint?: string;
readonly created_at?: string;
};
@@ -209,12 +213,6 @@ const getOrCreateInstallId = Effect.gen(function* () {
return installId;
}).pipe(Effect.catchAll(() => makeInstallId));
-const readApolloUserId = Effect.map(readAnalyticsState, state =>
- typeof state?.apollo_user_id === 'string' && state.apollo_user_id.length > 0
- ? state.apollo_user_id
- : null
-);
-
const mergeAnalyticsState = (installId: string, patch: Partial<AnalyticsState>) =>
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
@@ -253,8 +251,26 @@ const getUserApiKey = Effect.gen(function* () {
: null;
});
+const fingerprintApiKey = (apiKey: string): string =>
+ `${djb2Hash(apiKey).toString(36)}.${apiKey.length.toString(36)}`;
+
+const getApiKeyFingerprint = Effect.map(getUserApiKey, apiKey =>
+ apiKey ? fingerprintApiKey(apiKey) : null
+);
+
const getDistinctId = (installId: string) =>
- Effect.map(readApolloUserId, apolloUserId => apolloUserId ?? installId);
+ Effect.gen(function* () {
+ const state = yield* readAnalyticsState;
+ const apolloUserId = state?.apollo_user_id;
+ if (typeof apolloUserId !== 'string' || apolloUserId.length === 0) {
+ return installId;
+ }
+
+ const fingerprint = yield* getApiKeyFingerprint;
+ return fingerprint !== null && fingerprint === state?.api_key_fingerprint
+ ? apolloUserId
+ : installId;
+ });
const cwdHash = (cwd: string): string => djb2Hash(cwd).toString(36);
@@ -584,11 +600,17 @@ export const linkApolloIdentityForAnalytics = (apolloUserId: string) =>
const state = yield* readAnalyticsState;
const installId = yield* getOrCreateInstallId;
- if (state?.apollo_user_id !== resolved) {
- yield* mergeAnalyticsState(installId, { apollo_user_id: resolved });
+ const fingerprint = yield* getApiKeyFingerprint;
+ if (state?.apollo_user_id !== resolved || state?.api_key_fingerprint !== fingerprint) {
+ yield* mergeAnalyticsState(installId, {
+ apollo_user_id: resolved,
+ ...(fingerprint === null ? {} : { api_key_fingerprint: fingerprint }),
+ });
}
- if (state?.aliased_apollo_user_id === resolved) {
+ // PostHog refuses to merge an install that is already identified, so alias
+ // an install at most once — a second user on this device stays unaliased.
+ if (typeof state?.aliased_apollo_user_id === 'string') {
return;
}
if (yield* emitPostHogAlias(installId, resolved)) {
@@ -598,8 +620,14 @@ export const linkApolloIdentityForAnalytics = (apolloUserId: string) =>
/** Drops the Apollo identity on logout so later events revert to `install_id`. */
export const clearApolloIdentityForAnalytics = Effect.gen(function* () {
- const installId = yield* getOrCreateInstallId;
- yield* mergeAnalyticsState(installId, { apollo_user_id: undefined });
+ const state = yield* readAnalyticsState;
+ if (!state?.install_id || !state.apollo_user_id) {
+ return;
+ }
+ yield* mergeAnalyticsState(state.install_id, {
+ apollo_user_id: undefined,
+ api_key_fingerprint: undefined,
+ });
}).pipe(Effect.catchAllCause(() => Effect.void));
export const trackCliCodactFailureEffect = (failure: CliCodactFailure) =>
diff --git a/ts/packages/cli/test/src/analytics.dispatch.test.ts b/ts/packages/cli/test/src/analytics.dispatch.test.ts
index e000aaed6..b1b148458 100644
--- a/ts/packages/cli/test/src/analytics.dispatch.test.ts
+++ b/ts/packages/cli/test/src/analytics.dispatch.test.ts
@@ -427,27 +427,24 @@ describe('CLI analytics dispatch', () => {
it.effect('keys post-login events on the persisted apollo_user_id', () => {
const home = tempy.temporaryDirectory();
const scriptPath = `${home}/composio.ts`;
- enableTelemetry('');
+ enableTelemetry('uak_persisted');
process.argv[1] = scriptPath;
return Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
- const path = yield* Path.Path;
yield* fs.writeFileString(scriptPath, '');
- const composioDir = path.join(home, '.composio');
- yield* fs.makeDirectory(composioDir, { recursive: true });
- yield* fs.writeFileString(
- path.join(composioDir, 'analytics.json'),
- JSON.stringify({ install_id: 'install_persisted', apollo_user_id: 'om_apollo_123' })
- );
+
+ // Establish the identity under this credential; the id is only trusted
+ // while that same credential is active.
+ yield* linkApolloIdentityForAnalytics('om_apollo_123');
+ childProcessMocks.spawn.mockClear();
yield* trackCliEventEffect({ name: CLI_ANALYTICS_EVENTS.CLI_EXECUTE_SUCCEEDED });
expect(childProcessMocks.spawn).toHaveBeenCalledTimes(1);
const args = childProcessMocks.spawn.mock.calls[0]![1] as string[];
- const payload = decodeWorkerPayload<{ distinctId: string; installId: string }>(args[2]!);
+ const payload = decodeWorkerPayload<{ distinctId: string }>(args[2]!);
expect(payload.distinctId).toBe('om_apollo_123');
- expect(payload.installId).toBe('install_persisted');
}).pipe(Effect.provide(makePlatformLayer(home)));
});
@@ -657,4 +654,64 @@ describe('CLI analytics dispatch', () => {
expect(fetchSpy).not.toHaveBeenCalled();
});
});
+
+ describe('CLI analytics identity safety', () => {
+ it.effect('does not attribute events to a stale identity when the active key differs', () => {
+ const home = tempy.temporaryDirectory();
+ const scriptPath = `${home}/composio.ts`;
+ enableTelemetry('uak_user_a');
+ process.argv[1] = scriptPath;
+
+ return Effect.gen(function* () {
+ const fs = yield* FileSystem.FileSystem;
+ yield* fs.writeFileString(scriptPath, '');
+ yield* linkApolloIdentityForAnalytics('om_user_a');
+ childProcessMocks.spawn.mockClear();
+
+ // A script running under a different credential must not inherit user A.
+ vi.stubEnv('COMPOSIO_USER_API_KEY', 'uak_user_b');
+ yield* trackCliEventEffect({ name: 'producer_event' });
+
+ const args = childProcessMocks.spawn.mock.calls[0]![1] as string[];
+ const payload = decodeWorkerPayload<{ distinctId: string; installId: string }>(args[2]!);
+ expect(payload.distinctId).not.toBe('om_user_a');
+ expect(payload.distinctId).toBe(payload.installId);
+ }).pipe(Effect.provide(makePlatformLayer(home)));
+ });
+
+ it.effect('does not create the identity file when logging out with telemetry disabled', () => {
+ const home = tempy.temporaryDirectory();
+ enableTelemetry();
+ vi.stubEnv('COMPOSIO_DISABLE_TELEMETRY', 'true');
+
+ return Effect.gen(function* () {
+ yield* clearApolloIdentityForAnalytics;
+
+ const fs = yield* FileSystem.FileSystem;
+ const path = yield* Path.Path;
+ const exists = yield* fs.exists(path.join(home, '.composio', 'analytics.json'));
+ expect(exists).toBe(false);
+ }).pipe(Effect.provide(makePlatformLayer(home)));
+ });
+
+ it.effect('never re-aliases one install onto a second Apollo user', () => {
+ const home = tempy.temporaryDirectory();
+ const scriptPath = `${home}/composio.ts`;
+ enableTelemetry();
+ process.argv[1] = scriptPath;
+
+ return Effect.gen(function* () {
+ const fs = yield* FileSystem.FileSystem;
+ yield* fs.writeFileString(scriptPath, '');
+
+ yield* linkApolloIdentityForAnalytics('om_first_user');
+ expect(childProcessMocks.spawn).toHaveBeenCalledTimes(1);
+
+ // Logout, then log in as a different user on the same device.
+ yield* clearApolloIdentityForAnalytics;
+ yield* linkApolloIdentityForAnalytics('om_second_user');
+ expect(childProcessMocks.spawn).toHaveBeenCalledTimes(1);
+ }).pipe(Effect.provide(makePlatformLayer(home)));
+ });
+ });
});