use std::collections::BTreeMap;
use std::net::IpAddr;
use std::path::PathBuf;
use serde_json::Value;
use crate::Secret;
pub const MAX_WEB_BOARD_ID_CHARS: usize = 40;
pub const MAX_WEB_BOARD_TITLE_CHARS: usize = 60;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct WebBoard {
pub id: String,
pub title: String,
pub source: WebBoardSource,
pub settings: BTreeMap<String, Value>,
pub secrets: BTreeMap<String, Secret>,
pub permissions: BTreeMap<String, bool>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum WebBoardSource {
Url(String),
Folder(PathBuf),
}
pub fn valid_web_board_id(id: &str) -> bool {
!id.is_empty()
&& id.len() <= MAX_WEB_BOARD_ID_CHARS
&& id.starts_with(|c: char| c.is_ascii_lowercase())
&& id
.chars()
.all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == '-')
}
pub fn web_board_url_allowed(url: &url::Url) -> bool {
let plain = url.username().is_empty() && url.password().is_none();
let secure = match url.scheme() {
"https" => url.host().is_some(),
"http" => is_loopback(url),
_ => false,
};
plain && secure
}
fn is_loopback(url: &url::Url) -> bool {
match url.host() {
Some(url::Host::Domain(domain)) => domain.eq_ignore_ascii_case("localhost"),
Some(url::Host::Ipv4(address)) => IpAddr::V4(address).is_loopback(),
Some(url::Host::Ipv6(address)) => IpAddr::V6(address).is_loopback(),
None => false,
}
}
#[cfg(test)]
mod tests {
use super::{valid_web_board_id, web_board_url_allowed};
fn allowed(value: &str) -> bool {
url::Url::parse(value).is_ok_and(|url| web_board_url_allowed(&url))
}
#[test]
fn https_and_local_http_pages_are_allowed() {
for value in [
"https://example.com",
"https://example.com:8443/path?q=1#top",
"http://localhost:5173/",
"http://LOCALHOST:3000",
"http://127.0.0.1:8080",
"http://127.1.2.3",
"http://[::1]:4000/app",
] {
assert!(allowed(value), "{value}");
}
}
#[test]
fn other_schemes_remote_http_and_credentials_are_refused() {
for value in [
"http://example.com",
"http://localhost.example.com",
"http://10.0.0.1",
"https://user:secret@example.com",
"https://user@example.com",
"http://user@localhost:3000",
"file:///etc/passwd",
"javascript:alert(1)",
"data:text/html,<p>hi</p>",
"about:blank",
"ftp://example.com",
] {
assert!(!allowed(value), "{value}");
}
}
#[test]
fn ids_are_short_lower_case_names() {
for id in ["grafana", "my-app", "a1", "x"] {
assert!(valid_web_board_id(id), "{id}");
}
for id in [
"",
"Grafana",
"1app",
"-app",
"my_app",
"my app",
"é",
&"a".repeat(41),
] {
assert!(!valid_web_board_id(id), "{id}");
}
}
}