faraday_middleware_safeyaml 0.12.pre.safeyaml

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,39 @@
1
+ require "time"
2
+ require "faraday"
3
+
4
+ module FaradayMiddleware
5
+ # Parse dates from response body
6
+ class ParseDates < ::Faraday::Response::Middleware
7
+ ISO_DATE_FORMAT = /\A\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?(Z|((\+|-)\d{2}:?\d{2}))\Z/m
8
+
9
+ def initialize(app, options = {})
10
+ @regexp = options[:match] || ISO_DATE_FORMAT
11
+ super(app)
12
+ end
13
+
14
+ def call(env)
15
+ response = @app.call(env)
16
+ parse_dates! response.env[:body]
17
+ response
18
+ end
19
+
20
+ private
21
+
22
+ def parse_dates!(value)
23
+ case value
24
+ when Hash
25
+ value.each do |key, element|
26
+ value[key] = parse_dates!(element)
27
+ end
28
+ when Array
29
+ value.each_with_index do |element, index|
30
+ value[index] = parse_dates!(element)
31
+ end
32
+ when @regexp
33
+ Time.parse(value)
34
+ else
35
+ value
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,50 @@
1
+ require 'faraday_middleware/response_middleware'
2
+
3
+ module FaradayMiddleware
4
+ # Public: Parse response bodies as JSON.
5
+ class ParseJson < ResponseMiddleware
6
+ dependency do
7
+ require 'json' unless defined?(::JSON)
8
+ end
9
+
10
+ define_parser do |body|
11
+ ::JSON.parse body unless body.strip.empty?
12
+ end
13
+
14
+ # Public: Override the content-type of the response with "application/json"
15
+ # if the response body looks like it might be JSON, i.e. starts with an
16
+ # open bracket.
17
+ #
18
+ # This is to fix responses from certain API providers that insist on serving
19
+ # JSON with wrong MIME-types such as "text/javascript".
20
+ class MimeTypeFix < ResponseMiddleware
21
+ MIME_TYPE = 'application/json'.freeze
22
+
23
+ def process_response(env)
24
+ old_type = env[:response_headers][CONTENT_TYPE].to_s
25
+ new_type = MIME_TYPE.dup
26
+ new_type << ';' << old_type.split(';', 2).last if old_type.index(';')
27
+ env[:response_headers][CONTENT_TYPE] = new_type
28
+ end
29
+
30
+ BRACKETS = %w- [ { -
31
+ WHITESPACE = [ " ", "\n", "\r", "\t" ]
32
+
33
+ def parse_response?(env)
34
+ super and BRACKETS.include? first_char(env[:body])
35
+ end
36
+
37
+ def first_char(body)
38
+ idx = -1
39
+ begin
40
+ char = body[idx += 1]
41
+ char = char.chr if char
42
+ end while char and WHITESPACE.include? char
43
+ char
44
+ end
45
+ end
46
+ end
47
+ end
48
+
49
+ # deprecated alias
50
+ Faraday::Response::ParseJson = FaradayMiddleware::ParseJson
@@ -0,0 +1,13 @@
1
+ require 'faraday_middleware/response_middleware'
2
+
3
+ module FaradayMiddleware
4
+ # Public: Restore marshalled Ruby objects in response bodies.
5
+ class ParseMarshal < ResponseMiddleware
6
+ define_parser do |body|
7
+ ::Marshal.load body unless body.empty?
8
+ end
9
+ end
10
+ end
11
+
12
+ # deprecated alias
13
+ Faraday::Response::ParseMarshal = FaradayMiddleware::ParseMarshal
@@ -0,0 +1,15 @@
1
+ require 'faraday_middleware/response_middleware'
2
+
3
+ module FaradayMiddleware
4
+ # Public: parses response bodies with MultiXml.
5
+ class ParseXml < ResponseMiddleware
6
+ dependency 'multi_xml'
7
+
8
+ define_parser do |body|
9
+ ::MultiXml.parse(body)
10
+ end
11
+ end
12
+ end
13
+
14
+ # deprecated alias
15
+ Faraday::Response::ParseXml = FaradayMiddleware::ParseXml
@@ -0,0 +1,38 @@
1
+ require 'faraday_middleware/response_middleware'
2
+
3
+ module FaradayMiddleware
4
+ # Public: Parse response bodies as YAML.
5
+ #
6
+ # Warning: This is not backwards compatible with versions of this middleware prior to
7
+ # faraday_middleware v0.12 - prior to this version, we used YAML.load rather than
8
+ # YAMl.safe_load, which exposes serious remote code execution risks - see
9
+ # https://github.com/ruby/psych/issues/119 for details. If you're sure you can trust
10
+ # YAML you're passing, you can set up an unsafe version of this middleware as follows:
11
+ #
12
+ # class UnsafelyParseYaml < FaradayMiddleware::ResponseMiddleware
13
+ # dependency do
14
+ # require 'yaml'
15
+ # end
16
+ #
17
+ # define_parser do |body|
18
+ # YAML.load body
19
+ # end
20
+ # end
21
+ #
22
+ # Faraday.new(..) do |config|
23
+ # config.use UnsafelyParseYaml
24
+ # ...
25
+ # end
26
+ class ParseYaml < ResponseMiddleware
27
+ dependency do
28
+ require 'safe_yaml/load'
29
+ end
30
+
31
+ define_parser do |body|
32
+ SafeYAML.load body
33
+ end
34
+ end
35
+ end
36
+
37
+ # deprecated alias
38
+ Faraday::Response::ParseYaml = FaradayMiddleware::ParseYaml
@@ -0,0 +1,15 @@
1
+ require 'faraday_middleware/response/mashify'
2
+
3
+ module FaradayMiddleware
4
+ # Public: Converts parsed response bodies to a Hashie::Rash if they were of
5
+ # Hash or Array type.
6
+ class Rashify < Mashify
7
+ dependency do
8
+ require 'rash'
9
+ self.mash_class = ::Hashie::Rash
10
+ end
11
+ end
12
+ end
13
+
14
+ # deprecated alias
15
+ Faraday::Response::Rashify = FaradayMiddleware::Rashify
@@ -0,0 +1,112 @@
1
+ require 'faraday'
2
+
3
+ module FaradayMiddleware
4
+ # Internal: The base class for middleware that parses responses.
5
+ class ResponseMiddleware < Faraday::Middleware
6
+ CONTENT_TYPE = 'Content-Type'.freeze
7
+
8
+ class << self
9
+ attr_accessor :parser
10
+ end
11
+
12
+ # Store a Proc that receives the body and returns the parsed result.
13
+ def self.define_parser(parser = nil)
14
+ @parser = parser || Proc.new
15
+ end
16
+
17
+ def self.inherited(subclass)
18
+ super
19
+ subclass.load_error = self.load_error if subclass.respond_to? :load_error=
20
+ subclass.parser = self.parser
21
+ end
22
+
23
+ def initialize(app = nil, options = {})
24
+ super(app)
25
+ @options = options
26
+ @content_types = Array(options[:content_type])
27
+ end
28
+
29
+ def call(environment)
30
+ @app.call(environment).on_complete do |env|
31
+ if process_response_type?(response_type(env)) and parse_response?(env)
32
+ process_response(env)
33
+ end
34
+ end
35
+ end
36
+
37
+ def process_response(env)
38
+ env[:raw_body] = env[:body] if preserve_raw?(env)
39
+ env[:body] = parse(env[:body])
40
+ rescue Faraday::Error::ParsingError => err
41
+ raise Faraday::Error::ParsingError.new(err, env[:response])
42
+ end
43
+
44
+ # Parse the response body.
45
+ #
46
+ # Instead of overriding this method, consider using `define_parser`.
47
+ def parse(body)
48
+ if self.class.parser
49
+ begin
50
+ self.class.parser.call(body)
51
+ rescue StandardError, SyntaxError => err
52
+ raise err if err.is_a? SyntaxError and err.class.name != 'Psych::SyntaxError'
53
+ raise Faraday::Error::ParsingError, err
54
+ end
55
+ else
56
+ body
57
+ end
58
+ end
59
+
60
+ def response_type(env)
61
+ type = env[:response_headers][CONTENT_TYPE].to_s
62
+ type = type.split(';', 2).first if type.index(';')
63
+ type
64
+ end
65
+
66
+ def process_response_type?(type)
67
+ @content_types.empty? or @content_types.any? { |pattern|
68
+ pattern.is_a?(Regexp) ? type =~ pattern : type == pattern
69
+ }
70
+ end
71
+
72
+ def parse_response?(env)
73
+ env[:body].respond_to? :to_str
74
+ end
75
+
76
+ def preserve_raw?(env)
77
+ env[:request].fetch(:preserve_raw, @options[:preserve_raw])
78
+ end
79
+ end
80
+
81
+ # DRAGONS
82
+ module OptionsExtension
83
+ attr_accessor :preserve_raw
84
+
85
+ def to_hash
86
+ super.update(:preserve_raw => preserve_raw)
87
+ end
88
+
89
+ def each
90
+ return to_enum(:each) unless block_given?
91
+ super
92
+ yield :preserve_raw, preserve_raw
93
+ end
94
+
95
+ def fetch(key, *args)
96
+ if :preserve_raw == key
97
+ value = __send__(key)
98
+ value.nil? ? args.fetch(0) : value
99
+ else
100
+ super
101
+ end
102
+ end
103
+ end
104
+
105
+ if defined?(Faraday::RequestOptions)
106
+ begin
107
+ Faraday::RequestOptions.from(:preserve_raw => true)
108
+ rescue NoMethodError
109
+ Faraday::RequestOptions.send(:include, OptionsExtension)
110
+ end
111
+ end
112
+ end
@@ -0,0 +1,3 @@
1
+ module FaradayMiddleware
2
+ VERSION = "0.12-safeyaml" unless defined?(FaradayMiddleware::VERSION)
3
+ end
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: faraday_middleware_safeyaml
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.12.pre.safeyaml
5
+ platform: ruby
6
+ authors:
7
+ - Erik Michaels-Ober
8
+ - Wynn Netherland
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2017-07-03 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: faraday
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: 0.7.4
21
+ - - "<"
22
+ - !ruby/object:Gem::Version
23
+ version: '1.0'
24
+ type: :runtime
25
+ prerelease: false
26
+ version_requirements: !ruby/object:Gem::Requirement
27
+ requirements:
28
+ - - ">="
29
+ - !ruby/object:Gem::Version
30
+ version: 0.7.4
31
+ - - "<"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.0'
34
+ - !ruby/object:Gem::Dependency
35
+ name: safe_yaml
36
+ requirement: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ type: :runtime
42
+ prerelease: false
43
+ version_requirements: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ description:
49
+ email:
50
+ - sferik@gmail.com
51
+ - wynn.netherland@gmail.com
52
+ executables: []
53
+ extensions: []
54
+ extra_rdoc_files: []
55
+ files:
56
+ - LICENSE.md
57
+ - README.md
58
+ - lib/faraday_middleware.rb
59
+ - lib/faraday_middleware/addressable_patch.rb
60
+ - lib/faraday_middleware/backwards_compatibility.rb
61
+ - lib/faraday_middleware/gzip.rb
62
+ - lib/faraday_middleware/instrumentation.rb
63
+ - lib/faraday_middleware/rack_compatible.rb
64
+ - lib/faraday_middleware/request/encode_json.rb
65
+ - lib/faraday_middleware/request/method_override.rb
66
+ - lib/faraday_middleware/request/oauth.rb
67
+ - lib/faraday_middleware/request/oauth2.rb
68
+ - lib/faraday_middleware/response/caching.rb
69
+ - lib/faraday_middleware/response/chunked.rb
70
+ - lib/faraday_middleware/response/follow_redirects.rb
71
+ - lib/faraday_middleware/response/mashify.rb
72
+ - lib/faraday_middleware/response/parse_dates.rb
73
+ - lib/faraday_middleware/response/parse_json.rb
74
+ - lib/faraday_middleware/response/parse_marshal.rb
75
+ - lib/faraday_middleware/response/parse_xml.rb
76
+ - lib/faraday_middleware/response/parse_yaml.rb
77
+ - lib/faraday_middleware/response/rashify.rb
78
+ - lib/faraday_middleware/response_middleware.rb
79
+ - lib/faraday_middleware/version.rb
80
+ homepage: https://github.com/lostisland/faraday_middleware
81
+ licenses:
82
+ - MIT
83
+ metadata: {}
84
+ post_install_message:
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">"
96
+ - !ruby/object:Gem::Version
97
+ version: 1.3.1
98
+ requirements: []
99
+ rubyforge_project:
100
+ rubygems_version: 2.5.2
101
+ signing_key:
102
+ specification_version: 4
103
+ summary: Various middleware for Faraday - forked to fix security issue (Waiting on
104
+ https://github.com/lostisland/faraday_middleware/pull/157)
105
+ test_files: []