Extract the wayBetter framework core from wayBetter for X

Site-agnostic parts of the X app, behind a small site adapter:

- crates/waybetter-host: webview-label security policy and tab rect
  clamp, account profiles with native-owned accounts (AccountRules),
  plugin store with bundled-plugin upgrades (PluginHost), signed
  data-only hotfix channel, update scheduling and crash handshake, tab
  payloads/link routing/tab-chrome validation, and (feature "tauri")
  webview helpers and the Windows system menu.
- packages/runtime: DOM hub, request hub, plugin API core, OKLab theme
  color model, split-view rail hiding, link routing and account
  reporting, each configured by a SiteAdapter.
- packages/shell: address bar, pane, link-drop, profile, palette and
  update-chip models, and window controls with the Tauri API injected.

Tests moved/adapted from the app: 45 Rust (headless), 40 TypeScript.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
CRTkafa 2026-09-28 03:01:33 +07:00
commit 41267a22fd
48 changed files with 9768 additions and 0 deletions

View file

@ -0,0 +1,197 @@
//! The host's security boundary. Capabilities are keyed by webview label:
//! - `shell` (local app assets only) may call app, settings and tab-management commands;
//! - `tab-<id>` webviews may call only tab-chrome commands about themselves ([`tab_id`]), and
//! site-only commands while they are exactly on the site's origin ([`require_site_tab`]);
//! - nothing a page sends can name another webview, profile or file.
use crate::site::SiteSpec;
use url::Url;
/// The native window.
pub const MAIN_LABEL: &str = "main";
/// The trusted local shell webview (tabs, address bars, app UI).
pub const SHELL_LABEL: &str = "shell";
/// Browser-tab webviews are labelled `tab-<id>`.
pub const TAB_PREFIX: &str = "tab-";
/// The shell's Vite dev server, allowed only in debug builds.
pub const DEV_PORT: u16 = 1420;
pub fn valid_tab_id(id: &str) -> bool {
!id.is_empty() && id.len() <= 40 && id.bytes().all(|ch| ch.is_ascii_alphanumeric() || ch == b'-')
}
/// The tab id of a tab webview label, if `label` is one.
pub fn tab_id(label: &str) -> Option<&str> {
label.strip_prefix(TAB_PREFIX).filter(|id| valid_tab_id(id))
}
/// The webview label of tab `id`.
pub fn tab_label(id: &str) -> String {
format!("{TAB_PREFIX}{id}")
}
/// Titlebar height in logical px. A tab webview must never cover y < TITLEBAR_H.
pub const TITLEBAR_H: f64 = 44.0;
/// Outer resize-grip band, in logical px, reserved while the window is not maximized.
pub const RESIZE_BORDER: f64 = 5.0;
/// Clamps a requested tab rectangle so it never overlaps the custom titlebar row or (while
/// restored) the outer resize band, and rejects non-finite input. The insets apply only when the
/// window is `frameless` (no OS decorations); a decorated window has native chrome and resize
/// edges, so the rect is only clipped to the window. Returns `Ok(None)` when nothing of the rect
/// is left, in which case the caller hides the webview.
#[allow(clippy::too_many_arguments)]
pub fn clamp_tab_rect(
x: f64,
y: f64,
width: f64,
height: f64,
win_width: f64,
win_height: f64,
maximized: bool,
frameless: bool,
) -> Result<Option<(f64, f64, f64, f64)>, &'static str> {
if ![x, y, width, height, win_width, win_height].iter().all(|v| v.is_finite()) {
return Err("VALIDATION: non-finite rect");
}
let (top, inset) = match (frameless, maximized) {
(false, _) => (0.0, 0.0),
(true, true) => (TITLEBAR_H, 0.0),
(true, false) => (TITLEBAR_H, RESIZE_BORDER),
};
let left = x.max(inset);
let upper = y.max(top);
let right = (x + width).min(win_width - inset);
let bottom = (y + height).min(win_height - inset);
if right - left < 1.0 || bottom - upper < 1.0 {
return Ok(None);
}
Ok(Some((left, upper, right - left, bottom - upper)))
}
pub fn valid_identifier(value: &str) -> bool {
!value.is_empty()
&& value.len() <= 128
&& value
.bytes()
.all(|ch| ch.is_ascii_alphanumeric() || b"_-:.".contains(&ch))
}
/// No user name or password in the URL.
pub fn plain_origin(url: &Url) -> bool {
url.username().is_empty() && url.password().is_none()
}
/// The shell's own documents: bundled app assets, or the dev server in debug builds.
pub fn local_navigation_allowed(url: &Url, development: bool) -> bool {
if !plain_origin(url) {
return false;
}
match (url.scheme(), url.host_str(), url.port()) {
("tauri", Some("localhost"), None) => true,
("http" | "https", Some("tauri.localhost"), None) => true,
("http", Some("localhost" | "127.0.0.1"), Some(DEV_PORT)) => development,
_ => false,
}
}
/// Shell-only commands: the `shell` webview on a local document.
pub fn require_local(label: &str, url: &Url, development: bool) -> Result<(), String> {
if label == SHELL_LABEL && local_navigation_allowed(url, development) {
Ok(())
} else {
Err("FORBIDDEN: trusted local workspace required".into())
}
}
/// Site-only commands: a tab webview that is exactly on the site's origin right now.
pub fn require_site_tab(site: &SiteSpec, label: &str, url: &Url) -> Result<(), String> {
if tab_id(label).is_some() && site.is_origin(url) {
Ok(())
} else {
Err("FORBIDDEN: exact site origin required".into())
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::site::testing::SITE;
#[test]
fn clamp_tab_rect_restricts_titlebar_and_border() {
let (x, y, w, h) = clamp_tab_rect(0.0, 0.0, 800.0, 600.0, 1000.0, 700.0, false, true).unwrap().unwrap();
assert_eq!((x, y), (RESIZE_BORDER, TITLEBAR_H));
assert_eq!((w, h), (800.0 - RESIZE_BORDER, 600.0 - TITLEBAR_H));
}
#[test]
fn clamp_tab_rect_negative_and_titlebar_y() {
let (_, y, _, _) = clamp_tab_rect(10.0, -50.0, 100.0, 100.0, 1000.0, 700.0, false, true).unwrap().unwrap();
assert_eq!(y, TITLEBAR_H);
let (_, y, _, h) = clamp_tab_rect(10.0, 20.0, 100.0, 100.0, 1000.0, 700.0, false, true).unwrap().unwrap();
assert_eq!((y, h), (TITLEBAR_H, 76.0));
}
#[test]
fn clamp_tab_rect_maximized_has_no_border_inset() {
let rect = clamp_tab_rect(0.0, 0.0, 1000.0, 700.0, 1000.0, 700.0, true, true).unwrap();
assert_eq!(rect, Some((0.0, TITLEBAR_H, 1000.0, 700.0 - TITLEBAR_H)));
}
#[test]
fn clamp_tab_rect_decorated_window_is_not_inset() {
let rect = clamp_tab_rect(0.0, 0.0, 1000.0, 700.0, 1000.0, 700.0, false, false).unwrap();
assert_eq!(rect, Some((0.0, 0.0, 1000.0, 700.0)));
}
#[test]
fn clamp_tab_rect_shell_layout_passes_unchanged() {
// The shell insets the dock by RESIZE_BORDER and starts panes below TITLEBAR_H + toolbar.
let rect = clamp_tab_rect(5.0, 94.0, 990.0, 601.0, 1000.0, 700.0, false, true).unwrap();
assert_eq!(rect, Some((5.0, 94.0, 990.0, 601.0)));
}
#[test]
fn clamp_tab_rect_rejects_non_finite() {
assert!(clamp_tab_rect(f64::NAN, 0.0, 100.0, 100.0, 1000.0, 700.0, false, true).is_err());
assert!(clamp_tab_rect(0.0, f64::INFINITY, 100.0, 100.0, 1000.0, 700.0, false, true).is_err());
}
#[test]
fn clamp_tab_rect_empty_result_is_none() {
assert_eq!(clamp_tab_rect(999.0, 699.0, 50.0, 50.0, 1000.0, 700.0, false, true).unwrap(), None);
assert_eq!(clamp_tab_rect(10.0, 0.0, 100.0, 30.0, 1000.0, 700.0, true, true).unwrap(), None);
}
#[test]
fn tab_labels_are_strict() {
assert_eq!(tab_id("tab-abc-123"), Some("abc-123"));
assert_eq!(tab_label("abc"), "tab-abc");
for bad in ["tab-", "tab-../x", "tab-a b", "shell", "main", "xtab-1", &format!("tab-{}", "a".repeat(41))] {
assert_eq!(tab_id(bad), None, "{bad}");
}
}
#[test]
fn site_commands_need_a_tab_on_the_exact_origin() {
let site = Url::parse("https://x.com/home").unwrap();
assert!(require_site_tab(&SITE, "tab-1", &site).is_ok());
assert!(require_site_tab(&SITE, "shell", &site).is_err());
assert!(require_site_tab(&SITE, "popover-1", &site).is_err());
assert!(require_site_tab(&SITE, "tab-1", &Url::parse("https://example.com/").unwrap()).is_err());
assert!(require_site_tab(&SITE, "tab-1", &Url::parse("https://a@x.com/").unwrap()).is_err());
}
#[test]
fn local_origin_has_no_production_localhost_backdoor() {
for ok in ["tauri://localhost/", "http://tauri.localhost/", "https://tauri.localhost/index.html"] {
assert!(require_local("shell", &Url::parse(ok).unwrap(), false).is_ok(), "{ok}");
}
assert!(require_local("shell", &Url::parse("http://127.0.0.1:1420/").unwrap(), true).is_ok());
for bad in ["http://127.0.0.1:1420/", "http://localhost:1420/", "https://x.com/", "tauri://evil/", "http://u@tauri.localhost/"] {
assert!(require_local("shell", &Url::parse(bad).unwrap(), false).is_err(), "{bad}");
}
assert!(require_local("tab-1", &Url::parse("tauri://localhost/").unwrap(), false).is_err());
}
}