Skip to content

Add safe_load #119

Closed
Closed

Description

@postmodern

In lieu of the recent Rails YAML RCE vulnerability, Psych should provide a safe_load equivalent method, that only loads Ruby primitives.

Activity

  1. tenderlove commented on Jan 13, 2013

    @tenderlove
    Member

    Can you please be more specific about the desired behavior? Is it acceptable to load Symbols? Are subclasses of Ruby primitives considered "primitive"? If someone sets an instance variable on a string, then dumps / loads that string, should the instance variable survive?

    Can you please demonstrate how to use YAML dump / load to execute arbitrary code? AFAIK it depends on objects other than YAML.

  2. postmodern commented on Jan 14, 2013

    @postmodern
    MemberAuthor
    • Symbols, sure they are primitives. In order to prevent a DoS via Symbols, perhaps there should be a configuration mechanism to specify exactly what types/tags should be allowed.
    • Subclasses of primitives are no longer primitives. In safe mode the custom class could be ignored and the primitive class used instead.
    • Instance variables in primitives should not survive.

    Can you please demonstrate how to use YAML dump / load to execute arbitrary code?

    rails_rce.rb

    AFAIK it depends on objects other than YAML.

    Which is why I believe there should be a safe_load method or a configuration mechanism that restricts YAML to only loading primitives or explicitly allowed Classes.

  3. tenderlove commented on Jan 14, 2013

    @tenderlove
    Member

    @postmodern does the rails_rce.rb script depend on code other than YAML? I'm confused.

  4. tenderlove commented on Jan 14, 2013

    @tenderlove
    Member
    • Symbols, sure they are primitives. In order to prevent a DoS via Symbols, perhaps there should be a configuration > mechanism to specify exactly what types/tags should be allowed.
    • Subclasses of primitives are no longer primitives. In safe mode the custom class could be ignored and the primitive > class used instead.
    • Instance variables in primitives should not survive.

    Given these points, why not just use JSON?

  5. postmodern commented on Jan 14, 2013

    @postmodern
    MemberAuthor

    JSON does not support Symbols, does not support non-String-keyed Hashes and does not have special formatting such as:

    summary: |
      bla bla bla bla bla bla
      bla bla bla bla
    
        indented stuff
    
      bla bla bla bla
    

    Is there any reason YAML should not have a secure mode?

  6. postmodern commented on Jan 14, 2013

    @postmodern
    MemberAuthor

    @tenderlove rails_rce.rb uses !ruby/hash:MyClass and ActionController::Routing::RouteSet::NamedRouteCollection.

    @charliesome's PoC used ActiveSupport::Deprecation::DeprecatedInstanceVariableProxy, ERB, Gem::Requirement and Rack::Session::Cookie::Base64::Marshal.

    These exploits would not work if the YAML parser was configured to only allow primitives (aka safe_load).

  7. haileys commented on Jan 14, 2013

    @haileys

    +1. Only primitives that YAML can deserialize without using ! directives should be allowed.

    No symbols IMO. I can't see a use case where you would want to allow symbols but still restrict other deserialization.

  8. envygeeks commented on Jan 14, 2013

    @envygeeks

    👍 on what @postmodern and @charliesome are saying. I really wish it supported a way for it to skip over everything and raise what isn't in the specs by default, and certainly like what @charliesome said about skipping over symbols too... the symbols part is kind of important to me because it would make it easier to keep a consistent configuration file to API without having to run to_s on all the keys myself before I create an indifferent Hash.

  9. postmodern commented on Jan 14, 2013

    @postmodern
    MemberAuthor

    Instead of having a big case/when statement of all the tags in to_ruby.rb, Psych could have a table of parser methods/procs. This way Psych could be configured to run in "safe mode". When an unknown tag is encountered, it could skip over it (and it's children) or raise an exception.

  10. envygeeks commented on Jan 14, 2013

    @envygeeks

    There is no other option other than raise an exception so it 💣 and somebody knows they did bad.

  11. tenderlove commented on Jan 14, 2013

    @tenderlove
    Member

    @postmodern so YAML didn't execute the code, but Rails did?

    Is there any reason YAML should not have a secure mode?

    I don't particularly care whether it does or not. We just have to be specific about what it means. Also, we need someone to write the patch. :-)

  12. postmodern commented on Jan 14, 2013

    @postmodern
    MemberAuthor

    @tenderlove YAML just happened to call #[]= on an instance of the Class we specified. ActionController::Routing::RouteSet::NamedRouteCollection just so happens to pass the key value from #[]= down to module_eval. We could have used other non-Rails Classes that somehow pass our data into instance_eval or send. CVE-2013-0156 is not really specific to Rails, other applications that accept arbitrary YAML are also vulnerable; such as chef-server which uses extlib.

    Please see the Rails PoC write up for a full walk through.

  13. tenderlove commented on Jan 14, 2013

    @tenderlove
    Member

    Ah, so it's not specific to YAML, but anything that will feed strings to eval. Makes sense.

    Aaron Patterson
    http://tenderlovemaking.com/
    I'm on an iPhone so I apologize for top posting.

    On Jan 13, 2013, at 7:26 PM, Postmodern notifications@github.com wrote:

    @tenderlove YAML just happened to call #[]= on an instance of the Class we specified. ActionController::Routing::RouteSet::NamedRouteCollection just so happens to pass the key value from #[]= down to module_eval. We could have used other non-Rails Classes that somehow pass our data into instance_eval or send. CVE-2013-0156 is not really specific to Rails, other applications that accept arbitrary YAML are also vulnerable; such as chef-server which uses extlib.

    Please refer to the Rails PoC write up for a full walk through.

    �
    Reply to this email directly or view it on GitHub.

  14. benmmurphy commented on Jan 14, 2013

    @benmmurphy

    Marshal.load has this problem as well and so would any serialization mechanism that does the following with arbitrary Foo classes and arbitrary field values.

    obj = Foo.allocate
    obj.instance_variable_set(field, value)
    
  15. envygeeks commented on Jan 14, 2013

    @envygeeks

    @benmmurphy it's in Marshal's nature to be like this by simple definition of what it is, YAML however, it's not.

  16. 103 remaining items

  17. blambeau commented on Feb 12, 2013

    @blambeau

    @Kimcha

    We are discussing convenience vs security and I cannot believe you are in favour of convenience at such a risk.

    I'm affraid, my experience in the ruby world seems to suggest that ruby programmers tend to favor convenience over almost everything, including security. Given what you've already told about your project and concerns, I'm not sure the ruby/rails culture will fit very well.

  18. envygeeks commented on Feb 12, 2013

    @envygeeks

    @blambeau I'll just leave this here:

    YAML
    serialization language
    serialization
    broadly useful for programming needs
    broadly useful
    broadly
    configuration files to Internet messaging
    configuration files

    Just because it says it can be used for the internet doesn't mean it's a good idea and doesn't mean "should".

  19. postmodern commented on Feb 12, 2013

    @postmodern
    MemberAuthor

    @Kimcha changing YAML.load would not just break the application, it would break many dependencies. The change would be comparable to upgrading from 1.8 to 1.9.

    We are discussing convenience vs security and I cannot believe you are in favour of convenience at such a risk.

    I do not agree that this is a binary choice between convenience and security, that we must be forced into choosing one or the other. It is simply the matter of the right tool for the right job:

    • Is the YAML input trusted: YAML.load
    • Is the YAML input from the user: YAML.load_safe

    Just because nobody has reported to the rails security team that a part is vulnerable, doesnt mean it is not.

    This is where one should take extreme measures, such as auditing their code-base for any YAML.load calls. On a more philosophical note, we can never know every vulnerability before they are reported. We can only respond to vulnerabilities as they are reported.

    It is an never ending game to try to fix all the attacking vectors when you can simply fix it at the root of the issue in default behaviour.

    You assume that every developer (and user) would immediately upgrade to the latest version of Psych containing safe_load. :) Although, there is a bit of truth to this. Even if we were to audit every rubygem and every Rails app, a clueless (or sleep deprived) developer could still use YAML.load unsafely. The Ruby programming language does not prevent the developer from making bad design decisions. Even if we changed YAML.load to behave like YAML.safe_load, there is still eval, instance_eval, module_eval, send, system, CSV.load, JSON.load. Likewise, SQL injection and XSS are still possible in the Java and Haskell programming languages. A competent developer must be aware of the potential dangers of their languages/frameworks and avoid them. I feel that Secure Coding Guidelines are a part of the solution here.

  20. blambeau commented on Feb 12, 2013

    @blambeau

    @Kimcha something I haven't told you about the ruby world: confusion. A nice example from @postmodern here:

    It is simply the matter of the right tool for the right job:

    • Is the YAML input trusted: YAML.load
    • Is the YAML input from the user: YAML.load_safe

    Very strong confusion between functional requirements (loading data that might or might not contain only scalars) and non-functional requirements (keeping the operation safe).

    There is no such right tool for the right job, especially when your job tells you to load non-scalar data in a safe way. Anyway...

  21. mpapis commented on Feb 16, 2013

    @mpapis

    continuing #119 (comment) - I assume that every developer does not write to_s_safe, you just assume that default is the safe the same default in the feature should be safe, even if it takes 3 years to make it happen - we should make it happen.

    Would it be hard to announce an deprecation today, add a warning in a year, allow flag in two years and switch the new default in 3 years - is it enough time to make things safe?

  22. crazymykl commented on Feb 20, 2013

    @crazymykl

    I think YAML.load should remain as it is, .load implies that we are trusting the input, just as in Marshal.load or CSV.load. Perhaps, by analogy, the primitives-only version should be YAML.parse?

  23. envygeeks commented on Feb 20, 2013

    @envygeeks

    If you want to fall inline with JSON then sure, which I wouldn't mind since it means consistency.

  24. trevor commented on Jun 2, 2013

    @trevor

    since it hasn't been mentioned, here's documentation that contains examples of python's / pyyaml's take on safe_load: http://pyyaml.org/wiki/PyYAMLDocumentation

  25. tenderlove commented on Jul 5, 2013

    @tenderlove
    Member

    safe_load is added and released in 2.0.0, so I'm closing this.

  26. envygeeks commented on Jul 5, 2013

    @envygeeks

    Thank you!

  27. postmodern commented on Jul 5, 2013

    @postmodern
    MemberAuthor

    w00t finally!

  28. added a commit that references this issue on Jul 3, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions