212 lines
8.0 KiB
Rust
212 lines
8.0 KiB
Rust
// SPDX-License-Identifier: CPAL-1.0
|
|
// Copyright (c) 2026 Marek Novák. Part of Ianitor, under the Common Public Attribution
|
|
// License 1.0: see LICENSE (its Exhibit A is this file's notice).
|
|
|
|
//! The share-link visitor pages (docs/ui-api.md §4): one self-contained HTML page each, inline
|
|
//! CSS, no JavaScript, no external requests, under 8 KB. Only the hostname and a date come
|
|
//! from the request side, both escaped; the token is echoed only into the password form's
|
|
//! hidden field (it is already in the visitor's address bar at that point), escaped too.
|
|
|
|
use ianitor_core::share::PASSWORD_PATH;
|
|
use ianitor_core::time::long_date;
|
|
|
|
pub enum Problem {
|
|
WrongPassword,
|
|
TooMany,
|
|
}
|
|
|
|
pub enum Page<'a> {
|
|
Password {
|
|
host: &'a str,
|
|
token: &'a str,
|
|
until: u64,
|
|
problem: Option<Problem>,
|
|
},
|
|
Expired {
|
|
host: &'a str,
|
|
until: u64,
|
|
},
|
|
Invalid {
|
|
host: &'a str,
|
|
},
|
|
}
|
|
|
|
impl Page<'_> {
|
|
pub fn status(&self) -> u16 {
|
|
match self {
|
|
Page::Password {
|
|
problem: Some(Problem::TooMany),
|
|
..
|
|
} => 429,
|
|
Page::Password { .. } => 401,
|
|
Page::Expired { .. } => 410,
|
|
Page::Invalid { .. } => 403,
|
|
}
|
|
}
|
|
}
|
|
|
|
pub fn escape(s: &str) -> String {
|
|
let mut out = String::with_capacity(s.len());
|
|
for c in s.chars() {
|
|
match c {
|
|
'&' => out.push_str("&"),
|
|
'<' => out.push_str("<"),
|
|
'>' => out.push_str(">"),
|
|
'"' => out.push_str("""),
|
|
'\'' => out.push_str("'"),
|
|
c if c.is_control() => {}
|
|
c => out.push(c),
|
|
}
|
|
}
|
|
out
|
|
}
|
|
|
|
const STYLE: &str = "\
|
|
*{box-sizing:border-box}\
|
|
body{margin:0;min-height:100vh;display:flex;flex-direction:column;align-items:center;justify-content:center;\
|
|
background:#0c0b0f;color:#f1edf3;font-family:system-ui,-apple-system,\"Segoe UI\",Roboto,sans-serif;padding:16px}\
|
|
.card{width:100%;max-width:420px;padding:32px;background:#18151d;border:1px solid #3c3445;border-radius:18px}\
|
|
@media (max-width:480px){.card{padding:24px}}\
|
|
.host{color:#cfc6d4;font-size:.95rem;font-weight:600;margin:0 0 16px;overflow-wrap:anywhere}\
|
|
svg{display:block;margin:0 0 12px}\
|
|
h1{font-size:1.5rem;font-weight:700;color:#f1edf3;margin:0 0 8px}\
|
|
p{font-size:1rem;line-height:1.5;color:#cfc6d4;margin:0 0 16px}\
|
|
label{display:block;color:#cfc6d4;font-size:.9rem;margin:0 0 6px}\
|
|
input[type=password]{width:100%;height:48px;background:#0c0b0f;border:1px solid #7d7189;border-radius:10px;\
|
|
color:#f1edf3;font-size:1rem;padding:0 12px}\
|
|
input[type=password]:focus{outline:none;border-color:#e0a3dc;box-shadow:0 0 0 4px rgba(224,163,220,.16)}\
|
|
.err{color:#e2683a;font-size:.92rem;margin:8px 0 0;display:flex;gap:6px;align-items:center}\
|
|
button{margin-top:16px;width:100%;height:48px;border-radius:12px;border:1px solid #c77cc1;color:#fff;font-weight:700;\
|
|
font-size:1rem;background:linear-gradient(#8d3b89,#76306f 55%,#5c2257)}\
|
|
button:focus{outline:2px solid #f2c9ee;outline-offset:2px}\
|
|
button:disabled{opacity:.5}\
|
|
.small{font-size:.92rem;margin:16px 0 0}\
|
|
a{color:#e0a3dc;display:inline-flex;align-items:center;min-height:44px}\
|
|
footer{color:#a99eb1;font-size:.72rem;letter-spacing:.2em;text-transform:uppercase;margin-top:16px}";
|
|
|
|
fn icon(kind: &str) -> String {
|
|
let (colour, body) = match kind {
|
|
// lock
|
|
"lock" => (
|
|
"#e0a3dc",
|
|
"<rect x=\"8\" y=\"18\" width=\"24\" height=\"16\" rx=\"3\"/><path d=\"M13 18v-5a7 7 0 0 1 14 0v5\"/>",
|
|
),
|
|
// clock
|
|
"clock" => (
|
|
"#d99a35",
|
|
"<circle cx=\"20\" cy=\"20\" r=\"14\"/><path d=\"M20 12v8l5 4\"/>",
|
|
),
|
|
// broken link
|
|
_ => (
|
|
"#d99a35",
|
|
"<path d=\"M17 23l-3 3a5 5 0 0 1-7-7l3-3\"/><path d=\"M23 17l3-3a5 5 0 0 0-7-7l-3 3\"/><path d=\"M8 8l3 3M29 29l3 3M28 12h4M8 28H12\"/>",
|
|
),
|
|
};
|
|
format!(
|
|
"<svg width=\"40\" height=\"40\" viewBox=\"0 0 40 40\" fill=\"none\" stroke=\"{colour}\" stroke-width=\"2.2\" \
|
|
stroke-linecap=\"round\" stroke-linejoin=\"round\" aria-hidden=\"true\">{body}</svg>"
|
|
)
|
|
}
|
|
|
|
const RING: &str = "<svg width=\"14\" height=\"14\" viewBox=\"0 0 14 14\" fill=\"none\" stroke=\"#e2683a\" stroke-width=\"2\" aria-hidden=\"true\"><circle cx=\"7\" cy=\"7\" r=\"5\"/></svg>";
|
|
|
|
fn shell(host: &str, inner: &str, title: &str) -> String {
|
|
format!(
|
|
"<!doctype html><html lang=\"en\"><head><meta charset=\"utf-8\">\
|
|
<meta name=\"viewport\" content=\"width=device-width,initial-scale=1\">\
|
|
<meta name=\"referrer\" content=\"no-referrer\"><meta name=\"robots\" content=\"noindex\">\
|
|
<title>{title}</title><style>{STYLE}</style></head><body><main class=\"card\">\
|
|
<p class=\"host\">{}</p>{inner}</main><footer>Ianitor Domi</footer></body></html>",
|
|
escape(host)
|
|
)
|
|
}
|
|
|
|
pub fn render(p: &Page<'_>) -> String {
|
|
match p {
|
|
Page::Password {
|
|
host,
|
|
token,
|
|
until,
|
|
problem,
|
|
} => {
|
|
let h = escape(host);
|
|
let (msg, disabled) = match problem {
|
|
Some(Problem::WrongPassword) => (
|
|
format!(
|
|
"<p class=\"err\" role=\"alert\">{RING}That password is not right. Check it with the person who sent the link.</p>"
|
|
),
|
|
"",
|
|
),
|
|
Some(Problem::TooMany) => (
|
|
format!(
|
|
"<p class=\"err\" role=\"alert\">{RING}Too many tries. Wait a minute, then try again.</p>"
|
|
),
|
|
" disabled",
|
|
),
|
|
None => (String::new(), ""),
|
|
};
|
|
let inner = format!(
|
|
"{}<h1>This link is protected</h1>\
|
|
<p>Someone shared {h} with you. Enter the password they gave you.</p>\
|
|
<form method=\"post\" action=\"{PASSWORD_PATH}\">\
|
|
<input type=\"hidden\" name=\"t\" value=\"{}\">\
|
|
<label for=\"pw\">Password</label>\
|
|
<input id=\"pw\" type=\"password\" name=\"password\" autocomplete=\"current-password\" autofocus required>\
|
|
{msg}<button type=\"submit\"{disabled}>Open</button></form>\
|
|
<p class=\"small\">The link works until {}.</p>",
|
|
icon("lock"),
|
|
escape(token),
|
|
long_date(*until)
|
|
);
|
|
shell(host, &inner, "This link is protected")
|
|
}
|
|
Page::Expired { host, until } => {
|
|
let inner = format!(
|
|
"{}<h1>This link has expired</h1>\
|
|
<p>It worked until {}. Ask the person who sent it for a new one.</p>\
|
|
<a href=\"/\">Have an account? Sign in</a>",
|
|
icon("clock"),
|
|
long_date(*until)
|
|
);
|
|
shell(host, &inner, "This link has expired")
|
|
}
|
|
Page::Invalid { host } => {
|
|
let inner = format!(
|
|
"{}<h1>This link no longer works</h1>\
|
|
<p>It may have been stopped, or not copied in full. Ask the person who sent it for a new one.</p>\
|
|
<a href=\"/\">Have an account? Sign in</a>",
|
|
icon("broken")
|
|
);
|
|
shell(host, &inner, "This link no longer works")
|
|
}
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn pages_are_small_and_escaped() {
|
|
for p in [
|
|
Page::Password {
|
|
host: "<b>x</b>",
|
|
token: "\"><script>",
|
|
until: 1_794_666_000,
|
|
problem: Some(Problem::WrongPassword),
|
|
},
|
|
Page::Expired {
|
|
host: "a.test",
|
|
until: 0,
|
|
},
|
|
Page::Invalid { host: "a.test" },
|
|
] {
|
|
let html = render(&p);
|
|
assert!(html.len() < 8 * 1024, "{}", html.len());
|
|
assert!(!html.contains("<script"));
|
|
assert!(!html.contains("<b>x"));
|
|
assert!(!html.contains("http://") && !html.contains("https://"));
|
|
}
|
|
}
|
|
}
|