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

Conversation

rzjfr
Copy link

@rzjfr rzjfr commented Jan 1, 2021

Strip signed_id of Rails ActiveStorge::Blob by default like normal IDs and UUIDs

@coveralls
Copy link

coveralls commented Jan 1, 2021

Coverage Status

Coverage remained the same at 100.0% when pulling 0ac0fec on rzjfr:strip_signed_id into 8b20201 on prometheus:master.

@rzjfr rzjfr marked this pull request as draft January 1, 2021 11:32
Signed-off-by: Reza J. Bavaghoush <rzjfr@yahoo.com>
@@ -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.

@rzjfr rzjfr marked this pull request as ready for review February 6, 2021 10:12
@Sinjo
Copy link
Member

Sinjo commented Apr 9, 2022

Hi @rzjfr, sorry we went quiet on this PR.

After some issues caused by us trying to get too clever with how we process URLs in the collector middleware, we've decided to keep it simple for now.

For the time being, we're encouraging anyone who wants to customise how paths are generated to extend Prometheus::Middleware::Collector and override one or both of strip_ids_from_path and generate_path.

I appreciate that it's a little more code on your side, but we'd like to keep our path generation logic deliberately simple, and let people add their framework-specific customisations to it.

At some point we may revisit that decision, but the earliest that will happen is version 5 we've been treating changes to automatically generated labels as breaking (since they might break dashboards/alerts that people have set up).

@Sinjo Sinjo closed this Apr 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants