agentmux_common/
url_guard.rs1use url::{Origin, Url};
13
14pub fn is_nested_blob(url: &Url) -> bool {
19 url.scheme() == "blob" && Url::parse(url.path()).is_ok_and(|inner| inner.scheme() == "blob")
20}
21
22pub fn checked_origin(url: &Url) -> Option<Origin> {
24 if is_nested_blob(url) {
25 return None;
26 }
27 Some(url.origin())
28}
29
30pub 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 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 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 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 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}