-
Notifications
You must be signed in to change notification settings - Fork 415
Cheaper logcontext debug logs (random_string_insecure_fast(...))
#19094
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
Cheaper logcontext debug logs (random_string_insecure_fast(...))
#19094
Conversation
synapse/util/stringutils.py
Outdated
| return "".join(secrets.choice(_string_with_symbols) for _ in range(length)) | ||
|
|
||
|
|
||
| def pseudo_random_string(length: int) -> str: |
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 also considered naming this random_string_insecure_fast
🤷
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 we should call this insecure_random_string. Being pseudo-random doesn't necessarily imply insecure, that's what CSPRNGs are for, after all.
It's maybe not a huge deal, but given the sheer hazard of using the wrong type of random in the wrong place, I much prefer the clear and simple insecure label, because it brings your attention to an important (negative) caveat. In theory that could raise some alarm bells at a critical time during review.
On the other hand, I don't think it's important to say fast — if someone consciously thinks about the speed at PR review time, they can look it up. (But I'm also not against calling it 'fast', to be fair!)
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.
Originally, I didn't even consider that a simple, innocuous-sounding random_string(...) utility would have a noticeable impact in the app when using it in the logcontext code. There is no "cryptographically"/"crypt" hint about it from the outside that I would normally think about needing in a secure context.
Adding fast at-least sparks an idea that the other variation could be slow and better consideration on which one to choose.
I'll go with random_string_insecure_fast (suffix) so it appears more readily and obviously next to random_string in typeahead.
synapse/logging/context.py
Outdated
| instance_id = pseudo_random_string(5) | ||
| calling_context = current_context() | ||
| logcontext_debug_logger.debug( | ||
| "run_in_background(%s): called with logcontext=%s", instance_id, calling_context | ||
| ) |
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.
As another alternative, we could gate the random string creation behind if logcontext_debug_logger.isEnabledFor(logging.DEBUG)
synapse/util/stringutils.py
Outdated
| return "".join(secrets.choice(_string_with_symbols) for _ in range(length)) | ||
|
|
||
|
|
||
| def pseudo_random_string(length: int) -> str: |
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 we should call this insecure_random_string. Being pseudo-random doesn't necessarily imply insecure, that's what CSPRNGs are for, after all.
It's maybe not a huge deal, but given the sheer hazard of using the wrong type of random in the wrong place, I much prefer the clear and simple insecure label, because it brings your attention to an important (negative) caveat. In theory that could raise some alarm bells at a critical time during review.
On the other hand, I don't think it's important to say fast — if someone consciously thinks about the speed at PR review time, they can look it up. (But I'm also not against calling it 'fast', to be fair!)
pseudo_random_string(...))random_string_insecure_fast(...))
|
Thanks for the review @reivilibre 🐡 |
Cheaper logcontext debug logs (
random_string_insecure_fast(...))Follow-up to #18966
During the weekly Backend team meeting, it was mentioned that
random_string(...)was taking a significant amount of CPU onmatrix.org. This makes sense as it relies onsecrets.choice(...), a cryptographically secure function that is inherently computationally expensive. And since #18966, we're callingrandom_string(...)as part of a bunch of logcontext utilities.Since we don't need cryptographically secure random strings for our debug logs, this PR is introducing a new
random_string_insecure_fast(...)function that usesrandom.choice(...)which uses pseudo-random numbers that are "both fast and threadsafe".Dev notes
Pull Request Checklist
EventStoretoEventWorkerStore.".code blocks.