summaryrefslogtreecommitdiff
path: root/spec/bundler/bundler/source/git/git_proxy_spec.rb
blob: 1450316d59bdea4b92e840ca95d8a9ceab7057df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# frozen_string_literal: true

RSpec.describe Bundler::Source::Git::GitProxy do
  let(:path) { Pathname("path") }
  let(:uri) { "https://github.com/rubygems/rubygems.git" }
  let(:ref) { nil }
  let(:branch) { nil }
  let(:tag) { nil }
  let(:options) { { "ref" => ref, "branch" => branch, "tag" => tag }.compact }
  let(:revision) { nil }
  let(:git_source) { nil }
  let(:clone_result) { double(Process::Status, success?: true) }
  let(:base_clone_args) { ["clone", "--bare", "--no-hardlinks", "--quiet", "--no-tags", "--depth", "1", "--single-branch"] }
  subject(:git_proxy) { described_class.new(path, uri, options, revision, git_source) }

  context "with explicit ref" do
    context "with branch only" do
      let(:branch) { "main" }
      it "sets explicit ref to branch" do
        expect(git_proxy.explicit_ref).to eq(branch)
      end
    end

    context "with ref only" do
      let(:ref) { "HEAD" }
      it "sets explicit ref to ref" do
        expect(git_proxy.explicit_ref).to eq(ref)
      end
    end

    context "with tag only" do
      let(:tag) { "v1.0" }
      it "sets explicit ref to ref" do
        expect(git_proxy.explicit_ref).to eq(tag)
      end
    end

    context "with tag and branch" do
      let(:tag) { "v1.0" }
      let(:branch) { "main" }
      it "raises error" do
        expect { git_proxy }.to raise_error(Bundler::Source::Git::AmbiguousGitReference)
      end
    end

    context "with tag and ref" do
      let(:tag) { "v1.0" }
      let(:ref) { "HEAD" }
      it "raises error" do
        expect { git_proxy }.to raise_error(Bundler::Source::Git::AmbiguousGitReference)
      end
    end

    context "with branch and ref" do
      let(:branch) { "main" }
      let(:ref) { "HEAD" }
      it "honors ref over branch" do
        expect(git_proxy.explicit_ref).to eq(ref)
      end
    end
  end

  context "with configured credentials" do
    it "adds username and password to URI" do
      Bundler.settings.temporary(uri => "u:p") do
        allow(git_proxy).to receive(:git_local).with("--version").and_return("git version 2.14.0")
        expect(git_proxy).to receive(:capture).with([*base_clone_args, "--", "https://u:p@github.com/rubygems/rubygems.git", path.to_s], nil).and_return(["", "", clone_result])
        subject.checkout
      end
    end

    it "adds username and password to URI for host" do
      Bundler.settings.temporary("github.com" => "u:p") do
        allow(git_proxy).to receive(:git_local).with("--version").and_return("git version 2.14.0")
        expect(git_proxy).to receive(:capture).with([*base_clone_args, "--", "https://u:p@github.com/rubygems/rubygems.git", path.to_s], nil).and_return(["", "", clone_result])
        subject.checkout
      end
    end

    it "does not add username and password to mismatched URI" do
      Bundler.settings.temporary("https://u:p@github.com/rubygems/rubygems-mismatch.git" => "u:p") do
        allow(git_proxy).to receive(:git_local).with("--version").and_return("git version 2.14.0")
        expect(git_proxy).to receive(:capture).with([*base_clone_args, "--", uri, path.to_s], nil).and_return(["", "", clone_result])
        subject.checkout
      end
    end

    it "keeps original userinfo" do
      Bundler.settings.temporary("github.com" => "u:p") do
        original = "https://orig:info@github.com/rubygems/rubygems.git"
        git_proxy = described_class.new(Pathname("path"), original, options)
        allow(git_proxy).to receive(:git_local).with("--version").and_return("git version 2.14.0")
        expect(git_proxy).to receive(:capture).with([*base_clone_args, "--", original, path.to_s], nil).and_return(["", "", clone_result])
        git_proxy.checkout
      end
    end
  end

  describe "#version" do
    context "with a normal version number" do
      before do
        expect(git_proxy).to receive(:git_local).with("--version").
          and_return("git version 1.2.3")
      end

      it "returns the git version number" do
        expect(git_proxy.version).to eq("1.2.3")
      end

      it "does not raise an error when passed into Gem::Version.create" do
        expect { Gem::Version.create git_proxy.version }.not_to raise_error
      end
    end

    context "with a OSX version number" do
      before do
        expect(git_proxy).to receive(:git_local).with("--version").
          and_return("git version 1.2.3 (Apple Git-BS)")
      end

      it "strips out OSX specific additions in the version string" do
        expect(git_proxy.version).to eq("1.2.3")
      end

      it "does not raise an error when passed into Gem::Version.create" do
        expect { Gem::Version.create git_proxy.version }.not_to raise_error
      end
    end

    context "with a msysgit version number" do
      before do
        expect(git_proxy).to receive(:git_local).with("--version").
          and_return("git version 1.2.3.msysgit.0")
      end

      it "strips out msysgit specific additions in the version string" do
        expect(git_proxy.version).to eq("1.2.3")
      end

      it "does not raise an error when passed into Gem::Version.create" do
        expect { Gem::Version.create git_proxy.version }.not_to raise_error
      end
    end
  end

  describe "#full_version" do
    context "with a normal version number" do
      before do
        expect(git_proxy).to receive(:git_local).with("--version").
          and_return("git version 1.2.3")
      end

      it "returns the git version number" do
        expect(git_proxy.full_version).to eq("1.2.3")
      end
    end

    context "with a OSX version number" do
      before do
        expect(git_proxy).to receive(:git_local).with("--version").
          and_return("git version 1.2.3 (Apple Git-BS)")
      end

      it "does not strip out OSX specific additions in the version string" do
        expect(git_proxy.full_version).to eq("1.2.3 (Apple Git-BS)")
      end
    end

    context "with a msysgit version number" do
      before do
        expect(git_proxy).to receive(:git_local).with("--version").
          and_return("git version 1.2.3.msysgit.0")
      end

      it "does not strip out msysgit specific additions in the version string" do
        expect(git_proxy.full_version).to eq("1.2.3.msysgit.0")
      end
    end
  end

  it "doesn't allow arbitrary code execution through Gemfile uris with a leading dash" do
    gemfile <<~G
      gem "poc", git: "-u./pay:load.sh"
    G

    file = bundled_app("pay:load.sh")

    create_file file, <<~RUBY
      #!/bin/sh

      touch #{bundled_app("canary")}
    RUBY

    FileUtils.chmod("+x", file)

    bundle :lock, raise_on_error: false

    expect(Pathname.new(bundled_app("canary"))).not_to exist
  end
end