File: parse_yaml.rb

package info (click to toggle)
ruby-faraday-middleware 1.2.0-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 376 kB
  • sloc: ruby: 2,388; sh: 15; makefile: 6
file content (39 lines) | stat: -rw-r--r-- 1,183 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# frozen_string_literal: true

require 'faraday_middleware/response_middleware'

module FaradayMiddleware
  # Public: Parse response bodies as YAML.
  #
  # 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 like this:
  #
  #     class UnsafelyParseYaml < FaradayMiddleware::ResponseMiddleware
  #       dependency do
  #         require 'yaml'
  #       end
  #
  #       define_parser do |body|
  #         YAML.load body
  #       end
  #     end
  #
  #     Faraday.new(..) do |config|
  #       config.use UnsafelyParseYaml
  #       ...
  #     end
  class ParseYaml < ResponseMiddleware
    dependency 'yaml'

    define_parser do |body, parser_options|
      YAML.safe_load(body, **(parser_options || {})) || false
    end
  end
end

# deprecated alias
Faraday::Response::ParseYaml = FaradayMiddleware::ParseYaml