@smbcloud / smbcloud-ascapi Mirror · read-only / commits / d9893fd
Fork 0

Read-only mirror of github.com/smbcloudXYZ/smbcloud-ascapi.

Mirrored from https://github.com/smbcloudXYZ/smbcloud-ascapi.git

Synced Nov 25, 09:15 UTC

xiaomi: reject impossible dates, bound the year, name the HTTP error

parse_time accepted any day from 1 to 31, so 2026-02-30 rolled into March and queried a different window than the one typed. It now checks the real month length, leap years included. The year is limited to 1970 through 9999, which also keeps the civil-date arithmetic from overflowing on an absurd year. Error::Http2xx becomes HttpStatus: it is returned for statuses that are not 2xx. send() no longer swallows a failed body read on a success status. It used to turn into an empty string and a confusing JSON decode error. On an error status the status still comes first and the body is best effort. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Co-Authored-By: siGit Code v1.7.10 <noreply@sigit.si>

Seto Elkahfi committed Nov 19, 2026 at 12:43 UTC d9893fd734c8ae5746e231ac43692d11b90cfb2c
3 files changed +46 -5
crates/frontend/src/xiaomi.rs
+39 -1
@@ -83,6 +83,16 @@ pub fn redact_urls(value: &Value) -> Value {
83 }
84 }
85
86 +/// Days in month `m` (1 to 12) of year `y`, leap years included.
87 +fn days_in_month(y: i64, m: i64) -> i64 {
88 + match m {
89 + 2 if (y % 4 == 0 && y % 100 != 0) || y % 400 == 0 => 29,
90 + 2 => 28,
91 + 4 | 6 | 9 | 11 => 30,
92 + _ => 31,
93 + }
94 +}
95 +
96 /// Parse a time argument: Unix seconds, or a `YYYY-MM-DD` date taken as
97 /// 00:00 UTC.
98 pub fn parse_time(arg: &str) -> Result<u64, String> {
@@ -95,9 +105,15 @@ pub fn parse_time(arg: &str) -> Result<u64, String> {
105 return Err(format!("{arg}: expected Unix seconds or YYYY-MM-DD"));
106 };
107 let (y, m, d): (i64, i64, i64) = match (y.parse(), m.parse(), d.parse()) {
98 - (Ok(y), Ok(m), Ok(d)) if (1..=12).contains(&m) && (1..=31).contains(&d) => (y, m, d),
108 + (Ok(y), Ok(m), Ok(d)) if (1..=12).contains(&m) && d >= 1 && d <= days_in_month(y, m) => {
109 + (y, m, d)
110 + }
111 _ => return Err(format!("{arg}: expected Unix seconds or YYYY-MM-DD")),
112 };
113 + // 1970 is the Unix epoch. 9999 keeps the arithmetic far from overflow.
114 + if !(1970..=9999).contains(&y) {
115 + return Err(format!("{arg}: the year must be 1970 to 9999"));
116 + }
117 // Days from the civil date (Howard Hinnant's algorithm).
118 let y = if m <= 2 { y - 1 } else { y };
119 let era = y.div_euclid(400);
@@ -133,4 +149,26 @@ mod tests {
149 assert!(parse_time("2026-13-01").is_err());
150 assert!(parse_time("yesterday").is_err());
151 }
152 +
153 + #[test]
154 + fn impossible_dates_are_refused_not_rolled_over() {
155 + assert!(parse_time("2026-02-30").is_err());
156 + assert!(parse_time("2026-04-31").is_err());
157 + assert!(parse_time("2026-02-29").is_err());
158 + assert!(parse_time("2026-11-00").is_err());
159 + // 2024 is a leap year, and 2000 is one while 1900 was not.
160 + assert!(parse_time("2024-02-29").is_ok());
161 + assert!(parse_time("2000-02-29").is_ok());
162 + assert!(parse_time("2100-02-29").is_err());
163 + }
164 +
165 + #[test]
166 + fn years_outside_1970_to_9999_are_refused() {
167 + assert!(parse_time("1969-12-31")
168 + .unwrap_err()
169 + .contains("1970 to 9999"));
170 + assert!(parse_time("10000-01-01").is_err());
171 + assert!(parse_time("9999999999999999-01-01").is_err());
172 + assert!(parse_time("9999-12-31").is_ok());
173 + }
174 }
crates/xiaomi/src/client.rs
+6 -3
@@ -161,13 +161,16 @@ impl XiaomiClient {
161 }
162 let response = request.send().await?;
163 let status = response.status();
164 - let text = response.text().await.unwrap_or_default();
164 if !status.is_success() {
166 - return Err(Error::Http2xx {
165 + // The status is the news here. A body that can't be read only
166 + // costs the detail, so don't let it hide the status.
167 + return Err(Error::HttpStatus {
168 status: status.as_u16(),
168 - detail: text,
169 + detail: response.text().await.unwrap_or_default(),
170 });
171 }
172 + // On success a failed read is a transport error and says so.
173 + let text = response.text().await?;
174 unwrap_envelope(serde_json::from_str(&text)?)
175 }
176
crates/xiaomi/src/error.rs
+1 -1
@@ -25,7 +25,7 @@ pub enum Error {
25
26 /// A non-2xx HTTP status.
27 #[error("GetApps returned HTTP {status}: {detail}")]
28 - Http2xx { status: u16, detail: String },
28 + HttpStatus { status: u16, detail: String },
29
30 /// HTTP 200 with a `code` other than 200 in the envelope. 999 is a header
31 /// or permission problem, 1001/1002/3001/3002 are signature problems.