From f58a87a693a76db8b0dbdcaa361f6afec6b906ff Mon Sep 17 00:00:00 2001 From: TristanKruse Date: Wed, 25 Nov 2026 19:57:04 +0100 Subject: [PATCH] Reject ambiguous native source paths before graph extraction --- CHANGELOG.md | 2 ++ GUIDE.md | 3 +++ .../extraction/enumerate_source_files.rb | 3 ++- lib/archunit/extraction/locate_project.rb | 8 ++----- lib/archunit/extraction/source_identifier.rb | 24 +++++++++++++++++++ lib/archunit/extraction/source_walker.rb | 4 ++-- spec/extraction/locate_project_spec.rb | 10 ++++++++ spec/extraction/source_walker_spec.rb | 22 +++++++++++++++++ 8 files changed, 67 insertions(+), 9 deletions(-) create mode 100644 lib/archunit/extraction/source_identifier.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index 132155f..58e833a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,8 @@ from directory children in a pruned iterative source walker, preserving sorted graph inputs. - Fail closed on unreadable/disappearing included directory listings instead of inheriting Find's silent listing-error skip. Excluded trees are still pruned without opening their descendants. +- Refuse ambiguous POSIX source paths containing a literal backslash rather than checking a + different file or silently passing. Ordinary Windows separators remain supported. - Extend the existing extraction benchmark with allocation counts, full graph/evidence digests, and excluded-tree workloads; retain generous CI timing ceilings. diff --git a/GUIDE.md b/GUIDE.md index 770901e..7abcd30 100644 --- a/GUIDE.md +++ b/GUIDE.md @@ -43,6 +43,9 @@ ArchUnit.project_files('/workspace/my_app/Gemfile') Source discovery skips symlinked files and directories, including project-local aliases. This avoids duplicate file identities and unintentionally parsing linked source outside the project. Direct file-information extraction also checks the canonical source path against the project root. +On POSIX a literal backslash is legal in a name, but cannot be represented as an ArchUnit file +identifier. Rename that project root, or rename or exclude the affected source. Discovery reports +an error rather than silently analyzing a different file. ### Project-wide exclusions diff --git a/lib/archunit/extraction/enumerate_source_files.rb b/lib/archunit/extraction/enumerate_source_files.rb index 68fcdd2..b9f418d 100644 --- a/lib/archunit/extraction/enumerate_source_files.rb +++ b/lib/archunit/extraction/enumerate_source_files.rb @@ -4,6 +4,7 @@ require_relative '../error/technical_error' require_relative '../error/user_error' require_relative 'arch_ignore' +require_relative 'source_identifier' require_relative 'source_walker' require_relative '../common/fluentapi/check_options' @@ -109,7 +110,7 @@ def source_root(value) root = Pathname.new(value).expand_path raise UserError, 'project_root must be an existing directory' unless root.directory? - root.realpath + Pathname.new(SourceIdentifier.root(root.realpath)) rescue SystemCallError => e raise TechnicalError, "could not enumerate Ruby source files: #{e.message}" end diff --git a/lib/archunit/extraction/locate_project.rb b/lib/archunit/extraction/locate_project.rb index 77b8d83..b65e1a2 100644 --- a/lib/archunit/extraction/locate_project.rb +++ b/lib/archunit/extraction/locate_project.rb @@ -3,6 +3,7 @@ require 'pathname' require_relative '../error/technical_error' require_relative '../error/user_error' +require_relative 'source_identifier' module ArchUnit # Ruby-specific project discovery and source extraction. @@ -12,7 +13,7 @@ module Extraction def locate_project(locator = nil, working_directory: Dir.pwd) working_root = existing_directory(working_directory, :working_directory) root = project_root(locator, working_root) - normalize_path(root.realpath) + SourceIdentifier.root(root.realpath) rescue UserError, TechnicalError raise rescue SystemCallError => e @@ -71,10 +72,5 @@ def pathname(value, attribute) Pathname.new(value) end private_class_method :pathname - - def normalize_path(path) - path.to_s.tr('\\', '/') - end - private_class_method :normalize_path end end diff --git a/lib/archunit/extraction/source_identifier.rb b/lib/archunit/extraction/source_identifier.rb new file mode 100644 index 0000000..5a30616 --- /dev/null +++ b/lib/archunit/extraction/source_identifier.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +require_relative '../error/technical_error' + +module ArchUnit + module Extraction + # A native path component must never be reinterpreted as two project components. + # @api private + module SourceIdentifier + def self.validate(path) + return path unless path.include?('\\') + + raise TechnicalError, "cannot represent Ruby source path #{path.inspect}: " \ + 'literal backslash; rename the path or exclude the source' + end + + def self.root(path) + native = path.to_s + File::ALT_SEPARATOR ? native.tr(File::ALT_SEPARATOR, '/') : validate(native) + end + end + private_constant :SourceIdentifier + end +end diff --git a/lib/archunit/extraction/source_walker.rb b/lib/archunit/extraction/source_walker.rb index dc5cb14..96b9575 100644 --- a/lib/archunit/extraction/source_walker.rb +++ b/lib/archunit/extraction/source_walker.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true require_relative 'source_pattern_matcher' +require_relative 'source_identifier' module ArchUnit module Extraction @@ -53,7 +54,6 @@ def source_kind(path, name) end def child_identifier(prefix, name) - name = name.tr('\\', '/') prefix.empty? ? name : "#{prefix}/#{name}" end @@ -61,7 +61,7 @@ def collect_path(kind, path, identifier, pending, files) if kind == :directory pending << [path, identifier] else - files << identifier.freeze + files << SourceIdentifier.validate(identifier).freeze end end end diff --git a/spec/extraction/locate_project_spec.rb b/spec/extraction/locate_project_spec.rb index 3025b1f..9aa7aca 100644 --- a/spec/extraction/locate_project_spec.rb +++ b/spec/extraction/locate_project_spec.rb @@ -23,6 +23,16 @@ def normalized(path) path.realpath.to_s.tr('\\', '/') end + it 'does not substitute a different directory for a POSIX root containing a backslash' do + skip 'backslash is a native path separator' if File::ALT_SEPARATOR + + original = create_file('one\\two/Gemfile').dirname + create_file('one/two/Gemfile') + expect { described_class.locate_project(original) }.to raise_error(ArchUnit::TechnicalError) + expect { described_class.enumerate_source_files(original) } + .to raise_error(ArchUnit::TechnicalError, /literal backslash/) + end + it 'uses an explicit directory even when it has no project marker' do project = @temporary_root.join('explicit-project') project.mkdir diff --git a/spec/extraction/source_walker_spec.rb b/spec/extraction/source_walker_spec.rb index 4130882..89d6e9e 100644 --- a/spec/extraction/source_walker_spec.rb +++ b/spec/extraction/source_walker_spec.rb @@ -65,6 +65,28 @@ def legacy_excluded?(patterns, relative, name) expect(ArchUnit::Extraction.enumerate_source_files(@root)).to eq(['lib/kept.rb']) end + it 'never reinterprets a literal backslash in a directory entry as a file separator' do + name = 'actual\\name.rb' + allow(Dir).to receive(:children).with(@root.to_s, any_args).and_return([name]) + source = instance_double(File::Stat, symlink?: false, directory?: false, file?: true) + allow(File).to receive(:lstat).and_call_original + allow(File).to receive(:lstat).with(File.join(@root, name)).and_return(source) + expect { ArchUnit::Extraction.enumerate_source_files(@root, exclude_patterns: ['actual/**']) } + .to raise_error(ArchUnit::TechnicalError, /literal backslash/) + expect(ArchUnit::Extraction.enumerate_source_files(@root, exclude_patterns: ['*.rb'])).to eq([]) + end + + it 'rejects two native POSIX paths that would otherwise identify the same Ruby file' do + skip 'backslash is a native path separator' if File::ALT_SEPARATOR + + write_source('normal/nested/item.rb') + write_source('normal\\nested/item.rb') + expect { ArchUnit::Extraction.extract_graph(@root) } + .to raise_error(ArchUnit::TechnicalError, /literal backslash/) + expect(ArchUnit::Extraction.enumerate_source_files(@root, exclude_patterns: ['*nested'])) + .to be_empty + end + [Errno::EACCES, Errno::ENOENT, Errno::ENOTDIR].each do |error| it "fails closed when an included directory listing raises #{error}" do write_source('lib/source.rb')