@devries-cameron20 / composio Mirror · read-only / commits / 14a5b52b6
Fork 0

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

Mirrored from https://github.com/Camo67/composio.git

Synced Nov 21, 21:00 UTC

fix(connected-accounts): validate ACL input in ToolRouterSession.authorize()

Bugbot caught a parity gap (cursor[bot] review on #3392): authorize() accepted aclConfigForShared and passed it straight to the wire, while ConnectedAccounts.link() validated through CreateConnectedAccountLinkOptionsSchema (which enforces the 1000-entry list cap and 1..256 char user_id length from ConnectedAccountAclConfigSchema). Callers using authorize() with oversized lists or invalid user IDs got opaque backend 400s instead of clear SDK-level ValidationError, breaking the changeset's claim that "The SDK enforces these caps via Zod at the input boundary." Added a local `AuthorizeOptionsSchema` (callbackUrl + alias + accountType + aclConfigForShared) and a safeParse + ValidationError throw at the top of authorize(), mirroring the pattern in link() / updateAcl(). The shape is identical to link's options sub-shape so the same caps apply. Tests (toolRouter.test.ts): - forwards accountType + nested acl_config_for_shared on the wire - throws ValidationError when allowedUserIds exceeds 1000 entries - throws ValidationError when a user_id exceeds 256 chars - throws ValidationError when a user_id is empty 915 tests pass (4 new + 911 prior). pnpm typecheck clean. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

venkat82 committed May 12, 2026 at 00:26 UTC 14a5b52b60e1906b5f6d710b2ed14c52522edb0a
2 files changed +107 -10
ts/packages/core/src/models/ToolRouterSession.ts
+41 -10
@@ -27,10 +27,13 @@ import { SessionMetaToolOptions } from '../types/modifiers.types';
27 import { ConnectionRequest } from '../types/connectionRequest.types';
28 import { createConnectionRequest } from './ConnectionRequest';
29 import {
30 + ConnectedAccountAclConfigSchema,
31 ConnectedAccountStatuses,
32 ConnectedAccountType,
33 + ConnectedAccountTypeSchema,
34 ConnectedAccountAclConfig,
35 } from '../types/connectedAccounts.types';
36 +import { z } from 'zod/v3';
37 import { transform } from '../utils/transform';
38 import { ToolkitConnectionStateSchema } from '../types/toolRouter.types';
39 import { ComposioAclOnlyForSharedError, ValidationError } from '../errors';
@@ -69,6 +72,21 @@ const COMPOSIO_MULTI_EXECUTE_TOOL = 'COMPOSIO_MULTI_EXECUTE_TOOL';
72 export const DIRECT_CUSTOM_TOOL_DESCRIPTION_PREFIX =
73 '[Direct tool - call directly, no search needed beforehand.]';
74
75 +/**
76 + * Options accepted by {@link ToolRouterSession.authorize}.
77 + *
78 + * Validated at the SDK boundary so callers get clear `ValidationError`s for
79 + * oversized ACL lists or invalid user IDs — same Zod caps as the equivalent
80 + * `composio.connectedAccounts.link()` path (≤1000 entries per list, each
81 + * user_id 1..256 chars). Caught Bugbot review.
82 + */
83 +const AuthorizeOptionsSchema = z.object({
84 + callbackUrl: z.string().optional(),
85 + alias: z.string().optional(),
86 + accountType: ConnectedAccountTypeSchema.optional(),
87 + aclConfigForShared: ConnectedAccountAclConfigSchema.optional(),
88 +});
89 +
90 export class ToolRouterSession<
91 TToolCollection,
92 TTool,
@@ -312,6 +330,12 @@ export class ToolRouterSession<
330 * `accountType` and `aclConfigForShared` (Hermes #9860, #9902) let the
331 * caller create a SHARED connection with a per-user ACL in one flow.
332 * Default behaviour (omit both) creates a PRIVATE connection.
333 + *
334 + * `aclConfigForShared` is validated through `ConnectedAccountAclConfigSchema`
335 + * — same Zod caps as `composio.connectedAccounts.link()` (≤1000 entries
336 + * per list, each user_id 1..256 chars). Invalid input throws
337 + * `ValidationError` at the SDK boundary instead of hitting an opaque
338 + * backend 400.
339 */
340 async authorize(
341 toolkit: string,
@@ -322,25 +346,32 @@ export class ToolRouterSession<
346 aclConfigForShared?: ConnectedAccountAclConfig;
347 }
348 ): Promise<ConnectionRequest> {
349 + const requestOptions = AuthorizeOptionsSchema.safeParse(options ?? {});
350 + if (!requestOptions.success) {
351 + throw new ValidationError('Failed to parse tool router authorize options', {
352 + cause: requestOptions.error,
353 + });
354 + }
355 + const opts = requestOptions.data;
356 const aclWire: SessionLinkParams.ACLConfigForShared | undefined =
326 - options?.aclConfigForShared === undefined
357 + opts.aclConfigForShared === undefined
358 ? undefined
359 : {
329 - ...(options.aclConfigForShared.allowAllUsers !== undefined && {
330 - allow_all_users: options.aclConfigForShared.allowAllUsers,
360 + ...(opts.aclConfigForShared.allowAllUsers !== undefined && {
361 + allow_all_users: opts.aclConfigForShared.allowAllUsers,
362 }),
332 - ...(options.aclConfigForShared.allowedUserIds !== undefined && {
333 - allowed_user_ids: options.aclConfigForShared.allowedUserIds,
363 + ...(opts.aclConfigForShared.allowedUserIds !== undefined && {
364 + allowed_user_ids: opts.aclConfigForShared.allowedUserIds,
365 }),
335 - ...(options.aclConfigForShared.notAllowedUserIds !== undefined && {
336 - not_allowed_user_ids: options.aclConfigForShared.notAllowedUserIds,
366 + ...(opts.aclConfigForShared.notAllowedUserIds !== undefined && {
367 + not_allowed_user_ids: opts.aclConfigForShared.notAllowedUserIds,
368 }),
369 };
370 const body: SessionLinkParams = {
371 toolkit,
341 - ...(options?.callbackUrl !== undefined && { callback_url: options.callbackUrl }),
342 - ...(options?.alias !== undefined && { alias: options.alias }),
343 - ...(options?.accountType !== undefined && { account_type: options.accountType }),
372 + ...(opts.callbackUrl !== undefined && { callback_url: opts.callbackUrl }),
373 + ...(opts.alias !== undefined && { alias: opts.alias }),
374 + ...(opts.accountType !== undefined && { account_type: opts.accountType }),
375 ...(aclWire !== undefined && { acl_config_for_shared: aclWire }),
376 };
377
ts/packages/core/test/models/toolRouter.test.ts
+66
@@ -1848,6 +1848,72 @@ describe('ToolRouter', () => {
1848 expect(connectionRequest).toHaveProperty('toString');
1849 expect(typeof connectionRequest.waitForConnection).toBe('function');
1850 });
1851 +
1852 + // Bugbot-flagged gap: authorize() was passing aclConfigForShared straight
1853 + // to the wire while link() validated through Zod. These tests pin the
1854 + // parity — same 1000-entry list cap, same 256-char user_id cap.
1855 + it('forwards accountType + nested acl_config_for_shared on the wire', async () => {
1856 + mockClient.toolRouter.session.link.mockResolvedValueOnce(mockLinkResponse);
1857 +
1858 + const session = await toolRouter.create(userId);
1859 + await session.authorize(toolkit, {
1860 + accountType: 'SHARED',
1861 + aclConfigForShared: {
1862 + allowAllUsers: true,
1863 + notAllowedUserIds: ['user_bob'],
1864 + },
1865 + });
1866 +
1867 + expect(mockClient.toolRouter.session.link).toHaveBeenCalledWith(sessionId, {
1868 + toolkit,
1869 + account_type: 'SHARED',
1870 + acl_config_for_shared: {
1871 + allow_all_users: true,
1872 + not_allowed_user_ids: ['user_bob'],
1873 + },
1874 + });
1875 + });
1876 +
1877 + it('throws ValidationError when allowedUserIds exceeds the 1000-entry cap', async () => {
1878 + const session = await toolRouter.create(userId);
1879 + const oversizedList = Array.from({ length: 1001 }, (_, i) => `user_${i}`);
1880 +
1881 + await expect(
1882 + session.authorize(toolkit, {
1883 + accountType: 'SHARED',
1884 + aclConfigForShared: { allowedUserIds: oversizedList },
1885 + })
1886 + ).rejects.toMatchObject({ name: 'ValidationError' });
1887 +
1888 + expect(mockClient.toolRouter.session.link).not.toHaveBeenCalled();
1889 + });
1890 +
1891 + it('throws ValidationError when a user_id exceeds the 256-char length cap', async () => {
1892 + const session = await toolRouter.create(userId);
1893 + const tooLongUserId = 'u'.repeat(257);
1894 +
1895 + await expect(
1896 + session.authorize(toolkit, {
1897 + accountType: 'SHARED',
1898 + aclConfigForShared: { allowedUserIds: [tooLongUserId] },
1899 + })
1900 + ).rejects.toMatchObject({ name: 'ValidationError' });
1901 +
1902 + expect(mockClient.toolRouter.session.link).not.toHaveBeenCalled();
1903 + });
1904 +
1905 + it('throws ValidationError when a user_id is empty', async () => {
1906 + const session = await toolRouter.create(userId);
1907 +
1908 + await expect(
1909 + session.authorize(toolkit, {
1910 + accountType: 'SHARED',
1911 + aclConfigForShared: { notAllowedUserIds: [''] },
1912 + })
1913 + ).rejects.toMatchObject({ name: 'ValidationError' });
1914 +
1915 + expect(mockClient.toolRouter.session.link).not.toHaveBeenCalled();
1916 + });
1917 });
1918
1919 describe('toolkits function', () => {