Skip to content

Commit

Permalink
Add StackTrace class methods from_waf_result and include_stack_trace?
Browse files Browse the repository at this point in the history
  • Loading branch information
vpellan committed Jan 14, 2025
1 parent e8cb243 commit 0e5e082
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
36 changes: 28 additions & 8 deletions lib/datadog/appsec/stack_trace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,18 @@ class StackTrace
def initialize(id, stack_trace, message: nil)
@id = id
@message = message
@frames = stack_trace.each_with_index.map do |frame, index|
Frame.new(
index,
text: frame.to_s,
file: frame.absolute_path, # path will show 'main' instead of the actual file
line: frame.lineno,
function: frame.base_label # label will show 'block in function' instead of just 'function'
)
if stack_trace
@frames = stack_trace.each_with_index.map do |frame, index|
Frame.new(
index,
text: frame.to_s,
file: frame.absolute_path, # path will show 'main' instead of the actual file
line: frame.lineno,
function: frame.base_label # label will show 'block in function' instead of just 'function'
)
end
else
@frames = []
end
end

Expand All @@ -29,6 +33,22 @@ def to_h
}
end

class << self
# Create a stack trace from the result of WAF execution
def from_waf_result(waf_result)
stack_frames = caller_locations || []
# caller_locations without params always returns an array but rbs still thinks it can be nil
stack_frames.reject! { |loc| loc.path && loc.path.include?('lib/datadog') }
stack_trace_id = waf_result.actions['generate_stack']['stack_id']
stack_trace = AppSec::StackTrace.new(stack_trace_id, stack_frames)
end

# Whether to include the stack trace in the event
def include_stack_trace?(waf_result)
Datadog.configuration.appsec.stack_trace.enabled && waf_result.actions.key?('generate_stack')
end
end

# Stack frame
class Frame
attr_reader :id, :text, :file, :line, :function
Expand Down
10 changes: 9 additions & 1 deletion sig/datadog/appsec/stack_trace.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,15 @@ module Datadog

@to_h: ::Hash[::Symbol, untyped]

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

def to_h: () -> ::Hash[::Symbol, untyped]

private

def self.from_waf_result: (Datadog::AppSec::WAF::Result waf_result) -> Datadog::AppSec::StackTrace

def self.include_stack_trace?: (Datadog::AppSec::WAF::Result waf_result) -> bool

class Frame
attr_reader id: ::Integer
Expand Down

0 comments on commit 0e5e082

Please sign in to comment.