Skip to content

Commit

Permalink
Add AppSec::ActionsHandler#interrupt_execution
Browse files Browse the repository at this point in the history
This method should replace both ActionsHandler#block_request and
ActionsHandler#redirect_request
  • Loading branch information
y9v committed Jan 20, 2025
1 parent 5b39484 commit 558f96f
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 18 deletions.
10 changes: 3 additions & 7 deletions lib/datadog/appsec/actions_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,11 @@ def handle(actions_hash)
# stack and schema generation should be done before we throw an interrupt signal
generate_stack(actions_hash['generate_stack']) if actions_hash.key?('generate_stack')
generate_schema(actions_hash['generate_schema']) if actions_hash.key?('generate_schema')
redirect_request(actions_hash['redirect_request']) if actions_hash.key?('redirect_request')
block_request(actions_hash['block_request']) if actions_hash.key?('block_request')
interrupt_execution(actions_hash['redirect_request']) if actions_hash.key?('redirect_request')
interrupt_execution(actions_hash['block_request']) if actions_hash.key?('block_request')
end

def block_request(action_params)
throw(Datadog::AppSec::Ext::INTERRUPT, action_params)
end

def redirect_request(action_params)
def interrupt_execution(action_params)
throw(Datadog::AppSec::Ext::INTERRUPT, action_params)
end

Expand Down
6 changes: 1 addition & 5 deletions sig/datadog/appsec/actions_handler.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,11 @@ module Datadog
module ActionsHandler
def handle: (::Hash[::String, ::Hash[::String, ::String]] actions_hash) -> void

def block_request: (::Hash[::String, ::String] action_params) -> void

def redirect_request: (::Hash[::String, ::String] action_params) -> void
def interrupt_execution: (::Hash[::String, ::String] action_params) -> void

def generate_stack: (::Hash[::String, ::String] action_params) -> void

def generate_schema: (::Hash[::String, ::String] action_params) -> void

def monitor: (::Hash[::String, ::String] action_params) -> void
end
end
end
12 changes: 6 additions & 6 deletions spec/datadog/appsec/actions_handler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
end

it 'calls redirect_request with action parameters' do
expect(described_class).to receive(:redirect_request)
expect(described_class).to receive(:interrupt_execution)
.with(redirect_request_action['redirect_request']).and_call_original

catch(Datadog::AppSec::Ext::INTERRUPT) do
Expand All @@ -44,7 +44,7 @@
end

it 'calls block_request with action parameters' do
expect(described_class).to receive(:block_request)
expect(described_class).to receive(:interrupt_execution)
.with(block_request_action['block_request']).and_call_original

catch(Datadog::AppSec::Ext::INTERRUPT) do
Expand All @@ -53,10 +53,10 @@
end

it 'calls redirect_request only when both block_request and redirect_request are present' do
expect(described_class).to receive(:redirect_request)
expect(described_class).to receive(:interrupt_execution)
.with(redirect_request_action['redirect_request']).and_call_original

expect(described_class).not_to receive(:block_request)
expect(described_class).not_to receive(:interrupt_execution)

catch(Datadog::AppSec::Ext::INTERRUPT) do
described_class.handle(block_request_action.merge(redirect_request_action))
Expand All @@ -77,7 +77,7 @@
expect(described_class).to receive(:generate_stack)
.with(generate_stack_action['generate_stack']).and_call_original

expect(described_class).to receive(:block_request)
expect(described_class).to receive(:interrupt_execution)
.with(block_request_action['block_request']).and_call_original

catch(Datadog::AppSec::Ext::INTERRUPT) do
Expand All @@ -89,7 +89,7 @@
expect(described_class).to receive(:generate_stack)
.with(generate_stack_action['generate_stack']).and_call_original

expect(described_class).to receive(:redirect_request)
expect(described_class).to receive(:interrupt_execution)
.with(redirect_request_action['redirect_request']).and_call_original

catch(Datadog::AppSec::Ext::INTERRUPT) do
Expand Down

0 comments on commit 558f96f

Please sign in to comment.