Skip to content
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

let signed_id of ActiveStorge::Blob be striped from as an id #211

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/prometheus/middleware/collector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ def strip_ids_from_path(path)
path
.gsub(%r{/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}(/|$)}, '/:uuid\\1')
.gsub(%r{/\d+(/|$)}, '/:id\\1')
.gsub(%r{/[0-9a-zA-Z=]+--[0-9a-f]+(/|$)}, '/:signed_id\\1')
Copy link
Collaborator

@dmagliola dmagliola Jan 1, 2021

Choose a reason for hiding this comment

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

Thank you for your PR!

I'm a little worried that this Regex is a bit broad. It'll match almost any part of a path that has -- in the middle. This is admittedly rare, but i'm wondering if we can narrow it down a bit more, probably by specifying the length of these parts, like we do for uuid.

I have no experience with ActiveStorage unfortunately, but reading through the code, i believe this is where the Signed ID gets generated: https://github.com/rails/rails/blob/afc79e3cb0906d06b035ef30411c44ddc2132409/activesupport/lib/active_support/message_verifier.rb#L186-L203

And if i'm reading that correctly, i'd expect the first half of the signed ID (before the --) can be any length, but the digest should always be the same length (40 chars), right?

I notice your test case has a shorter digest, i'm not sure whether you're using an actual Signed ID generated by Rails, which would invalidate my suggestion, or if it was manual random typing...

Thoughts?

Copy link
Author

Choose a reason for hiding this comment

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

Thank you for reviewing the PR,
I completely agree with that point, we should make the regex stricter.
Regarding the signature id, though my example was bogus, unfortunately lengths is not always 40, thought the default is SHA1, but it could be changed:
https://github.com/rails/rails/blob/afc79e3cb0906d06b035ef30411c44ddc2132409/activesupport/lib/active_support/message_verifier.rb#L109

Most likely to something like SHA-256 which is 64 chars but extremely rare (rarer than "having -- in path" rare) for anyone to change it to anything less than MD5 which is 32.

Also the first part (before --) also should be a valid base64 encoded string so a not a complex overkill regex might look like this:
(?=(.{4})*--)[A-Za-z0-9]+={0,2}--[0-9a-f]{32,}

I can also add negative test case to the specs.

end
end
end
Expand Down
14 changes: 14 additions & 0 deletions spec/prometheus/middleware/collector_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,20 @@
expect(registry.get(metric).get(labels: labels)).to include("0.1" => 0, "0.5" => 1)
end

it 'normalizes paths containing signed_ids by default' do
expect(Benchmark).to receive(:realtime).and_yield.and_return(0.3)

get '/foo/eyJfcmFpbHMiOnsibWVzc2FnZSI6--a419915169c2dc3419/bars'

metric = :http_server_requests_total
labels = { method: 'get', path: '/foo/:signed_id/bars', code: '200' }
expect(registry.get(metric).get(labels: labels)).to eql(1.0)

metric = :http_server_request_duration_seconds
labels = { method: 'get', path: '/foo/:signed_id/bars' }
expect(registry.get(metric).get(labels: labels)).to include("0.1" => 0, "0.5" => 1)
end

context 'when the app raises an exception' do
let(:original_app) do
lambda do |env|
Expand Down