Skip to content

Commit

Permalink
don't allow object hashes in excludes (#450)
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Aug 7, 2022
1 parent 79e0eaf commit b889953
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
15 changes: 13 additions & 2 deletions git-refspec/src/parse.rs
Expand Up @@ -6,6 +6,8 @@ pub enum Error {
NegativeWithDestination,
#[error("Negative specs must not be empty")]
NegativeEmpty,
#[error("Negative specs must be object hashes")]
NegativeObjectHash,
#[error("Cannot push into an empty destination")]
PushToEmpty,
#[error("glob patterns may only involved a single '*' character, found {pattern:?}")]
Expand Down Expand Up @@ -88,8 +90,17 @@ pub(crate) mod function {
}
};

if mode == Mode::Negative && src.is_none() {
return Err(Error::NegativeEmpty);
if mode == Mode::Negative {
match src {
Some(spec) => {
if spec.len() >= git_hash::Kind::shortest().len_in_hex()
&& spec.iter().all(|b| b.is_ascii_hexdigit())
{
return Err(Error::NegativeObjectHash);
}
}
None => return Err(Error::NegativeEmpty),
}
}

if let Some(spec) = src.as_mut() {
Expand Down
10 changes: 10 additions & 0 deletions git-refspec/tests/parse/invalid.rs
Expand Up @@ -13,6 +13,16 @@ fn negative_must_not_be_empty() {
}
}

#[test]
fn negative_must_not_be_object_hash() {
for op in [Operation::Fetch, Operation::Push] {
assert!(matches!(
try_parse("^e69de29bb2d1d6434b8b29ae775ad8c2e48c5391", op).unwrap_err(),
Error::NegativeObjectHash
));
}
}

#[test]
fn negative_with_destination() {
for op in [Operation::Fetch, Operation::Push] {
Expand Down

0 comments on commit b889953

Please sign in to comment.