-
Notifications
You must be signed in to change notification settings - Fork 599
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
search: check for null bytes before querying #9473
Conversation
[PostgreSQL has an unusual definition of `UTF8` that doesn't allow for null bytes][null-bytes], even though they are technically valid UTF-8 by any other definition (including Rust's `str` type). Sending a crate search request that includes a null byte will eventually result in a database error anyway, but let's save Sentry the trouble and just 400 out early. Fixes rust-lang#9444. [null-bytes]: https://www.commandprompt.com/blog/null-characters-workarounds-arent-good-enough/
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #9473 +/- ##
==========================================
+ Coverage 89.09% 89.10% +0.01%
==========================================
Files 286 286
Lines 29035 29039 +4
==========================================
+ Hits 25868 25875 +7
+ Misses 3167 3164 -3 ☔ View full report in Codecov by Sentry. |
let option_param = |s| match params.get(s).map(|v| v.as_str()) { | ||
Some(v) if v.contains('\0') => Err(bad_request(format!( | ||
"parameter {s} cannot contain a null byte" | ||
))), | ||
Some(v) => Ok(Some(v)), | ||
None => Ok(None), | ||
}; |
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.
Is it okay to simply write it as:
let option_param = |s| match params.get(s).map(|v| v.as_str()) { | |
Some(v) if v.contains('\0') => Err(bad_request(format!( | |
"parameter {s} cannot contain a null byte" | |
))), | |
Some(v) => Ok(Some(v)), | |
None => Ok(None), | |
}; | |
let option_param = |s| match params.get(s).map(|v| v.as_str()) { | |
Some(v) if v.contains('\0') => Err(bad_request(format!( | |
"parameter {s} cannot contain a null byte" | |
))), | |
v @ _ => Ok(v), // or just v => Ok(v) | |
}; |
Or will it be more difficult to read in this way?
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'll treat this comment as non-blocking. up to Adam whether he wants to pull this into a follow-up PR :)
PostgreSQL has an unusual definition of
UTF8
that doesn't allow for null bytes, even though they are technically valid UTF-8 by any other definition (including Rust'sstr
type). Sending a crate search request that includes a null byte will eventually result in a database error anyway, but let's save Sentry the trouble and just 400 out early.I did a quick audit of other places that we use query parameters and don't see any other code paths where we use a string directly as a query parameter, but I won't pretend this was a super detailed survey. (Fun fact: I can actually see the power usage for my laptop increase on my fancy new charger when I hit "show references" on the
query
method inRequestUtils
.)Fixes #9444.