Skip to content
This repository was archived by the owner on Oct 19, 2026. It is now read-only.

Commit 2398a92

Browse files
authored
fix(lfs): reject invalid lock list limit instead of panicking (#2175)
* fix(lfs): reject invalid lock list limit instead of panicking The `limit` query parameter of GET /info/lfs/locks flows as a raw string into `limit.parse::<i64>().unwrap()`: - `?limit=abc` panics on the unwrap. - A negative value such as `?limit=-1` passes the parse, then `size as usize` wraps to usize::MAX and `split_off` / indexing panic out of bounds. Both are unauthenticated request-triggered handler panics. Parse the limit as usize (rejecting negatives naturally), map malformed values to a GeneralError that the router maps to a 400, and extract the pagination into a pure helper so it is directly unit-testable. Signed-off-by: Tyagiquamar <Tyagiquamar@users.noreply.github.com> * fix(lfs): keep invalid lock list limit classified as 400 lfs_retrieve_lock replaced every error from lfs_get_filtered_locks with the generic 'Lookup operation failed!', so the 'Invalid limit parameter' error produced by apply_lock_limit lost its classification and the router's map_lfs_error turned it into a 500 instead of the intended 400. Pass input-validation errors through unmasked and keep masking only genuine lookup failures; strengthen the limit-rejection test to pin the 'Invalid' message prefix the router contract depends on. Signed-off-by: Tyagiquamar <Tyagiquamar@users.noreply.github.com> Signed-off-by: Tyagiquamar <mohdquamartyagi@gmail.com> --------- Signed-off-by: Tyagiquamar <Tyagiquamar@users.noreply.github.com> Signed-off-by: Tyagiquamar <mohdquamartyagi@gmail.com> Co-authored-by: Tyagiquamar <Tyagiquamar@users.noreply.github.com>
1 parent 3b813a0 commit 2398a92

1 file changed

Lines changed: 78 additions & 6 deletions

File tree

‎ceres/src/lfs/handler.rs‎

Lines changed: 78 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{cmp::min, time::Duration};
1+
use std::time::Duration;
22

33
use anyhow::Result;
44
use bytes::Bytes;
@@ -45,6 +45,12 @@ pub async fn lfs_retrieve_lock(
4545
lock_list.next_cursor = next;
4646
Ok(lock_list)
4747
}
48+
// Client-input errors (e.g. a malformed `limit`) must reach the router
49+
// unmasked so `map_lfs_error` can classify them as 400; only genuine
50+
// lookup failures are hidden behind the generic message.
51+
Err(GitLFSError::GeneralError(msg)) if msg.starts_with("Invalid") => {
52+
Err(GitLFSError::GeneralError(msg))
53+
}
4854
Err(_) => Err(GitLFSError::GeneralError(
4955
"Lookup operation failed!".to_string(),
5056
)),
@@ -391,15 +397,27 @@ async fn lfs_get_filtered_locks(
391397
locks = filterd;
392398
}
393399

400+
apply_lock_limit(locks, limit)
401+
}
402+
403+
/// Applies the `limit` parameter to an ordered lock list, returning the page and
404+
/// the id of the first lock past the page (empty when no further page exists).
405+
///
406+
/// The limit comes straight from the query string, so anything non-numeric
407+
/// (including negative values) is rejected instead of being parsed leniently.
408+
fn apply_lock_limit(locks: Vec<Lock>, limit: &str) -> Result<(Vec<Lock>, String), GitLFSError> {
394409
let mut next = "".to_string();
410+
let mut locks = locks;
395411
if !limit.is_empty() {
396-
let mut size = limit.parse::<i64>().unwrap();
397-
size = min(size, locks.len() as i64);
412+
let size = limit
413+
.parse::<usize>()
414+
.map_err(|_| GitLFSError::GeneralError(format!("Invalid limit parameter: {limit}")))?;
415+
let size = size.min(locks.len());
398416

399-
if size + 1 < locks.len() as i64 {
400-
locks[size as usize].id.clone_into(&mut next);
417+
if size + 1 < locks.len() {
418+
locks[size].id.clone_into(&mut next);
401419
}
402-
let _ = locks.split_off(size as usize);
420+
let _ = locks.split_off(size);
403421
}
404422

405423
Ok((locks, next))
@@ -625,6 +643,60 @@ mod tests {
625643
use super::*;
626644
use crate::lfs::lfs_structs::{Action, Ref, ResCondition, ResponseObject};
627645

646+
fn lock(id: &str) -> Lock {
647+
Lock {
648+
id: id.to_string(),
649+
path: format!("/dir/{id}.bin"),
650+
owner: None,
651+
locked_at: "2026-01-01T00:00:00Z".to_string(),
652+
}
653+
}
654+
655+
#[test]
656+
fn lock_limit_slices_page_and_reports_next_cursor() {
657+
let locks = vec![lock("1"), lock("2"), lock("3"), lock("4")];
658+
659+
let (page, next) = apply_lock_limit(locks, "2").unwrap();
660+
assert_eq!(
661+
page.iter().map(|l| l.id.as_str()).collect::<Vec<_>>(),
662+
["1", "2"]
663+
);
664+
assert_eq!(next, "3");
665+
}
666+
667+
#[test]
668+
fn lock_limit_beyond_list_returns_everything_without_cursor() {
669+
let locks = vec![lock("1"), lock("2")];
670+
671+
let (page, next) = apply_lock_limit(locks, "10").unwrap();
672+
assert_eq!(page.len(), 2);
673+
assert_eq!(next, "");
674+
}
675+
676+
#[test]
677+
fn empty_limit_returns_unpaged_locks() {
678+
let locks = vec![lock("1")];
679+
680+
let (page, next) = apply_lock_limit(locks, "").unwrap();
681+
assert_eq!(page.len(), 1);
682+
assert_eq!(next, "");
683+
}
684+
685+
#[test]
686+
fn non_numeric_and_negative_limits_are_rejected() {
687+
// The router classifies by message content ("Invalid..." -> 400), so the
688+
// rejection must carry that prefix to survive the lookup-error masking
689+
// in `lfs_retrieve_lock`.
690+
for limit in ["abc", "-1"] {
691+
match apply_lock_limit(vec![lock("1")], limit) {
692+
Err(GitLFSError::GeneralError(msg)) => {
693+
assert!(msg.starts_with("Invalid"), "unexpected message: {msg}");
694+
}
695+
other => panic!("expected GeneralError, got {other:?}"),
696+
}
697+
}
698+
}
699+
628700
#[test]
629701
fn response_object_download_existing() {
630702
let meta = MetaObject {

0 commit comments

Comments
 (0)