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

Use safe Marshal deserialization for dependency API response. #6141

Merged
merged 1 commit into from
Dec 18, 2022

Conversation

simi
Copy link
Member

@simi simi commented Dec 17, 2022

ℹ️ I'm looking at introducing real-word spec to cover the real response from Dependency API as well.

- adds Bundler#safe_load_marshal and Bundler::SAFE_MARSHAL_CLASSES listing safe classes to deserialize
@@ -39,6 +39,16 @@ module Bundler
environment_preserver.replace_with_backup
SUDO_MUTEX = Thread::Mutex.new

SAFE_MARSHAL_CLASSES = [Symbol, TrueClass, String, Array, Hash].freeze
Copy link
Member Author

Choose a reason for hiding this comment

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

🤔 I'm not sure why TrueClass is needed, but it is passed to proc even not part of the structure.

irb(main):013:0> simple_structure = { "name" => [:abc] }
=> {"name"=>[:abc]}
irb(main):014:0> Marshal.load(Marshal.dump(simple_structure), ->(object) { object.tap { puts object.class }})
TrueClass
String
Symbol
Array
Hash
=> {"name"=>[:abc]}

Copy link
Member Author

Choose a reason for hiding this comment

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

I was able to strip this down to string deserialization.

Marshal.load(Marshal.dump('string'), ->(o) { o.tap { puts o.class } })
String
TrueClass

Copy link
Contributor

Choose a reason for hiding this comment

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

In case it's useful, I think TrueClass is related to the string encoding. That seems to be what the ET at the end of the dump is about:

Marshal.dump('string')
#=> "\x04\bI\"\vstring\x06:\x06ET"

whereas we get false in this case:

Marshal.dump('string'.encode!('ASCII'))
#=> "\x04\bI\"\vstring\x06:\x06EF"

or a string for other encodings:

Marshal.dump('string'.encode!('ISO-8859-1'))
#=> "\x04\bI\"\vstring\x06:\rencoding\"\x0FISO-8859-1"

@simi simi marked this pull request as ready for review December 17, 2022 14:27
Copy link
Member

@indirect indirect left a comment

Choose a reason for hiding this comment

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

This seems good for the dependency API! Can we also use SafeMarshal for loading Marshal.4.8.specs.gz and latest_specs? Could be in another PR if that's easier.

@simi
Copy link
Member Author

simi commented Dec 19, 2022

@indirect those includes Gem::Version, I'll open another PR to handle those as well.

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

Successfully merging this pull request may close these issues.

None yet

4 participants