Skip to content

Commit

Permalink
Parse YAML safely (#157)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Rogers authored and iMacTia committed Jul 28, 2017
1 parent f33339e commit 84be491
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ gem 'rack-cache', '>= 1.1', '< 1.3'
gem 'rake', '>= 0.9', '< 11'
gem 'rash_alt', '>= 0.4.3'
gem 'simple_oauth', '>= 0.1', '< 0.3'
gem 'safe_yaml'

group :test do
gem 'cane', '>= 2.2.2', :platforms => [:mri_19, :mri_20, :mri_21]
Expand Down
24 changes: 14 additions & 10 deletions lib/faraday_middleware/response/parse_yaml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,31 @@
module FaradayMiddleware
# Public: Parse response bodies as YAML.
#
# Warning: this uses `YAML.load()` by default and as such is not safe against
# code injection or DoS attacks. If you're loading resources from an
# untrusted host or over HTTP, you should subclass this middleware and
# redefine it to use `safe_load()` if you're using a Psych version that
# supports it:
# Warning: This is not backwards compatible with versions of this middleware prior to
# faraday_middleware v0.12 - prior to this version, we used YAML.load rather than
# YAMl.safe_load, which exposes serious remote code execution risks - see
# https://github.com/ruby/psych/issues/119 for details. If you're sure you can trust
# YAML you're passing, you can set up an unsafe version of this middleware as follows:
#
# class UnsafelyParseYaml < FaradayMiddleware::ResponseMiddleware
# dependency do
# require 'yaml'
# end
#
# class SafeYaml < FaradayMiddleware::ParseYaml
# define_parser do |body|
# YAML.safe_load(body)
# YAML.load body
# end
# end
#
# Faraday.new(..) do |config|
# config.use SafeYaml
# config.use UnsafelyParseYaml
# ...
# end
class ParseYaml < ResponseMiddleware
dependency 'yaml'
dependency 'safe_yaml/load'

define_parser do |body|
::YAML.load body
SafeYAML.load body
end
end
end
Expand Down

0 comments on commit 84be491

Please sign in to comment.