@devries-cameron20 / composio-1 / commits / cdd830659
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): check for device names after fitting the file name

Truncation and trailing space or dot removal ran after the reserved device check, so they could expose one: `NUL` followed by 200 spaces and `x.txt` fitted to `NUL<spaces>.txt`, which Windows opens as the null device. Fit first, check the fitted name, and fit again after adding the `_` prefix so the byte bound still holds. Same order in both SDKs.

jkomyno committed Sep 29, 2026 at 15:04 UTC cdd83065984f47077f1742971485a3d6d4888e70
4 files changed +41 -20
python/composio/utils/safe_path.py
+14 -10
@@ -202,10 +202,10 @@ def safe_basename(name: str, *, label: str = "filename") -> str:
202 ordinary files have them (``report_2026-09-29T10:30:00.csv``,
203 ``What is this?.png``), on every platform so a name does not depend on
204 where the SDK runs: control and Windows-reserved characters become ``_``,
205 - reserved device names get a ``_`` prefix, names over
206 - :data:`MAX_COMPONENT_LENGTH` bytes are truncated with their extension kept,
207 - and trailing spaces and dots are dropped as Windows would. ``safeBasename``
208 - in the TypeScript SDK applies the same rules in the same order.
205 + names over :data:`MAX_COMPONENT_LENGTH` bytes are truncated with their
206 + extension kept, trailing spaces and dots are dropped as Windows would, and
207 + a resulting reserved device name gets a ``_`` prefix. ``safeBasename`` in
208 + the TypeScript SDK applies the same rules in the same order.
209
210 ``str.strip`` removes surrounding whitespace first, and the usability check
211 runs last, on the value that gets written, so neither ``"\\u00a0.\\u00a0"``
@@ -231,14 +231,18 @@ def safe_basename(name: str, *, label: str = "filename") -> str:
231 f"Refusing to write {label} containing invalid Unicode: {name!r}"
232 ) from e
233
234 - portable = _WINDOWS_INVALID_FILENAME_CHARS.sub("_", basename)
235 - # Compare everything before the first dot: on Windows `NUL.tar.gz` opens
236 - # the null device just as `NUL` does, so any number of extensions provides
237 - # no protection.
234 + def fit(value: str) -> str:
235 + return _fit_filename_bytes(value).rstrip(". ")
236 +
237 + portable = fit(_WINDOWS_INVALID_FILENAME_CHARS.sub("_", basename))
238 + # Checked on the fitted name, because truncation and trailing-dot removal
239 + # can expose one (`NUL` followed by spaces and a long tail). Compare
240 + # everything before the first dot: on Windows `NUL.tar.gz` opens the null
241 + # device just as `NUL` does, so extensions provide no protection. Fitting
242 + # again keeps the byte bound, and a `_`-prefixed name is never a device.
243 device_name = portable.split(".", 1)[0].rstrip(" ").upper()
244 if device_name in WINDOWS_RESERVED_NAMES:
240 - portable = f"_{portable}"
241 - portable = _fit_filename_bytes(portable).rstrip(". ")
245 + portable = fit(f"_{portable}")
246
247 if not portable:
248 raise UnsafePathComponentError(
python/tests/test_safe_path.py
+6
@@ -230,6 +230,12 @@ class TestSafeBasename:
230 def test_prefixes_windows_device_names_with_any_extension(self, value, expected):
231 assert safe_basename(value) == expected
232
233 + def test_prefixes_a_device_name_exposed_by_truncation(self):
234 + # Truncation keeps `NUL` plus spaces before `.txt`, and Windows ignores
235 + # the spaces, so the checked name must be the fitted one.
236 + assert safe_basename("NUL" + " " * 200 + "x.txt") == "_NUL" + " " * 120 + ".txt"
237 + assert safe_basename("CON" + " " * 200 + "x") == "_CON"
238 +
239 def test_keeps_a_filename_at_the_byte_limit(self):
240 name = "x" * MAX_COMPONENT_LENGTH
241 assert safe_basename(name) == name
ts/packages/core/src/utils/safePath.ts
+14 -10
@@ -150,11 +150,11 @@ function fitFilenameBytes(name: string): string {
150 * Names that are merely unportable are made portable instead, because ordinary
151 * files have them (`report_2026-09-29T10:30:00.csv`, `What is this?.png`), on
152 * every platform so a name does not depend on where the SDK runs:
153 - * control and Windows-reserved characters become `_`, reserved device names
154 - * get a `_` prefix, names over {@link MAX_FILENAME_BYTES} are truncated with
155 - * their extension kept, and trailing spaces and dots are dropped as Windows
156 - * would. `safe_basename` in the Python SDK applies the same rules in the same
157 - * order.
153 + * control and Windows-reserved characters become `_`, names over
154 + * {@link MAX_FILENAME_BYTES} are truncated with their extension kept, trailing
155 + * spaces and dots are dropped as Windows would, and a resulting reserved
156 + * device name gets a `_` prefix. `safe_basename` in the Python SDK applies the
157 + * same rules in the same order.
158 *
159 * Python-compatible stripping removes surrounding whitespace first, and the
160 * usability check runs last, on the value that gets written, so neither
@@ -180,14 +180,18 @@ export function safeBasename(name: string, label: string = 'filename'): string {
180 );
181 }
182
183 - let portable = basename.replace(WINDOWS_INVALID_CHARS, '_');
184 - // Compare everything before the first dot: on Windows `NUL.tar.gz` opens the
185 - // null device just as `NUL` does, so extensions provide no protection.
183 + const fit = (value: string): string =>
184 + fitFilenameBytes(value).replace(TRAILING_SPACES_AND_DOTS, '');
185 + let portable = fit(basename.replace(WINDOWS_INVALID_CHARS, '_'));
186 + // Checked on the fitted name, because truncation and trailing-dot removal
187 + // can expose one (`NUL` followed by spaces and a long tail). Compare
188 + // everything before the first dot: on Windows `NUL.tar.gz` opens the null
189 + // device just as `NUL` does, so extensions provide no protection. Fitting
190 + // again keeps the byte bound, and a `_`-prefixed name is never a device.
191 const deviceName = portable.split('.', 1)[0].replace(/ +$/, '').toUpperCase();
192 if (WINDOWS_RESERVED_NAMES.has(deviceName)) {
188 - portable = `_${portable}`;
193 + portable = fit(`_${portable}`);
194 }
190 - portable = fitFilenameBytes(portable).replace(TRAILING_SPACES_AND_DOTS, '');
195
196 if (!portable) {
197 throw new ValidationError(
ts/packages/core/test/utils/safePath.test.ts
+7
@@ -140,6 +140,13 @@ describe('safeBasename', () => {
140 expect(safeBasename(input)).toBe(expected);
141 });
142
143 + it('prefixes a device name exposed by truncation or trailing-space removal', () => {
144 + // Truncation keeps `NUL` plus spaces before `.txt`, and Windows ignores
145 + // the spaces, so the checked name must be the fitted one.
146 + expect(safeBasename(`NUL${' '.repeat(200)}x.txt`)).toBe(`_NUL${' '.repeat(120)}.txt`);
147 + expect(safeBasename(`CON${' '.repeat(200)}x`)).toBe('_CON');
148 + });
149 +
150 it('truncates a long name to the byte limit and keeps its extension', () => {
151 const result = safeBasename(`${'x'.repeat(200)}.pdf`);
152 expect(result).toBe(`${'x'.repeat(MAX_FILENAME_BYTES - 4)}.pdf`);