Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent infinite loops during symlink traversal #9748

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/fix_prevent_infinite_loops_during_symlink.md
@@ -0,0 +1 @@
* [#9748](https://github.com/rubocop/rubocop/pull/9748): Prevent infinite loops during symlink traversal. ([@Tonkpils][])
11 changes: 9 additions & 2 deletions lib/rubocop/target_finder.rb
Expand Up @@ -100,12 +100,19 @@ def wanted_dir_patterns(base_dir, exclude_pattern, flags)
next true if dir.end_with?('/./', '/../')
next true if File.fnmatch?(exclude_pattern, dir, flags)

File.symlink?(dir.chomp('/')) && File.fnmatch?(exclude_pattern,
"#{File.realpath(dir)}/", flags)
symlink_excluded_or_infinite_loop?(base_dir, dir, exclude_pattern, flags)
end
dirs.flat_map { |dir| wanted_dir_patterns(dir, exclude_pattern, flags) }.unshift(base_dir)
end

def symlink_excluded_or_infinite_loop?(base_dir, current_dir, exclude_pattern, flags)
dir_realpath = File.realpath(current_dir)
File.symlink?(current_dir.chomp('/')) && (
File.fnmatch?(exclude_pattern, "#{dir_realpath}/", flags) ||
File.realpath(base_dir).start_with?(dir_realpath)
)
end

def combined_exclude_glob_patterns(base_dir)
exclude = @config_store.for(base_dir).for_all_cops['Exclude']
patterns = exclude.select { |pattern| pattern.is_a?(String) && pattern.end_with?('/**/*') }
Expand Down
6 changes: 6 additions & 0 deletions spec/rubocop/target_finder_spec.rb
Expand Up @@ -448,6 +448,12 @@
end
end

it 'prevents infinite loops when traversing symlinks' do
create_link('dir1/link/', File.expand_path('dir1'))

expect(found_basenames).to include('ruby1.rb').once
end

it 'resolves symlinks when looking for excluded directories' do
create_link('link', 'dir1')

Expand Down