148 lines
5.5 KiB
Rust
148 lines
5.5 KiB
Rust
//! Who may call what. Pure functions, so tests/security.rs can pin every rule.
|
|
//!
|
|
//! The capability file already limits the app commands to the `main` window with local content; every command checks
|
|
//! again here (window label and the exact origin of the page that called), so a future second window, a stray
|
|
//! navigation or a remote page can never reach them, whatever the capability files say.
|
|
use serde_json::{Map, Value};
|
|
use url::Url;
|
|
|
|
/// The one window that may call app commands.
|
|
pub const MAIN: &str = "main";
|
|
|
|
/// The only arguments the sidecar is ever started with.
|
|
pub const SIDECAR_ARGS: [&str; 2] = ["serve", "--stdio"];
|
|
/// Name of the sidecar binary (`bundle.externalBin` = binaries/storebound).
|
|
pub const SIDECAR: &str = "storebound";
|
|
|
|
/// Dev server origin (`build.devUrl`); accepted only in debug builds.
|
|
pub const DEV_HOST: &str = "127.0.0.1";
|
|
pub const DEV_PORT: u16 = 1420;
|
|
|
|
/// The app's own bundled pages: `tauri://localhost` (macOS, Linux) or `http(s)://tauri.localhost` (Windows).
|
|
/// No port, no user info, nothing that only looks similar. In debug builds also the Vite dev server.
|
|
pub fn is_local_origin(url: &Url, dev: bool) -> bool {
|
|
if !url.username().is_empty() || url.password().is_some() {
|
|
return false;
|
|
}
|
|
let host = url.host_str().unwrap_or_default();
|
|
match url.scheme() {
|
|
"tauri" => host == "localhost" && url.port().is_none(),
|
|
"http" | "https" if host == "tauri.localhost" => url.port().is_none(),
|
|
"http" if dev => host == DEV_HOST && url.port() == Some(DEV_PORT),
|
|
_ => false,
|
|
}
|
|
}
|
|
|
|
pub fn require_main(label: &str, url: &Url, dev: bool) -> Result<(), String> {
|
|
if label != MAIN {
|
|
return Err(format!("not allowed from window '{label}'"));
|
|
}
|
|
if !is_local_origin(url, dev) {
|
|
return Err("not allowed from this page".into());
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
/// Method kinds the sidecar announces in its `session.hello` descriptors.
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
pub enum Kind {
|
|
Read,
|
|
Write,
|
|
System,
|
|
Portfolio,
|
|
Session,
|
|
/// Account-scoped store reads (apps.list, account.check): the host runs them with one account's key.
|
|
Account,
|
|
}
|
|
|
|
impl Kind {
|
|
pub fn parse(s: &str) -> Option<Kind> {
|
|
Some(match s {
|
|
"read" => Kind::Read,
|
|
"write" => Kind::Write,
|
|
"system" => Kind::System,
|
|
"portfolio" => Kind::Portfolio,
|
|
"session" => Kind::Session,
|
|
"account" => Kind::Account,
|
|
_ => return None,
|
|
})
|
|
}
|
|
}
|
|
|
|
/// Params keys the webview may send for a sidecar method. The project folder comes from the host's project list (the
|
|
/// webview names a project id), and `confirmToken`, `credentials` and `dryRun` are the host's: tokens are minted only
|
|
/// after the native dialog, credentials come from the keychain.
|
|
const JS_PARAM_KEYS: &[&str] = &["args", "live"];
|
|
|
|
/// Checks a webview call to the sidecar and returns the params to send. Writes from the webview are always dry runs;
|
|
/// the real run goes through `write_with_confirm` (native dialog, then a host-minted token).
|
|
pub fn sanitize_js_call(method: &str, kind: Option<Kind>, params: &Value) -> Result<Value, String> {
|
|
let kind = kind.ok_or_else(|| format!("unknown method: {method}"))?;
|
|
match kind {
|
|
Kind::Session => return Err(format!("{method} is the host's")),
|
|
Kind::Portfolio => return Err(format!("{method}: use portfolio_status")),
|
|
Kind::Account => return Err(format!("{method}: use the account commands")),
|
|
_ => {}
|
|
}
|
|
let empty = Map::new();
|
|
let obj = match params {
|
|
Value::Object(o) => o,
|
|
Value::Null => &empty,
|
|
_ => return Err("params must be an object".into()),
|
|
};
|
|
for k in obj.keys() {
|
|
if !JS_PARAM_KEYS.contains(&k.as_str()) {
|
|
return Err(format!("param '{k}' is not allowed from the window"));
|
|
}
|
|
}
|
|
let mut out = obj.clone();
|
|
if kind == Kind::Write {
|
|
out.insert("dryRun".into(), Value::Bool(true));
|
|
}
|
|
Ok(Value::Object(out))
|
|
}
|
|
|
|
/// Hosts `open_external` may open in the browser: the two store consoles and storebound's own site.
|
|
pub const EXTERNAL_HOSTS: &[&str] = &[
|
|
"play.google.com",
|
|
"appstoreconnect.apple.com",
|
|
"developer.apple.com",
|
|
"storebound.dev",
|
|
// Dependencies screen link-outs
|
|
"developer.android.com",
|
|
"ffmpeg.org",
|
|
"googlechromelabs.github.io",
|
|
"git-scm.com",
|
|
// account health fixes (enable an API in the service account's Google Cloud project)
|
|
"console.cloud.google.com",
|
|
];
|
|
|
|
/// Executable of a Chrome for Testing download, relative to its unpacked folder (app data `tools/chrome/`).
|
|
pub fn chrome_executable() -> &'static str {
|
|
if cfg!(target_os = "macos") {
|
|
"Google Chrome for Testing.app/Contents/MacOS/Google Chrome for Testing"
|
|
} else if cfg!(windows) {
|
|
"chrome.exe"
|
|
} else {
|
|
"chrome"
|
|
}
|
|
}
|
|
|
|
pub fn external_allowed(url: &Url) -> bool {
|
|
url.scheme() == "https"
|
|
&& url.username().is_empty()
|
|
&& url.password().is_none()
|
|
&& url.port().is_none()
|
|
&& url.host_str().is_some_and(|h| EXTERNAL_HOSTS.contains(&h))
|
|
}
|
|
|
|
/// Deep links the app reacts to: `storebound://<screen>` for a known screen, nothing else.
|
|
pub const DEEP_LINK_SCREENS: &[&str] = &["portfolio", "apps", "accounts", "ignored", "dependencies", "settings"];
|
|
|
|
pub fn deep_link_screen(url: &Url) -> Option<&'static str> {
|
|
if url.scheme() != "storebound" {
|
|
return None;
|
|
}
|
|
let host = url.host_str().unwrap_or_default();
|
|
DEEP_LINK_SCREENS.iter().copied().find(|s| *s == host)
|
|
}
|