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 | import { afterEach, describe, it, expect, vi } from 'vitest'; |
| 2 | import * as path from 'node:path'; |
| 3 | import * as os from 'node:os'; |
| 4 | import { mkdtempSync, writeFileSync, symlinkSync, rmSync, mkdirSync } from 'node:fs'; |
| 5 | import { |
| 6 | assertSafeFileUploadPath, |
| 7 | isBlockedSensitiveFileUploadPath, |
| 8 | } from '../../src/utils/sensitiveFileUploadPaths'; |
| 9 | import { platform } from '../../src/platform/node'; |
| 10 | import * as publicApi from '../../src'; |
| 11 | |
| 12 | describe('sensitiveFileUploadPaths', () => { |
| 13 | afterEach(() => { |
| 14 | vi.restoreAllMocks(); |
| 15 | }); |
| 16 | |
| 17 | it('allows normal project files', () => { |
| 18 | const p = path.join('/tmp', 'composio-test', 'document.pdf'); |
| 19 | expect(isBlockedSensitiveFileUploadPath(p)).toBe(false); |
| 20 | expect(() => assertSafeFileUploadPath(p)).not.toThrow(); |
| 21 | }); |
| 22 | |
| 23 | it('blocks common credential directory segments', () => { |
| 24 | expect(isBlockedSensitiveFileUploadPath(path.join(os.homedir(), '.aws', 'credentials'))).toBe( |
| 25 | true |
| 26 | ); |
| 27 | expect(isBlockedSensitiveFileUploadPath(path.join(os.homedir(), '.ssh', 'id_ed25519'))).toBe( |
| 28 | true |
| 29 | ); |
| 30 | expect( |
| 31 | isBlockedSensitiveFileUploadPath(path.join(os.homedir(), '.claude', 'settings.json')) |
| 32 | ).toBe(true); |
| 33 | }); |
| 34 | |
| 35 | it('blocks .env-style basenames', () => { |
| 36 | expect(isBlockedSensitiveFileUploadPath(path.join('/app', 'repo', '.env'))).toBe(true); |
| 37 | expect(isBlockedSensitiveFileUploadPath(path.join('/app', 'repo', '.env.local'))).toBe(true); |
| 38 | }); |
| 39 | |
| 40 | it('blocks default private key basenames (defense in depth)', () => { |
| 41 | expect(isBlockedSensitiveFileUploadPath(path.join('/tmp', 'id_ed25519'))).toBe(true); |
| 42 | }); |
| 43 | |
| 44 | it('does not block public key files by basename', () => { |
| 45 | expect(isBlockedSensitiveFileUploadPath(path.join('/tmp', 'id_ed25519.pub'))).toBe(false); |
| 46 | }); |
| 47 | |
| 48 | it('honors additional deny segments from config', () => { |
| 49 | expect( |
| 50 | isBlockedSensitiveFileUploadPath(path.join('/data', 'secrets', 'x.txt'), ['secrets']) |
| 51 | ).toBe(true); |
| 52 | expect(isBlockedSensitiveFileUploadPath(path.join('/data', 'ok', 'x.txt'), ['secrets'])).toBe( |
| 53 | false |
| 54 | ); |
| 55 | }); |
| 56 | |
| 57 | it('matches deny segments without case on a case-insensitive filesystem', () => { |
| 58 | vi.spyOn(platform, 'isFileSystemCaseSensitive').mockReturnValue(false); |
| 59 | |
| 60 | expect(isBlockedSensitiveFileUploadPath('/Users/me/.Kube/config')).toBe(true); |
| 61 | expect(isBlockedSensitiveFileUploadPath('/data/Secrets/x.txt', ['secrets'])).toBe(true); |
| 62 | }); |
| 63 | |
| 64 | it('preserves distinct segment casing on a case-sensitive filesystem', () => { |
| 65 | vi.spyOn(platform, 'isFileSystemCaseSensitive').mockReturnValue(true); |
| 66 | |
| 67 | expect(isBlockedSensitiveFileUploadPath('/home/me/.kube/config')).toBe(true); |
| 68 | expect(isBlockedSensitiveFileUploadPath('/home/me/.Kube/config')).toBe(false); |
| 69 | expect(isBlockedSensitiveFileUploadPath('/data/secrets/x.txt', ['secrets'])).toBe(true); |
| 70 | expect(isBlockedSensitiveFileUploadPath('/data/Secrets/x.txt', ['secrets'])).toBe(false); |
| 71 | }); |
| 72 | |
| 73 | it('is re-exported from the package root for downstream consumers (e.g. @composio/cli)', () => { |
| 74 | // The CLI imports the guard from `@composio/core` to share this one denylist |
| 75 | // (issue #3746). Lock the public surface so a refactor cannot silently drop it. |
| 76 | expect(typeof publicApi.assertSafeFileUploadPath).toBe('function'); |
| 77 | expect(typeof publicApi.isBlockedSensitiveFileUploadPath).toBe('function'); |
| 78 | expect(Array.isArray(publicApi.BUILTIN_FILE_UPLOAD_PATH_DENY_SEGMENTS)).toBe(true); |
| 79 | expect(publicApi.BUILTIN_FILE_UPLOAD_PATH_DENY_SEGMENTS).toContain('.ssh'); |
| 80 | }); |
| 81 | |
| 82 | it('blocks after realpath resolves a symlink into a sensitive directory', () => { |
| 83 | const root = mkdtempSync(path.join(os.tmpdir(), 'composio-symlink-')); |
| 84 | try { |
| 85 | const awsDir = path.join(root, 'nested', '.aws'); |
| 86 | mkdirSync(awsDir, { recursive: true }); |
| 87 | const target = path.join(awsDir, 'creds'); |
| 88 | writeFileSync(target, 'x'); |
| 89 | const link = path.join(root, 'innocent-name'); |
| 90 | symlinkSync(target, link); |
| 91 | expect(isBlockedSensitiveFileUploadPath(link)).toBe(true); |
| 92 | } finally { |
| 93 | rmSync(root, { recursive: true, force: true }); |
| 94 | } |
| 95 | }); |
| 96 | |
| 97 | // The inverse of the case above: realpath moves the denied segment *out* of |
| 98 | // the path instead of into it. Dotfile managers (chezmoi, stow, yadm) and |
| 99 | // containerised home directories lay `~/.claude -> /state/claude` out exactly |
| 100 | // this way, so matching only the resolved path turned the denylist off for |
| 101 | // them. |
| 102 | it('blocks a sensitive directory that is itself a symlink to a plain path', () => { |
| 103 | const root = mkdtempSync(path.join(os.tmpdir(), 'composio-symlink-dir-')); |
| 104 | try { |
| 105 | const store = path.join(root, 'state', 'claude'); |
| 106 | mkdirSync(store, { recursive: true }); |
| 107 | writeFileSync(path.join(store, 'settings.json'), '{}'); |
| 108 | const home = path.join(root, 'home'); |
| 109 | mkdirSync(home, { recursive: true }); |
| 110 | const link = path.join(home, '.claude'); |
| 111 | symlinkSync(store, link); |
| 112 | |
| 113 | expect(isBlockedSensitiveFileUploadPath(path.join(link, 'settings.json'))).toBe(true); |
| 114 | expect(() => assertSafeFileUploadPath(path.join(link, 'settings.json'))).toThrow(); |
| 115 | } finally { |
| 116 | rmSync(root, { recursive: true, force: true }); |
| 117 | } |
| 118 | }); |
| 119 | |
| 120 | // Same hiding trick applied to the basename rather than a directory segment: |
| 121 | // the written name is `.env`, the resolved one is not. |
| 122 | it('blocks a denied basename whose symlink target is named innocuously', () => { |
| 123 | const root = mkdtempSync(path.join(os.tmpdir(), 'composio-symlink-base-')); |
| 124 | try { |
| 125 | const store = path.join(root, 'store'); |
| 126 | mkdirSync(store, { recursive: true }); |
| 127 | const target = path.join(store, 'plain-config'); |
| 128 | writeFileSync(target, 'SECRET=1'); |
| 129 | const project = path.join(root, 'project'); |
| 130 | mkdirSync(project, { recursive: true }); |
| 131 | const link = path.join(project, '.env'); |
| 132 | symlinkSync(target, link); |
| 133 | |
| 134 | expect(isBlockedSensitiveFileUploadPath(link)).toBe(true); |
| 135 | } finally { |
| 136 | rmSync(root, { recursive: true, force: true }); |
| 137 | } |
| 138 | }); |
| 139 | |
| 140 | it('still allows an ordinary file reached through a symlinked directory', () => { |
| 141 | const root = mkdtempSync(path.join(os.tmpdir(), 'composio-symlink-ok-')); |
| 142 | try { |
| 143 | const store = path.join(root, 'store'); |
| 144 | mkdirSync(store, { recursive: true }); |
| 145 | writeFileSync(path.join(store, 'document.pdf'), 'x'); |
| 146 | const link = path.join(root, 'docs'); |
| 147 | symlinkSync(store, link); |
| 148 | |
| 149 | expect(isBlockedSensitiveFileUploadPath(path.join(link, 'document.pdf'))).toBe(false); |
| 150 | } finally { |
| 151 | rmSync(root, { recursive: true, force: true }); |
| 152 | } |
| 153 | }); |
| 154 | }); |