rusteero 2.0.0

Async Rust client for the Eero (Amazon) mesh Wi-Fi cloud API — a port of the Python eero-api library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
//! Shared query-parameter validation and identifier-resolution helpers.
//!
//! Ported from `eero-api`'s `src/eero/api/_params.py` at `v8.0.4`. Small, dependency-free
//! helpers shared by more than one domain module, so that validation rules and
//! identifier-resolution rules have exactly one implementation each rather than one copy per
//! module.

use regex_free_matchers::path_matches_nested_family;
use serde_json::Value;
use url::Url;

use crate::error::Error;
use crate::links::{child_url, resolve_link, resource_url, self_url};
use crate::routes::ApiVersion;
use crate::util::id_from_url;

/// Valid values for a `cadence` query/body parameter, shared by every family that buckets a time
/// series into "daily" or "hourly" points (data usage, insights, data-usage report settings).
///
/// Ported from `CADENCE_VALUES` (`_params.py:25`).
pub const CADENCE_VALUES: &[&str] = &["daily", "hourly"];

/// Renders a single value the way Python's `repr()` renders a `str`: single-quoted, verbatim.
///
/// Every value this crate ever formats this way (mode names, actions, cadences, IP literals) is
/// plain ASCII with no embedded quote or backslash, so this is always exactly Python's `repr()`
/// output for that value — it is not a general-purpose Python `repr()` implementation.
#[must_use]
pub fn py_quote(value: &str) -> String {
    format!("'{value}'")
}

/// Renders `items` as Python's `repr()` renders a `list` of `str`, **sorted** ascending first —
/// every call site that formats a vocabulary this way ports a Python `sorted(...)` call (e.g.
/// `eeros.py:681-683`), so the sort is not optional.
///
/// # Examples
///
/// ```
/// # use rusteero::params::py_list;
/// assert_eq!(py_list(&["b", "a"]), "['a', 'b']");
/// ```
#[must_use]
pub fn py_list(items: &[&str]) -> String {
    let mut sorted: Vec<&str> = items.to_vec();
    sorted.sort_unstable();
    let joined = sorted
        .iter()
        .map(|item| py_quote(item))
        .collect::<Vec<_>>()
        .join(", ");
    format!("[{joined}]")
}

/// Renders `items` as Python's `repr()` renders a `tuple` of `str`, in the exact order given —
/// every call site that formats a vocabulary this way ports a Python `tuple(...)` call over a
/// sequence that is already in the order the message must show (e.g. `_params.py:47-50`'s
/// `tuple(allowed)`), so, unlike [`py_list`], this never sorts.
///
/// # Examples
///
/// ```
/// # use rusteero::params::py_tuple;
/// assert_eq!(py_tuple(&["daily", "hourly"]), "('daily', 'hourly')");
/// ```
#[must_use]
pub fn py_tuple(items: &[&str]) -> String {
    let joined = items
        .iter()
        .map(|item| py_quote(item))
        .collect::<Vec<_>>()
        .join(", ");
    format!("({joined})")
}

/// Validates a `cadence` value against its endpoint's accepted buckets.
///
/// Ported from `validate_cadence` (`_params.py:28-53`).
///
/// # Errors
///
/// Returns [`Error::validation`] with `field: "cadence"` if `value` is not one of `allowed`.
pub fn validate_cadence<'a>(value: &'a str, allowed: &[&str]) -> Result<&'a str, Error> {
    if !allowed.contains(&value) {
        return Err(Error::validation(
            "cadence",
            format!(
                "must be one of {}, got {}",
                py_tuple(allowed),
                py_quote(value)
            ),
        ));
    }
    Ok(value)
}

/// Resolves a network identifier to its absolute base [`Url`].
///
/// The network's own URL ([`self_url`]) is preferred whenever `parent` is supplied and carries
/// one; otherwise the URL is built from `network` — a bare ID, a host-relative path, or an
/// absolute API-host URL — exactly as [`resource_url`] does.
///
/// Ported from `resolve_network_url` (`_params.py:59-98`).
///
/// # Errors
///
/// Returns [`Error::validation`] as [`resource_url`].
pub fn resolve_network_url(
    host: &Url,
    network: &str,
    parent: Option<&Value>,
    version: ApiVersion,
) -> Result<Url, Error> {
    if let Some(parent) = parent
        && let Some(resolved) = self_url(host, parent)?
    {
        return Ok(resolved);
    }
    resource_url(host, network, "networks/{id}", version)
}

/// Resolves a two-level nested resource URL — `networks/{network}/{prefix}/{child}{suffix}` —
/// without double-`str.format`-ing either id.
///
/// Ported from `resolve_nested_url` (`_params.py:101-169`).
///
/// # Errors
///
/// Returns [`Error::validation`] if `child` is not a non-empty string, is a bare id that is not
/// a single path segment, or is a path/URL that does not belong to the addressed network's
/// `prefix` family. As [`resolve_network_url`] for an invalid `network`.
#[allow(clippy::too_many_arguments)]
pub fn resolve_nested_url(
    host: &Url,
    network: &str,
    child: &str,
    prefix: &str,
    suffix: &str,
    link: Option<&str>,
    parent: Option<&Value>,
    version: ApiVersion,
) -> Result<Url, Error> {
    if let (Some(parent), Some(link)) = (parent, link)
        && let Some(resolved) = resolve_link(host, parent, link)?
    {
        return Ok(resolved);
    }

    if child.starts_with("http://") || child.starts_with("https://") || child.starts_with('/') {
        let template = format!("{{id}}{suffix}");
        let resolved = resource_url(host, child, &template, version)?;
        return require_nested_family(&resolved, network, prefix, suffix);
    }
    if child.is_empty() {
        return Err(Error::validation("child", "must be a non-empty string"));
    }

    let network_url = resolve_network_url(host, network, None, version)?;
    let collection = format!("{}/{prefix}", network_url.as_str().trim_end_matches('/'));
    let collection_url = Url::parse(&collection)
        .map_err(|err| Error::validation("child", format!("not a valid URL: {err}")))?;
    let member = child_url(&collection_url, child)?;
    let with_suffix = format!("{}{suffix}", member.as_str());
    Url::parse(&with_suffix)
        .map_err(|err| Error::validation("child", format!("not a valid URL: {err}")))
}

/// Checks that a caller-supplied nested path names the expected resource family:
/// `/<version>/networks/<network>/<prefix>/<child><suffix>` with single-segment ids and nothing
/// after the suffix.
///
/// Ported from `_require_nested_family` (`_params.py:172-215`).
///
/// # Errors
///
/// Returns [`Error::validation`] with `field: "child"` if `url` carries a query or fragment,
/// does not match the expected path shape, or names a different network than `network`.
fn require_nested_family(
    url: &Url,
    network: &str,
    prefix: &str,
    suffix: &str,
) -> Result<Url, Error> {
    if url.query().is_some() || url.fragment().is_some() {
        return Err(Error::validation(
            "child",
            "must not carry a query or fragment",
        ));
    }
    let Some((matched_network, _matched_child)) =
        path_matches_nested_family(url.path(), prefix, suffix)
    else {
        return Err(Error::validation(
            "child",
            format!("must be a path under networks/{{id}}/{prefix} on the API host"),
        ));
    };
    if !network.is_empty() {
        let expected_network = id_from_url(network)?;
        if matched_network != expected_network {
            return Err(Error::validation(
                "child",
                "must belong to the addressed network",
            ));
        }
    }
    Ok(url.clone())
}

/// Checks that a resolved scheduled-pause URL names the expected three-level resource family:
/// `/<version>/networks/<network>/profiles/<profile>/schedules/<schedule>`, with single-segment
/// ids and nothing after the final id.
///
/// Security finding 2 (phase-security-review): `ScheduleApi::update_schedule`/
/// `ScheduleApi::delete_schedule` used to accept *any* same-host path or absolute URL as a
/// schedule's own URL, as long as it resolved via [`crate::links::resource_url`]'s generic
/// `{id}`-template substitution — a bare identifier such as `"networks"`, an unrelated path such
/// as `"/2.2/networks/1"`, or a network's own envelope (whose `url` points at the network, not a
/// schedule) would all resolve to *some* URL and then be sent a `PUT`/`DELETE` without ever being
/// checked to actually be a schedule. This closes that gap the same way
/// [`require_nested_family`] already closes it for every two-level nested resource: after
/// resolution, the URL's path is required to match the schedule family shape below, or the call
/// is rejected before any request is sent.
///
/// # Errors
///
/// Returns [`Error::validation`] with `field: "schedule"` if `url` carries a query or fragment,
/// or its path does not match `networks/{id}/profiles/{id}/schedules/{id}`.
pub(crate) fn require_schedule_family(url: &Url) -> Result<Url, Error> {
    if url.query().is_some() || url.fragment().is_some() {
        return Err(Error::validation(
            "schedule",
            "must not carry a query or fragment",
        ));
    }
    if regex_free_matchers::path_matches_schedule_family(url.path()).is_none() {
        return Err(Error::validation(
            "schedule",
            "must be a schedule path, URL or envelope",
        ));
    }
    Ok(url.clone())
}

/// Hand-written matcher for the fixed path shapes this module validates, standing in for
/// `_params.py`'s compiled regexes (`_require_nested_family`, `_params.py:196-203`, and
/// `schedule.py`'s own URL-shape expectations).
///
/// A regex crate is not among this crate's dependencies (no new dependencies without a stated
/// reason, per this port's conventions); both shapes are fixed and small enough to check by hand.
/// Every id is validated to be `[A-Za-z0-9._:-]+` — the same character class the Python regex
/// enforces inline.
mod regex_free_matchers {
    /// Returns `(network, child)` if `path` matches
    /// `/<digits>.<digits>/networks/<network>/<prefix>/<child><suffix>` with both `network` and
    /// `child` being single-segment identifiers (`[A-Za-z0-9._:-]+`), else `None`.
    pub(super) fn path_matches_nested_family<'a>(
        path: &'a str,
        prefix: &str,
        suffix: &str,
    ) -> Option<(&'a str, &'a str)> {
        let rest = path.strip_prefix('/')?;
        let (version, rest) = rest.split_once('/')?;
        if !is_version_segment(version) {
            return None;
        }
        let rest = rest.strip_prefix("networks/")?;
        let (network, rest) = rest.split_once('/')?;
        let rest = rest.strip_prefix(prefix)?;
        let rest = rest.strip_prefix('/')?;
        let rest = rest.strip_suffix(suffix)?;
        let child = rest;
        if !is_identifier_segment(network) || !is_identifier_segment(child) {
            return None;
        }
        Some((network, child))
    }

    /// Returns `(network, profile, schedule)` if `path` matches
    /// `/<digits>.<digits>/networks/<network>/profiles/<profile>/schedules/<schedule>`, with
    /// every id a single-segment identifier (`[A-Za-z0-9._:-]+`), else `None`.
    pub(super) fn path_matches_schedule_family(path: &str) -> Option<(&str, &str, &str)> {
        let rest = path.strip_prefix('/')?;
        let (version, rest) = rest.split_once('/')?;
        if !is_version_segment(version) {
            return None;
        }
        let rest = rest.strip_prefix("networks/")?;
        let (network, rest) = rest.split_once('/')?;
        let rest = rest.strip_prefix("profiles/")?;
        let (profile, rest) = rest.split_once('/')?;
        let rest = rest.strip_prefix("schedules/")?;
        let schedule = rest;
        if !is_identifier_segment(network)
            || !is_identifier_segment(profile)
            || !is_identifier_segment(schedule)
        {
            return None;
        }
        Some((network, profile, schedule))
    }

    fn is_version_segment(segment: &str) -> bool {
        let Some((major, minor)) = segment.split_once('.') else {
            return false;
        };
        !major.is_empty()
            && !minor.is_empty()
            && major.chars().all(|c| c.is_ascii_digit())
            && minor.chars().all(|c| c.is_ascii_digit())
    }

    fn is_identifier_segment(segment: &str) -> bool {
        !segment.is_empty()
            && segment
                .chars()
                .all(|c| c.is_ascii_alphanumeric() || matches!(c, '.' | '_' | ':' | '-'))
    }
}

#[cfg(test)]
mod tests {
    use super::{
        CADENCE_VALUES, require_schedule_family, resolve_nested_url, resolve_network_url,
        validate_cadence,
    };
    use crate::error::Error;
    use crate::routes::ApiVersion;
    use serde_json::json;
    use url::Url;

    fn host() -> Url {
        Url::parse("http://mock.test:1234").unwrap()
    }

    // ===================== TestValidateCadence =====================

    #[test]
    fn validate_cadence_accepts_default_values() {
        assert_eq!(validate_cadence("daily", CADENCE_VALUES).unwrap(), "daily");
        assert_eq!(
            validate_cadence("hourly", CADENCE_VALUES).unwrap(),
            "hourly"
        );
    }

    #[test]
    fn validate_cadence_rejects_unknown_value() {
        let err = validate_cadence("weekly", CADENCE_VALUES).unwrap_err();
        assert!(matches!(err, Error::Validation { field, .. } if field == "cadence"));
    }

    #[test]
    fn validate_cadence_honours_a_custom_allowed_set() {
        assert_eq!(
            validate_cadence("weekly", &["daily", "hourly", "weekly"]).unwrap(),
            "weekly"
        );
    }

    // ===================== TestResolveNetworkUrl =====================

    #[test]
    fn resolve_network_url_prefers_parents_self_url() {
        let parent = json!({"url": "/2.4/networks/100"});
        let url = resolve_network_url(&host(), "100", Some(&parent), ApiVersion::V2_2).unwrap();
        assert_eq!(url.as_str(), "http://mock.test:1234/2.4/networks/100");
    }

    #[test]
    fn resolve_network_url_falls_back_to_bare_id() {
        let url = resolve_network_url(&host(), "100", None, ApiVersion::V2_2).unwrap();
        assert_eq!(url.as_str(), "http://mock.test:1234/2.2/networks/100");
    }

    // ===================== TestResolveNestedUrlChildValidation =====================

    #[test]
    fn resolve_nested_url_bare_child_id_is_appended() {
        let url = resolve_nested_url(
            &host(),
            "100",
            "p1",
            "profiles",
            "",
            None,
            None,
            ApiVersion::V2_2,
        )
        .unwrap();
        assert_eq!(
            url.as_str(),
            "http://mock.test:1234/2.2/networks/100/profiles/p1"
        );
    }

    #[test]
    fn resolve_nested_url_empty_child_is_rejected() {
        let err = resolve_nested_url(
            &host(),
            "100",
            "",
            "profiles",
            "",
            None,
            None,
            ApiVersion::V2_2,
        )
        .unwrap_err();
        assert!(matches!(err, Error::Validation { field, .. } if field == "child"));
    }

    #[test]
    fn resolve_nested_url_prefers_named_link_on_parent() {
        let parent = json!({"resources": {"schedules": "/2.5/networks/100/profiles/p1/schedules"}});
        let url = resolve_nested_url(
            &host(),
            "100",
            "p1",
            "profiles",
            "",
            Some("schedules"),
            Some(&parent),
            ApiVersion::V2_2,
        )
        .unwrap();
        assert_eq!(
            url.as_str(),
            "http://mock.test:1234/2.5/networks/100/profiles/p1/schedules"
        );
    }

    // ===================== TestResolveNestedUrlPathChildFamily =====================

    #[test]
    fn resolve_nested_url_path_child_must_belong_to_the_addressed_network() {
        let err = resolve_nested_url(
            &host(),
            "100",
            "/2.2/networks/200/profiles/p1",
            "profiles",
            "",
            None,
            None,
            ApiVersion::V2_2,
        )
        .unwrap_err();
        assert!(matches!(err, Error::Validation { field, .. } if field == "child"));
    }

    #[test]
    fn resolve_nested_url_path_child_matching_network_is_accepted() {
        let url = resolve_nested_url(
            &host(),
            "100",
            "/2.2/networks/100/profiles/p1",
            "profiles",
            "",
            None,
            None,
            ApiVersion::V2_2,
        )
        .unwrap();
        assert_eq!(
            url.as_str(),
            "http://mock.test:1234/2.2/networks/100/profiles/p1"
        );
    }

    #[test]
    fn resolve_nested_url_path_child_wrong_family_is_rejected() {
        let err = resolve_nested_url(
            &host(),
            "100",
            "/2.2/account",
            "profiles",
            "",
            None,
            None,
            ApiVersion::V2_2,
        )
        .unwrap_err();
        assert!(matches!(err, Error::Validation { field, .. } if field == "child"));
    }

    #[test]
    fn resolve_nested_url_child_with_query_is_rejected() {
        // Security finding 3 moved this rejection earlier: `resource_url`'s own path branch (via
        // `links::validate_link_path`) now rejects the query string before `require_nested_family`
        // ever runs, so the error surfaces as `field: "link"` rather than `field: "child"` — still
        // `Error::Validation`, just caught one layer sooner. See `links.rs`'s own
        // `resource_url_path_prefixed_id_with_query_is_rejected` for the same fix in isolation.
        let err = resolve_nested_url(
            &host(),
            "100",
            "/2.2/networks/100/profiles/p1?x=1",
            "profiles",
            "",
            None,
            None,
            ApiVersion::V2_2,
        )
        .unwrap_err();
        assert!(matches!(err, Error::Validation { field, .. } if field == "link"));
    }

    #[test]
    fn resolve_nested_url_with_suffix() {
        let url = resolve_nested_url(
            &host(),
            "100",
            "aa:bb:cc",
            "insights/devices",
            "/history",
            None,
            None,
            ApiVersion::V2_2,
        )
        .unwrap();
        assert_eq!(
            url.as_str(),
            "http://mock.test:1234/2.2/networks/100/insights/devices/aa:bb:cc/history"
        );
    }

    // ===================== require_schedule_family (security finding 2) =====================

    #[test]
    fn require_schedule_family_accepts_a_genuine_schedule_path() {
        let url =
            Url::parse("http://mock.test:1234/2.2/networks/100/profiles/p1/schedules/s1").unwrap();
        assert_eq!(require_schedule_family(&url).unwrap(), url);
    }

    #[test]
    fn require_schedule_family_rejects_a_bare_networks_collection_path() {
        let url = Url::parse("http://mock.test:1234/2.2/networks").unwrap();
        let err = require_schedule_family(&url).unwrap_err();
        assert!(matches!(err, Error::Validation { field, .. } if field == "schedule"));
    }

    #[test]
    fn require_schedule_family_rejects_an_unrelated_network_path() {
        let url = Url::parse("http://mock.test:1234/2.2/networks/1").unwrap();
        let err = require_schedule_family(&url).unwrap_err();
        assert!(matches!(err, Error::Validation { field, .. } if field == "schedule"));
    }

    #[test]
    fn require_schedule_family_rejects_a_query_or_fragment() {
        let url = Url::parse("http://mock.test:1234/2.2/networks/100/profiles/p1/schedules/s1?x=1")
            .unwrap();
        let err = require_schedule_family(&url).unwrap_err();
        assert!(matches!(err, Error::Validation { field, .. } if field == "schedule"));

        let url =
            Url::parse("http://mock.test:1234/2.2/networks/100/profiles/p1/schedules/s1#frag")
                .unwrap();
        let err = require_schedule_family(&url).unwrap_err();
        assert!(matches!(err, Error::Validation { field, .. } if field == "schedule"));
    }

    #[test]
    fn require_schedule_family_rejects_trailing_extra_segments() {
        let url =
            Url::parse("http://mock.test:1234/2.2/networks/100/profiles/p1/schedules/s1/extra")
                .unwrap();
        let err = require_schedule_family(&url).unwrap_err();
        assert!(matches!(err, Error::Validation { field, .. } if field == "schedule"));
    }
}