-
Notifications
You must be signed in to change notification settings - Fork 394
AppSec: Fix undefined method request_method=
#4870
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
spec/datadog/appsec/contrib/grape/integration_test_spec.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
require 'datadog/tracing/contrib/support/spec_helper' | ||
require 'rack/test' | ||
|
||
require 'datadog/tracing' | ||
require 'datadog/appsec' | ||
|
||
RSpec.describe 'Grape integration tests' do | ||
include Rack::Test::Methods | ||
|
||
let(:sorted_spans) do | ||
chain = lambda do |start| | ||
loop.with_object([start]) do |_, o| | ||
break o if o.last.parent_id == 0 | ||
|
||
parent = spans.find { |span| span.id == o.last.parent_id } | ||
break o if parent.nil? | ||
|
||
o << parent | ||
end | ||
end | ||
sort = ->(list) { list.sort_by { |e| chain.call(e).count } } | ||
sort.call(spans) | ||
end | ||
|
||
let(:rack_span) { sorted_spans.reverse.find { |x| x.name == Datadog::Tracing::Contrib::Rack::Ext::SPAN_REQUEST } } | ||
|
||
let(:tracing_enabled) { true } | ||
let(:appsec_enabled) { true } | ||
|
||
let(:api_security_enabled) { false } | ||
let(:api_security_sample) { 0 } | ||
|
||
before do | ||
Datadog.configure do |c| | ||
c.tracing.enabled = tracing_enabled | ||
c.tracing.instrument :rack | ||
|
||
c.appsec.enabled = appsec_enabled | ||
c.appsec.instrument :rack | ||
|
||
c.appsec.api_security.enabled = api_security_enabled | ||
c.appsec.api_security.sample_delay = api_security_sample.to_i | ||
end | ||
|
||
allow_any_instance_of(Datadog::Tracing::Transport::HTTP::Client).to receive(:send_request) | ||
allow_any_instance_of(Datadog::Tracing::Transport::Traces::Transport).to receive(:native_events_supported?) | ||
.and_return(true) | ||
end | ||
|
||
after do | ||
Datadog.configuration.reset! | ||
Datadog.registry[:rack].reset_configuration! | ||
end | ||
|
||
context 'for a mounted Grape API' do | ||
let(:middlewares) do | ||
[ | ||
Datadog::Tracing::Contrib::Rack::TraceMiddleware, | ||
Datadog::AppSec::Contrib::Rack::RequestMiddleware | ||
] | ||
end | ||
|
||
let(:app) do | ||
skip 'grape gem not available' unless Gem.loaded_specs['grape'] | ||
require 'grape' | ||
|
||
api_class = Class.new(Grape::API) do | ||
format :json | ||
get('/users/:id') { { ok: true } } | ||
end | ||
|
||
app_middlewares = middlewares | ||
|
||
Rack::Builder.new do | ||
app_middlewares.each { |m| use m } | ||
map '/' do | ||
run api_class | ||
end | ||
end.to_app | ||
end | ||
|
||
let(:service_span) do | ||
sorted_spans.reverse.find { |s| s.metrics.fetch('_dd.top_level', -1.0) > 0.0 } | ||
end | ||
|
||
let(:span) { rack_span } | ||
let(:remote_addr) { '127.0.0.1' } | ||
|
||
describe 'with sample_delay' do | ||
subject(:response) { get url, params, env } | ||
|
||
let(:api_security_enabled) { true } | ||
let(:api_security_sample) { 30 } | ||
|
||
let(:url) { '/users/123' } | ||
let(:params) { {} } | ||
let(:headers) { {} } | ||
let(:env) { { 'REMOTE_ADDR' => remote_addr }.merge!(headers) } | ||
|
||
it 'samples and caches check result' do | ||
get url, params, env | ||
first_span = spans.find { |s| s.name == 'rack.request' } | ||
expect(first_span.tags).to have_key('_dd.appsec.s.req.headers') | ||
|
||
clear_traces! | ||
|
||
get url, params, env | ||
second_span = spans.find { |s| s.name == 'rack.request' } | ||
expect(second_span.tags).not_to have_key('_dd.appsec.s.req.headers') | ||
end | ||
end | ||
end | ||
end | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @marcotc, thanks for taking care of that. We don't have
grape
contrib, but there was a plan to start supporting it as main API framework.The init of the router will generate new instance with unprocessed env, could you please explain what is the actual issue here of using stored router?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The main issue is that
recognize(request)
expects a wrapped ActionPack request, not a raw Rack request, and it throws the errorundefined method request_method=
when running.Thankfully this is easy to reproduce in tests (which are being added).
Tbh, I only really have to fix the Rails issue today, but I noticed that the Grape and Sinatra paths were not tested in integration tests (which the the only way to trigger the Rails failure today), so I started adding them in this PR as well.
Still WIP though!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤔 Hmmm, we have tested it with Rails and we decide to scope out Sinatra to system tests, but both working. In which case it do you get that error?
If it happens solely in tests like an old "integration" test we poses, than I would challenge the quality of the test.
We also have a test app where I run it across different Rails app versions.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BTW, here are integrations tests that will use route extraction (but not on all Rails versions)
If you don't mind could you share the error case you are fixing? I would love to help on this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I get the issue now, problem starts not because sampling window, but because the
HEAD
request. It will take a path into the router which will assign the method into it to be able to find it across the available routes.Rails will try to modify the request in order to find it.
As a suggestion, to avoid this expensive path, could we wrap the request only for this specific scenario of HEAD request then?