Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_steel/tests/admin_dashboard_off.rs

6.3 KiB, 1 run

created by r1870400018:65935, 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 vhost that has switched the dashboard off answers 404 for `/admin` and everything under it.
2//!
3//! The dashboard is one surface for the whole process, and until a vhost could say otherwise it was
4//! served on every name Steel answers to: ontheism.org, a static site with no database and nothing
5//! to administer, showed the operator's login page. Two vhosts go through `handle_https` here, both
6//! backed by a real dashboard state and a web root holding a file at `/admin/page.html`. The one
7//! left at its default serves the dashboard; the one with `admin_dashboard` off serves neither the
8//! dashboard nor the file, for a `GET` or a `POST`, at `/admin`, `/admin/`, and any depth below.
9//! Its neighbours (`/admin.html`, `/administrator`) are not the prefix and are still its own.
10//!
11//! The setting is read from config as a boolean that defaults to on, so every config written
12//! before it existed keeps its dashboard.
13//!
14//! [Written with AI entirely](https://need2know.ai/entirely-ai/code)\
15//! Anthropic Claude
16
17mod vhost_rig;
18
19use vhost_rig::{
20 Rig,
21 Scratch,
22 Site,
23};
24
25use oxedyne_fe2o3_core::prelude::*;
26use oxedyne_fe2o3_jdat::prelude::*;
27use oxedyne_fe2o3_steel::srv::cfg::VhostConfig;
28
29const ON: &str = "dash.test";
30const OFF: &str = "public.test";
31
32#[test]
33fn a_vhost_with_the_dashboard_off_has_no_admin() -> Outcome<()> {
34 let web = res!(Scratch::new("admin_off"));
35 res!(web.put("index.html", "<p>home</p>"));
36 res!(web.put("admin.html", "<p>a page that is merely named admin</p>"));
37 res!(web.put("admin/page.html", "<p>a file under the prefix</p>"));
38 res!(web.put("administrator", "neighbour"));
39 let on = Site::new(ON, &web.0);
40 let mut off = Site::new(OFF, &web.0);
41 off.admin_dashboard = false;
42 let rig = res!(Rig::new(vec![on, off]));
43 let runtime = res!(vhost_rig::runtime());
44 runtime.block_on(async {
45 // The vhost left at its default still serves the dashboard, so a 404 on the other is the
46 // setting and not a rig that never had one.
47 for path in ["/admin", "/admin/", "/admin/login", "/admin/database"] {
48 let r = res!(rig.fetch(ON, "GET", path, &[], "").await);
49 assert_ne!(r.status(), 404, "the default vhost must serve the dashboard at {}: {:?}",
50 path, r);
51 }
52 // The vhost with it off answers 404 at every depth, and the file under the prefix, which
53 // a plain static server would hand over, stays unread.
54 for path in ["/admin", "/admin/", "/admin/login", "/admin/database", "/admin/database/x",
55 "/admin/page.html", "/admin/a/b/c", "/admin?next=/x", "/admin/page.html?x=1"]
56 {
57 let r = res!(rig.fetch(OFF, "GET", path, &[], "").await);
58 assert_eq!(r.status(), 404, "GET {} must be a 404: {:?}", path, r);
59 assert!(!r.body.contains("a file under the prefix"), "GET {} leaked the file: {:?}",
60 path, r);
61 assert!(!r.body.to_lowercase().contains("passphrase"),
62 "GET {} showed a login page: {:?}", path, r);
63 }
64 for path in ["/admin", "/admin/login", "/admin/logout"] {
65 let r = res!(rig.fetch(OFF, "POST", path,
66 &["Content-Type: application/x-www-form-urlencoded"], "passphrase=x").await);
67 assert_eq!(r.status(), 404, "POST {} must be a 404: {:?}", path, r);
68 }
69 let r = res!(rig.fetch(OFF, "HEAD", "/admin", &[], "").await);
70 assert_eq!(r.status(), 404, "HEAD /admin must be a 404: {:?}", r);
71 // What is not under the prefix is untouched.
72 for (path, want) in [("/", "home"), ("/admin.html", "merely named admin"),
73 ("/administrator", "neighbour")]
74 {
75 let r = res!(rig.fetch(OFF, "GET", path, &[], "").await);
76 assert_eq!(r.status(), 200, "GET {} must still be served: {:?}", path, r);
77 assert!(r.body.contains(want), "GET {}: {:?}", path, r);
78 }
79 Ok(())
80 })
81}
82
83/// The static router reads `//admin/page.html`, `/./admin/page.html` and `/x/../admin/page.html`
84/// as `admin/page.html`, so the 404 for the prefix has to be decided on the path as the router
85/// reads it. Tested on the raw text it let each of these through to the file under the prefix.
86#[test]
87fn a_vhost_with_the_dashboard_off_has_no_admin_under_any_spelling_of_the_path() -> Outcome<()> {
88 let web = res!(Scratch::new("admin_off_spelling"));
89 res!(web.put("index.html", "<p>home</p>"));
90 res!(web.put("admin/page.html", "<p>a file under the prefix</p>"));
91 let mut off = Site::new(OFF, &web.0);
92 off.admin_dashboard = false;
93 let rig = res!(Rig::new(vec![off]));
94 let runtime = res!(vhost_rig::runtime());
95 runtime.block_on(async {
96 for path in ["//admin/page.html", "///admin/page.html", "/./admin/page.html",
97 "/x/../admin/page.html", "/admin/../admin/page.html", "//admin", "//admin/",
98 "/./admin", "/x/../admin/login", "//admin/login"]
99 {
100 let r = res!(rig.fetch(OFF, "GET", path, &[], "").await);
101 assert_eq!(r.status(), 404, "GET {} must be a 404: {:?}", path, r);
102 assert!(!r.body.contains("a file under the prefix"), "GET {} leaked the file: {:?}",
103 path, r);
104 }
105 for path in ["//admin/login", "/./admin/login", "/x/../admin/login"] {
106 let r = res!(rig.fetch(OFF, "POST", path,
107 &["Content-Type: application/x-www-form-urlencoded"], "passphrase=x").await);
108 assert_eq!(r.status(), 404, "POST {} must be a 404: {:?}", path, r);
109 }
110 Ok(())
111 })
112}
113
114fn vhost(extra: &str) -> Outcome<VhostConfig> {
115 let text = fmt!("{{\"hostnames\": [\"ontheism.org\"] {}}}", extra);
116 match res!(Dat::decode_string(text)) {
117 Dat::Map(m) => VhostConfig::from_datmap(&m),
118 _ => Err(err!("The test config is not a map."; Test, Invalid)),
119 }
120}
121
122#[test]
123fn admin_dashboard_defaults_to_on_and_must_be_a_boolean() -> Outcome<()> {
124 assert!(res!(vhost("")).admin_dashboard,
125 "a vhost written before the setting existed must keep its dashboard");
126 assert!(res!(vhost(", \"admin_dashboard\": (true)")).admin_dashboard);
127 assert!(!res!(vhost(", \"admin_dashboard\": (false)")).admin_dashboard);
128 assert!(vhost(", \"admin_dashboard\": \"no\"").is_err(),
129 "a non-boolean 'admin_dashboard' must be refused, not read as on");
130 Ok(())
131}