Skip to content

Commit 6b754b3

Browse files
committed
fix(sftp): decode each escape run once and join upload names without decoding again
1 parent a9cb2ac commit 6b754b3

3 files changed

Lines changed: 31 additions & 13 deletions

File tree

‎apps/sim/lib/internal/sftp/client.test.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ vi.mock('ssh2', () => ({
6363
import {
6464
createSftpConnection,
6565
isPathSafe,
66+
joinRemotePath,
6667
readSftpFileCapped,
6768
sanitizeFileName,
6869
sanitizePath,
@@ -151,7 +152,14 @@ describe('SFTP client boundary', () => {
151152
expect(isPathSafe('/reports/100% done.csv')).toBe(true)
152153
expect(sanitizePath('/reports/100% done.csv')).toBe('/reports/100% done.csv')
153154
expect(sanitizePath('/reports/q1%20summary.csv')).toBe('/reports/q1 summary.csv')
154-
expect(isPathSafe('/reports/%2e%2e%2f100%')).toBe(true)
155+
expect(isPathSafe('/reports/%2e%2e%2f100%')).toBe(false)
155156
expect(isPathSafe('/reports/%2e%2e%2fsecret')).toBe(false)
157+
expect(sanitizePath('/q1%20reports/100% done.csv')).toBe('/q1 reports/100% done.csv')
158+
expect(isPathSafe('/reports/100%/%2e%2e%2fsecret')).toBe(false)
159+
})
160+
161+
it('keeps a double-encoded upload name inside the target directory', () => {
162+
const destination = joinRemotePath('/uploads', sanitizeFileName('%252e%252e%252fsecret.txt'))
163+
expect(destination).toBe('/uploads/%2e%2e%2fsecret.txt')
156164
})
157165
})

‎apps/sim/lib/internal/sftp/client.ts‎

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -246,19 +246,32 @@ export function readSftpFileCapped(
246246
return readNodeStreamToBufferWithLimit(stream, { maxBytes, label, signal })
247247
}
248248

249-
/** Percent-decodes a remote path; a path that is not valid percent-encoding (a literal `%`) is used as typed. */
249+
/**
250+
* Percent-decodes each run of escapes on its own, so a literal `%` (as in `100% done.csv`) stays as
251+
* typed without stopping the other escapes in the path from decoding.
252+
*/
250253
function decodeRemotePath(path: string): string {
251-
try {
252-
return decodeURIComponent(path)
253-
} catch {
254-
return path
255-
}
254+
return path.replace(/(?:%[0-9a-fA-F]{2})+/g, (run) => {
255+
try {
256+
return decodeURIComponent(run)
257+
} catch {
258+
return run
259+
}
260+
})
256261
}
257262

258263
export function sanitizePath(path: string): string {
259264
return decodeRemotePath(path.replace(/\0/g, '')).replace(/\\/g, '/').replace(/\/+/g, '/').trim()
260265
}
261266

267+
/**
268+
* Joins a sanitized directory and a sanitized file name without decoding again, so an encoded
269+
* separator left in the name cannot turn into a path separator.
270+
*/
271+
export function joinRemotePath(directory: string, fileName: string): string {
272+
return directory.endsWith('/') ? `${directory}${fileName}` : `${directory}/${fileName}`
273+
}
274+
262275
export function sanitizeFileName(fileName: string): string {
263276
const sanitized = decodeRemotePath(fileName.replace(/\0/g, ''))
264277
.replace(/[/\\]+/g, '_')

‎apps/sim/lib/internal/sftp/operations.ts‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
getFileType,
99
getSftp,
1010
isPathSafe,
11+
joinRemotePath,
1112
MAX_SFTP_READ_BYTES,
1213
parsePermissions,
1314
readSftpFileCapped,
@@ -464,9 +465,7 @@ export async function executeSftpUpload(
464465
)
465466
resolvedTotal += buffer.length
466467
const safeName = sanitizeFileName(file.name)
467-
const destination = sanitizePath(
468-
remotePath.endsWith('/') ? `${remotePath}${safeName}` : `${remotePath}/${safeName}`
469-
)
468+
const destination = joinRemotePath(remotePath, safeName)
470469
if (!input.overwrite && (await sftpExists(sftp, destination, context.signal))) continue
471470
await writeSftpFile(sftp, destination, buffer, input.permissions, context.signal)
472471
uploadedFiles.push({ name: safeName, remotePath: destination, size: buffer.length })
@@ -493,9 +492,7 @@ export async function executeSftpUpload(
493492

494493
if (hasDirectContent) {
495494
const safeName = sanitizeFileName(input.fileName ?? '')
496-
const destination = sanitizePath(
497-
remotePath.endsWith('/') ? `${remotePath}${safeName}` : `${remotePath}/${safeName}`
498-
)
495+
const destination = joinRemotePath(remotePath, safeName)
499496
if (!input.overwrite && (await sftpExists(sftp, destination, context.signal))) {
500497
return Response.json(
501498
{ error: 'File already exists and overwrite is disabled' },

0 commit comments

Comments
 (0)