Skip to content

Commit 0354821

Browse files
committed
move timezone_offset bound check to format_datetime
So it is checked in all cases - before it was only checked in backup create, not backup restore.
1 parent c415f77 commit 0354821

File tree

3 files changed

+30
-17
lines changed

3 files changed

+30
-17
lines changed

src/rust/bitbox02-rust/src/hww/api/backup.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,16 +75,10 @@ pub async fn create(
7575
timezone_offset,
7676
}: &pb::CreateBackupRequest,
7777
) -> Result<Response, Error> {
78-
const MAX_EAST_UTC_OFFSET: i32 = 50400; // 14 hours in seconds
79-
const MAX_WEST_UTC_OFFSET: i32 = -43200; // 12 hours in seconds
80-
81-
if !(MAX_WEST_UTC_OFFSET..=MAX_EAST_UTC_OFFSET).contains(&timezone_offset) {
82-
return Err(Error::InvalidInput);
83-
}
84-
8578
confirm::confirm(&confirm::Params {
8679
title: "Is today?",
87-
body: &bitbox02::format_datetime(timestamp, timezone_offset, true),
80+
body: &bitbox02::format_datetime(timestamp, timezone_offset, true)
81+
.map_err(|_| Error::InvalidInput)?,
8882
..Default::default()
8983
})
9084
.await?;

src/rust/bitbox02-rust/src/hww/api/restore.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ pub async fn from_file(request: &pb::RestoreBackupRequest) -> Result<Response, E
4747
#[cfg(feature = "app-u2f")]
4848
{
4949
let datetime_string =
50-
bitbox02::format_datetime(request.timestamp, request.timezone_offset, false);
50+
bitbox02::format_datetime(request.timestamp, request.timezone_offset, false)
51+
.map_err(|_| Error::InvalidInput)?;
5152
let params = confirm::Params {
5253
title: "Is now?",
5354
body: &datetime_string,
@@ -92,7 +93,8 @@ pub async fn from_mnemonic(
9293
) -> Result<Response, Error> {
9394
#[cfg(feature = "app-u2f")]
9495
{
95-
let datetime_string = bitbox02::format_datetime(timestamp, timezone_offset, false);
96+
let datetime_string = bitbox02::format_datetime(timestamp, timezone_offset, false)
97+
.map_err(|_| Error::InvalidInput)?;
9698
confirm::confirm(&confirm::Params {
9799
title: "Is now?",
98100
body: &datetime_string,

src/rust/bitbox02/src/lib.rs

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -135,15 +135,26 @@ pub fn strftime(timestamp: u32, format: &str) -> String {
135135
/// timestamp is the unix timestamp in seconds.
136136
/// timezone_offset is added to the timestamp, timezone part.
137137
/// date_only: if true, only the date is formatted. If false, both date and time are.
138-
pub fn format_datetime(timestamp: u32, timezone_offset: i32, date_only: bool) -> String {
139-
strftime(
138+
pub fn format_datetime(
139+
timestamp: u32,
140+
timezone_offset: i32,
141+
date_only: bool,
142+
) -> Result<String, ()> {
143+
const MAX_EAST_UTC_OFFSET: i32 = 50400; // 14 hours in seconds
144+
const MAX_WEST_UTC_OFFSET: i32 = -43200; // 12 hours in seconds
145+
146+
if !(MAX_WEST_UTC_OFFSET..=MAX_EAST_UTC_OFFSET).contains(&timezone_offset) {
147+
return Err(());
148+
}
149+
150+
Ok(strftime(
140151
((timestamp as i64) + (timezone_offset as i64)) as u32,
141152
if date_only {
142153
"%a %Y-%m-%d"
143154
} else {
144155
"%a %Y-%m-%d\n%H:%M"
145156
},
146-
)
157+
))
147158
}
148159

149160
#[cfg(not(feature = "testing"))]
@@ -199,18 +210,24 @@ mod tests {
199210

200211
#[test]
201212
fn test_format_datetime() {
202-
assert_eq!(format_datetime(1601281809, 0, true), "Mon 2020-09-28");
213+
assert_eq!(
214+
format_datetime(1601281809, 0, true),
215+
Ok("Mon 2020-09-28".into())
216+
);
203217
assert_eq!(
204218
format_datetime(1601281809, 0, false),
205-
"Mon 2020-09-28\n08:30"
219+
Ok("Mon 2020-09-28\n08:30".into()),
206220
);
207221
assert_eq!(
208222
format_datetime(1601281809, 18000, false),
209-
"Mon 2020-09-28\n13:30"
223+
Ok("Mon 2020-09-28\n13:30".into()),
210224
);
211225
assert_eq!(
212226
format_datetime(1601281809, -32400, false),
213-
"Sun 2020-09-27\n23:30"
227+
Ok("Sun 2020-09-27\n23:30".into()),
214228
);
229+
230+
assert!(format_datetime(1601281809, 50401, false).is_err());
231+
assert!(format_datetime(1601281809, -43201, false).is_err());
215232
}
216233
}

0 commit comments

Comments
 (0)