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, 04:45 UTC
| 1 | /** |
| 2 | * Tests for the filename containment primitive in `src/utils/safePath.ts`. |
| 3 | * |
| 4 | * Covers the filename safety policy shared with `TestSafeBasename` in |
| 5 | * `python/tests/test_safe_path.py`. |
| 6 | */ |
| 7 | import { describe, it, expect } from 'vitest'; |
| 8 | import { MAX_FILENAME_BYTES, safeBasename, untrustedBasename } from '../../src/utils/safePath'; |
| 9 | import { ValidationError } from '../../src/errors'; |
| 10 | |
| 11 | /** Bytes a changed name gains: `-` and 16 hex digits of the original's digest. */ |
| 12 | const TAG_BYTES = 17; |
| 13 | |
| 14 | const escapeRegExp = (value: string): string => value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); |
| 15 | |
| 16 | /** Matches `stem`, the digest tag of a changed name, then `extension`. */ |
| 17 | const tagged = (stem: string, extension: string = ''): RegExp => |
| 18 | new RegExp(`^${escapeRegExp(stem)}-[0-9a-f]{16}${escapeRegExp(extension)}$`, 'u'); |
| 19 | |
| 20 | describe('untrustedBasename', () => { |
| 21 | it.each([ |
| 22 | ['report.pdf', 'report.pdf'], |
| 23 | ['C:report.txt', 'report.txt'], |
| 24 | ['c:report.txt', 'report.txt'], |
| 25 | ['C:', ''], |
| 26 | ['output/report.pdf', 'report.pdf'], |
| 27 | ['output/report.pdf/', 'report.pdf'], |
| 28 | ['..\\..\\evil', 'evil'], |
| 29 | ['C:\\Users\\me\\report.pdf', 'report.pdf'], |
| 30 | ['', ''], |
| 31 | ['/', ''], |
| 32 | ['sub/.', '.'], |
| 33 | ['foo/..', '..'], |
| 34 | ])('reduces %j to %j without throwing', (input, expected) => { |
| 35 | expect(untrustedBasename(input)).toBe(expected); |
| 36 | }); |
| 37 | }); |
| 38 | |
| 39 | describe('safeBasename', () => { |
| 40 | describe('accepts legitimate filenames', () => { |
| 41 | it.each([ |
| 42 | ['report.pdf', 'report.pdf'], |
| 43 | ['C:report.txt', 'report.txt'], |
| 44 | ['c:report.txt', 'report.txt'], |
| 45 | ['\ufeffreport.txt', '\ufeffreport.txt'], |
| 46 | ['report.txt\ufeff', 'report.txt\ufeff'], |
| 47 | ['\u0085report.txt\u0085', 'report.txt'], |
| 48 | ['\u001creport.txt\u001f', 'report.txt'], |
| 49 | ['output/report.pdf', 'report.pdf'], |
| 50 | ['output/subdir/data.json', 'data.json'], |
| 51 | ['/absolute/report.pdf', 'report.pdf'], |
| 52 | ['output/report.pdf/', 'report.pdf'], |
| 53 | ['..\\..\\evil', 'evil'], |
| 54 | ['archive.tar.gz', 'archive.tar.gz'], |
| 55 | ['.gitignore', '.gitignore'], |
| 56 | [' report.pdf', 'report.pdf'], |
| 57 | ['report.pdf ', 'report.pdf'], |
| 58 | ['café.txt', 'café.txt'], |
| 59 | ['😀.png', '😀.png'], |
| 60 | ])('reduces %j to %j', (input, expected) => { |
| 61 | expect(safeBasename(input)).toBe(expected); |
| 62 | }); |
| 63 | |
| 64 | it('keeps a filename at the byte limit unchanged', () => { |
| 65 | const name = 'x'.repeat(MAX_FILENAME_BYTES); |
| 66 | expect(safeBasename(name)).toBe(name); |
| 67 | }); |
| 68 | }); |
| 69 | |
| 70 | describe('rejects names that leave no usable basename', () => { |
| 71 | // Each of these would make the save path equal its own directory or the |
| 72 | // parent, surfacing as a raw EISDIR at write time. |
| 73 | it.each(['', '.', '..', '...', 'sub/.', 'foo/..', './', '/', '//', ' ', 'C:'])( |
| 74 | 'rejects %j', |
| 75 | input => { |
| 76 | expect(() => safeBasename(input)).toThrow(ValidationError); |
| 77 | expect(() => safeBasename(input)).toThrow(/leaves no usable basename/); |
| 78 | } |
| 79 | ); |
| 80 | |
| 81 | // Python-compatible whitespace stripping would write these as `.` or `..`: |
| 82 | // the usability check has to see the trimmed value, not the raw segment. |
| 83 | it.each([ |
| 84 | '\u00a0.\u00a0', |
| 85 | '.\u00a0', |
| 86 | '\u00a0.', |
| 87 | '\u2007..\u2007', |
| 88 | '\u2028.\u2029', |
| 89 | '\u0085..\u0085', |
| 90 | '\u001c..\u001f', |
| 91 | ])('rejects whitespace-wrapped %j, which trims to a dot run', input => { |
| 92 | expect(() => safeBasename(input)).toThrow(/leaves no usable basename/); |
| 93 | }); |
| 94 | }); |
| 95 | |
| 96 | describe('rejects names that cannot be written', () => { |
| 97 | it.each(['report\u0000.pdf', 'report.pdf\u0000.exe'])('rejects NUL byte in %j', input => { |
| 98 | expect(() => safeBasename(input)).toThrow(/NUL byte/); |
| 99 | }); |
| 100 | |
| 101 | it('rejects a lone surrogate, which cannot be encoded as UTF-8', () => { |
| 102 | expect(() => safeBasename('report-\ud800.txt')).toThrow(/invalid Unicode/); |
| 103 | expect(() => safeBasename('report-\udc00.txt')).toThrow(/invalid Unicode/); |
| 104 | }); |
| 105 | |
| 106 | it.each(['. .', '.. ', ' . . '])( |
| 107 | 'rejects %j, which is a dot run once Windows trims it', |
| 108 | input => { |
| 109 | expect(() => safeBasename(input)).toThrow(/leaves no usable basename/); |
| 110 | } |
| 111 | ); |
| 112 | }); |
| 113 | |
| 114 | describe('makes unportable names portable on every platform', () => { |
| 115 | it.each([ |
| 116 | ['report_2026-09-29T10:30:00.csv', 'report_2026-09-29T10_30_00', '.csv'], |
| 117 | ['What is this?.png', 'What is this_', '.png'], |
| 118 | ['invoice "final".pdf', 'invoice _final_', '.pdf'], |
| 119 | ['report.txt:payload', 'report', '.txt_payload'], |
| 120 | ['report<1>.txt', 'report_1_', '.txt'], |
| 121 | ['a|b*.txt', 'a_b_', '.txt'], |
| 122 | ['tab\there.txt', 'tab_here', '.txt'], |
| 123 | ['output/C:report.txt', 'C_report', '.txt'], |
| 124 | ])('replaces reserved characters in %j', (input, stem, extension) => { |
| 125 | expect(safeBasename(input)).toMatch(tagged(stem, extension)); |
| 126 | }); |
| 127 | |
| 128 | it.each([ |
| 129 | ['report.txt.', 'report', '.txt'], |
| 130 | ['report. .', 'report', ''], |
| 131 | ['report.\u00a0', 'report', ''], |
| 132 | ['report.\u0085', 'report', ''], |
| 133 | ['\ufeff..', '\ufeff', ''], |
| 134 | ])('drops trailing spaces and dots from %j', (input, stem, extension) => { |
| 135 | expect(safeBasename(input)).toMatch(tagged(stem, extension)); |
| 136 | }); |
| 137 | |
| 138 | it.each([ |
| 139 | ['NUL', '_NUL', ''], |
| 140 | ['nul', '_nul', ''], |
| 141 | ['NUL.tar.gz', '_NUL.tar', '.gz'], |
| 142 | ['COM1.log.bak', '_COM1.log', '.bak'], |
| 143 | ['COM¹.txt', '_COM¹', '.txt'], |
| 144 | ['LPT³.data', '_LPT³', '.data'], |
| 145 | ['aux.txt', '_aux', '.txt'], |
| 146 | ['CON .txt', '_CON ', '.txt'], |
| 147 | ['COM1:.txt', 'COM1_', '.txt'], |
| 148 | ])('prefixes reserved device name %j', (input, stem, extension) => { |
| 149 | expect(safeBasename(input)).toMatch(tagged(stem, extension)); |
| 150 | }); |
| 151 | |
| 152 | it('prefixes a device name exposed by truncation or trailing-space removal', () => { |
| 153 | // Truncation keeps `NUL` plus spaces before `.txt`, and Windows ignores |
| 154 | // the spaces, so the checked name must be the fitted one. |
| 155 | expect(safeBasename(`NUL${' '.repeat(200)}x.txt`)).toMatch( |
| 156 | tagged(`_NUL${' '.repeat(MAX_FILENAME_BYTES - TAG_BYTES - 8)}`, '.txt') |
| 157 | ); |
| 158 | expect(safeBasename(`CON${' '.repeat(200)}x`)).toMatch(tagged('_CON')); |
| 159 | }); |
| 160 | |
| 161 | it('truncates a long name to the byte limit and keeps its extension', () => { |
| 162 | const result = safeBasename(`${'x'.repeat(200)}.pdf`); |
| 163 | expect(result).toMatch(tagged('x'.repeat(MAX_FILENAME_BYTES - TAG_BYTES - 4), '.pdf')); |
| 164 | expect(result).toHaveLength(MAX_FILENAME_BYTES); |
| 165 | }); |
| 166 | |
| 167 | it('truncates by whole code points, measured in bytes', () => { |
| 168 | // Each CJK character is one UTF-16 code unit but three UTF-8 bytes, and |
| 169 | // each emoji is two code units but four bytes. |
| 170 | const cjk = safeBasename(`${'請'.repeat(70)}.pdf`); |
| 171 | expect(cjk).toMatch(tagged('請'.repeat(35), '.pdf')); |
| 172 | expect(new TextEncoder().encode(cjk).length).toBeLessThanOrEqual(MAX_FILENAME_BYTES); |
| 173 | expect(safeBasename('😀'.repeat(33))).toMatch(tagged('😀'.repeat(27))); |
| 174 | }); |
| 175 | |
| 176 | it('truncates an over-long extension with the rest of the name', () => { |
| 177 | const result = safeBasename(`report.${'x'.repeat(200)}`); |
| 178 | expect(result).toMatch(tagged(`report.${'x'.repeat(MAX_FILENAME_BYTES - TAG_BYTES - 7)}`)); |
| 179 | }); |
| 180 | |
| 181 | it('drops a trailing dot exposed by truncation', () => { |
| 182 | const name = `${'x'.repeat(MAX_FILENAME_BYTES - 1)}.${'y'.repeat(40)}`; |
| 183 | expect(safeBasename(name)).toMatch(tagged('x'.repeat(MAX_FILENAME_BYTES - TAG_BYTES))); |
| 184 | }); |
| 185 | }); |
| 186 | |
| 187 | describe('keeps names that portability changed from colliding', () => { |
| 188 | // `TestSafeBasename` in the Python SDK asserts the same vectors, so both |
| 189 | // SDKs write a given server name to the same file. |
| 190 | it.each([ |
| 191 | ['report?.png', 'report_-05fcb95aa5b918e9.png'], |
| 192 | ['report*.png', 'report_-aa921bdab2b33292.png'], |
| 193 | ['report_2026-09-29T10:30:00.csv', 'report_2026-09-29T10_30_00-d7211bb25cb815fe.csv'], |
| 194 | ['NUL.txt', '_NUL-d0848f78ce05ded6.txt'], |
| 195 | ])('tags %j with a digest of the original name', (input, expected) => { |
| 196 | expect(safeBasename(input)).toBe(expected); |
| 197 | }); |
| 198 | |
| 199 | it('writes distinct names that normalize alike to distinct files', () => { |
| 200 | const names = ['report?.png', 'report*.png', 'report:.png', 'report_.png']; |
| 201 | expect(new Set(names.map(name => safeBasename(name))).size).toBe(names.length); |
| 202 | }); |
| 203 | |
| 204 | it('writes distinct long names that share a truncated prefix to distinct files', () => { |
| 205 | const prefix = 'a'.repeat(200); |
| 206 | expect(safeBasename(`${prefix}-1.txt`)).not.toBe(safeBasename(`${prefix}-2.txt`)); |
| 207 | }); |
| 208 | |
| 209 | it('returns an already portable name unchanged', () => { |
| 210 | expect(safeBasename('report_.png')).toBe('report_.png'); |
| 211 | }); |
| 212 | |
| 213 | it('treats a tagged name as already portable', () => { |
| 214 | const written = safeBasename('report?.png'); |
| 215 | expect(safeBasename(written)).toBe(written); |
| 216 | }); |
| 217 | }); |
| 218 | |
| 219 | it('names the value in the error using the given label', () => { |
| 220 | expect(() => safeBasename('..', 'mount path')).toThrow(/mount path/); |
| 221 | }); |
| 222 | }); |