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.

security: fix 3 CodeQL alerts (2026-07-22) (#3909)

Saransh Rana577a3d40139e
diff --git a/.changeset/fix-workerd-slash-trim-redos.md b/.changeset/fix-workerd-slash-trim-redos.md
new file mode 100644
index 000000000..b1b8b0579
--- /dev/null
+++ b/.changeset/fix-workerd-slash-trim-redos.md
@@ -0,0 +1,5 @@
+---
+'@composio/core': patch
+---
+
+Replace the backtracking leading/trailing-slash-trim regexes in the Cloudflare Workers/Edge platform path helpers with index-walk loops, closing a polynomial-time regular expression denial-of-service (CodeQL js/polynomial-redos) on long runs of slash characters. Output is unchanged for every input.
diff --git a/ts/packages/core/src/platform/workerd.ts b/ts/packages/core/src/platform/workerd.ts
index bf57730b8..f671741b6 100644
--- a/ts/packages/core/src/platform/workerd.ts
+++ b/ts/packages/core/src/platform/workerd.ts
@@ -1,5 +1,32 @@
import type { Platform, Uint8ArrayEncoding } from './types';
+const SLASH_CHAR_CODE = 47; // '/'
+
+/**
+ * Trailing-slash trim without a regex. The equivalent pattern backtracks
+ * quadratically on long runs of slashes (CodeQL js/polynomial-redos), so this
+ * walks the string from the end instead. Returns the input unchanged when
+ * there is nothing to trim, so the common case allocates nothing.
+ */
+const trimTrailingSlashes = (value: string): string => {
+ let end = value.length;
+ while (end > 0 && value.charCodeAt(end - 1) === SLASH_CHAR_CODE) end--;
+ return end === value.length ? value : value.slice(0, end);
+};
+
+/**
+ * Leading-and-trailing slash trim without a regex, matching the semantics of
+ * the previous leading/trailing slash-trim replace for every input, including
+ * a string that is entirely slashes.
+ */
+const trimSurroundingSlashes = (value: string): string => {
+ let start = 0;
+ let end = value.length;
+ while (start < end && value.charCodeAt(start) === SLASH_CHAR_CODE) start++;
+ while (end > start && value.charCodeAt(end - 1) === SLASH_CHAR_CODE) end--;
+ return start === 0 && end === value.length ? value : value.slice(start, end);
+};
+
/**
* Cloudflare Workers / Edge runtime platform implementation.
* Provides stub implementations for file system operations that are unavailable in edge runtimes.
@@ -18,9 +45,9 @@ export const platform: Platform = {
return paths
.map((segment, index) => {
if (index === 0) {
- return segment.replace(/\/+$/, '');
+ return trimTrailingSlashes(segment);
}
- return segment.replace(/^\/+|\/+$/g, '');
+ return trimSurroundingSlashes(segment);
})
.filter(Boolean)
.join('/');
@@ -40,7 +67,7 @@ export const platform: Platform = {
basename(filePath: string): string {
// Simple basename extraction without Node.js path module
- const segments = filePath.replace(/\/+$/, '').split('/');
+ const segments = trimTrailingSlashes(filePath).split('/');
return segments[segments.length - 1] || '';
},
diff --git a/ts/packages/core/test/platform/workerd.test.ts b/ts/packages/core/test/platform/workerd.test.ts
new file mode 100644
index 000000000..7832e5f3c
--- /dev/null
+++ b/ts/packages/core/test/platform/workerd.test.ts
@@ -0,0 +1,103 @@
+import { readFileSync } from 'node:fs';
+import path from 'node:path';
+import { fileURLToPath } from 'node:url';
+import { describe, expect, it } from 'vitest';
+import { platform } from '../../src/platform/workerd';
+
+const __dirname = path.dirname(fileURLToPath(import.meta.url));
+const WORKERD_SOURCE = path.resolve(__dirname, '../../src/platform/workerd.ts');
+
+/**
+ * Byte-for-byte copies of the regex logic that shipped before the
+ * js/polynomial-redos fix. The rewritten implementation must agree with these
+ * on every input, so behavior is pinned without asserting on timing.
+ */
+const referenceJoinPath = (...paths: string[]): string =>
+ paths
+ .map((segment, index) =>
+ index === 0 ? segment.replace(/\/+$/, '') : segment.replace(/^\/+|\/+$/g, '')
+ )
+ .filter(Boolean)
+ .join('/');
+
+const referenceBasename = (filePath: string): string => {
+ const segments = filePath.replace(/\/+$/, '').split('/');
+ return segments[segments.length - 1] || '';
+};
+
+const CASES = [
+ '',
+ '/',
+ '//',
+ '///',
+ 'a',
+ '/a',
+ 'a/',
+ '/a/',
+ '//a//',
+ 'a/b',
+ '/a/b/',
+ 'a//b',
+ '.',
+ '..',
+ './a',
+ 'foo.txt',
+ '/tmp/foo.txt',
+ 'tmp//',
+ '//tmp//foo.txt//',
+ ' ',
+ '/ /',
+ '/'.repeat(64),
+ `${'/'.repeat(32)}a${'/'.repeat(32)}`,
+];
+
+describe('workerd platform path helpers (js/polynomial-redos regression)', () => {
+ it('basename matches the pre-fix regex implementation for every input', () => {
+ for (const input of CASES) {
+ expect(platform.basename(input)).toBe(referenceBasename(input));
+ }
+ });
+
+ it('joinPath matches the pre-fix regex implementation for single segments', () => {
+ for (const input of CASES) {
+ expect(platform.joinPath(input)).toBe(referenceJoinPath(input));
+ }
+ });
+
+ it('joinPath matches the pre-fix regex implementation for every segment pair', () => {
+ for (const first of CASES) {
+ for (const second of CASES) {
+ expect(platform.joinPath(first, second)).toBe(referenceJoinPath(first, second));
+ }
+ }
+ });
+
+ it('joinPath matches the pre-fix regex implementation for three segments', () => {
+ for (const middle of CASES) {
+ expect(platform.joinPath('/base', middle, 'leaf.txt')).toBe(
+ referenceJoinPath('/base', middle, 'leaf.txt')
+ );
+ }
+ });
+
+ it('returns the documented result for long runs of slashes', () => {
+ const allSlashes = '/'.repeat(50_000);
+ expect(platform.basename(allSlashes)).toBe('');
+ expect(platform.joinPath(allSlashes)).toBe('');
+ expect(platform.joinPath(allSlashes, `${allSlashes}name.txt${allSlashes}`)).toBe('name.txt');
+ });
+
+ it('rejects backtracking-prone slash-trim regexes at the source level', () => {
+ const source = readFileSync(WORKERD_SOURCE, 'utf8');
+ // Matches an escaped slash immediately followed by a `+`/`*` or a
+ // `{n,m}` quantifier -- the shape that backtracks quadratically on long
+ // slash runs, regardless of exact spelling or whitespace. This catches
+ // not just the original `/\/+$/` and `/^\/+|\/+$/g` literals but
+ // reformatted equivalents like `/\/{1,}$/` or `new RegExp('\\/+$')`.
+ // Scans raw file text, including comments, so quoting a vulnerable
+ // pattern in a comment would also fail this assertion -- intentional,
+ // since that's the only way to catch a silent revert to the regex form.
+ const VULNERABLE_SLASH_QUANTIFIER = /\\\/(?:[+*]|\{\d*,?\d*\})/;
+ expect(source).not.toMatch(VULNERABLE_SLASH_QUANTIFIER);
+ });
+});