Skip to content

Commit 71a6fcc

Browse files
committed
fix: use expandTilde before expandFilename: to try to support both ~ and relative paths
try to replace all usages in `ct`: hopefully it isn't a security issue in any cases: e.g. some kind of injection?
1 parent 5582742 commit 71a6fcc

File tree

7 files changed

+14
-13
lines changed

7 files changed

+14
-13
lines changed

src/ct/db_backend_record.nim

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ proc record(
189189
let shellArgs = @[cmd].concat(args)
190190
var executable = cmd.split(" ", 1)[0]
191191
try:
192-
executable = expandFilename(executable)
192+
executable = expandFilename(expandTilde(executable))
193193
except OsError:
194194
let foundExe = findExe(executable)
195195
if foundExe == "":
@@ -267,7 +267,7 @@ proc record(
267267
var activationPathResolved = pythonActivationPath
268268
if activationPathResolved.len > 0:
269269
try:
270-
activationPathResolved = expandFilename(activationPathResolved)
270+
activationPathResolved = expandFilename(expandTilde(activationPathResolved))
271271
except OsError:
272272
discard
273273

@@ -336,7 +336,7 @@ proc exportRecord(
336336
# trying to find full path
337337
# a hack: writing first there, otherwise i think expandFilename fails in some cases, when no such file yets
338338
writeFile(exportZipPath, "")
339-
let exportZipFullPath = expandFilename(exportZipPath)
339+
let exportZipFullPath = expandFilename(expandTilde(exportZipPath))
340340
# otherwise zip seems to try to add to it and because it's not a valid archive, it leads to an error
341341
removeFile(exportZipPath)
342342

@@ -395,7 +395,7 @@ proc main*(): Trace =
395395
displayHelp()
396396
return
397397
createDir args[i + 1]
398-
outputFolder = expandFilename(args[i + 1])
398+
outputFolder = expandFilename(expandTilde(args[i + 1]))
399399
i += 2
400400
elif arg == "-e" or arg == "--export":
401401
isExportedWithArg = true
@@ -545,7 +545,7 @@ proc main*(): Trace =
545545
createDir(exportFolder)
546546
exportRecord(program, recordArgs, traceId, exportZipPath, outputFolder, cleanupOutputFolder)
547547

548-
traceZipFullPath = expandFilename(exportZipPath)
548+
traceZipFullPath = expandFilename(expandTilde(exportZipPath))
549549

550550
if shouldSendEvents:
551551
let lastLine = loadLine(sessionId, sessionLogPath)

src/ct/trace/host.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ proc hostCommand*(
5353
let traceFolder = traceArg
5454
var traceFolderFullPath = ""
5555
try:
56-
traceFolderFullPath = expandFilename(traceFolder)
56+
traceFolderFullPath = expandFilename(expandTilde(traceFolder))
5757
except OsError as e:
5858
echo "ct host error: folder os error: ", e.msg
5959
quit(1)

src/ct/trace/import_command.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,5 @@ proc importCommand*(traceZipPath: string, importedTraceFolder: string) =
2828
removeDir(outputFolder)
2929

3030
createDir(outputFolder)
31-
let outputFolderFullPath = expandFilename(outputFolder)
31+
let outputFolderFullPath = expandFilename(expandTilde(outputFolder))
3232
importTraceInPreparedFolder(traceZipPath, outputFolderFullPath)

src/ct/trace/multitrace.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ proc findDiff(diffSpecification: string): string =
1313
var path = diffSpecification
1414
try:
1515
# try to support arguments like `~/<path>`
16-
path = expandFileName(diffSpecification)
16+
path = expandFileName(expandTilde(diffSpecification))
1717
except OsError:
1818
discard
1919

src/ct/trace/replay.nim

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ import std / [options, os, osproc, strutils, strformat ],
1010

1111
proc replayMultitrace*(archivePath: string, indexDiff: bool = false): bool =
1212
# TODO: a more unique path? or is this enough
13-
let outputFolder = getTempDir() / "codetracer" / archivePath.extractFilename.changeFileExt("")
14-
unzipIntoFolder(archivePath, outputFolder)
13+
let fullArchivePath = expandFilename(expandTilde(archivePath))
14+
let outputFolder = getTempDir() / "codetracer" / fullArchivePath.extractFilename.changeFileExt("")
15+
unzipIntoFolder(fullArchivePath, outputFolder)
1516

1617
var traceDir = ""
1718
for kind, file in walkDir(outputFolder, relative=true):

src/ct/trace/shell.nim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ proc findTraceForArgs*(
4343
return nil
4444
elif traceFolderArg.isSome:
4545
let folder = traceFolderArg.get
46-
var trace = trace_index.findByPath(expandFilename(folder), test=false)
46+
var trace = trace_index.findByPath(expandFilename(expandTilde(folder)), test=false)
4747
if trace.isNil:
48-
trace = trace_index.findByPath(expandFilename(folder) & "/", test=false)
48+
trace = trace_index.findByPath(expandFilename(expandTilde(folder)) & "/", test=false)
4949
if not trace.isNil:
5050
return trace
5151
else:

src/ct/utilities/zip.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ proc zipFolder*(source, output: string, onProgress: proc(progressPercent: int) =
3030
proc unzipIntoFolder*(zipPath, targetDir: string) {.raises: [IOError, OSError, Exception].} =
3131
var zip: ZipArchive
3232
if not zip.open(zipPath, fmRead):
33-
raise newException(IOError, "Failed to open decrypted ZIP: " & zipPath)
33+
raise newException(IOError, "Failed to open ZIP: " & zipPath)
3434

3535
createDir(targetDir)
3636
zip.extractAll(targetDir)

0 commit comments

Comments
 (0)