Skip to content

Commit 7f6cab6

Browse files
committed
Correct typing for ephemeral data
1 parent 934fc35 commit 7f6cab6

File tree

9 files changed

+29
-11
lines changed

9 files changed

+29
-11
lines changed

lib/datadog/appsec/contrib/excon/ssrf_detection_middleware.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ def request_call(data)
2323
mark_body_sampling!(data, context: context)
2424

2525
headers = normalize_headers(data[:headers])
26+
# @type var ephemeral_data: ::Datadog::AppSec::Context::input_data
2627
ephemeral_data = {
2728
'server.io.net.url' => request_url(data),
2829
'server.io.net.request.method' => data[:method].to_s.upcase,
@@ -45,6 +46,7 @@ def response_call(data)
4546
return super unless context && AppSec.rasp_enabled?
4647

4748
headers = normalize_headers(data.dig(:response, :headers))
49+
# @type var ephemeral_data: ::Datadog::AppSec::Context::input_data
4850
ephemeral_data = {
4951
'server.io.net.response.status' => data.dig(:response, :status).to_s,
5052
'server.io.net.response.headers' => headers

lib/datadog/appsec/contrib/faraday/ssrf_detection_middleware.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ def call(env)
2121
mark_body_sampling!(env, context: context)
2222

2323
headers = normalize_headers(env.request_headers)
24+
# @type var ephemeral_data: ::Datadog::AppSec::Context::input_data
2425
ephemeral_data = {
2526
'server.io.net.url' => env.url.to_s,
2627
'server.io.net.request.method' => env.method.to_s.upcase,
@@ -42,6 +43,7 @@ def call(env)
4243

4344
def on_complete(env, context:)
4445
headers = normalize_headers(env.response_headers)
46+
# @type var ephemeral_data: ::Datadog::AppSec::Context::input_data
4547
ephemeral_data = {
4648
'server.io.net.response.status' => env.status.to_s,
4749
'server.io.net.response.headers' => headers

lib/datadog/appsec/contrib/rest_client/request_ssrf_detection_patch.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ def execute(&block)
1717
return super unless context && AppSec.rasp_enabled?
1818

1919
headers = normalize_request_headers
20+
# @type var ephemeral_data: ::Datadog::AppSec::Context::input_data
2021
ephemeral_data = {
2122
'server.io.net.url' => url,
2223
'server.io.net.request.method' => method.to_s.upcase,
@@ -35,6 +36,7 @@ def execute(&block)
3536
response = super
3637

3738
headers = normalize_response_headers(response)
39+
# @type var ephemeral_data: ::Datadog::AppSec::Context::input_data
3840
ephemeral_data = {
3941
'server.io.net.response.status' => response.code.to_s,
4042
'server.io.net.response.headers' => headers

sig/datadog/appsec/contrib/excon/ssrf_detection_middleware.rbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ module Datadog
1313

1414
def mark_body_sampling!: (::Excon::Middleware::Base::datum data, context: Context) -> void
1515

16-
def parse_body: (Utils::HTTP::Body::body body, content_type: ::String?) -> untyped
16+
def parse_body: (Utils::HTTP::Body::body body, content_type: ::String?) -> ::Helpers::json?
1717

1818
def request_url: (::Excon::Middleware::Base::datum data) -> ::String
1919

sig/datadog/appsec/contrib/faraday/ssrf_detection_middleware.rbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ module Datadog
1313

1414
def mark_body_sampling!: (::Faraday::Env env, context: Context) -> void
1515

16-
def parse_body: (Utils::HTTP::Body::body body, content_type: ::String?) -> untyped
16+
def parse_body: (Utils::HTTP::Body::body body, content_type: ::String?) -> ::Helpers::json?
1717

1818
def normalize_headers: (::Faraday::Utils::Headers? headers) -> ::Hash[::String, ::String]
1919

sig/datadog/appsec/contrib/rest_client/request_ssrf_detection_patch.rbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ module Datadog
1111

1212
def mark_body_sampling!: (Context context) -> bool
1313

14-
def parse_body: (::String body, content_type: ::String?) -> untyped
14+
def parse_body: (::String body, content_type: ::String?) -> ::Helpers::json?
1515

1616
def normalize_request_headers: () -> headers
1717

sig/datadog/appsec/security_engine/runner.rbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module Datadog
22
module AppSec
33
module SecurityEngine
44
class Runner
5-
type input_data = ::Hash[::String, untyped]
5+
type input_data = ::Hash[::String, any]
66

77
@mutex: ::Mutex
88

sig/datadog/appsec/utils/http/body.rbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module Datadog
55
module Body
66
type body = ::String | ::StringIO | ::IO | nil
77

8-
def self.parse: (body, media_type: MediaType) -> any?
8+
def self.parse: (body, media_type: MediaType) -> ::Helpers::json?
99
end
1010
end
1111
end

spec/datadog/appsec/utils/http/body_spec.rb

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,27 @@
1010
RSpec.describe Datadog::AppSec::Utils::HTTP::Body do
1111
describe '.parse' do
1212
context 'when body is nil' do
13-
let(:media_type) { Datadog::AppSec::Utils::HTTP::MediaType.new('application/json') }
13+
let(:media_type) do
14+
Datadog::AppSec::Utils::HTTP::MediaType.new(type: 'application', subtype: 'json')
15+
end
1416
let(:result) { described_class.parse(nil, media_type: media_type) }
1517

1618
it { expect(result).to be_nil }
1719
end
1820

1921
context 'when body is empty' do
20-
let(:media_type) { Datadog::AppSec::Utils::HTTP::MediaType.new('application/x-www-form-urlencoded') }
22+
let(:media_type) do
23+
Datadog::AppSec::Utils::HTTP::MediaType.new(type: 'application', subtype: 'x-www-form-urlencoded')
24+
end
2125
let(:result) { described_class.parse('', media_type: media_type) }
2226

2327
it { expect(result).to be_nil }
2428
end
2529

2630
context 'when media type is application/json' do
27-
let(:media_type) { Datadog::AppSec::Utils::HTTP::MediaType.new('application/json') }
31+
let(:media_type) do
32+
Datadog::AppSec::Utils::HTTP::MediaType.new(type: 'application', subtype: 'json')
33+
end
2834

2935
context 'when body is a String' do
3036
let(:result) { described_class.parse('{"key":"value"}', media_type: media_type) }
@@ -66,14 +72,18 @@
6672
end
6773

6874
context 'when media type is application/vnd.api+json' do
69-
let(:media_type) { Datadog::AppSec::Utils::HTTP::MediaType.new('application/vnd.api+json') }
75+
let(:media_type) do
76+
Datadog::AppSec::Utils::HTTP::MediaType.new(type: 'application', subtype: 'vnd.api+json')
77+
end
7078
let(:result) { described_class.parse('{"data":"value"}', media_type: media_type) }
7179

7280
it { expect(result).to eq({'data' => 'value'}) }
7381
end
7482

7583
context 'when media type is application/x-www-form-urlencoded' do
76-
let(:media_type) { Datadog::AppSec::Utils::HTTP::MediaType.new('application/x-www-form-urlencoded') }
84+
let(:media_type) do
85+
Datadog::AppSec::Utils::HTTP::MediaType.new(type: 'application', subtype: 'x-www-form-urlencoded')
86+
end
7787

7888
context 'when body is a String' do
7989
let(:result) { described_class.parse('key=value&foo=bar', media_type: media_type) }
@@ -89,7 +99,9 @@
8999
end
90100

91101
context 'when media type is unsupported' do
92-
let(:media_type) { Datadog::AppSec::Utils::HTTP::MediaType.new('text/plain') }
102+
let(:media_type) do
103+
Datadog::AppSec::Utils::HTTP::MediaType.new(type: 'text', subtype: 'plain')
104+
end
93105
let(:result) { described_class.parse('some text', media_type: media_type) }
94106

95107
it { expect(result).to be_nil }

0 commit comments

Comments
 (0)