@@ -170,6 +170,18 @@ proc open {args} {
170170 uplevel 1 real_open $args
171171}
172172
173+ # Wrap open to sanitize arguments
174+
175+ proc safe_open_file {filename flags} {
176+ # a file name starting with " |" would attempt to run a process
177+ # but such a file name must be treated as a relative path
178+ # hide the " |" behind " ./"
179+ if {[ string index $filename 0] eq " |" } {
180+ set filename [ file join . $filename ]
181+ }
182+ open $filename $flags
183+ }
184+
173185######################################################################
174186##
175187## locate our library
@@ -494,7 +506,7 @@ proc _git_cmd {name} {
494506 # Tcl on Windows doesn't know it.
495507 #
496508 set p [gitexec git-$name ]
497- set f [open $p r]
509+ set f [safe_open_file $p r]
498510 set s [gets $f ]
499511 close $f
500512
@@ -527,7 +539,7 @@ proc _git_cmd {name} {
527539# Test a file for a hashbang to identify executable scripts on Windows.
528540proc is_shellscript {filename } {
529541 if {![file exists $filename ]} {return 0}
530- set f [open $filename r]
542+ set f [safe_open_file $filename r]
531543 fconfigure $f -encoding binary
532544 set magic [read $f 2]
533545 close $f
@@ -683,7 +695,7 @@ proc sq {value} {
683695proc load_current_branch {} {
684696 global current_branch is_detached
685697
686- set fd [open [gitdir HEAD] r]
698+ set fd [safe_open_file [gitdir HEAD] r]
687699 fconfigure $fd -translation binary -encoding utf-8
688700 if {[gets $fd ref] < 1} {
689701 set ref {}
@@ -1045,7 +1057,7 @@ You are using [git-version]:
10451057# # configure our library
10461058
10471059set idx [file join $oguilib tclIndex]
1048- if {[catch {set fd [open $idx r]} err]} {
1060+ if {[catch {set fd [safe_open_file $idx r]} err]} {
10491061 catch {wm withdraw .}
10501062 tk_messageBox \
10511063 -icon error \
@@ -1382,7 +1394,7 @@ proc repository_state {ctvar hdvar mhvar} {
13821394 set merge_head [gitdir MERGE_HEAD]
13831395 if {[file exists $merge_head ]} {
13841396 set ct merge
1385- set fd_mh [open $merge_head r]
1397+ set fd_mh [safe_open_file $merge_head r]
13861398 while {[gets $fd_mh line] >= 0} {
13871399 lappend mh $line
13881400 }
@@ -1530,7 +1542,7 @@ proc load_message {file {encoding {}}} {
15301542
15311543 set f [gitdir $file ]
15321544 if {[file isfile $f ]} {
1533- if {[catch {set fd [open $f r]}]} {
1545+ if {[catch {set fd [safe_open_file $f r]}]} {
15341546 return 0
15351547 }
15361548 fconfigure $fd -eofchar {}
@@ -1554,23 +1566,23 @@ proc run_prepare_commit_msg_hook {} {
15541566 # it will be .git/MERGE_MSG (merge), .git/SQUASH_MSG (squash), or an
15551567 # empty file but existent file.
15561568
1557- set fd_pcm [open [gitdir PREPARE_COMMIT_MSG] a]
1569+ set fd_pcm [safe_open_file [gitdir PREPARE_COMMIT_MSG] a]
15581570
15591571 if {[file isfile [gitdir MERGE_MSG]]} {
15601572 set pcm_source " merge"
1561- set fd_mm [open [gitdir MERGE_MSG] r]
1573+ set fd_mm [safe_open_file [gitdir MERGE_MSG] r]
15621574 fconfigure $fd_mm -encoding utf-8
15631575 puts -nonewline $fd_pcm [read $fd_mm ]
15641576 close $fd_mm
15651577 } elseif {[file isfile [gitdir SQUASH_MSG]]} {
15661578 set pcm_source " squash"
1567- set fd_sm [open [gitdir SQUASH_MSG] r]
1579+ set fd_sm [safe_open_file [gitdir SQUASH_MSG] r]
15681580 fconfigure $fd_sm -encoding utf-8
15691581 puts -nonewline $fd_pcm [read $fd_sm ]
15701582 close $fd_sm
15711583 } elseif {[file isfile [get_config commit.template]]} {
15721584 set pcm_source " template"
1573- set fd_sm [open [get_config commit.template] r]
1585+ set fd_sm [safe_open_file [get_config commit.template] r]
15741586 fconfigure $fd_sm -encoding utf-8
15751587 puts -nonewline $fd_pcm [read $fd_sm ]
15761588 close $fd_sm
@@ -2271,7 +2283,7 @@ proc do_quit {{rc {1}}} {
22712283 if {![string match amend* $commit_type ]
22722284 && $msg ne {}} {
22732285 catch {
2274- set fd [open $save w]
2286+ set fd [safe_open_file $save w]
22752287 fconfigure $fd -encoding utf-8
22762288 puts -nonewline $fd $msg
22772289 close $fd
@@ -4032,7 +4044,7 @@ if {[winfo exists $ui_comm]} {
40324044 }
40334045 } elseif {$m } {
40344046 catch {
4035- set fd [open [gitdir GITGUI_BCK] w]
4047+ set fd [safe_open_file [gitdir GITGUI_BCK] w]
40364048 fconfigure $fd -encoding utf-8
40374049 puts -nonewline $fd $msg
40384050 close $fd
0 commit comments