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

NPM author name parsing can crash if package.json contains an array of authors #960

Closed
ikaronen-relex opened this issue Dec 16, 2022 · 5 comments

Comments

@ikaronen-relex
Copy link

Hi! While upgrading some dependencies in our project, I noticed that our build process (which automatically runs this gem) crashes after upgrading to LicenseFinder v7.1.0.

This happens because one of our NPM dependencies (https://github.com/chenglou/react-motion) contains "author": ["nkbt", "chenglou"] in its package.json. The author metadata parsing introduced in 78fc9ab doesn't expect the value of this field to be an array and crashes with a TypeError:

bundler: failed to load command: license_finder (/Users/ilmarikaronen/.relex-gems/9.4.0.0/bin/license_finder)
TypeError: no implicit conversion of String into Integer
  org/jruby/RubyArray.java:1661:in `[]'
  /Users/ilmarikaronen/.relex-gems/9.4.0.0/gems/license_finder-7.1.0/lib/license_finder/packages/npm_package.rb:95:in `author_name'
  /Users/ilmarikaronen/.relex-gems/9.4.0.0/gems/license_finder-7.1.0/lib/license_finder/packages/npm_package.rb:83:in `author_names'
  /Users/ilmarikaronen/.relex-gems/9.4.0.0/gems/license_finder-7.1.0/lib/license_finder/packages/npm_package.rb:75:in `initialize'
  org/jruby/RubyClass.java:904:in `new'
  /Users/ilmarikaronen/.relex-gems/9.4.0.0/gems/license_finder-7.1.0/lib/license_finder/packages/npm_package.rb:26:in `flattened_dependencies'
  /Users/ilmarikaronen/.relex-gems/9.4.0.0/gems/license_finder-7.1.0/lib/license_finder/packages/npm_package.rb:28:in `block in flattened_dependencies'
  org/jruby/RubyArray.java:2812:in `map'
  /Users/ilmarikaronen/.relex-gems/9.4.0.0/gems/license_finder-7.1.0/lib/license_finder/packages/npm_package.rb:27:in `flattened_dependencies'
  /Users/ilmarikaronen/.relex-gems/9.4.0.0/gems/license_finder-7.1.0/lib/license_finder/packages/npm_package.rb:9:in `packages_from_json'
  /Users/ilmarikaronen/.relex-gems/9.4.0.0/gems/license_finder-7.1.0/lib/license_finder/package_managers/npm.rb:14:in `current_packages'
  /Users/ilmarikaronen/.relex-gems/9.4.0.0/gems/license_finder-7.1.0/lib/license_finder/package_manager.rb:105:in `current_packages_with_relations'
  org/jruby/RubyArray.java:1988:in `each'
  org/jruby/RubyEnumerable.java:911:in `flat_map'
  /Users/ilmarikaronen/.relex-gems/9.4.0.0/gems/license_finder-7.1.0/lib/license_finder/scanner.rb:42:in `active_packages'
  /Users/ilmarikaronen/.relex-gems/9.4.0.0/gems/license_finder-7.1.0/lib/license_finder/core.rb:84:in `current_packages'
  /Users/ilmarikaronen/.relex-gems/9.4.0.0/gems/license_finder-7.1.0/lib/license_finder/core.rb:79:in `decision_applier'
  uri:classloader:/META-INF/jruby.home/lib/ruby/stdlib/forwardable.rb:232:in `any_packages?'
  /Users/ilmarikaronen/.relex-gems/9.4.0.0/gems/license_finder-7.1.0/lib/license_finder/license_aggregator.rb:17:in `block in any_packages?'
  org/jruby/RubyArray.java:2812:in `map'
  /Users/ilmarikaronen/.relex-gems/9.4.0.0/gems/license_finder-7.1.0/lib/license_finder/license_aggregator.rb:15:in `any_packages?'
  /Users/ilmarikaronen/.relex-gems/9.4.0.0/gems/license_finder-7.1.0/lib/license_finder/cli/main.rb:121:in `action_items'
  /Users/ilmarikaronen/.relex-gems/9.4.0.0/gems/thor-1.2.1/lib/thor/command.rb:27:in `run'
  /Users/ilmarikaronen/.relex-gems/9.4.0.0/gems/thor-1.2.1/lib/thor/invocation.rb:127:in `invoke_command'
  /Users/ilmarikaronen/.relex-gems/9.4.0.0/gems/thor-1.2.1/lib/thor.rb:392:in `dispatch'
  /Users/ilmarikaronen/.relex-gems/9.4.0.0/gems/thor-1.2.1/lib/thor/base.rb:485:in `start'
  /Users/ilmarikaronen/.relex-gems/9.4.0.0/gems/license_finder-7.1.0/bin/license_finder:6:in `<main>'
  org/jruby/RubyKernel.java:1091:in `load'
  /Users/ilmarikaronen/.relex-gems/9.4.0.0/bin/license_finder:25:in `<main>'
  org/jruby/RubyKernel.java:1091:in `load'

To be fair, this package.json is invalid according to the NPM documentation, which says that multiple authors should be listed under contributors, and I've already reported it at chenglou/react-motion#630. Nonetheless it's still found in the wild (and this might not be the only case) and probably shouldn't crash LicenseFinder completely. Either the author_names method should be tweaked to accept this syntax, or at least the TypeError (and other possible errors caused by such invalid package metadata) should be rescued and handled gracefully.

@cf-gitbot
Copy link
Collaborator

We have created an issue in Pivotal Tracker to manage this. Unfortunately, the Pivotal Tracker project is private so you may be unable to view the contents of the story.

The labels on this github issue will be updated when the story is started.

@ikaronen-relex
Copy link
Author

FWIW, here's a quick "belt and suspenders" solution that does both:

    def author_names
      names = []
      if @json['author'].is_a?(Array)
        # "author":["foo","bar"] isn't valid according to the NPM package.json schema, but can be found in the wild.
        names += @json['author'].map { |a| author_name(a) }
      else
        names << author_name(@json['author']) unless @json['author'].nil?
      end
      names += @json['contributors'].map { |c| author_name(c) } if @json['contributors'].is_a?(Array)
      names.compact.join(', ')
    rescue TypeError
      puts "Warning: Invalid author and/or contributors metadata found in package.json for #{@identifier}"
      nil
    end

@xtreme-shane-lattanzio
Copy link
Contributor

Hey @ikaronen-relex ! Someone made a PR for this #959 but I do like how your solution as well. I think this is more clean so I asked them to update it to this method

tomcornelis added a commit to tomcornelis/LicenseFinder that referenced this issue Jan 5, 2023
tomcornelis added a commit to tomcornelis/LicenseFinder that referenced this issue Jan 5, 2023
@ikaronen-relex
Copy link
Author

I believe this can be closed now that #959 has been merged.

@nbaulesglobalsolutions
Copy link

nbaulesglobalsolutions commented Jan 4, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants