oxedyne/fe2o3/fe2o3_steel/tests/publish_origin.rs
7.1 KiB, 1 run
created by r1870400018:68858, 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 | //! A public write that a browser sent from another site is refused, and a sign-up says one thing. |
| 2 | //! |
| 3 | //! These call `AppWebHandler::handle_post` as the server does, against a real Ozone instance. They |
| 4 | //! do not skip when no database starts: a skipped test cannot be shown red. |
| 5 | //! |
| 6 | //! [Written with AI entirely](https://need2know.ai/entirely-ai/code)\ |
| 7 | //! Anthropic Claude |
| 8 | |
| 9 | use oxedyne_fe2o3_core::{ |
| 10 | file::OsPath, |
| 11 | prelude::*, |
| 12 | }; |
| 13 | use oxedyne_fe2o3_net::http::{ |
| 14 | fields::{ |
| 15 | HeaderFieldValue, |
| 16 | HeaderFields, |
| 17 | HeaderName, |
| 18 | }, |
| 19 | handler::WebHandler, |
| 20 | header::HttpHeadline, |
| 21 | loc::HttpLocator, |
| 22 | msg::HttpMessage, |
| 23 | status::HttpStatus, |
| 24 | }; |
| 25 | use oxedyne_fe2o3_steel::{ |
| 26 | app::https::AppWebHandler, |
| 27 | srv::{ |
| 28 | api::ApiHandlerRegistry, |
| 29 | cfg::ServerConfig, |
| 30 | id, |
| 31 | publish::{ |
| 32 | PublishConfig, |
| 33 | Source, |
| 34 | rate::Window, |
| 35 | send::MailSender, |
| 36 | subscribe::{ |
| 37 | self, |
| 38 | SubState, |
| 39 | }, |
| 40 | }, |
| 41 | webhook::WebhookRegistry, |
| 42 | }, |
| 43 | }; |
| 44 | |
| 45 | use std::{ |
| 46 | collections::HashMap, |
| 47 | net::SocketAddr, |
| 48 | sync::Arc, |
| 49 | }; |
| 50 | |
| 51 | mod common; |
| 52 | |
| 53 | const SITE: &str = "https://site.test"; |
| 54 | |
| 55 | type Web = AppWebHandler<HashMap<String, OsPath>>; |
| 56 | |
| 57 | fn web() -> Outcome<Web> { |
| 58 | let cfg = PublishConfig { |
| 59 | path: fmt!("/news"), |
| 60 | base_url: fmt!("{}", SITE), |
| 61 | comments: true, |
| 62 | source: Source::Store, |
| 63 | ..Default::default() |
| 64 | }; |
| 65 | let sender = res!(MailSender::new("mail.site.test".to_string(), Vec::new(), "news@site.test".to_string())); |
| 66 | Ok(AppWebHandler::new( |
| 67 | ServerConfig::default(), |
| 68 | std::env::temp_dir(), |
| 69 | HashMap::new(), |
| 70 | vec![fmt!("index.html")], |
| 71 | true, |
| 72 | Vec::new(), |
| 73 | Vec::new(), |
| 74 | Arc::new(WebhookRegistry::new()), |
| 75 | Arc::new(ApiHandlerRegistry::new()), |
| 76 | None, |
| 77 | None, |
| 78 | None, |
| 79 | Some(Arc::new(cfg)), |
| 80 | Some(Arc::new(sender)), |
| 81 | Arc::new(Vec::new()), |
| 82 | )) |
| 83 | } |
| 84 | |
| 85 | // A POST as the server hands it to the handler: the path and query, the lines a browser (or not) |
| 86 | // sent, and a form body. |
| 87 | async fn post( |
| 88 | web: &Web, |
| 89 | handle: &(Arc<std::sync::RwLock<common::TestDb>>, id::Uid), |
| 90 | target: &str, |
| 91 | lines: &[(&str, &str)], |
| 92 | body: &str, |
| 93 | ) |
| 94 | -> Outcome<HttpMessage> |
| 95 | { |
| 96 | let mut fields = HeaderFields::default(); |
| 97 | for (name, value) in lines { |
| 98 | let nam = HeaderName::from(*name); |
| 99 | let val = res!(HeaderFieldValue::new(&nam, value)); |
| 100 | fields.insert(nam, val, None); |
| 101 | } |
| 102 | let peer: SocketAddr = res!("127.0.0.1:50000".parse(), Network, Init); |
| 103 | let sid: Option<id::Sid> = None; |
| 104 | let got = res!(web.handle_post( |
| 105 | res!(HttpLocator::new(target)), |
| 106 | None, |
| 107 | body.as_bytes().to_vec(), |
| 108 | Arc::new(fields), |
| 109 | Some((handle.0.clone(), handle.1.clone())), |
| 110 | &sid, |
| 111 | peer, |
| 112 | &fmt!("test"), |
| 113 | ).await); |
| 114 | got.ok_or_else(|| err!("the handler answered nothing for {}", target; Test, Missing)) |
| 115 | } |
| 116 | |
| 117 | fn status_of(resp: &HttpMessage) -> Option<HttpStatus> { |
| 118 | match &resp.header.headline { |
| 119 | HttpHeadline::Response { status } => Some(*status), |
| 120 | _ => None, |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | const SUBSCRIBE: &str = "/news/subscribe"; |
| 125 | |
| 126 | #[tokio::test] |
| 127 | async fn test_a_cross_site_subscribe_is_refused_and_stores_nothing_00() -> Outcome<()> { |
| 128 | let (db, uid, _tmp) = res!(common::test_db()); |
| 129 | let handle = (db, uid); |
| 130 | let web = res!(web()); |
| 131 | let resp = res!(post(&web, &handle, SUBSCRIBE, &[("sec-fetch-site", "cross-site")], |
| 132 | "email=reader%40nowhere.invalid").await); |
| 133 | assert_eq!(status_of(&resp), Some(HttpStatus::Forbidden), "a cross-site sign-up was not refused"); |
| 134 | assert_eq!(res!(subscribe::count(&handle, "test")), 0, "a refused sign-up stored a subscriber"); |
| 135 | Ok(()) |
| 136 | } |
| 137 | |
| 138 | #[tokio::test] |
| 139 | async fn test_a_same_origin_subscribe_passes_01() -> Outcome<()> { |
| 140 | let (db, uid, _tmp) = res!(common::test_db()); |
| 141 | let handle = (db, uid); |
| 142 | let web = res!(web()); |
| 143 | // A malformed address reaches the handler and is told so, without a word on the network. |
| 144 | let resp = res!(post(&web, &handle, SUBSCRIBE, &[("sec-fetch-site", "same-origin")], |
| 145 | "email=not-an-address").await); |
| 146 | assert_eq!(status_of(&resp), Some(HttpStatus::OK), "a same-origin sign-up was refused"); |
| 147 | assert!(resp.body_as_string().contains("does not look like an email"), |
| 148 | "the sign-up did not reach its handler: {}", resp.body_as_string()); |
| 149 | Ok(()) |
| 150 | } |
| 151 | |
| 152 | #[tokio::test] |
| 153 | async fn test_a_cross_site_comment_is_refused_02() -> Outcome<()> { |
| 154 | let (db, uid, _tmp) = res!(common::test_db()); |
| 155 | let handle = (db, uid); |
| 156 | let web = res!(web()); |
| 157 | let resp = res!(post(&web, &handle, "/news/a-post/comment", &[("origin", "https://evil.example")], |
| 158 | "name=x&body=y").await); |
| 159 | assert_eq!(status_of(&resp), Some(HttpStatus::Forbidden), "a cross-site comment was not refused"); |
| 160 | Ok(()) |
| 161 | } |
| 162 | |
| 163 | #[tokio::test] |
| 164 | async fn test_an_unsubscribe_is_never_refused_03() -> Outcome<()> { |
| 165 | let (db, uid, _tmp) = res!(common::test_db()); |
| 166 | let handle = (db, uid); |
| 167 | let web = res!(web()); |
| 168 | let sub = match res!(subscribe::add_pending(&handle, "reader@nowhere.invalid", &Window::default(), 0)) { |
| 169 | Some(s) => s, |
| 170 | None => return Err(err!("the reader did not pend"; Test, Missing)), |
| 171 | }; |
| 172 | res!(subscribe::confirm(&handle, &sub.token, "test")); |
| 173 | // A mail client's one-click post carries no `Origin`; even one that does is not refused. |
| 174 | let target = fmt!("/news/unsubscribe?token={}", sub.token); |
| 175 | let resp = res!(post(&web, &handle, &target, |
| 176 | &[("sec-fetch-site", "cross-site"), ("origin", "https://mail.example")], "").await); |
| 177 | assert_eq!(status_of(&resp), Some(HttpStatus::OK), "an unsubscribe was refused"); |
| 178 | match res!(subscribe::get(&handle, "reader@nowhere.invalid")) { |
| 179 | Some(s) => assert_eq!(s.state, SubState::Unsubscribed, "the unsubscribe did not take"), |
| 180 | None => return Err(err!("the reader vanished"; Test, Missing)), |
| 181 | } |
| 182 | Ok(()) |
| 183 | } |
| 184 | |
| 185 | #[tokio::test] |
| 186 | async fn test_a_new_and_a_confirmed_address_get_the_same_json_04() -> Outcome<()> { |
| 187 | let (db, uid, _tmp) = res!(common::test_db()); |
| 188 | let handle = (db, uid); |
| 189 | let web = res!(web()); |
| 190 | let sub = match res!(subscribe::add_pending(&handle, "known@nowhere.invalid", &Window::default(), 0)) { |
| 191 | Some(s) => s, |
| 192 | None => return Err(err!("the reader did not pend"; Test, Missing)), |
| 193 | }; |
| 194 | res!(subscribe::confirm(&handle, &sub.token, "test")); |
| 195 | let lines = [("sec-fetch-site", "same-origin"), ("accept", "application/json")]; |
| 196 | let fresh = res!(post(&web, &handle, SUBSCRIBE, &lines, "email=fresh%40nowhere.invalid").await); |
| 197 | let known = res!(post(&web, &handle, SUBSCRIBE, &lines, "email=known%40nowhere.invalid").await); |
| 198 | assert_eq!(status_of(&fresh), Some(HttpStatus::OK)); |
| 199 | assert_eq!(status_of(&known), Some(HttpStatus::OK)); |
| 200 | assert!(fresh.body_as_string().contains("\"said\": \"sent\""), |
| 201 | "a new address was not told sent: {}", fresh.body_as_string()); |
| 202 | assert_eq!(fresh.body, known.body, "a new and a confirmed address were told apart"); |
| 203 | Ok(()) |
| 204 | } |