Skip to content

controllers/helpers/pagination: Return 400 Bad Request for invalid seek parameters #7775

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 1 commit into from
Dec 20, 2023
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
4 changes: 2 additions & 2 deletions src/controllers/helpers/pagination.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ pub(crate) struct RawSeekPayload(String);

impl RawSeekPayload {
pub(crate) fn decode<D: for<'a> Deserialize<'a>>(&self) -> AppResult<D> {
decode_seek(&self.0)
decode_seek(&self.0).map_err(|_| bad_request("invalid seek parameter"))
}
}

Expand Down Expand Up @@ -294,7 +294,7 @@ pub(crate) fn encode_seek<S: Serialize>(params: S) -> AppResult<String> {
}

/// Decode a list of params previously encoded with [`encode_seek`].
pub(crate) fn decode_seek<D: for<'a> Deserialize<'a>>(seek: &str) -> AppResult<D> {
pub(crate) fn decode_seek<D: for<'a> Deserialize<'a>>(seek: &str) -> anyhow::Result<D> {
let decoded = serde_json::from_slice(&general_purpose::URL_SAFE_NO_PAD.decode(seek)?)?;
Ok(decoded)
}
Expand Down
10 changes: 10 additions & 0 deletions src/tests/routes/crates/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crates_io::schema::crates;
use diesel::{dsl::*, prelude::*, update};
use googletest::prelude::*;
use http::StatusCode;
use insta::assert_json_snapshot;

#[test]
fn index() {
Expand Down Expand Up @@ -793,6 +794,15 @@ fn test_pages_work_even_with_seek_based_pagination() {
assert!(second.meta.next_page.unwrap().contains("page=3"));
}

#[test]
fn invalid_seek_parameter() {
let (_app, anon, _cookie) = TestApp::init().with_user();

let response = anon.get::<()>("/api/v1/crates?seek=broken");
assert_eq!(response.status(), StatusCode::OK);
assert_json_snapshot!(response.into_json());
}

#[test]
fn pagination_parameters_only_accept_integers() {
let (app, anon, user) = 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.into_json()
---
{
"errors": [
{
"detail": "invalid seek parameter"
}
]
}