Everything in Rust on Servo 0.7 (trimmed: no WebXR, WebGPU, multiprocess): tabs and windows, address bar with suggestions, history, bookmarks and reading list, downloads with resume and external download managers, encrypted password vault with autofill, ad blocker (adblock-rust, 22 lists), VPN through Tor or a proxy and secure DNS via a local bridge proxy, picture in picture, Discord Rich Presence, media player and visualizer, spell check, site apps, user scripts and styles, import from other browsers, gamepads, Web Bluetooth, and every setting of the original Nacre in a plain egui interface. Not compiled yet; docs/HANDOFF.md has the first build checklist. Co-Authored-By: Claude Opus 5.5 <[email protected]>
252 lines
8.3 KiB
Rust
252 lines
8.3 KiB
Rust
//! What each site may do: remembered answers to permission prompts.
|
|
|
|
use std::collections::BTreeMap;
|
|
use std::path::PathBuf;
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use crate::store::settings::{PermissionDefault, Settings};
|
|
use crate::util;
|
|
|
|
/// Things a site can ask for. The names are also the keys in the settings file.
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
|
|
#[serde(rename_all = "snake_case")]
|
|
pub enum Kind {
|
|
Location,
|
|
Notifications,
|
|
Camera,
|
|
Microphone,
|
|
Clipboard,
|
|
Popups,
|
|
Midi,
|
|
Storage,
|
|
WakeLock,
|
|
Sound,
|
|
Javascript,
|
|
Images,
|
|
/// Opening links in other programs (mailto:, magnet: and so on).
|
|
ExternalApps,
|
|
DeviceInfo,
|
|
BackgroundSync,
|
|
Bluetooth,
|
|
Gamepad,
|
|
}
|
|
|
|
impl Kind {
|
|
pub const ALL: &'static [Kind] = &[
|
|
Kind::Location,
|
|
Kind::Notifications,
|
|
Kind::Camera,
|
|
Kind::Microphone,
|
|
Kind::Clipboard,
|
|
Kind::Popups,
|
|
Kind::Midi,
|
|
Kind::Storage,
|
|
Kind::WakeLock,
|
|
Kind::Sound,
|
|
Kind::Javascript,
|
|
Kind::Images,
|
|
Kind::ExternalApps,
|
|
Kind::DeviceInfo,
|
|
Kind::BackgroundSync,
|
|
Kind::Bluetooth,
|
|
Kind::Gamepad,
|
|
];
|
|
|
|
pub fn key(self) -> &'static str {
|
|
match self {
|
|
Kind::Location => "location",
|
|
Kind::Notifications => "notifications",
|
|
Kind::Camera => "camera",
|
|
Kind::Microphone => "microphone",
|
|
Kind::Clipboard => "clipboard",
|
|
Kind::Popups => "popups",
|
|
Kind::Midi => "midi",
|
|
Kind::Storage => "storage",
|
|
Kind::WakeLock => "wake_lock",
|
|
Kind::Sound => "sound",
|
|
Kind::Javascript => "javascript",
|
|
Kind::Images => "images",
|
|
Kind::ExternalApps => "external_apps",
|
|
Kind::DeviceInfo => "device_info",
|
|
Kind::BackgroundSync => "background_sync",
|
|
Kind::Bluetooth => "bluetooth",
|
|
Kind::Gamepad => "gamepad",
|
|
}
|
|
}
|
|
|
|
pub fn label(self) -> &'static str {
|
|
match self {
|
|
Kind::Location => "Location",
|
|
Kind::Notifications => "Notifications",
|
|
Kind::Camera => "Camera",
|
|
Kind::Microphone => "Microphone",
|
|
Kind::Clipboard => "Clipboard",
|
|
Kind::Popups => "Pop-ups and new windows",
|
|
Kind::Midi => "MIDI devices",
|
|
Kind::Storage => "Persistent storage",
|
|
Kind::WakeLock => "Keep the screen on",
|
|
Kind::Sound => "Sound",
|
|
Kind::Javascript => "JavaScript",
|
|
Kind::Images => "Images",
|
|
Kind::ExternalApps => "Open other apps",
|
|
Kind::DeviceInfo => "Device list",
|
|
Kind::BackgroundSync => "Background sync",
|
|
Kind::Bluetooth => "Bluetooth devices",
|
|
Kind::Gamepad => "Game controllers",
|
|
}
|
|
}
|
|
|
|
/// What the prompt asks: "<site> wants to <this>".
|
|
pub fn request_text(self) -> &'static str {
|
|
match self {
|
|
Kind::Location => "know your location",
|
|
Kind::Notifications => "show notifications",
|
|
Kind::Camera => "use your camera",
|
|
Kind::Microphone => "use your microphone",
|
|
Kind::Clipboard => "read the clipboard",
|
|
Kind::Popups => "open a new window",
|
|
Kind::Midi => "use your MIDI devices",
|
|
Kind::Storage => "keep its data permanently",
|
|
Kind::WakeLock => "keep the screen on",
|
|
Kind::Sound => "play sound",
|
|
Kind::Javascript => "run JavaScript",
|
|
Kind::Images => "show images",
|
|
Kind::ExternalApps => "open another app",
|
|
Kind::DeviceInfo => "list your cameras and microphones",
|
|
Kind::BackgroundSync => "sync in the background",
|
|
Kind::Bluetooth => "connect to Bluetooth devices",
|
|
Kind::Gamepad => "use your game controllers",
|
|
}
|
|
}
|
|
|
|
/// The answer when the user has set nothing: sensitive things ask, harmless ones are allowed.
|
|
pub fn builtin_default(self) -> PermissionDefault {
|
|
match self {
|
|
Kind::Sound | Kind::Javascript | Kind::Images | Kind::WakeLock | Kind::Storage | Kind::BackgroundSync | Kind::Gamepad => PermissionDefault::Allow,
|
|
_ => PermissionDefault::Ask,
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
|
#[serde(rename_all = "snake_case")]
|
|
pub enum Decision {
|
|
Allow,
|
|
Block,
|
|
}
|
|
|
|
/// The outcome of looking a permission up.
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
|
pub enum Answer {
|
|
Allow,
|
|
Block,
|
|
Ask,
|
|
}
|
|
|
|
pub struct Permissions {
|
|
path: PathBuf,
|
|
/// host -> kind -> decision
|
|
sites: BTreeMap<String, BTreeMap<Kind, Decision>>,
|
|
dirty: bool,
|
|
read_only: bool,
|
|
}
|
|
|
|
impl Permissions {
|
|
pub fn load(path: PathBuf, read_only: bool) -> Permissions {
|
|
let sites = if read_only { BTreeMap::new() } else { util::load_json(&path) };
|
|
Permissions { path, sites, dirty: false, read_only }
|
|
}
|
|
|
|
/// The decision stored for exactly this site, if any.
|
|
pub fn site_decision(&self, host: &str, kind: Kind) -> Option<Decision> {
|
|
self.sites.get(host).and_then(|kinds| kinds.get(&kind)).copied()
|
|
}
|
|
|
|
/// What to do when `host` asks for `kind`: the site's own answer, else the user's default for
|
|
/// that kind, else the built-in default.
|
|
pub fn answer(&self, settings: &Settings, host: &str, kind: Kind) -> Answer {
|
|
if let Some(decision) = self.site_decision(host, kind) {
|
|
return match decision {
|
|
Decision::Allow => Answer::Allow,
|
|
Decision::Block => Answer::Block,
|
|
};
|
|
}
|
|
let default = settings.privacy.permission_defaults.get(kind.key()).copied().unwrap_or_else(|| kind.builtin_default());
|
|
match default {
|
|
PermissionDefault::Allow => Answer::Allow,
|
|
PermissionDefault::Block => Answer::Block,
|
|
PermissionDefault::Ask => Answer::Ask,
|
|
}
|
|
}
|
|
|
|
pub fn set(&mut self, host: &str, kind: Kind, decision: Option<Decision>) {
|
|
if host.is_empty() {
|
|
return;
|
|
}
|
|
match decision {
|
|
Some(decision) => {
|
|
self.sites.entry(host.to_owned()).or_default().insert(kind, decision);
|
|
},
|
|
None => {
|
|
if let Some(kinds) = self.sites.get_mut(host) {
|
|
kinds.remove(&kind);
|
|
if kinds.is_empty() {
|
|
self.sites.remove(host);
|
|
}
|
|
}
|
|
},
|
|
}
|
|
self.dirty = true;
|
|
}
|
|
|
|
/// Every stored decision: (host, kind, decision).
|
|
pub fn all(&self) -> Vec<(String, Kind, Decision)> {
|
|
self.sites.iter().flat_map(|(host, kinds)| kinds.iter().map(move |(kind, decision)| (host.clone(), *kind, *decision))).collect()
|
|
}
|
|
|
|
pub fn for_host(&self, host: &str) -> Vec<(Kind, Decision)> {
|
|
self.sites.get(host).map(|kinds| kinds.iter().map(|(k, d)| (*k, *d)).collect()).unwrap_or_default()
|
|
}
|
|
|
|
pub fn clear_host(&mut self, host: &str) {
|
|
if self.sites.remove(host).is_some() {
|
|
self.dirty = true;
|
|
}
|
|
}
|
|
|
|
pub fn clear_all(&mut self) {
|
|
if !self.sites.is_empty() {
|
|
self.sites.clear();
|
|
self.dirty = true;
|
|
}
|
|
}
|
|
|
|
pub fn flush(&mut self) {
|
|
if self.dirty && !self.read_only {
|
|
if let Err(error) = util::save_json(&self.path, &self.sites) {
|
|
log::error!("could not save site permissions: {error}");
|
|
}
|
|
}
|
|
self.dirty = false;
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn site_answer_beats_default() {
|
|
let mut p = Permissions::load(PathBuf::from("/nonexistent/permissions.json"), true);
|
|
let mut s = Settings::default();
|
|
assert_eq!(p.answer(&s, "a.example", Kind::Camera), Answer::Ask);
|
|
assert_eq!(p.answer(&s, "a.example", Kind::Sound), Answer::Allow);
|
|
s.privacy.permission_defaults.insert("camera".into(), PermissionDefault::Block);
|
|
assert_eq!(p.answer(&s, "a.example", Kind::Camera), Answer::Block);
|
|
p.set("a.example", Kind::Camera, Some(Decision::Allow));
|
|
assert_eq!(p.answer(&s, "a.example", Kind::Camera), Answer::Allow);
|
|
p.set("a.example", Kind::Camera, None);
|
|
assert!(p.all().is_empty());
|
|
}
|
|
}
|