Skip to content

Commit d0c3063

Browse files
committed
Fix typing
1 parent 4b91c61 commit d0c3063

File tree

7 files changed

+59
-29
lines changed

7 files changed

+59
-29
lines changed

lib/datadog/appsec/actions_handler.rb

+13-5
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,31 @@ def generate_stack(action_params)
2424
if Datadog.configuration.appsec.stack_trace.enabled
2525
context = AppSec::Context.active
2626
# We use methods defined in Tracing::Metadata::Tagging,
27-
# which means we can work on both the trace and the service entry span
28-
service_entry_operation = context && (context.trace || context.span)
27+
# which means we can use both the trace and the service entry span
28+
service_entry_operation = (context.trace || context.span) if context
2929

3030
unless service_entry_operation
3131
Datadog.logger.debug { 'Cannot find trace or service entry span to add stack trace' }
3232
return
3333
end
3434

35+
# caller_locations without params always returns an array but steep still thinks it can be nil
36+
# So we add || [] but it will never run the second part anyway (either this or steep:ignore)
3537
stack_frames = caller_locations || []
36-
# caller_locations without params always returns an array but rbs still thinks it can be nil
37-
stack_frames.reject! { |loc| loc.path && loc.path.include?('lib/datadog') }
38+
# Steep that path can still be nil and that include? is not a method of nil
39+
# A way to fix this is to assign loc.path to a variable and use it instead, but it adds an operation
40+
# Which is why we are ignoring this line
41+
stack_frames.reject! do |loc|
42+
loc.path &&
43+
loc.path.include?('lib/datadog') # steep:ignore NoMethod
44+
end
3845

3946
collected_stack_frames = AppSec::StackTrace::Collection.new(stack_frames)
4047

4148
# ASCII-8BIT is encoded as byte array and backend cannot decode it properly so we enforce UTF-8 on stack_id.
4249
# We must copy it as it can be frozen.
43-
utf8_stack_id = action_params['stack_id'] && action_params['stack_id'].encode('UTF-8')
50+
utf8_stack_id = action_params['stack_id'].encode('UTF-8') if action_params['stack_id']
51+
# We should always have a stack_id, but we should still send the stack trace if not (visible with feature flags)
4452
stack_trace = AppSec::StackTrace::Representation.new(utf8_stack_id, collected_stack_frames)
4553

4654
# Add stack trace to trace or service entry span by modifying meta_struct['_dd.stack']

lib/datadog/appsec/stack_trace.rb

+6-2
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,14 @@ def initialize(frames)
3434
text: frame.to_s && frame.to_s.encode('UTF-8'),
3535
# path will show 'main' instead of the actual file
3636
# but absolute_path does not work for e.g. <internal:kernel>
37-
file: (frame.absolute_path || frame.path) && (frame.absolute_path || frame.path).encode('UTF-8'),
37+
# Steep says that label can still be nil and that include? is not a method of nil
38+
# Which is why we are ignoring this line
39+
file: (frame.absolute_path || frame.path) &&
40+
(frame.absolute_path || frame.path).encode('UTF-8'), # steep:ignore
3841
line: frame.lineno,
3942
# label will show 'block in function' instead of just 'function'
40-
function: frame.label && frame.label.encode('UTF-8')
43+
function: frame.label &&
44+
frame.label.encode('UTF-8') # steep:ignore
4145
)
4246
end
4347
@frames = cropped_frames

sig/datadog/appsec/context.rbs

+5-5
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ module Datadog
1313

1414
type input_data = SecurityEngine::Runner::input_data
1515

16-
@trace: Tracing::TraceOperation
16+
@trace: Tracing::TraceOperation?
1717

18-
@span: Tracing::SpanOperation
18+
@span: Tracing::SpanOperation?
1919

2020
@events: ::Array[untyped]
2121

@@ -29,9 +29,9 @@ module Datadog
2929

3030
ActiveContextError: ::StandardError
3131

32-
attr_reader trace: Tracing::TraceOperation
32+
attr_reader trace: Tracing::TraceOperation?
3333

34-
attr_reader span: Tracing::SpanOperation
34+
attr_reader span: Tracing::SpanOperation?
3535

3636
attr_reader events: ::Array[untyped]
3737

@@ -41,7 +41,7 @@ module Datadog
4141

4242
def self.deactivate: () -> void
4343

44-
def self.active: () -> Context
44+
def self.active: () -> Context?
4545

4646
def initialize: (Tracing::TraceOperation trace, Tracing::SpanOperation span, AppSec::Processor security_engine) -> void
4747

sig/datadog/appsec/event.rbs

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ module Datadog
2424

2525
def self.gzip: (untyped value) -> untyped
2626

27-
def self.add_distributed_tags: (Tracing::TraceOperation trace) -> void
27+
def self.add_distributed_tags: (Tracing::TraceOperation? trace) -> void
2828
end
2929
end
3030
end

sig/datadog/appsec/stack_trace.rbs

+27-11
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
module Datadog
22
module AppSec
3-
class StackTrace
4-
attr_reader id: String
5-
attr_reader message: String?
6-
attr_reader frames: Array[Frame]
3+
module StackTrace
4+
class Collection
5+
include Enumerable[Frame]
76

8-
@to_h: Hash[Symbol, untyped]
7+
@frames: Array[Frame]
98

10-
def initialize: (String id, Array[Thread::Backtrace::Location]? stack_trace, ?message: String?) -> void
9+
def initialize: (Array[Thread::Backtrace::Location] frames) -> void
1110

12-
def to_h: () -> Hash[Symbol, untyped]
11+
def each: () { (Frame) -> void } -> void
1312

14-
def self.include_stack_trace?: (Hash[String, untyped] action_params) -> bool
13+
def []: (Integer index) -> Frame
1514

16-
private
17-
18-
def self.from_waf_result: (Hash[String, untyped] action_params) -> Datadog::AppSec::StackTrace
15+
def to_msgpack: (::MessagePack::Packer | nil packer) -> ::MessagePack::Packer
16+
end
1917

2018
class Frame
2119
attr_reader id: Integer
@@ -29,6 +27,24 @@ module Datadog
2927
def initialize: (Integer id, ?text: String?, ?file: String?, ?line: Integer?, ?function: String?) -> void
3028

3129
def to_h: () -> Hash[Symbol, untyped]
30+
31+
def to_msgpack: ((::MessagePack::Packer | nil) packer) -> ::MessagePack::Packer
32+
end
33+
34+
class Representation
35+
attr_reader id: String?
36+
attr_reader stack_trace: Collection
37+
attr_reader message: String?
38+
39+
@to_h: Hash[Symbol, String | Array[Hash[Symbol, untyped]] | nil]
40+
41+
def initialize: (String? id, Collection stack_trace, ?message: String?) -> void
42+
43+
def to_h: () -> Hash[Symbol, String | Array[Hash[Symbol, untyped]] | nil]
44+
45+
def to_msgpack: ((::MessagePack::Packer | nil) packer) -> ::MessagePack::Packer
46+
47+
def pretty_print: (PP q) -> void
3248
end
3349
end
3450
end

sig/datadog/tracing/metadata/tagging.rbs

+6-4
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,19 @@ module Datadog
88
def get_tag: (untyped key) -> untyped
99
def set_tag: (untyped key, ?untyped? value) -> untyped
1010
def set_tags: (untyped tags) -> untyped
11+
def set_meta_struct_tags: (Hash[Symbol, untyped] hash) -> Hash[Symbol, untyped]
1112
def has_tag?: (untyped tag) -> untyped
1213
def clear_tag: (untyped key) -> untyped
1314
alias []= set_tag
1415
alias [] get_tag
1516
def get_metric: (untyped key) -> untyped
1617
def set_metric: (untyped key, untyped value) -> untyped
1718
def clear_metric: (untyped key) -> untyped
18-
def get_struct_tag: (untyped key) -> untyped
19-
def set_struct_tag: (untyped key, ?untyped? value) -> untyped
20-
def has_struct_tag?: (untyped tag) -> untyped
21-
def clear_struct_tag: (untyped key) -> untyped
19+
def get_meta_struct_tag: (untyped key) -> untyped
20+
def set_meta_struct_tag: (untyped key, untyped value) -> untyped
21+
def modify_meta_struct_tag: (untyped key) { (untyped) -> untyped } -> untyped
22+
def has_meta_struct_tag?: (untyped tag) -> untyped
23+
def clear_meta_struct_tag: (untyped key) -> untyped
2224
def meta: () -> untyped
2325
def metrics: () -> untyped
2426
def meta_struct: () -> untyped

sig/datadog/tracing/span.rbs

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ module Datadog
2626
?end_time: Time?,
2727
?id: Integer?,
2828
?meta: Hash[String, String]?,
29-
?meta_struct: Hash[String, untyped]?
29+
?meta_struct: Hash[String, untyped]?,
3030
?metrics: Hash[String, Float]?,
3131
?parent_id: Integer,
3232
?resource: String,

0 commit comments

Comments
 (0)