Cheaper logcontext debug logs (random_string_insecure_fast(...))#19094
Cheaper logcontext debug logs (random_string_insecure_fast(...))#19094MadLittleMods merged 4 commits intodevelopfrom
random_string_insecure_fast(...))#19094Conversation
| 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.
I also considered naming this random_string_insecure_fast
🤷
There was a problem hiding this comment.
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.
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.
There was a problem hiding this comment.
As another alternative, we could gate the random string creation behind if logcontext_debug_logger.isEnabledFor(logging.DEBUG)
| 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.
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.