Skip to content

search: check for null bytes before querying #9473

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

Merged
merged 2 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 19 additions & 18 deletions src/controllers/krate/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,28 +55,29 @@ pub async fn search(app: AppState, req: Parts) -> AppResult<Json<Value>> {
use seek::*;

let params = req.query();
let option_param = |s| params.get(s).map(|v| v.as_str());
let sort = option_param("sort");
let include_yanked = option_param("include_yanked")
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),
};
Comment on lines +58 to +64
Copy link
Contributor

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:

Suggested change
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?

Copy link
Member

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 :)

let sort = option_param("sort")?;
let include_yanked = option_param("include_yanked")?
.map(|s| s == "yes")
.unwrap_or(true);

// Remove 0x00 characters from the query string because Postgres can not
// handle them and will return an error, which would cause us to throw
// an Internal Server Error ourselves.
let q_string = option_param("q").map(|q| q.replace('\u{0}', ""));

let filter_params = FilterParams {
q_string: q_string.as_deref(),
q_string: option_param("q")?,
include_yanked,
category: option_param("category"),
all_keywords: option_param("all_keywords"),
keyword: option_param("keyword"),
letter: option_param("letter"),
user_id: option_param("user_id").and_then(|s| s.parse::<i32>().ok()),
team_id: option_param("team_id").and_then(|s| s.parse::<i32>().ok()),
following: option_param("following").is_some(),
has_ids: option_param("ids[]").is_some(),
category: option_param("category")?,
all_keywords: option_param("all_keywords")?,
keyword: option_param("keyword")?,
letter: option_param("letter")?,
user_id: option_param("user_id")?.and_then(|s| s.parse::<i32>().ok()),
team_id: option_param("team_id")?.and_then(|s| s.parse::<i32>().ok()),
following: option_param("following")?.is_some(),
has_ids: option_param("ids[]")?.is_some(),
..Default::default()
};

Expand All @@ -95,7 +96,7 @@ pub async fn search(app: AppState, req: Parts) -> AppResult<Json<Value>> {
.left_join(recent_crate_downloads::table)
.select(selection);

if let Some(q_string) = &q_string {
if let Some(q_string) = &filter_params.q_string {
if !q_string.is_empty() {
let sort = sort.unwrap_or("relevance");

Expand Down
16 changes: 11 additions & 5 deletions src/tests/routes/crates/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,6 @@ async fn index_queries() {
assert_eq!(cl.crates.len(), 0);
assert_eq!(cl.meta.total, 0);
}

// ignores 0x00 characters that Postgres does not support
for cl in search_both(&anon, "q=k%00w1").await {
assert_eq!(cl.meta.total, 3);
}
}

#[tokio::test(flavor = "multi_thread")]
Expand Down Expand Up @@ -1015,6 +1010,17 @@ async fn test_pages_work_even_with_seek_based_pagination() {
assert_eq!(second.meta.total, 3);
}

#[tokio::test(flavor = "multi_thread")]
async fn invalid_params_with_null_bytes() {
let (_app, anon, _cookie) = TestApp::init().with_user();

for name in ["q", "category", "all_keywords", "keyword", "letter"] {
let response = anon.get::<()>(&format!("/api/v1/crates?{name}=%00")).await;
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
assert_json_snapshot!(response.json());
}
}

#[tokio::test(flavor = "multi_thread")]
async fn invalid_seek_parameter() {
let (_app, anon, _cookie) = TestApp::init().with_user();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
source: src/tests/routes/crates/list.rs
expression: response.json()
---
{
"errors": [
{
"detail": "parameter category cannot contain a null byte"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
source: src/tests/routes/crates/list.rs
expression: response.json()
---
{
"errors": [
{
"detail": "parameter all_keywords cannot contain a null byte"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
source: src/tests/routes/crates/list.rs
expression: response.json()
---
{
"errors": [
{
"detail": "parameter keyword cannot contain a null byte"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
source: src/tests/routes/crates/list.rs
expression: response.json()
---
{
"errors": [
{
"detail": "parameter letter cannot contain a null byte"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
source: src/tests/routes/crates/list.rs
expression: response.json()
---
{
"errors": [
{
"detail": "parameter q cannot contain a null byte"
}
]
}