@devries-cameron20 / composio-1 / commits / a69f82d67
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.

fix(sdk): run typedoc without a shell in generate-docs (SEC-899) (#4416)

## Summary `ts/packages/core/scripts/generate-docs.ts` joined `npx typedoc` and every discovered `src/models/*.ts` file name into one string and ran it with `execSync`, so a model file whose name contains shell metacharacters would execute as a command. The script runs in CI on every push to `next` with a write-scoped app token (`generate-sdk-docs.yml`). Reported by AppSecure as SEC-899 (command injection via documentation generation). Fixes SEC-899 (internal tracker). ## Changes - Build the typedoc argument vector as an array (`buildTypeDocArgs`) and run it with `execFileSync`, so no shell is involved. - Skip model files whose names fall outside `[A-Za-z0-9_.-]` (with a warning) in `discoverModelFiles`. - Regression tests in `test/scripts/generate-docs.test.ts`: metacharacter names are dropped, entry points stay separate arguments. ## Type of change - [x] Bug fix - [ ] New feature - [ ] Refactor/Chore - [ ] Documentation - [ ] Breaking change ## How Has This Been Tested? ``` pnpm --filter @composio/core exec vitest run test/scripts/generate-docs.test.ts Test Files 1 passed (1) Tests 12 passed (12) pnpm exec prettier --check ts/packages/core/scripts/generate-docs.ts ts/packages/core/test/scripts/generate-docs.test.ts All matched files use Prettier code style! ``` Node 24.17.0 and pnpm 11.8.0 via mise. ## Screenshots (if applicable) ## Checklist - [x] I have read the Code of Conduct and this PR adheres to it - [x] I ran linters/tests locally and they passed - [x] I updated documentation as needed - [x] I added tests or explain why not applicable - [x] I added a changeset if this change affects published packages (not needed: `scripts/` is a build-time script, not part of the published package) ## Additional context The generated docs output is unchanged; only how typedoc is invoked changes. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com> Co-authored-by: jkomyno <alberto@composio.dev>

Saransh Rana committed Sep 10, 2026 at 20:44 UTC a69f82d6761f2e9200c4a65fa01289dbe867f990
2 files changed +123 -18
ts/packages/core/scripts/generate-docs.ts
+60 -17
@@ -10,7 +10,7 @@
10 import { readFileSync } from 'fs';
11 import { mkdir, writeFile, rm, readdir, readFile } from 'fs/promises';
12 import { join, dirname, resolve } from 'path';
13 -import { execSync } from 'child_process';
13 +import { execFileSync } from 'child_process';
14 import { fileURLToPath, pathToFileURL } from 'url';
15
16 // Paths (relative to ts/packages/core)
@@ -19,6 +19,11 @@ const PACKAGE_DIR = join(SCRIPT_DIR, '..');
19 const MODELS_DIR = join(PACKAGE_DIR, 'src/models');
20 const OUTPUT_DIR = join(PACKAGE_DIR, '../../../docs/content/reference/sdk-reference/typescript');
21 const TEMP_JSON = join(PACKAGE_DIR, '.typedoc-output.json');
22 +const TYPEDOC_BIN = join(
23 + dirname(fileURLToPath(import.meta.resolve('typedoc/package.json'))),
24 + 'bin',
25 + 'typedoc'
26 +);
27
28 // Internal classes that should NOT be documented (accessed via other APIs)
29 const INTERNAL_CLASSES = new Set([
@@ -55,14 +60,65 @@ function slugFor(className: string): string {
60 return SLUG_OVERRIDES[className] ?? toKebabCase(className);
61 }
62
63 +// Model file names become typedoc arguments. They are never interpolated into a
64 +// shell, but a name outside this charset is still not a model and is skipped
65 +// rather than passed through.
66 +const MODEL_FILE_NAME = /^[A-Za-z0-9_.-]+\.ts$/;
67 +
68 // Discover model files automatically
59 -async function discoverModelFiles(): Promise<string[]> {
60 - const files = await readdir(MODELS_DIR);
69 +export async function discoverModelFiles(modelsDir: string = MODELS_DIR): Promise<string[]> {
70 + const files = await readdir(modelsDir);
71 return files
72 .filter(f => f.endsWith('.ts') && !f.includes('.test.') && !f.includes('.spec.'))
73 + .filter(f => {
74 + if (MODEL_FILE_NAME.test(f)) {
75 + return true;
76 + }
77 + console.warn(` Skipping model file with an unexpected name: ${JSON.stringify(f)}`);
78 + return false;
79 + })
80 .map(f => `src/models/${f}`);
81 }
82
83 +// TypeDoc arguments, one array element per argument so the entry points are
84 +// never joined into a shell command line.
85 +function buildTypeDocArgs(
86 + entryPoints: readonly string[],
87 + outputJson: string = TEMP_JSON
88 +): string[] {
89 + return [
90 + '--json',
91 + outputJson,
92 + '--tsconfig',
93 + 'tsconfig.json',
94 + '--excludePrivate',
95 + '--excludeProtected',
96 + '--excludeInternal',
97 + '--skipErrorChecking', // Skip TS errors, we just want the documentation
98 + ...entryPoints,
99 + ];
100 +}
101 +
102 +interface TypeDocCommandOptions {
103 + outputJson?: string;
104 + packageDir?: string;
105 + typedocBin?: string;
106 +}
107 +
108 +export function runTypeDocCommand(
109 + entryPoints: readonly string[],
110 + {
111 + outputJson = TEMP_JSON,
112 + packageDir = PACKAGE_DIR,
113 + typedocBin = TYPEDOC_BIN,
114 + }: TypeDocCommandOptions = {}
115 +): void {
116 + execFileSync(process.execPath, [typedocBin, ...buildTypeDocArgs(entryPoints, outputJson)], {
117 + stdio: 'pipe',
118 + cwd: packageDir,
119 + });
120 +}
121 +
122 // Discover classes to document from TypeDoc output
123 function discoverClassesToDocument(project: TypeDocProject): string[] {
124 const classes: string[] = [];
@@ -1132,21 +1188,8 @@ async function runTypeDoc(): Promise<TypeDocProject> {
1188
1189 console.log(` Found ${entryPoints.length} entry points`);
1190
1135 - const cmd = [
1136 - 'npx typedoc',
1137 - '--json',
1138 - TEMP_JSON,
1139 - '--tsconfig',
1140 - 'tsconfig.json',
1141 - '--excludePrivate',
1142 - '--excludeProtected',
1143 - '--excludeInternal',
1144 - '--skipErrorChecking', // Skip TS errors, we just want the documentation
1145 - ...entryPoints,
1146 - ].join(' ');
1147 -
1191 try {
1149 - execSync(cmd, { stdio: 'pipe', cwd: PACKAGE_DIR });
1192 + runTypeDocCommand(entryPoints);
1193 } catch (error) {
1194 console.error('TypeDoc failed:', error);
1195 throw error;
ts/packages/core/test/scripts/generate-docs.test.ts
+63 -1
@@ -1,8 +1,13 @@
1 -import { describe, expect, it } from 'vitest';
1 +import { mkdtemp, readFile, rm, writeFile } from 'node:fs/promises';
2 +import { tmpdir } from 'node:os';
3 +import { join } from 'node:path';
4 +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
5 import {
6 + discoverModelFiles,
7 escapeTextForMdx,
8 escapeTypeForMdx,
9 parseSourceSignatureTypesAtLine,
10 + runTypeDocCommand,
11 simplifyTypeForSignature,
12 simplifyTypeForTable,
13 } from '../../scripts/generate-docs';
@@ -119,3 +124,60 @@ class Example {
124 expect(signature?.returnType).toBe('void');
125 });
126 });
127 +
128 +describe('generate-docs command construction', () => {
129 + let modelsDir: string;
130 +
131 + beforeEach(async () => {
132 + modelsDir = await mkdtemp(join(tmpdir(), 'composio-generate-docs-'));
133 + });
134 +
135 + afterEach(async () => {
136 + await rm(modelsDir, { recursive: true, force: true });
137 + vi.restoreAllMocks();
138 + });
139 +
140 + it('keeps only plainly named model files and drops shell metacharacter names', async () => {
141 + vi.spyOn(console, 'warn').mockImplementation(() => undefined);
142 + for (const name of [
143 + 'Files.ts',
144 + 'tool_router.ts',
145 + 'x;touch pwned.ts',
146 + '$(id).ts',
147 + 'a b.ts',
148 + 'Files.test.ts',
149 + 'notes.md',
150 + ]) {
151 + await writeFile(join(modelsDir, name), '');
152 + }
153 +
154 + const discovered = await discoverModelFiles(modelsDir);
155 +
156 + expect(discovered.sort()).toEqual(['src/models/Files.ts', 'src/models/tool_router.ts']);
157 + });
158 +
159 + it('runs TypeDoc with Node and passes every entry point as its own argument', async () => {
160 + const typedocBin = join(modelsDir, 'typedoc');
161 + const outputJson = join(modelsDir, 'typedoc-args.txt');
162 + await writeFile(
163 + typedocBin,
164 + `const { writeFileSync } = require('node:fs');
165 +const args = process.argv.slice(2);
166 +const outputJson = args[args.indexOf('--json') + 1];
167 +writeFileSync(outputJson, args.join('\\n'));
168 +`
169 + );
170 +
171 + runTypeDocCommand(['src/composio.ts', 'src/models/a b.ts'], {
172 + outputJson,
173 + packageDir: modelsDir,
174 + typedocBin,
175 + });
176 +
177 + const args = (await readFile(outputJson, 'utf8')).split('\n');
178 + expect(args[0]).toBe('--json');
179 + expect(args).toContain('src/models/a b.ts');
180 + expect(args.some(arg => arg.includes('npx'))).toBe(false);
181 + expect(args.slice(-2)).toEqual(['src/composio.ts', 'src/models/a b.ts']);
182 + });
183 +});