|
| 1 | +import type { CompleteFileSystemConnectionData } from '@microsoft/vscode-extension-logic-apps'; |
| 2 | +import { ChildProcess, type SpawnOptions } from 'child_process'; |
| 3 | +import { platform } from 'os'; |
| 4 | +import * as path from 'path'; |
| 5 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 6 | +import { createFileSystemConnection } from '../fileSystemConnection'; |
| 7 | + |
| 8 | +const { mockSpawn } = vi.hoisted(() => ({ mockSpawn: vi.fn() })); |
| 9 | + |
| 10 | +vi.mock('child_process', async () => ({ |
| 11 | + ...(await vi.importActual<typeof import('child_process')>('child_process')), |
| 12 | + spawn: mockSpawn, |
| 13 | +})); |
| 14 | + |
| 15 | +vi.mock('os', () => ({ platform: vi.fn() })); |
| 16 | +vi.mock('../../../../../../localize', () => ({ |
| 17 | + localize: (_key: string, message: string) => message, |
| 18 | +})); |
| 19 | + |
| 20 | +const credentials = { |
| 21 | + rootFolder: '\\\\CWE532-ROOT\\share folder\\caf\u00e9', |
| 22 | + username: 'CWE532-USER\\user name', |
| 23 | + password: 'CWE532-PASSWORD \u00e9 "&|<>^%!\r\n$()', |
| 24 | +}; |
| 25 | +const connectionInfo = { displayName: 'SMB connection', connectionParameters: credentials }; |
| 26 | +const genericFailure = { |
| 27 | + errorMessage: 'Unable to create the file system connection. Check the SMB share, credentials, network access, and existing connections.', |
| 28 | +}; |
| 29 | + |
| 30 | +describe('createFileSystemConnection', () => { |
| 31 | + let child: ChildProcess; |
| 32 | + |
| 33 | + beforeEach(() => { |
| 34 | + vi.clearAllMocks(); |
| 35 | + vi.stubEnv('SystemRoot', 'C:\\Windows'); |
| 36 | + vi.mocked(platform).mockReturnValue('win32'); |
| 37 | + child = new ChildProcess(); |
| 38 | + mockSpawn.mockReturnValue(child); |
| 39 | + }); |
| 40 | + |
| 41 | + afterEach(() => { |
| 42 | + child.removeAllListeners(); |
| 43 | + vi.unstubAllEnvs(); |
| 44 | + }); |
| 45 | + |
| 46 | + it('runs net.exe without a shell or diagnostic pipes and preserves the credential-free success contract', async () => { |
| 47 | + const result = createFileSystemConnection(connectionInfo); |
| 48 | + |
| 49 | + expect(mockSpawn).toHaveBeenCalledWith( |
| 50 | + path.join('C:\\Windows', 'System32', 'net.exe'), |
| 51 | + ['use', credentials.rootFolder, credentials.password, `/user:${credentials.username}`], |
| 52 | + { shell: false, windowsHide: true, stdio: 'ignore' } |
| 53 | + ); |
| 54 | + child.emit('close', 0, null); |
| 55 | + expect(await result).toEqual({ |
| 56 | + errorMessage: '', |
| 57 | + connection: { displayName: connectionInfo.displayName, connectionParameters: { mountPath: credentials.rootFolder } }, |
| 58 | + }); |
| 59 | + expect(JSON.stringify(await result)).not.toContain('CWE532-PASSWORD'); |
| 60 | + expect(JSON.stringify(await result)).not.toContain('CWE532-USER'); |
| 61 | + expect(connectionInfo.connectionParameters).toEqual(credentials); |
| 62 | + }); |
| 63 | + |
| 64 | + it.each(['', '*', '/persistent:no', '"quoted"\\'])( |
| 65 | + 'passes password %j as a separate argument without shell interpolation', |
| 66 | + async (password) => { |
| 67 | + const result = createFileSystemConnection({ connectionParameters: { ...credentials, password } }); |
| 68 | + expect(mockSpawn.mock.lastCall?.[1]).toEqual(['use', credentials.rootFolder, password, `/user:${credentials.username}`]); |
| 69 | + child.emit('close', 0, null); |
| 70 | + expect(await result).toHaveProperty('connection.connectionParameters.mountPath', credentials.rootFolder); |
| 71 | + } |
| 72 | + ); |
| 73 | + |
| 74 | + it('returns an error string compatible with the completion message contract on success', async () => { |
| 75 | + const result = createFileSystemConnection(connectionInfo); |
| 76 | + child.emit('close', 0, null); |
| 77 | + const { connection, errorMessage } = await result; |
| 78 | + const completion: CompleteFileSystemConnectionData = { |
| 79 | + connectionName: 'smb-connection', |
| 80 | + connection, |
| 81 | + error: errorMessage, |
| 82 | + }; |
| 83 | + expect(completion.error).toBe(''); |
| 84 | + expect(completion.connection).toEqual({ |
| 85 | + displayName: connectionInfo.displayName, |
| 86 | + connectionParameters: { mountPath: credentials.rootFolder }, |
| 87 | + }); |
| 88 | + }); |
| 89 | + |
| 90 | + it.each([1, 2, 5, 53, 1219, null])('returns only the fixed safe failure for exit code %s', async (code) => { |
| 91 | + const result = createFileSystemConnection(connectionInfo); |
| 92 | + child.emit('close', code, null); |
| 93 | + expect(await result).toEqual(genericFailure); |
| 94 | + }); |
| 95 | + |
| 96 | + it('fails on signal termination, even if a zero exit code is reported', async () => { |
| 97 | + const result = createFileSystemConnection(connectionInfo); |
| 98 | + child.emit('close', 0, 'SIGTERM'); |
| 99 | + expect(await result).toEqual(genericFailure); |
| 100 | + }); |
| 101 | + |
| 102 | + it.each(['ENOENT', 'EACCES', 'EINVAL'])('contains all diagnostics for process error %s', async (code) => { |
| 103 | + const result = createFileSystemConnection(connectionInfo); |
| 104 | + const error = Object.assign(new Error(`Command failed: net use ${JSON.stringify(credentials)}`), { |
| 105 | + code, |
| 106 | + path: credentials.rootFolder, |
| 107 | + spawnargs: [credentials.password, credentials.username], |
| 108 | + stdout: credentials.password, |
| 109 | + stderr: credentials.password, |
| 110 | + cause: new Error(credentials.password), |
| 111 | + }); |
| 112 | + child.emit('error', error); |
| 113 | + child.emit('close', null, null); |
| 114 | + expect(await result).toEqual(genericFailure); |
| 115 | + }); |
| 116 | + |
| 117 | + it('does not inspect or serialize raw errors', async () => { |
| 118 | + const error = new Error(); |
| 119 | + Object.defineProperty(error, 'message', { |
| 120 | + get: () => { |
| 121 | + throw new Error(credentials.password); |
| 122 | + }, |
| 123 | + }); |
| 124 | + const result = createFileSystemConnection(connectionInfo); |
| 125 | + child.emit('error', error); |
| 126 | + child.emit('close', 1, null); |
| 127 | + expect(await result).toEqual(genericFailure); |
| 128 | + }); |
| 129 | + |
| 130 | + it('does not turn a process error into success or expose subsequent errors', async () => { |
| 131 | + const result = createFileSystemConnection(connectionInfo); |
| 132 | + child.emit('error', new Error(credentials.password)); |
| 133 | + child.emit('close', 0, null); |
| 134 | + child.emit('error', new Error(credentials.username)); |
| 135 | + expect(await result).toEqual(genericFailure); |
| 136 | + }); |
| 137 | + |
| 138 | + it('contains synchronous launch exceptions', async () => { |
| 139 | + mockSpawn.mockImplementation(() => { |
| 140 | + throw new Error(credentials.password); |
| 141 | + }); |
| 142 | + expect(await createFileSystemConnection(connectionInfo)).toEqual(genericFailure); |
| 143 | + }); |
| 144 | + |
| 145 | + it.each([undefined, ''])('reports a safe failure when the Windows system directory is %j', async (systemRoot) => { |
| 146 | + vi.stubEnv('SystemRoot', systemRoot); |
| 147 | + expect(await createFileSystemConnection(connectionInfo)).toEqual(genericFailure); |
| 148 | + expect(mockSpawn).not.toHaveBeenCalled(); |
| 149 | + }); |
| 150 | + |
| 151 | + it.each(['linux', 'darwin'] as const)('does not launch net.exe on %s', async (os) => { |
| 152 | + vi.mocked(platform).mockReturnValue(os); |
| 153 | + expect(await createFileSystemConnection(connectionInfo)).toEqual({ |
| 154 | + errorMessage: 'File system connections to SMB shares are supported only on Windows.', |
| 155 | + }); |
| 156 | + expect(mockSpawn).not.toHaveBeenCalled(); |
| 157 | + }); |
| 158 | + |
| 159 | + it.each([ |
| 160 | + undefined, |
| 161 | + {}, |
| 162 | + { ...credentials, rootFolder: undefined }, |
| 163 | + { ...credentials, rootFolder: 123 }, |
| 164 | + { ...credentials, rootFolder: 'C:\\local' }, |
| 165 | + { ...credentials, rootFolder: `${credentials.rootFolder}\0` }, |
| 166 | + { ...credentials, username: undefined }, |
| 167 | + { ...credentials, username: '' }, |
| 168 | + { ...credentials, username: 123 }, |
| 169 | + { ...credentials, username: `${credentials.username}\0` }, |
| 170 | + { ...credentials, password: undefined }, |
| 171 | + { ...credentials, password: 123 }, |
| 172 | + { ...credentials, password: `${credentials.password}\0` }, |
| 173 | + ])('rejects invalid parameters without echoing them or starting a process: %j', async (connectionParameters) => { |
| 174 | + const result = await createFileSystemConnection({ connectionParameters }); |
| 175 | + expect(result).toEqual({ |
| 176 | + errorMessage: 'Provide a valid UNC root folder, username, and password for the SMB share.', |
| 177 | + }); |
| 178 | + expect(mockSpawn).not.toHaveBeenCalled(); |
| 179 | + }); |
| 180 | + |
| 181 | + it.each([credentials.password, ''])( |
| 182 | + 'preserves arguments and discards sensitive output through a real child process: %j', |
| 183 | + async (password) => { |
| 184 | + const cp = await vi.importActual<typeof import('child_process')>('child_process'); |
| 185 | + const expectedArgs = ['use', credentials.rootFolder, password, `/user:${credentials.username}`]; |
| 186 | + const script = `const expected = ${JSON.stringify(expectedArgs)}; |
| 187 | + console.log(expected); |
| 188 | + console.error(expected); |
| 189 | + process.exit(JSON.stringify(process.argv.slice(1)) === JSON.stringify(expected) ? 0 : 1);`; |
| 190 | + mockSpawn.mockImplementation((_file: string, args: string[], options: SpawnOptions) => { |
| 191 | + child = cp.spawn(process.execPath, ['-e', script, '--', ...args], options); |
| 192 | + return child; |
| 193 | + }); |
| 194 | + |
| 195 | + expect(await createFileSystemConnection({ connectionParameters: { ...credentials, password } })).toEqual({ |
| 196 | + errorMessage: '', |
| 197 | + connection: { connectionParameters: { mountPath: credentials.rootFolder } }, |
| 198 | + }); |
| 199 | + expect(child.stdin).toBeNull(); |
| 200 | + expect(child.stdout).toBeNull(); |
| 201 | + expect(child.stderr).toBeNull(); |
| 202 | + }, |
| 203 | + 10000 |
| 204 | + ); |
| 205 | + |
| 206 | + it('contains a real child-process launch error with sensitive spawnargs', async () => { |
| 207 | + const cp = await vi.importActual<typeof import('child_process')>('child_process'); |
| 208 | + mockSpawn.mockImplementation((_file: string, args: string[], options: SpawnOptions) => { |
| 209 | + child = cp.spawn(path.join(__dirname, 'CWE532-missing-net.exe'), args, options); |
| 210 | + return child; |
| 211 | + }); |
| 212 | + expect(await createFileSystemConnection(connectionInfo)).toEqual(genericFailure); |
| 213 | + }, 10000); |
| 214 | + |
| 215 | + it.skipIf(process.platform !== 'win32')( |
| 216 | + 'contains a real net.exe local-option failure without contacting a share', |
| 217 | + async () => { |
| 218 | + const cp = await vi.importActual<typeof import('child_process')>('child_process'); |
| 219 | + mockSpawn.mockImplementation((file: string, _args: string[], options: SpawnOptions) => { |
| 220 | + child = cp.spawn(file, ['use', '/__logicapps_invalid_option__'], options); |
| 221 | + return child; |
| 222 | + }); |
| 223 | + expect(await createFileSystemConnection(connectionInfo)).toEqual(genericFailure); |
| 224 | + }, |
| 225 | + 10000 |
| 226 | + ); |
| 227 | +}); |
0 commit comments