Skip to content

Commit 2945bfd

Browse files
authored
controllers/helpers/pagination: Return 400 Bad Request for invalid seek parameters (#7775)
well... it's still `200 OK` due to the cargo rewrite middleware, but it would be 400 if that wasn't there...
1 parent 76927e0 commit 2945bfd

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

src/controllers/helpers/pagination.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ pub(crate) struct RawSeekPayload(String);
248248

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

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

296296
/// Decode a list of params previously encoded with [`encode_seek`].
297-
pub(crate) fn decode_seek<D: for<'a> Deserialize<'a>>(seek: &str) -> AppResult<D> {
297+
pub(crate) fn decode_seek<D: for<'a> Deserialize<'a>>(seek: &str) -> anyhow::Result<D> {
298298
let decoded = serde_json::from_slice(&general_purpose::URL_SAFE_NO_PAD.decode(seek)?)?;
299299
Ok(decoded)
300300
}

src/tests/routes/crates/list.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use crates_io::schema::crates;
66
use diesel::{dsl::*, prelude::*, update};
77
use googletest::prelude::*;
88
use http::StatusCode;
9+
use insta::assert_json_snapshot;
910

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

797+
#[test]
798+
fn invalid_seek_parameter() {
799+
let (_app, anon, _cookie) = TestApp::init().with_user();
800+
801+
let response = anon.get::<()>("/api/v1/crates?seek=broken");
802+
assert_eq!(response.status(), StatusCode::OK);
803+
assert_json_snapshot!(response.into_json());
804+
}
805+
796806
#[test]
797807
fn pagination_parameters_only_accept_integers() {
798808
let (app, anon, user) = TestApp::init().with_user();
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
source: src/tests/routes/crates/list.rs
3+
expression: response.into_json()
4+
---
5+
{
6+
"errors": [
7+
{
8+
"detail": "invalid seek parameter"
9+
}
10+
]
11+
}

0 commit comments

Comments
 (0)