oxedyne/fe2o3/fe2o3_net/tests/origin.rs
9.6 KiB, 1 run
created by r1870400018:68848, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | //! Which posts a browser sent from another site, and which callers want JSON. |
| 2 | //! |
| 3 | //! A post that changes state on a public endpoint is a form anyone can aim a visitor's browser at. |
| 4 | //! `origin::cross_site` reads what the browser itself declares about where the post came from |
| 5 | //! (`Sec-Fetch-Site`, else `Origin`) and says whether to refuse it. A client that is not a browser |
| 6 | //! declares neither, and passes. |
| 7 | //! |
| 8 | //! The table tests build the fields directly. The wire test sends the same repeated lines through |
| 9 | //! the real parser, since a caller chooses how many lines it sends and what case it writes the name in. |
| 10 | //! |
| 11 | //! [Written with AI entirely](https://need2know.ai/entirely-ai/code)\ |
| 12 | //! Anthropic Claude |
| 13 | |
| 14 | use oxedyne_fe2o3_core::prelude::*; |
| 15 | use oxedyne_fe2o3_net::http::{ |
| 16 | fields::{ |
| 17 | HeaderFieldValue, |
| 18 | HeaderFields, |
| 19 | HeaderName, |
| 20 | }, |
| 21 | origin::cross_site, |
| 22 | }; |
| 23 | |
| 24 | const SITE: &str = "https://example.com"; |
| 25 | |
| 26 | /// Fields as the wire parser would hold them: the name read from its lower-case text. |
| 27 | fn fields(lines: &[(&str, &str)]) -> Outcome<HeaderFields> { |
| 28 | let mut f = HeaderFields::default(); |
| 29 | for (name, value) in lines { |
| 30 | let nam = HeaderName::from(*name); |
| 31 | let val = res!(HeaderFieldValue::new(&nam, value)); |
| 32 | f.insert(nam, val, None); |
| 33 | } |
| 34 | Ok(f) |
| 35 | } |
| 36 | |
| 37 | fn refused(lines: &[(&str, &str)]) -> Outcome<bool> { |
| 38 | Ok(cross_site(&res!(fields(lines)), SITE)) |
| 39 | } |
| 40 | |
| 41 | #[test] |
| 42 | fn test_the_fetch_metadata_names_are_known_00() -> Outcome<()> { |
| 43 | for (text, name) in [ |
| 44 | ("sec-fetch-site", HeaderName::SecFetchSite), |
| 45 | ("sec-fetch-mode", HeaderName::SecFetchMode), |
| 46 | ("sec-fetch-dest", HeaderName::SecFetchDest), |
| 47 | ("sec-fetch-user", HeaderName::SecFetchUser), |
| 48 | ] { |
| 49 | assert_eq!(HeaderName::from(text), name, "{} did not parse to its own name", text); |
| 50 | assert_eq!(fmt!("{}", name), text, "the name did not print as {}", text); |
| 51 | } |
| 52 | Ok(()) |
| 53 | } |
| 54 | |
| 55 | #[test] |
| 56 | fn test_a_post_with_neither_header_passes_01() -> Outcome<()> { |
| 57 | assert!(!res!(refused(&[])), "a client declaring nothing is not a browser, and passes"); |
| 58 | assert!(!res!(refused(&[("host", "example.com"), ("accept", "text/html")]))); |
| 59 | Ok(()) |
| 60 | } |
| 61 | |
| 62 | #[test] |
| 63 | fn test_fetch_metadata_passes_only_same_origin_02() -> Outcome<()> { |
| 64 | assert!(!res!(refused(&[("sec-fetch-site", "same-origin")]))); |
| 65 | assert!(!res!(refused(&[("sec-fetch-site", "Same-Origin")])), "the token is not case-sensitive"); |
| 66 | for other in ["cross-site", "same-site", "none", "", "same-origin, cross-site", "nonsense"] { |
| 67 | assert!(res!(refused(&[("sec-fetch-site", other)])), |
| 68 | "Sec-Fetch-Site: {:?} must be refused", other); |
| 69 | } |
| 70 | Ok(()) |
| 71 | } |
| 72 | |
| 73 | #[test] |
| 74 | fn test_fetch_metadata_outranks_origin_03() -> Outcome<()> { |
| 75 | // Where the browser sends both, the fetch metadata decides: it cannot be set from script. |
| 76 | assert!(res!(refused(&[("sec-fetch-site", "cross-site"), ("origin", SITE)]))); |
| 77 | assert!(!res!(refused(&[("sec-fetch-site", "same-origin"), ("origin", "https://evil.example")]))); |
| 78 | Ok(()) |
| 79 | } |
| 80 | |
| 81 | #[test] |
| 82 | fn test_an_origin_alone_must_be_ours_04() -> Outcome<()> { |
| 83 | assert!(!res!(refused(&[("origin", SITE)]))); |
| 84 | assert!(!res!(refused(&[("origin", "HTTPS://Example.COM")])), "scheme and host have no case"); |
| 85 | assert!(!res!(refused(&[("origin", "https://example.com:443")])), "an explicit default port is ours"); |
| 86 | for other in [ |
| 87 | "https://evil.example", |
| 88 | "null", |
| 89 | "", |
| 90 | "http://example.com", // another scheme |
| 91 | "https://example.com:8443", // another port |
| 92 | "https://www.example.com", // another host |
| 93 | "https://example.com.evil.example", // our name as a prefix |
| 94 | "https://example.com@evil.example", // our name as userinfo |
| 95 | "https://evil.example/https://example.com", |
| 96 | "https://example.com/post", // an origin has no path |
| 97 | "file://", |
| 98 | ] { |
| 99 | assert!(res!(refused(&[("origin", other)])), "Origin: {:?} must be refused", other); |
| 100 | } |
| 101 | Ok(()) |
| 102 | } |
| 103 | |
| 104 | #[test] |
| 105 | fn test_an_expected_origin_is_read_as_a_browser_writes_it_05() -> Outcome<()> { |
| 106 | let f = res!(fields(&[("origin", "https://example.com")])); |
| 107 | assert!(!cross_site(&f, "https://example.com/"), "a trailing slash on the configured origin"); |
| 108 | assert!(!cross_site(&f, "https://EXAMPLE.com"), "case on the configured origin"); |
| 109 | let g = res!(fields(&[("origin", "http://localhost:8080")])); |
| 110 | assert!(!cross_site(&g, "http://localhost:8080")); |
| 111 | assert!(cross_site(&g, "http://localhost:9090")); |
| 112 | Ok(()) |
| 113 | } |
| 114 | |
| 115 | #[test] |
| 116 | fn test_every_value_of_a_repeated_header_is_read_06() -> Outcome<()> { |
| 117 | // Neither header is a singleton, so a caller may send several lines; one bad line refuses. |
| 118 | assert!(res!(refused(&[("origin", SITE), ("origin", "https://evil.example")]))); |
| 119 | assert!(res!(refused(&[("origin", "https://evil.example"), ("origin", SITE)]))); |
| 120 | assert!(!res!(refused(&[("origin", SITE), ("origin", SITE)]))); |
| 121 | assert!(res!(refused(&[("sec-fetch-site", "same-origin"), ("sec-fetch-site", "cross-site")]))); |
| 122 | assert!(res!(refused(&[("sec-fetch-site", "cross-site"), ("sec-fetch-site", "same-origin")]))); |
| 123 | assert!(!res!(refused(&[("sec-fetch-site", "same-origin"), ("sec-fetch-site", "same-origin")]))); |
| 124 | Ok(()) |
| 125 | } |
| 126 | |
| 127 | #[test] |
| 128 | fn test_a_site_with_no_origin_of_its_own_is_checked_against_host_07() -> Outcome<()> { |
| 129 | // No configured origin: the host the visitor addressed is the only account of who we are. |
| 130 | for expected in ["", "not a url"] { |
| 131 | let ours = res!(fields(&[("host", "Example.com"), ("origin", "https://example.com")])); |
| 132 | assert!(!cross_site(&ours, expected), "expected {:?}", expected); |
| 133 | let theirs = res!(fields(&[("host", "example.com"), ("origin", "https://evil.example")])); |
| 134 | assert!(cross_site(&theirs, expected), "expected {:?}", expected); |
| 135 | let blind = res!(fields(&[("origin", "https://example.com")])); |
| 136 | assert!(cross_site(&blind, expected), "an Origin that cannot be checked is not trusted"); |
| 137 | let port = res!(fields(&[("host", "example.com:8443"), ("origin", "https://example.com:8443")])); |
| 138 | assert!(!cross_site(&port, expected)); |
| 139 | let wrong = res!(fields(&[("host", "example.com"), ("origin", "https://example.com:8443")])); |
| 140 | assert!(cross_site(&wrong, expected)); |
| 141 | // No Origin and no fetch metadata still passes, whatever we know of ourselves. |
| 142 | assert!(!cross_site(&res!(fields(&[("host", "example.com")])), expected)); |
| 143 | } |
| 144 | Ok(()) |
| 145 | } |
| 146 | |
| 147 | #[test] |
| 148 | fn test_wants_json_reads_every_accept_line_08() -> Outcome<()> { |
| 149 | assert!(res!(fields(&[("accept", "application/json")])).wants_json()); |
| 150 | assert!(res!(fields(&[("accept", "text/html, application/json;q=0.9")])).wants_json()); |
| 151 | assert!(res!(fields(&[("accept", "Application/JSON")])).wants_json()); |
| 152 | assert!(res!(fields(&[("accept", "text/html"), ("accept", "application/json")])).wants_json()); |
| 153 | assert!(!res!(fields(&[("accept", "text/html,application/xhtml+xml,*/*;q=0.8")])).wants_json()); |
| 154 | assert!(!res!(fields(&[("accept", "*/*")])).wants_json()); |
| 155 | assert!(!res!(fields(&[])).wants_json()); |
| 156 | Ok(()) |
| 157 | } |
| 158 | |
| 159 | #[cfg(feature = "async")] |
| 160 | mod wire { |
| 161 | use super::*; |
| 162 | |
| 163 | use oxedyne_fe2o3_net::http::msg::HttpMessage; |
| 164 | |
| 165 | use std::pin::Pin; |
| 166 | |
| 167 | use tokio::io::AsyncWriteExt; |
| 168 | |
| 169 | async fn parse_request(raw: &str) -> Outcome<HttpMessage> { |
| 170 | let (mut near, mut far) = tokio::io::duplex(8192); |
| 171 | let bytes = raw.as_bytes().to_vec(); |
| 172 | tokio::spawn(async move { |
| 173 | let _ = far.write_all(&bytes).await; |
| 174 | let _ = far.flush().await; |
| 175 | }); |
| 176 | let read = HttpMessage::read::<1024, 1024, _>( |
| 177 | Pin::new(&mut near), |
| 178 | &Vec::new(), |
| 179 | Some(true), |
| 180 | None, |
| 181 | ).await; |
| 182 | match res!(read) { |
| 183 | (Some(msg), _) => Ok(msg), |
| 184 | (None, _) => Err(err!("The test request did not parse."; Test, Invalid, Input)), |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | #[tokio::test] |
| 189 | async fn test_a_parsed_post_is_judged_by_what_the_browser_sent_09() -> Outcome<()> { |
| 190 | let head = |extra: &str| fmt!("POST /news/subscribe HTTP/1.1\r\nHost: example.com\r\n\ |
| 191 | Content-Length: 0\r\n{}\r\n", extra); |
| 192 | |
| 193 | let cross = res!(parse_request(&head("Sec-Fetch-Site: cross-site\r\n")).await); |
| 194 | assert!(cross_site(&cross.header.fields, SITE)); |
| 195 | |
| 196 | // The name's case and the number of lines are the sender's choice. |
| 197 | let twice = res!(parse_request(&head( |
| 198 | "ORIGIN: https://example.com\r\norigin: https://evil.example\r\n")).await); |
| 199 | assert!(cross_site(&twice.header.fields, SITE)); |
| 200 | |
| 201 | let same = res!(parse_request(&head("sec-fetch-site: same-origin\r\n")).await); |
| 202 | assert!(!cross_site(&same.header.fields, SITE)); |
| 203 | |
| 204 | let none = res!(parse_request(&head("")).await); |
| 205 | assert!(!cross_site(&none.header.fields, SITE)); |
| 206 | |
| 207 | let json = res!(parse_request(&head("Accept: application/json\r\n")).await); |
| 208 | assert!(json.header.fields.wants_json()); |
| 209 | Ok(()) |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | // F2 B-1: a second hostname of the site, from a browser that sends no `Sec-Fetch-Site`, is the site. |
| 214 | #[test] |
| 215 | fn test_an_alias_host_without_fetch_metadata_is_let_on_09() -> Outcome<()> { |
| 216 | assert!(!res!(refused(&[("host", "www.example.com"), ("origin", "https://www.example.com")])), |
| 217 | "the site's own origin on its second hostname was refused"); |
| 218 | // Only the request's own origin is let on: a foreign page, and the other scheme, still are not. |
| 219 | assert!(res!(refused(&[("host", "www.example.com"), ("origin", "https://evil.test")]))); |
| 220 | assert!(res!(refused(&[("host", "www.example.com"), ("origin", "http://www.example.com")]))); |
| 221 | assert!(res!(refused(&[("host", "example.com"), ("origin", "https://www.example.com")]))); |
| 222 | Ok(()) |
| 223 | } |