Created
January 31, 2013 20:12
-
-
Save mnaberez/4685994 to your computer and use it in GitHub Desktop.
Rails initializer that patches Psych to only resolve whitelisted classes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Patch Psych to only resolve classes required for RubyGems. Whitelist | |
| # from: https://github.com/rubygems/rubygems.org/pull/516/files#L2R15 | |
| require "yaml" | |
| unless defined?(Psych) && Psych == YAML | |
| abort "Psych is not the YAML parser so unable to patch" | |
| end | |
| module Psych | |
| class ForbiddenClassException < Exception | |
| end | |
| module Visitors | |
| class ToRuby | |
| WHITELISTED_CLASSES = %w( | |
| Gem::Dependency | |
| Gem::Platform | |
| Gem::Requirement | |
| Gem::Specification | |
| Gem::Version | |
| Gem::Version::Requirement | |
| ) | |
| private | |
| alias_method :unsafe_resolve_class, :resolve_class | |
| def resolve_class(name) | |
| if WHITELISTED_CLASSES.include?(name) | |
| unsafe_resolve_class(name) | |
| else | |
| raise ForbiddenClassException, name | |
| end | |
| end | |
| end # ToRuby | |
| end # Visitors | |
| end # Psych |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment