Skip to content
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

fix(packet): add precheck for timestamps in Prepares #707

Merged
merged 5 commits into from
Apr 16, 2021
Merged
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
21 changes: 21 additions & 0 deletions crates/interledger-packet/src/packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,19 @@ impl TryFrom<BytesMut> for Prepare {
let content_len = content.len();
let amount = content.read_u64::<BigEndian>()?;

// Fixed Length DateTime format - RFC 0027
// https://github.com/interledger/rfcs/blob/2dfdcf47ac52489a4ad473a5d869cd9f0217db67/0027-interledger-protocol-4/0027-interledger-protocol-4.md#ilp-prepare
let mut expires_at = [0x00; 17];
content.read_exact(&mut expires_at)?;

if !expires_at
.iter()
.map(|e| (b'0'..=b'9').contains(e))
.fold(true, |a, b| a & b)
{
return Err(ParseError::InvalidPacket("DateTime must be numeric".into()));
}

let expires_at = str::from_utf8(&expires_at[..])?;
let expires_at: DateTime<Utc> =
Utc.datetime_from_str(&expires_at, INTERLEDGER_TIMESTAMP_FORMAT)?;
Expand Down Expand Up @@ -741,6 +752,16 @@ mod test_prepare {
assert!(Prepare::try_from(prep).is_err());
}

#[test]
fn test_invalid_ts() {
for i in 12..(12 + 17) {
let mut prep = BytesMut::from(PREPARE_BYTES);
prep[i] = 9; // convert a byte from the address to a junk character
let err = Prepare::try_from(prep).unwrap_err();
assert_eq!("Invalid Packet: DateTime must be numeric", &err.to_string());
}
}

#[test]
fn test_try_from() {
assert_eq!(
Expand Down