gitlimit 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: bf73bb9561bffc3078978015c021ea36ddbed380932b7da028c54d8e46177593
4
+ data.tar.gz: fb126e389317dc271373546b695235277b70330fed01a4a05709e41317bf5c9d
5
+ SHA512:
6
+ metadata.gz: 3b5dc633ae8be51d8ca58014a34014238a277336678d9b0ce1f459bc57f947110647827be44d03d9d0f9a32f7a3c7ea68cc624237117f4b4e0d31fe9a91e2564
7
+ data.tar.gz: a3710ac57bdb7acd73f76afcdd167ebce6bc5df29a6d549207800273a5711ec9c5d0bd61302a4977349cd95896d120a452ba6a257976e95d7afa32dcb6c9b388
data/LICENSE ADDED
@@ -0,0 +1,14 @@
1
+ Copyright (c) 2014 Damien Miller <djm@mindrot.org>
2
+ Copyright (c) 2026 Chris Hasinski <krzysztof.hasinski@gmail.com>
3
+
4
+ Permission to use, copy, modify, and distribute this software for any
5
+ purpose with or without fee is hereby granted, provided that the above
6
+ copyright notice and this permission notice appear in all copies.
7
+
8
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9
+ WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10
+ MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11
+ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12
+ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13
+ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14
+ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
data/bin/gitlimit ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "gitlimit"
5
+
6
+ Gitlimit::CLI.run
@@ -0,0 +1,102 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Gitlimit
4
+ MODE_READ = 0b01
5
+ MODE_WRITE = 0b10
6
+
7
+ MODE_PARSE = {
8
+ "r" => MODE_READ,
9
+ "w" => MODE_WRITE,
10
+ "rw" => MODE_READ | MODE_WRITE,
11
+ "wr" => MODE_READ | MODE_WRITE
12
+ }.freeze
13
+
14
+ ALLOWED_COMMANDS = {
15
+ "git-upload-pack" => MODE_READ,
16
+ "git-receive-pack" => MODE_WRITE
17
+ }.freeze
18
+
19
+ class AccessDenied < StandardError; end
20
+
21
+ class Access
22
+ attr_reader :command, :repository
23
+
24
+ def initialize(ssh_command:, mode:, permitted_repos:)
25
+ @mode = mode
26
+ @permitted_repos = permitted_repos
27
+ @command, @repository = parse_ssh_command(ssh_command)
28
+ end
29
+
30
+ def check!
31
+ check_command_allowed!
32
+ check_repo_permitted!
33
+ true
34
+ end
35
+
36
+ private
37
+
38
+ def parse_ssh_command(command_line)
39
+ command_line = command_line.to_s.strip
40
+ raise AccessDenied, "empty git command" if command_line.empty?
41
+
42
+ # SSH sends: git-upload-pack '/path/to/repo.git'
43
+ # The repo path is always single-quoted by the git client.
44
+ match = command_line.match(/\A(git-(?:upload|receive)-pack)\s+'(.*)'\z/)
45
+ raise AccessDenied, "invalid git command #{command_line.inspect}" unless match
46
+
47
+ command = match[1]
48
+ repository = match[2]
49
+
50
+ raise AccessDenied, "invalid repository #{repository.inspect}" if repository.empty?
51
+ raise AccessDenied, "invalid repository #{repository.inspect}" if repository.include?("'")
52
+ raise AccessDenied, "invalid repository #{repository.inspect}" if repository.start_with?("-")
53
+
54
+ repository = canonicalize(repository)
55
+
56
+ [command, repository]
57
+ end
58
+
59
+ def canonicalize(path)
60
+ # Collapse /./, /../, and // without touching the filesystem.
61
+ # This prevents path traversal like /git/../etc/shadow from
62
+ # bypassing an allowlist entry of /git/repo.git.
63
+ parts = path.split("/")
64
+ result = []
65
+
66
+ parts.each do |part|
67
+ case part
68
+ when "", "."
69
+ next
70
+ when ".."
71
+ result.pop
72
+ else
73
+ result << part
74
+ end
75
+ end
76
+
77
+ prefix = path.start_with?("/") ? "/" : ""
78
+ "#{prefix}#{result.join("/")}"
79
+ end
80
+
81
+ def check_command_allowed!
82
+ required_mode = ALLOWED_COMMANDS[@command]
83
+ raise AccessDenied, "command not permitted #{@command.inspect}" unless required_mode
84
+
85
+ return unless (@mode & required_mode).zero?
86
+
87
+ action = required_mode == MODE_READ ? "download" : "upload"
88
+ raise AccessDenied, "#{action} not permitted for #{@command.inspect}"
89
+ end
90
+
91
+ def check_repo_permitted!
92
+ canonical_repo = @repository
93
+
94
+ permitted = @permitted_repos.any? do |pattern|
95
+ pattern = canonicalize(pattern)
96
+ File.fnmatch?(pattern, canonical_repo, File::FNM_PATHNAME)
97
+ end
98
+
99
+ raise AccessDenied, "access to repository #{canonical_repo.inspect} is denied" unless permitted
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "optparse"
4
+
5
+ module Gitlimit
6
+ class CLI
7
+ def self.run(argv = ARGV, env = ENV)
8
+ new(argv, env).run
9
+ end
10
+
11
+ def initialize(argv, env)
12
+ @argv = argv.dup
13
+ @env = env
14
+ @mode = MODE_READ | MODE_WRITE
15
+ @check = false
16
+ end
17
+
18
+ def run
19
+ parse_options!
20
+
21
+ if @argv.empty?
22
+ abort @parser.to_s
23
+ end
24
+
25
+ ssh_command = @env["SSH_ORIGINAL_COMMAND"]
26
+ access = Access.new(ssh_command: ssh_command, mode: @mode, permitted_repos: @argv)
27
+ access.check!
28
+
29
+ log("permitted: #{access.command} #{access.repository}")
30
+
31
+ if @check
32
+ # Dry run - just report success
33
+ return
34
+ end
35
+
36
+ exec("git-shell", "-c", "#{access.command} '#{access.repository}'")
37
+ rescue AccessDenied => e
38
+ log("denied: #{e.message}")
39
+ abort "gitlimit: #{e.message}"
40
+ end
41
+
42
+ private
43
+
44
+ def parse_options!
45
+ @parser = OptionParser.new do |opts|
46
+ opts.banner = "Usage: gitlimit [flags] repository [repository ...]"
47
+
48
+ opts.on("-m MODE", "Mode: r (read-only), w (write-only), rw (default)") do |m|
49
+ @mode = MODE_PARSE[m]
50
+ abort "gitlimit: mode (-m) must be r / w / rw" unless @mode
51
+ end
52
+
53
+ opts.on("-c", "Check access / dry run mode") do
54
+ @check = true
55
+ end
56
+ end
57
+
58
+ @parser.parse!(@argv)
59
+ end
60
+
61
+ def log(message)
62
+ $stderr.puts "gitlimit: #{message}"
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Gitlimit
4
+ VERSION = "0.1.0"
5
+ end
data/lib/gitlimit.rb ADDED
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "gitlimit/version"
4
+ require_relative "gitlimit/access"
5
+ require_relative "gitlimit/cli"
metadata ADDED
@@ -0,0 +1,51 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gitlimit
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Chris Hasinski
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies: []
12
+ description: A simple tool to limit access to git repositories for SSH git clients,
13
+ intended to be run from authorized_keys. Supports read/write mode control, glob
14
+ patterns, and path canonicalization.
15
+ email:
16
+ - krzysztof.hasinski@gmail.com
17
+ executables:
18
+ - gitlimit
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - LICENSE
23
+ - bin/gitlimit
24
+ - lib/gitlimit.rb
25
+ - lib/gitlimit/access.rb
26
+ - lib/gitlimit/cli.rb
27
+ - lib/gitlimit/version.rb
28
+ homepage: https://github.com/khasinski/gitlimit
29
+ licenses:
30
+ - ISC
31
+ metadata:
32
+ homepage_uri: https://github.com/khasinski/gitlimit
33
+ source_code_uri: https://github.com/khasinski/gitlimit
34
+ rdoc_options: []
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '3.0'
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ requirements: []
48
+ rubygems_version: 4.0.3
49
+ specification_version: 4
50
+ summary: Control access to git repositories via SSH
51
+ test_files: []