Skip to content
This repository was archived by the owner on Mar 26, 2021. It is now read-only.

Commit 64d6cff

Browse files
authored
Use addenv to ensure git commands don't leak env variables (#45)
1 parent bf21cfd commit 64d6cff

File tree

4 files changed

+37
-19
lines changed

4 files changed

+37
-19
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "GitCommand"
22
uuid = "49b5b516-ca3f-4003-a081-42bdcf55082d"
33
authors = ["Dilum Aluthge", "Brown Center for Biomedical Informatics"]
4-
version = "2.1.0"
4+
version = "2.2.0"
55

66
[deps]
77
JLLWrappers = "692b3bcd-3c85-4b1f-b108-f13ce0eb3210"

README.md

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ your packages!
1010

1111
GitCommand provides a Git binary via
1212
[Git_jll](https://github.com/JuliaBinaryWrappers/Git_jll.jl).
13-
Git_jll uses the Pkg Artifacts system, and therefore Git_jll and GitCommand
14-
require at least Julia 1.3.
13+
The latest version of GitCommand requires at least Julia 1.6.
1514

1615
GitCommand is intended to work on any platform that supports Julia,
1716
including (but not limited to) Windows, macOS, Linux, and FreeBSD.
@@ -21,9 +20,7 @@ including (but not limited to) Windows, macOS, Linux, and FreeBSD.
2120
```julia
2221
julia> using GitCommand
2322

24-
julia> git() do git
25-
run(`$git clone https://github.com/JuliaRegistries/General`)
26-
end
23+
julia> run(`$(git()) clone https://github.com/JuliaRegistries/General`)
2724
```
2825

2926
## Git REPL mode

src/git.jl

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
11
function _git_cmd(str::AbstractString;
22
adjust_PATH::Bool = true,
33
adjust_LIBPATH::Bool = true)
4-
git_path, env_mapping = _env_mapping(; adjust_PATH = adjust_PATH,
5-
adjust_LIBPATH = adjust_LIBPATH)
6-
new_env = copy(ENV)
7-
for p in env_mapping
8-
new_env[p[1]] = p[2]
9-
end
10-
new_cmd = Cmd(`$(git_path) $(split(str))`; env = new_env)
11-
return new_cmd
4+
git_path, env_mapping = _env_mapping(; adjust_PATH, adjust_LIBPATH)
5+
return addenv(`$(git_path) $(split(str))`, env_mapping)
126
end
137

148
macro git_cmd(ex)
@@ -25,12 +19,16 @@ function _gitrepl_parser(repl_input::AbstractString)
2519
end
2620
end
2721

22+
function git(;
23+
adjust_PATH::Bool = true,
24+
adjust_LIBPATH::Bool = true)
25+
git_path, env_mapping = _env_mapping(; adjust_PATH, adjust_LIBPATH)
26+
return addenv(`$(git_path)`, env_mapping...)
27+
end
28+
29+
# This function should be deprecated, it's kept only for backward-compatibility
2830
function git(f::Function;
2931
adjust_PATH::Bool = true,
3032
adjust_LIBPATH::Bool = true)
31-
git_path, env_mapping = _env_mapping(; adjust_PATH = adjust_PATH,
32-
adjust_LIBPATH = adjust_LIBPATH)
33-
return withenv(env_mapping...) do
34-
return f(git_path)
35-
end
33+
return f(git(; adjust_PATH, adjust_LIBPATH))
3634
end

test/runtests.jl

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
using GitCommand
22
using Test
3+
using JLLWrappers
4+
5+
get_env(env) = get(ENV, env, nothing)
6+
const orig_libpath = get_env(JLLWrappers.LIBPATH_env)
7+
const orig_execpath = get_env("GIT_EXEC_PATH")
8+
const orig_cainfo = get_env("GIT_SSL_CAINFO")
9+
const orig_templatedir = get_env("GIT_TEMPLATE_DIR")
310

411
include("test-utils.jl")
512

@@ -10,6 +17,14 @@ include("test-utils.jl")
1017
@test GitCommand._separator() == ':'
1118
end
1219

20+
with_temp_dir() do tmp_dir
21+
@test !isdir("GitCommand.jl")
22+
@test !isfile(joinpath("GitCommand.jl", "Project.toml"))
23+
run(`$(git()) clone https://github.com/JuliaVersionControl/GitCommand.jl`)
24+
@test isdir("GitCommand.jl")
25+
@test isfile(joinpath("GitCommand.jl", "Project.toml"))
26+
end
27+
1328
with_temp_dir() do tmp_dir
1429
@test !isdir("GitCommand.jl")
1530
@test !isfile(joinpath("GitCommand.jl", "Project.toml"))
@@ -46,3 +61,11 @@ include("test-utils.jl")
4661
@test isfile(joinpath("GitCommand.jl", "Project.toml"))
4762
end
4863
end
64+
65+
@testset "Safety" begin
66+
# Make sure `git` commands don't leak environment variables
67+
@test orig_libpath == get_env(JLLWrappers.LIBPATH_env)
68+
@test orig_execpath == get_env("GIT_EXEC_PATH")
69+
@test orig_cainfo == get_env("GIT_SSL_CAINFO")
70+
@test orig_templatedir == get_env("GIT_TEMPLATE_DIR")
71+
end

0 commit comments

Comments
 (0)