Skip to content

Commit 23bde67

Browse files
committed
fix(sftp): reject a trailing parent segment in remote paths
1 parent 8cba605 commit 23bde67

2 files changed

Lines changed: 16 additions & 5 deletions

File tree

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,17 @@ describe('SFTP client boundary', () => {
158158
expect(isPathSafe('/reports/100%/%2e%2e%2fsecret')).toBe(false)
159159
})
160160

161+
it.each(['/data/..', '..', '/data/%2e%2e', 'data\\..\\etc'])(
162+
'rejects the parent segment in %j',
163+
(path) => {
164+
expect(isPathSafe(path)).toBe(false)
165+
}
166+
)
167+
168+
it.each(['/data/..hidden', '/data/v1..v2/report.csv', '/data/.'])('accepts %j', (path) => {
169+
expect(isPathSafe(path)).toBe(true)
170+
})
171+
161172
it('keeps a double-encoded upload name inside the target directory', () => {
162173
const destination = joinRemotePath('/uploads', sanitizeFileName('%252e%252e%252fsecret.txt'))
163174
expect(destination).toBe('/uploads/%2e%2e%2fsecret.txt')

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -281,12 +281,12 @@ export function sanitizeFileName(fileName: string): string {
281281
return sanitized || 'unnamed_file'
282282
}
283283

284+
/** Rejects a `..` segment anywhere in the path, including a trailing one, raw or percent-decoded. */
284285
export function isPathSafe(path: string): boolean {
285-
const normalizedPath = path.replace(/\\/g, '/')
286-
if (normalizedPath.includes('../') || normalizedPath.includes('..\\')) return false
287-
const decoded = decodeRemotePath(normalizedPath)
288-
if (decoded.includes('../') || decoded.includes('..\\')) return false
289-
return !normalizedPath.includes('\0')
286+
const hasParentSegment = (value: string) =>
287+
value.split(/[/\\]/).some((segment) => segment === '..')
288+
if (hasParentSegment(path) || hasParentSegment(decodeRemotePath(path))) return false
289+
return !path.includes('\0')
290290
}
291291

292292
export function parsePermissions(mode: number): string {

0 commit comments

Comments
 (0)