Commit 0c7dd30c authored by Douwe Maan's avatar Douwe Maan

Make .gitmodules parsing more resilient to syntax errors

parent 8039b9c3
---
title: Make .gitmodules parsing more resilient to syntax errors
merge_request:
author:
......@@ -1008,25 +1008,34 @@ module Gitlab
def parse_gitmodules(commit, content)
results = {}
current = ""
content.split("\n").each do |txt|
if txt =~ /^\s*\[/
current = txt.match(/(?<=").*(?=")/)[0]
results[current] = {}
else
next unless results[current]
match_data = txt.match(/(\w+)\s*=\s*(.*)/)
next unless match_data
target = match_data[2].chomp
results[current][match_data[1]] = target
if match_data[1] == "path"
name = nil
entry = nil
content.each_line do |line|
case line.strip
when /\A\[submodule "(?<name>[^"]+)"\]\z/ # Submodule header
name = $~[:name]
entry = results[name] = {}
when /\A(?<key>\w+)\s*=\s*(?<value>.*)\z/ # Key/value pair
key = $~[:key]
value = $~[:value].chomp
next unless name && entry
entry[key] = value
if key == 'path'
begin
results[current]["id"] = blob_content(commit, target)
entry['id'] = blob_content(commit, value)
rescue InvalidBlobName
results.delete(current)
# The current entry is invalid
results.delete(name)
name = entry = nil
end
end
when /\A#/ # Comment
next
else # Invalid line
name = entry = nil
end
end
......@@ -1086,7 +1095,12 @@ module Gitlab
elsif tmp_entry.nil?
return nil
else
begin
tmp_entry = rugged.lookup(tmp_entry[:oid])
rescue Rugged::OdbError, Rugged::InvalidError, Rugged::ReferenceError
return nil
end
return nil unless tmp_entry.type == :tree
tmp_entry = tmp_entry[dir]
end
......
......@@ -381,6 +381,19 @@ describe Gitlab::Git::Repository, seed_helper: true do
}
])
end
it 'should not break on invalid syntax' do
allow(repository).to receive(:blob_content).and_return(<<-GITMODULES.strip_heredoc)
[submodule "six"]
path = six
url = git://github.com/randx/six.git
[submodule]
foo = bar
GITMODULES
expect(submodules).to have_key('six')
end
end
context 'where repo doesn\'t have submodules' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment