Skip to content

Self-hosted project management for devs and their clients: tickets, time tracking, docs and meetings, with an AI assistant that cites its sources. AGPL-3.0.

Check out repository symlinks as plain files

Guido Boykea0e6b40a239e
diff --git a/app/services/lalabase/indexer/git_cloner.rb b/app/services/lalabase/indexer/git_cloner.rb
index 0a23636..5e82987 100644
--- a/app/services/lalabase/indexer/git_cloner.rb
+++ b/app/services/lalabase/indexer/git_cloner.rb
@@ -125,7 +125,12 @@ module Lalabase
end
def clone_args(dir)
- ['git', 'clone', '--depth', @depth.to_s,
+ # core.symlinks=false checks every symlink out as a plain file holding the
+ # link target. Every reader of the working tree follows links, so a link
+ # to a server file (the app's .env, /proc/self/environ) would otherwise
+ # be read and published as a documentation page. `clone -c` writes the
+ # setting into the clone's config, so the ref checkout below keeps it.
+ ['git', 'clone', '-c', 'core.symlinks=false', '--depth', @depth.to_s,
'--branch', @repository.default_branch.to_s,
'--single-branch', clone_url, dir]
end
diff --git a/spec/services/lalabase/indexer/git_cloner_spec.rb b/spec/services/lalabase/indexer/git_cloner_spec.rb
index 32ff572..93a157f 100644
--- a/spec/services/lalabase/indexer/git_cloner_spec.rb
+++ b/spec/services/lalabase/indexer/git_cloner_spec.rb
@@ -72,7 +72,7 @@ RSpec.describe Lalabase::Indexer::GitCloner do
stub_popen3(exit_status: 0)
described_class.new(repository).call { |_| }
expect(Open3).to have_received(:popen3).with(
- 'git', 'clone', '--depth', '1', '--branch', 'main', '--single-branch',
+ 'git', 'clone', '-c', 'core.symlinks=false', '--depth', '1', '--branch', 'main', '--single-branch',
'https://x-access-token:[email protected]/acme/backend.git',
a_string_matching(%r{/lalabase-clone-})
)
@@ -82,7 +82,7 @@ RSpec.describe Lalabase::Indexer::GitCloner do
stub_popen3(exit_status: 0)
described_class.new(repository, depth: 5).call { |_| }
expect(Open3).to have_received(:popen3).with(
- 'git', 'clone', '--depth', '5', anything, anything, anything, anything, anything
+ 'git', 'clone', '-c', 'core.symlinks=false', '--depth', '5', anything, anything, anything, anything, anything
)
end
@@ -286,7 +286,7 @@ RSpec.describe Lalabase::Indexer::GitCloner do
described_class.new(repository, ref: 'deadbeef123').call { |_| }
expect(Open3).to have_received(:popen3).with(
- 'git', 'clone', '--depth', '1', '--branch', 'main', '--single-branch', anything, anything
+ 'git', 'clone', '-c', 'core.symlinks=false', '--depth', '1', '--branch', 'main', '--single-branch', anything, anything
)
expect(Open3).to have_received(:popen3).with('git', '-C', anything, 'fetch', '--depth', '1', 'origin', 'deadbeef123')
expect(Open3).to have_received(:popen3).with('git', '-C', anything, 'checkout', '--quiet', 'deadbeef123')
@@ -343,7 +343,7 @@ RSpec.describe Lalabase::Indexer::GitCloner do
stub_popen3(exit_status: 0)
described_class.new(feature_repo).call { |_| }
expect(Open3).to have_received(:popen3).with(
- 'git', 'clone', '--depth', '1', '--branch', 'feature/foo', '--single-branch', anything, anything
+ 'git', 'clone', '-c', 'core.symlinks=false', '--depth', '1', '--branch', 'feature/foo', '--single-branch', anything, anything
)
end
end
@@ -394,4 +394,58 @@ RSpec.describe Lalabase::Indexer::GitCloner do
expect(cloner.remote_head_commit).to be_nil
end
end
+
+ # Every reader of the working tree (documentation sync, RepoScanner, symbol
+ # walker) follows symlinks. A repository that commits `docs/x.md -> /etc/passwd`
+ # or a link to the app's own .env made the documentation sync publish that
+ # server file as a page. The working tree must hold no symlink at all.
+ describe 'symlinks in the repository' do
+ let(:outside) { Dir.mktmpdir('cloner-outside-') }
+ let(:source) { Dir.mktmpdir('cloner-symlinks-') }
+
+ before do
+ File.write(File.join(outside, 'secret.txt'), 'SERVER_SECRET=do-not-leak')
+ system('git', 'init', '-b', 'main', source, out: File::NULL, err: File::NULL)
+ system('git', '-C', source, 'config', 'user.email', '[email protected]', out: File::NULL)
+ system('git', '-C', source, 'config', 'user.name', 'Spec', out: File::NULL)
+ system('git', '-C', source, 'config', 'uploadpack.allowAnySHA1InWant', 'true', out: File::NULL)
+ FileUtils.mkdir_p(File.join(source, 'docs'))
+ File.write(File.join(source, 'docs', 'index.md'), '# Docs')
+ File.symlink(File.join(outside, 'secret.txt'), File.join(source, 'docs', 'leak.md'))
+ File.symlink(outside, File.join(source, 'linked_dir'))
+ system('git', '-C', source, 'add', '.', out: File::NULL)
+ system('git', '-C', source, 'commit', '-m', 'init', out: File::NULL, err: File::NULL)
+ end
+
+ after { [outside, source].each { |d| FileUtils.remove_entry(d, true) } }
+
+ def working_tree_after_clone(cloner)
+ allow(cloner).to receive(:clone_url).and_return("file://#{source}")
+ seen = {}
+ cloner.call do |path|
+ seen[:links] = Dir.glob('**/*', File::FNM_DOTMATCH, base: path.to_s)
+ .select { |p| File.symlink?(path.join(p)) }
+ seen[:leak] = path.join('docs', 'leak.md').read
+ seen[:dir_is_directory] = path.join('linked_dir').directory?
+ end
+ seen
+ end
+
+ it 'checks every link out as a plain file holding the target path' do
+ seen = working_tree_after_clone(described_class.new(repository))
+
+ expect(seen[:links]).to be_empty
+ expect(seen[:leak]).not_to include('do-not-leak')
+ expect(seen[:leak]).to eq(File.join(outside, 'secret.txt'))
+ expect(seen[:dir_is_directory]).to be(false)
+ end
+
+ it 'does the same when a pinned ref is fetched and checked out' do
+ sha = `git -C #{source} rev-parse HEAD`.strip
+ seen = working_tree_after_clone(described_class.new(repository, ref: sha))
+
+ expect(seen[:links]).to be_empty
+ expect(seen[:leak]).not_to include('do-not-leak')
+ end
+ end
end