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

(PDK-1587) Reject paths with non-ASCII characters when building #832

Merged
merged 1 commit into from
Jan 31, 2020

Conversation

rodjek
Copy link
Contributor

@rodjek rodjek commented Jan 19, 2020

No description provided.

@coveralls
Copy link

coveralls commented Jan 19, 2020

Coverage Status

Coverage increased (+0.007%) to 90.992% when pulling 21fb345 on rodjek:pdk-1587 into c1ce363 on puppetlabs:master.

@rodjek rodjek marked this pull request as ready for review January 20, 2020 01:14
@rodjek rodjek requested a review from a team as a code owner January 20, 2020 01:14
# @raise [ArgumentError] if the path contains non-ASCII characters.
#
# @return [nil]
def validate_path_encoding!(path)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Encoding may or may not be an issue in this regex. A quick look on my Windows laptop showed the string encoding is coming in as the system locale (In my case EN-AU)

[2] pry(#<PDK::Module::Build>)> path.encoding
=> #<Encoding:Windows-1252>
[3] pry(#<PDK::Module::Build>)> relative_path
=> #<Pathname:.github>
[4] pry(#<PDK::Module::Build>)> relative_path.to_s.encoding
=> #<Encoding:Windows-1252>
[5] pry(#<PDK::Module::Build>)> whereami

From: C:/Source/tmp/pdk-pr832/lib/pdk/module/build.rb @ line 119 PDK::Module::Build#stage_path:

    113:       def stage_path(path)
    114:         require 'pathname'
    115:
    116:         relative_path = Pathname.new(path).relative_path_from(Pathname.new(module_dir))
    117:         dest_path = File.join(build_dir, relative_path)
    118: require 'pry'; binding.pry
 => 119:         validate_path_encoding!(relative_path.to_path)
    120:
    121:         if PDK::Util::Filesystem.directory?(path)
    122:           PDK::Util::Filesystem.mkdir_p(dest_path, mode: PDK::Util::Filesystem.stat(path).mode)
    123:         elsif PDK::Util::Filesystem.symlink?(path)
    124:           warn_symlink(path)
    125:         else
    126:           validate_ustar_path!(relative_path.to_path)
    127:           PDK::Util::Filesystem.cp(path, dest_path, preserve: true)
    128:         end
    129:       rescue ArgumentError => e
    130:         raise PDK::CLI::ExitWithError, _(
    131:           '%{message} Rename the file or exclude it from the package ' \
    132:           'by adding it to the .pdkignore file in your module.',
    133:         ) % { message: e.message }
    134:       end

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the question is, is there a case where the regex will fail to match a non-ASCII character for a given encoding and vice versa, is there a case of false positives

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried a bunch of things but couldn't make it break. So this seems ok!

Copy link
Contributor

@glennsarti glennsarti left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test is successfully raising but for the wrong reason

PDK::Module::Build
  #stage_path
    when the path contains non-ASCII characters
      exits with an error (FAILED - 1)

Failures:

  1) PDK::Module::Build#stage_path when the path contains non-ASCII characters exits with an error
     Failure/Error:
       raise PDK::CLI::ExitWithError, _(
         '%{message} Rename the file or exclude it from the package ' \
         'by adding it to the .pdkignore file in your module.',
       ) % { message: e.message }

     PDK::CLI::ExitWithError:
       different prefix: "" and "C:/tmp/my-module" Rename the file or exclude it from the package by adding it to the .pdkignore file in your module.
     # ./lib/pdk/module/build.rb:129:in `rescue in stage_path'
     # ./lib/pdk/module/build.rb:112:in `stage_path'
     # ./spec/unit/pdk/module/build_spec.rb:189:in `block (4 levels) in <top (required)>'
     # ------------------
     # --- Caused by: ---
     # ArgumentError:
     #   different prefix: "" and "C:/tmp/my-module"
     #   ./lib/pdk/module/build.rb:115:in `stage_path'

@@ -181,6 +181,14 @@
allow(instance).to receive(:release_name).and_return(release_name)
end

context 'when the path contains non-ASCII characters' do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps

    context 'when the path contains non-ASCII characters' do
      RSpec.shared_examples "a failing path" do |relative_path|
        let (:path) do
          # We enforce encoding here as the filesystem encoding can be different from the default
          # String encoding. And encoding can make the resulting string behave differently
          "#{module_dir}\\#{relative_path}".force_encoding(Encoding.find("filesystem"))
        end

        before(:each) do
          allow(PDK::Util::Filesystem).to receive(:directory?).with(path).and_return(false)
          allow(PDK::Util::Filesystem).to receive(:symlink?).with(path).and_return(false)
          allow(PDK::Util::Filesystem).to receive(:cp).with(path, Object, Object).and_return(true)
        end

        it "exits with an error for #{relative_path}" do
          expect {
            instance.stage_path(path)
          }.to raise_error(PDK::CLI::ExitWithError, %r{can not include non-ASCII characters})
          #puts instance.stage_path(path)
        end
      end

      include_examples "a failing path", "strange_unicode_\u{000100}"
      include_examples "a failing path", "\330\271to"
    end

return unless path =~ %r{[^\x00-\x7F]}

raise ArgumentError, _(
"'%{path}' can not include non-ASCII characters in its path or " \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Not a huge fan of double negatives here. Perhaps can only include ASCII characters in its path ...

@glennsarti
Copy link
Contributor

Rekicking the build to get the license_finder fix

@glennsarti
Copy link
Contributor

The license_finder failures are not a result of this PR.

@glennsarti
Copy link
Contributor

If you rebase onto master it should go green.

@rodjek rodjek merged commit b68a249 into puppetlabs:master Jan 31, 2020
@rodjek rodjek deleted the pdk-1587 branch January 31, 2020 03:10
@rodjek rodjek added this to the 1.16.0 milestone Feb 3, 2020
@rodjek rodjek added the feature label Feb 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants