Skip to main content

agentmux_common/
url_guard.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! The origin of an address, asked through one guard.
5//!
6//! A `blob:` address carries another address (`blob:https://example.com/<id>`), and its origin is the
7//! carried address's. An address that carries another `blob:` address has no origin here: it is
8//! refused, so the answer is "no origin", and every caller treats that as "not allowed". Every place
9//! that decides something from an address's origin asks through [`checked_origin`] (or
10//! [`origin_string`]), never through `Url::origin` directly.
11
12use url::{Origin, Url};
13
14/// Whether `url` is a `blob:` address whose carried address is itself a `blob:` address.
15///
16/// The carried address is read once, the way `Url::origin` reads it, so the two never disagree about
17/// what is carried; nothing here follows a chain.
18pub fn is_nested_blob(url: &Url) -> bool {
19    url.scheme() == "blob" && Url::parse(url.path()).is_ok_and(|inner| inner.scheme() == "blob")
20}
21
22/// `url`'s origin, or `None` for an address this guard refuses (a nested `blob:` address).
23pub fn checked_origin(url: &Url) -> Option<Origin> {
24    if is_nested_blob(url) {
25        return None;
26    }
27    Some(url.origin())
28}
29
30/// [`checked_origin`] as `scheme://host[:port]` (`null` for an opaque origin), or `None` for a refused address.
31pub fn origin_string(url: &Url) -> Option<String> {
32    checked_origin(url).map(|origin| origin.ascii_serialization())
33}
34
35#[cfg(test)]
36mod tests {
37    use super::*;
38
39    fn url(s: &str) -> Url {
40        Url::parse(s).unwrap()
41    }
42
43    /// `blob:` repeated `depth` times in front of `rest`.
44    fn nested(depth: usize, rest: &str) -> String {
45        format!("{}{rest}", "blob:".repeat(depth))
46    }
47
48    #[test]
49    fn plain_addresses_keep_their_origin() {
50        assert_eq!(origin_string(&url("https://example.com/a?b#c")).as_deref(), Some("https://example.com"));
51        assert_eq!(origin_string(&url("http://localhost:3000/x")).as_deref(), Some("http://localhost:3000"));
52        assert_eq!(origin_string(&url("https://example.com:443/")).as_deref(), Some("https://example.com"));
53        assert_eq!(origin_string(&url("about:blank")).as_deref(), Some("null"));
54        assert_eq!(origin_string(&url("data:text/plain,hi")).as_deref(), Some("null"));
55    }
56
57    #[test]
58    fn an_ordinary_blob_address_keeps_the_origin_it_carries() {
59        assert_eq!(origin_string(&url("blob:https://example.com/3f2a")).as_deref(), Some("https://example.com"));
60        assert_eq!(origin_string(&url("blob:http://localhost:8080/3f2a")).as_deref(), Some("http://localhost:8080"));
61        // A blob that carries no origin keeps having none.
62        assert_eq!(origin_string(&url("blob:null/3f2a")).as_deref(), Some("null"));
63        assert_eq!(origin_string(&url("blob:not-an-address")).as_deref(), Some("null"));
64        assert_eq!(origin_string(&url("blob:data:text/plain,hi")).as_deref(), Some("null"));
65        assert!(!is_nested_blob(&url("blob:https://example.com/3f2a")));
66    }
67
68    #[test]
69    fn a_blob_address_that_carries_a_blob_address_has_no_origin() {
70        for text in [
71            nested(2, "https://example.com/3f2a"),
72            nested(3, "https://example.com/3f2a"),
73            "BLOB:Blob:https://example.com/3f2a".to_string(),
74            "blob:\tblob:https://example.com/3f2a".to_string(),
75            "blob: blob:https://example.com/3f2a".to_string(),
76            nested(2, ""),
77        ] {
78            let u = url(&text);
79            assert!(is_nested_blob(&u), "{text:?}");
80            assert_eq!(checked_origin(&u), None, "{text:?}");
81            assert_eq!(origin_string(&u), None, "{text:?}");
82        }
83    }
84
85    #[test]
86    fn deeply_nested_input_is_refused_and_the_stack_holds() {
87        // Large enough that following the chain would not fit in a thread's stack.
88        for depth in [100, 10_000, 1_000_000] {
89            let u = url(&nested(depth, "https://example.com/3f2a"));
90            assert_eq!(checked_origin(&u), None, "depth {depth}");
91        }
92        // The same on a thread with a small stack: the guard reads one level and stops.
93        let handle = std::thread::Builder::new()
94            .stack_size(64 * 1024)
95            .spawn(|| origin_string(&url(&nested(1_000_000, "https://example.com/3f2a"))))
96            .unwrap();
97        assert_eq!(handle.join().unwrap(), None);
98    }
99
100    #[test]
101    fn a_blob_address_inside_another_scheme_is_not_a_nested_blob() {
102        let u = url("https://example.com/blob:https://other.example/x");
103        assert_eq!(origin_string(&u).as_deref(), Some("https://example.com"));
104        assert!(!is_nested_blob(&u));
105    }
106}