Skip to content

Commit de1446d

Browse files
committed
Add relative git object dir envvars to check access request
1 parent bbda5bd commit de1446d

File tree

5 files changed

+140
-7
lines changed

5 files changed

+140
-7
lines changed

CHANGELOG

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
v5.9.4
2+
- Add relative git object dir envvars to check access request
3+
14
v5.9.3
25
- Expose GitLab username to hooks in `GL_USERNAME` environment variable
36

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
5.9.3
1+
5.9.4

lib/gitlab_access.rb

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
require_relative 'gitlab_access_status'
44
require_relative 'names_helper'
55
require_relative 'gitlab_metrics'
6+
require_relative 'object_dirs_helper'
67
require 'json'
78

89
class GitlabAccess
@@ -23,12 +24,7 @@ def initialize(gl_repository, repo_path, actor, changes, protocol)
2324

2425
def exec
2526
status = GitlabMetrics.measure('check-access:git-receive-pack') do
26-
env = {
27-
"GIT_ALTERNATE_OBJECT_DIRECTORIES" => ENV["GIT_ALTERNATE_OBJECT_DIRECTORIES"],
28-
"GIT_OBJECT_DIRECTORY" => ENV["GIT_OBJECT_DIRECTORY"]
29-
}
30-
31-
api.check_access('git-receive-pack', @gl_repository, @repo_path, @actor, @changes, @protocol, env: env.to_json)
27+
api.check_access('git-receive-pack', @gl_repository, @repo_path, @actor, @changes, @protocol, env: ObjectDirsHelper.all_attributes.to_json)
3228
end
3329

3430
raise AccessDeniedError, status.message unless status.allowed?

lib/object_dirs_helper.rb

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
require 'pathname'
2+
3+
class ObjectDirsHelper
4+
class << self
5+
def all_attributes
6+
{
7+
"GIT_ALTERNATE_OBJECT_DIRECTORIES" => absolute_alt_object_dirs,
8+
"GIT_ALTERNATE_OBJECT_DIRECTORIES_RELATIVE" => relative_alt_object_dirs,
9+
"GIT_OBJECT_DIRECTORY" => absolute_object_dir,
10+
"GIT_OBJECT_DIRECTORY_RELATIVE" => relative_object_dir
11+
}
12+
end
13+
14+
def absolute_object_dir
15+
ENV['GIT_OBJECT_DIRECTORY']
16+
end
17+
18+
def relative_object_dir
19+
relative_path(absolute_object_dir)
20+
end
21+
22+
def absolute_alt_object_dirs
23+
ENV['GIT_ALTERNATE_OBJECT_DIRECTORIES'].to_s.split(File::PATH_SEPARATOR)
24+
end
25+
26+
def relative_alt_object_dirs
27+
absolute_alt_object_dirs.map { |dir| relative_path(dir) }.compact
28+
end
29+
30+
private
31+
32+
def relative_path(absolute_path)
33+
return if absolute_path.nil?
34+
35+
repo_dir = Dir.pwd
36+
Pathname.new(absolute_path).relative_path_from(Pathname.new(repo_dir)).to_s
37+
end
38+
end
39+
end

spec/object_dirs_helper_spec.rb

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
require_relative 'spec_helper'
2+
require_relative '../lib/object_dirs_helper'
3+
4+
describe ObjectDirsHelper do
5+
before do
6+
allow(Dir).to receive(:pwd).and_return('/home/git/repositories/foo/bar.git')
7+
end
8+
9+
describe '.all_attributes' do
10+
it do
11+
expect(described_class.all_attributes.keys).to include(*%w[
12+
GIT_OBJECT_DIRECTORY
13+
GIT_OBJECT_DIRECTORY_RELATIVE
14+
GIT_ALTERNATE_OBJECT_DIRECTORIES
15+
GIT_ALTERNATE_OBJECT_DIRECTORIES_RELATIVE
16+
])
17+
end
18+
end
19+
20+
describe '.absolute_object_dir' do
21+
subject { described_class.absolute_object_dir }
22+
23+
context 'when GIT_OBJECT_DIRECTORY is set' do
24+
let(:dir) { '/home/git/repositories/foo/bar.git/./objects' }
25+
26+
before do
27+
allow(ENV).to receive(:[]).with('GIT_OBJECT_DIRECTORY').and_return(dir)
28+
end
29+
30+
it { expect(subject).to eq(dir) }
31+
end
32+
33+
context 'when GIT_OBJECT_DIRECTORY is not set' do
34+
it { expect(subject).to be_nil }
35+
end
36+
end
37+
38+
describe '.absolute_alt_object_dirs' do
39+
subject { described_class.absolute_alt_object_dirs }
40+
41+
context 'when GIT_ALTERNATE_OBJECT_DIRECTORIES is set' do
42+
let(:dirs) { [
43+
'/home/git/repositories/foo/bar.git/./incoming-UKU6Gl',
44+
'/home/git/repositories/foo/bar.git/./incoming-AcU7Qr'
45+
] }
46+
47+
before do
48+
allow(ENV).to receive(:[]).with('GIT_ALTERNATE_OBJECT_DIRECTORIES').and_return(dirs.join(File::PATH_SEPARATOR))
49+
end
50+
51+
it { expect(subject).to eq(dirs) }
52+
end
53+
54+
context 'when GIT_ALTERNATE_OBJECT_DIRECTORIES is not set' do
55+
it { expect(subject).to eq([]) }
56+
end
57+
end
58+
59+
describe '.relative_alt_object_dirs' do
60+
subject { described_class.relative_alt_object_dirs }
61+
62+
context 'when GIT_ALTERNATE_OBJECT_DIRECTORIES is set' do
63+
let(:dirs) { [
64+
'/home/git/repositories/foo/bar.git/./objects/incoming-UKU6Gl',
65+
'/home/git/repositories/foo/bar.git/./objects/incoming-AcU7Qr'
66+
] }
67+
68+
before do
69+
allow(ENV).to receive(:[]).with('GIT_ALTERNATE_OBJECT_DIRECTORIES').and_return(dirs.join(File::PATH_SEPARATOR))
70+
end
71+
72+
it { expect(subject).to eq(['objects/incoming-UKU6Gl', 'objects/incoming-AcU7Qr']) }
73+
end
74+
75+
context 'when GIT_ALTERNATE_OBJECT_DIRECTORIES is not set' do
76+
it { expect(subject).to eq([]) }
77+
end
78+
end
79+
80+
describe '.relative_object_dir' do
81+
subject { described_class.relative_object_dir }
82+
83+
context 'when GIT_OBJECT_DIRECTORY is set' do
84+
before do
85+
allow(ENV).to receive(:[]).with('GIT_OBJECT_DIRECTORY').and_return('/home/git/repositories/foo/bar.git/./objects')
86+
end
87+
88+
it { expect(subject).to eq('objects') }
89+
end
90+
91+
context 'when GIT_OBJECT_DIRECTORY is not set' do
92+
it { expect(subject).to be_nil }
93+
end
94+
end
95+
end

0 commit comments

Comments
 (0)