Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions Steepfile
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,6 @@ target :datadog do
ignore 'lib/datadog/appsec/contrib/rails/patches/render_to_body_patch.rb'
ignore 'lib/datadog/appsec/contrib/rails/request.rb'
ignore 'lib/datadog/appsec/contrib/rails/request_middleware.rb'
ignore 'lib/datadog/appsec/contrib/sinatra/framework.rb'
ignore 'lib/datadog/appsec/contrib/sinatra/gateway/request.rb'
ignore 'lib/datadog/appsec/contrib/sinatra/gateway/route_params.rb'
ignore 'lib/datadog/appsec/contrib/sinatra/gateway/watcher.rb'
ignore 'lib/datadog/appsec/contrib/sinatra/integration.rb'
ignore 'lib/datadog/appsec/contrib/sinatra/patcher.rb'
ignore 'lib/datadog/appsec/contrib/sinatra/patches/json_patch.rb'
ignore 'lib/datadog/appsec/contrib/sinatra/request_middleware.rb'
ignore 'lib/datadog/appsec/monitor/gateway/watcher.rb'
ignore 'lib/datadog/core/buffer/thread_safe.rb'
ignore 'lib/datadog/core/configuration.rb'
Expand Down
9 changes: 5 additions & 4 deletions lib/datadog/appsec/contrib/sinatra/gateway/watcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def watch

def watch_request_dispatch(gateway = Instrumentation.gateway)
gateway.watch('sinatra.request.dispatch', :appsec) do |stack, gateway_request|
context = gateway_request.env[AppSec::Ext::CONTEXT_KEY]
context = gateway_request.env[AppSec::Ext::CONTEXT_KEY] # : Context
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry if I'm out of the loop: is this the "new" syntax for type overrides? # : ...


persistent_data = {
'server.request.body' => gateway_request.form_hash
Expand All @@ -49,8 +49,9 @@ def watch_request_dispatch(gateway = Instrumentation.gateway)
end

def watch_request_routed(gateway = Instrumentation.gateway)
gateway.watch('sinatra.request.routed', :appsec) do |stack, (gateway_request, gateway_route_params)|
context = gateway_request.env[AppSec::Ext::CONTEXT_KEY]
gateway.watch('sinatra.request.routed', :appsec) do |stack, args|
gateway_request, gateway_route_params = args # : [Gateway::Request, Gateway::RouteParams]
Comment on lines +52 to +53
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's annoying that you have to do this, just to add a type annotation. 😢

context = gateway_request.env[AppSec::Ext::CONTEXT_KEY] # : Context

persistent_data = {
'server.request.path_params' => gateway_route_params.params
Expand All @@ -75,7 +76,7 @@ def watch_request_routed(gateway = Instrumentation.gateway)

def watch_response_body_json(gateway = Instrumentation.gateway)
gateway.watch('sinatra.response.body.json', :appsec) do |stack, container|
context = container.context
context = container.context # : Context

persistent_data = {
'server.response.body' => container.data
Expand Down
2 changes: 1 addition & 1 deletion lib/datadog/appsec/contrib/sinatra/integration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def self.loaded?
end

def self.compatible?
super && version >= MINIMUM_VERSION
!!(super && (version&.>= MINIMUM_VERSION))
end

def self.auto_instrument?
Expand Down
4 changes: 2 additions & 2 deletions lib/datadog/appsec/contrib/sinatra/patcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def dispatch!
# path params are returned by pattern.params in process_route, then
# merged with normal params, so we get both
module RoutePatch
def process_route(*)
def process_route(*args)
env = @request.env

context = env[Datadog::AppSec::Ext::CONTEXT_KEY]
Expand Down Expand Up @@ -123,7 +123,7 @@ def patch
end

def patch_json?
defined?(::Sinatra::JSON) && ::Sinatra::Base < ::Sinatra::JSON
!!(defined?(::Sinatra::JSON) && ::Sinatra::Base < ::Sinatra::JSON)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/datadog/appsec/contrib/sinatra/patches/json_patch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module Patches
# body right before it is serialized.
module JsonPatch
def json(object, options = {})
context = @request.env[Datadog::AppSec::Ext::CONTEXT_KEY]
context = @request.env[Datadog::AppSec::Ext::CONTEXT_KEY] # : Context?
return super unless context

data = Utils::HashCoercion.coerce(object)
Expand Down
30 changes: 16 additions & 14 deletions sig/datadog/appsec/contrib/rack/gateway/request.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,35 @@ module Datadog
module Rack
module Gateway
class Request < Instrumentation::Gateway::Argument
attr_reader env: untyped
@env: ::Hash[::String, top]

def initialize: (untyped env) -> void
attr_reader env: ::Hash[::String, top]

def request: () -> untyped
def initialize: (::Hash[::String, top] env) -> void

def query: () -> untyped
def request: () -> ::Rack::Request

def headers: () -> untyped
def query: () -> ::Hash[::String, top]

def body: () -> untyped
def headers: () -> ::Hash[::String, ::String]

def method: () -> untyped
def body: () -> ::String

def url: () -> untyped
def method: () -> ::String

def cookies: () -> untyped
def url: () -> ::String

def host: () -> untyped
def cookies: () -> ::Hash[::String, ::String]

def user_agent: () -> untyped
def host: () -> ::String

def remote_addr: () -> untyped
def user_agent: () -> ::String

def form_hash: () -> untyped
def remote_addr: () -> ::String

def client_ip: () -> (nil | untyped)
def form_hash: () -> ::Hash[::String, top]

def client_ip: () -> ::String?
end
end
end
Expand Down
6 changes: 1 addition & 5 deletions sig/datadog/appsec/contrib/sinatra/framework.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@ module Datadog
module Contrib
module Sinatra
module Framework
def self.setup: () -> untyped

def self.config_with_defaults: (untyped datadog_config) -> untyped

def self.activate_rack!: (untyped datadog_config, untyped sinatra_config) -> untyped
def self.setup: () -> void
end
end
end
Expand Down
6 changes: 4 additions & 2 deletions sig/datadog/appsec/contrib/sinatra/gateway/route_params.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ module Datadog
module Sinatra
module Gateway
class RouteParams < Instrumentation::Gateway::Argument
attr_reader params: untyped
@params: ::Hash[::String, top]

def initialize: (untyped params) -> void
attr_reader params: ::Hash[::String, top]

def initialize: (::Hash[::String, top] params) -> void
end
end
end
Expand Down
8 changes: 5 additions & 3 deletions sig/datadog/appsec/contrib/sinatra/gateway/watcher.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ module Datadog
module Sinatra
module Gateway
module Watcher
def self.watch: () -> untyped
def self.watch: () -> void

def self.watch_request_dispatch: (?Datadog::AppSec::Instrumentation::Gateway gateway) -> untyped
def self.watch_request_dispatch: (?Datadog::AppSec::Instrumentation::Gateway gateway) -> void

def self.watch_request_routed: (?Datadog::AppSec::Instrumentation::Gateway gateway) -> untyped
def self.watch_request_routed: (?Datadog::AppSec::Instrumentation::Gateway gateway) -> void

def self.watch_response_body_json: (?Datadog::AppSec::Instrumentation::Gateway gateway) -> void
end
end
end
Expand Down
13 changes: 6 additions & 7 deletions sig/datadog/appsec/contrib/sinatra/integration.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,19 @@ module Datadog
module Sinatra
class Integration
include Datadog::AppSec::Contrib::Integration
extend Datadog::AppSec::Contrib::Integration::ClassMethods

MINIMUM_VERSION: untyped
MINIMUM_VERSION: Gem::Version

def self.version: () -> untyped
def self.version: () -> Gem::Version?

def self.loaded?: () -> untyped
def self.loaded?: () -> bool

def self.compatible?: () -> untyped
def self.compatible?: () -> bool

def self.auto_instrument?: () -> true

def default_configuration: () -> untyped

def patcher: () -> untyped
def patcher: () -> singleton(Patcher)
end
end
end
Expand Down
18 changes: 14 additions & 4 deletions sig/datadog/appsec/contrib/sinatra/patcher.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ module Datadog
module AppSec
module Contrib
module Sinatra
interface _SinatraRequest
def env: () -> rack_env
end

module AppSecSetupPatch
def setup_middleware: (*untyped args) ?{ () -> untyped } -> untyped
end
Expand All @@ -11,19 +15,25 @@ module Datadog
end

module DispatchPatch
@request: _SinatraRequest

def dispatch!: () -> untyped
end

module RoutePatch
def process_route: () { (untyped) -> untyped } -> untyped
@request: _SinatraRequest

def process_route: (*untyped args) { (*untyped) -> untyped } -> untyped

def params: () -> ::Hash[::String, top]
end

module Patcher
def self?.patched?: () -> untyped
def self?.patched?: () -> bool

def self?.target_version: () -> untyped
def self?.target_version: () -> Gem::Version?

def self?.patch: () -> untyped
def self?.patch: () -> void

def self?.patch_json?: () -> bool
end
Expand Down
4 changes: 3 additions & 1 deletion sig/datadog/appsec/contrib/sinatra/patches/json_patch.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ module Datadog
module Sinatra
module Patches
module JsonPatch
def json: (untyped object, ?::Hash[untyped, untyped] options) -> untyped
@request: Sinatra::_SinatraRequest

def json: (untyped object, ?untyped options) -> ::String
end
end
end
Expand Down
6 changes: 4 additions & 2 deletions sig/datadog/appsec/contrib/sinatra/request_middleware.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ module Datadog
module Contrib
module Sinatra
class RequestMiddleware
def initialize: (untyped app, ?::Hash[untyped, untyped] opt) -> void
@app: rack_app

def call: (untyped env) -> untyped
def initialize: (rack_app app, ?::Hash[::Symbol, top] opt) -> void

def call: (rack_env env) -> rack_response
end
end
end
Expand Down
11 changes: 11 additions & 0 deletions sig/datadog/appsec/contrib/sinatra/types.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module Datadog
module AppSec
module Contrib
module Sinatra
type rack_env = ::Hash[::String, top]
type rack_response = [::Integer, ::Hash[::String, ::String], ::Array[::String]]
type rack_app = ^(rack_env) -> rack_response
end
end
end
end
7 changes: 5 additions & 2 deletions sig/datadog/appsec/instrumentation/gateway.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Datadog
type final_call_result = untyped
type final_call = ^() -> final_call_result
type stack = ::Proc
type argument = Gateway::Argument | ::String
type argument = Gateway::Argument | ::String | ::Array[Gateway::Argument]

type stack_result = ::Array[nil | middleware_result | final_call_result]

Expand All @@ -17,7 +17,10 @@ module Datadog

def push: (::String name, argument env) ?{ () -> final_call_result } -> stack_result

def watch: (::String name, ::Symbol key) { (stack next, argument env) -> stack_result } -> void
def watch: ("sinatra.request.dispatch", ::Symbol key) { (stack, ::Datadog::AppSec::Contrib::Sinatra::Gateway::Request) -> stack_result } -> void
| ("sinatra.request.routed", ::Symbol key) { (stack, [::Datadog::AppSec::Contrib::Sinatra::Gateway::Request, ::Datadog::AppSec::Contrib::Sinatra::Gateway::RouteParams]) -> stack_result } -> void
| ("sinatra.response.body.json", ::Symbol key) { (stack, Gateway::DataContainer) -> stack_result } -> void
| (::String name, ::Symbol key) { (stack, argument) -> stack_result } -> void
Comment on lines +20 to +23
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Strech what do you think of this approach?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess our "dynamically-typed" language loses its charm in RBS signatures.

But regarding this change, I don't see how it could be made much better, it looks ok I think.

The only thing we could theoretically do is move each argument type (or signature) to the integration package: e.g. declare a type for [::Datadog::AppSec::Contrib::Sinatra::Gateway::Request, ::Datadog::AppSec::Contrib::Sinatra::Gateway::RouteParams], or maybe even a type for the whole ("sinatra.request.routed", ::Symbol key) { (stack, [::Datadog::AppSec::Contrib::Sinatra::Gateway::Request, ::Datadog::AppSec::Contrib::Sinatra::Gateway::RouteParams]) -> stack_result } -> void in the Sinatra contrib RBS namespace).
Maybe that looks better... maybe not 🤷


def pushed?: (::String name) -> bool
end
Expand Down
4 changes: 2 additions & 2 deletions sig/datadog/appsec/instrumentation/gateway/middleware.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ module Datadog
attr_reader key: ::Symbol
attr_reader block: ::Proc

def initialize: (::Symbol key) { (Gateway::stack next, Gateway::Argument env) -> stack_result } -> void
def call: (Gateway::stack next, Gateway::Argument? env) -> stack_result
def initialize: (::Symbol key) { (Gateway::stack next, Gateway::argument env) -> stack_result } -> void
def call: (Gateway::stack next, Gateway::argument env) -> stack_result
end
end
end
Expand Down
17 changes: 17 additions & 0 deletions vendor/rbs/rack/0/rack.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module Rack
class Request
def initialize: (::Hash[::String, top] env) -> void

def env: () -> ::Hash[::String, top]

def params: () -> ::Hash[::String, top]

def body: () -> top

def get?: () -> bool

def post?: () -> bool

def path: () -> ::String
end
end
3 changes: 3 additions & 0 deletions vendor/rbs/sinatra/0/sinatra.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@ module Sinatra

class Base
end

module JSON
end
end
Loading