Module: Datadog::AppSec::Utils::HTTP::Body

Defined in:
lib/datadog/appsec/utils/http/body.rb

Overview

Module for handling HTTP body parsing

Constant Summary collapse

DEFAULT_BYTESIZE_LIMIT =

Matches Rack's default query bytesize limit

4 * 1024 * 1024

Class Method Summary collapse

Class Method Details

.parse(body, media_type:, limit: DEFAULT_BYTESIZE_LIMIT) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/datadog/appsec/utils/http/body.rb', line 17

def self.parse(body, media_type:, limit: DEFAULT_BYTESIZE_LIMIT)
  return if body.nil?

  body.rewind if body.respond_to?(:rewind) # steep:ignore NoMethod
  # @type var content: ::String?
  content = body.respond_to?(:read) ? body.read : body # steep:ignore NoMethod, IncompatibleAssignment
  body.rewind if body.respond_to?(:rewind) # steep:ignore NoMethod

  return if content.nil? || content.empty?

  if media_type.subtype == "json" || media_type.subtype.end_with?("+json")
    JSON.parse(content)
  elsif media_type.subtype == "x-www-form-urlencoded"
    URLEncoded.parse(content, limit: limit)
  end
rescue => e
  AppSec.telemetry.report(e, description: "AppSec: Failed to parse body")

  nil
end