S063: web pages can't open nacre:// pages; the passwords page asks on every open and isn't kept in the session

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
astar76 2026-11-18 05:12:09 -06:00
commit 583a92b854
9 changed files with 273 additions and 24 deletions

View file

@ -1317,7 +1317,7 @@ impl App {
return;
}
let cert_error = tab.cert_error.take();
if url.as_str() == "nacre://allowcert" {
if url.as_str() == crate::tabs::ALLOW_CERT {
request.deny();
if let (Some(error), Some(engine)) = (cert_error, &self.engine) {
engine.allow_certificate(&error);
@ -1328,13 +1328,10 @@ impl App {
return;
}
match url.scheme() {
// D-049: never one of Nacre's pages from a web page (the engine's policy already denied it).
"nacre" => {
eprintln!("tab {}: navigation to {url} denied: web pages can't open Nacre's pages", self.windows[&window].tabs[index].id);
request.deny();
if let Some(page) = Page::from_url(url.as_str()) {
let win = self.windows.get_mut(&window).expect("found");
win.tabs[index].page = Some(page);
self.redraw(window);
}
},
"view-source" => {
request.deny();
@ -1583,6 +1580,8 @@ impl App {
other => {
drop(menu);
match other {
// The link came from the page (D-049).
MenuAction::OpenLink(url, _) if !crate::tabs::web_may_open(&url) => eprintln!("tab {tab_id}: link to {url} not opened: web pages can't open Nacre's pages"),
MenuAction::OpenLink(url, how) => self.open(window, url, how, Some(tab_id)),
MenuAction::CopyText(text) => {
if let Ok(mut clipboard) = arboard::Clipboard::new() {

View file

@ -982,6 +982,16 @@ impl App {
/// addresses the ad lists block never get here: WebKit's content filters stop them.
fn on_create_new(&mut self, parent: ViewId, view: View, url: Option<Url>, gesture: bool) {
use crate::store::permissions::{Answer, Kind};
// D-049: a pop-up never opens one of Nacre's pages (WebKit's policy check denies it first; this is the
// backstop).
if url.as_ref().is_some_and(|u| !crate::tabs::web_may_open(u.as_str())) {
eprintln!("pop-up to {} denied: web pages can't open Nacre's pages", url.as_ref().map(Url::as_str).unwrap_or(""));
if let Some(engine) = &self.engine {
engine.forget(view.id());
}
view.close();
return;
}
let found = self.find_webview(parent);
let blocked = found.is_some_and(|(w, i)| {
let host = self.windows[&w].tabs[i].host();
@ -1878,7 +1888,11 @@ impl App {
tab.pinned = saved.pinned;
tab.muted = saved.muted;
tab.title = saved.title.clone();
match Page::from_url(&saved.url) {
let url = crate::store::session::kept_url(&saved.url);
if url != saved.url {
tab.title = String::new();
}
match Page::from_url(url) {
Some(page) => tab.page = Some(page),
None => {
tab.page = None;

View file

@ -721,6 +721,15 @@ impl Engine {
webkit::PolicyDecisionType::NavigationAction | webkit::PolicyDecisionType::NewWindowAction => {
let Some(nav) = decision.downcast_ref::<webkit::NavigationPolicyDecision>() else { return false };
let Some(mut action) = nav.navigation_action() else { return false };
// D-049: a page, its frames and its pop-ups never go to one of Nacre's pages (all of these come from
// web content: Nacre opens its pages without a web view).
if let Some(uri) = action.request().and_then(|r| r.uri())
&& !crate::tabs::web_may_open(&uri)
{
eprintln!("view {}: navigation to {uri} denied: web pages can't open Nacre's pages", id.0);
decision.ignore();
return true;
}
// A link clicked with the middle button or Ctrl opens in a new tab (Ctrl+Shift: in front), as in
// other browsers; WebKit would load it here.
let mods = gtk::gdk::ModifierType::from_bits_truncate(action.modifiers());

View file

@ -917,9 +917,10 @@ impl App {
let b = self.shared.bookmarks.borrow();
[crate::store::bookmarks::BAR, crate::store::bookmarks::OTHER].iter().filter_map(|id| b.get(*id)).map(|n| n.children.iter().filter(|c| c.is_folder()).count()).sum::<usize>()
};
// Web views alive (in use, forgotten but kept) and a shortcut being recorded (S059).
// Web views alive (in use, forgotten but kept), a shortcut being recorded (S059), the passwords page past its
// question (S063).
let views = self.engine.as_ref().map(|e| e.view_counts()).unwrap_or_default();
let lifecycle = serde_json::json!({"views": views.0, "views_leaked": views.1, "capture_key": self.windows.get(&window).map(|w| w.ui.pages.capture_key.is_some())});
let lifecycle = serde_json::json!({"views": views.0, "views_leaked": views.1, "capture_key": self.windows.get(&window).map(|w| w.ui.pages.capture_key.is_some()), "passwords_open": self.windows.get(&window).map(|w| w.ui.pages.passwords_open)});
serde_json::json!({
"windows": self.windows.len(),
"notifications": self.notifications_shown,

View file

@ -50,6 +50,12 @@ pub struct SavedSession {
pub saved_at: u64,
}
/// The address a tab is saved and restored with: the passwords page never goes into the session (it restores as
/// a new tab, S063).
pub fn kept_url(url: &str) -> &str {
if crate::tabs::Page::from_url(url) == Some(crate::tabs::Page::Passwords) { "nacre://newtab" } else { url }
}
pub struct Session {
path: PathBuf,
/// What was on disk at start: the previous run's windows.
@ -111,3 +117,17 @@ impl Session {
}
}
}
#[cfg(test)]
mod tests {
use super::*;
// S063: the passwords page is never saved or restored.
#[test]
fn passwords_page_not_kept() {
assert_eq!(kept_url("nacre://passwords"), "nacre://newtab");
assert_eq!(kept_url("nacre:Passwords"), "nacre://newtab");
assert_eq!(kept_url("nacre://settings/privacy"), "nacre://settings/privacy");
assert_eq!(kept_url("https://example.com/passwords"), "https://example.com/passwords");
}
}

View file

@ -104,6 +104,18 @@ impl Page {
}
}
/// The certificate warning's "Open anyway" (`assets/pages/badcert.html`).
pub const ALLOW_CERT: &str = "nacre://allowcert";
/// D-049: whether web content (a page's links, redirects, scripts, frames, pop-ups, the links of its context menu)
/// may go to this address. Never to one of Nacre's pages: only the user opens those (address bar, menus, buttons,
/// shortcuts, bookmarks). The one exception is the certificate warning's "Open anyway", which the browser takes only
/// from the tab showing that warning.
pub fn web_may_open(url: &str) -> bool {
let nacre = |scheme: &str| scheme.eq_ignore_ascii_case("nacre");
url == ALLOW_CERT || !(Url::parse(url).is_ok_and(|u| nacre(u.scheme())) || nacre(url.trim_start().split(':').next().unwrap_or("")))
}
/// A question the tab is asking the user (shown as a bar above the page).
pub enum Prompt {
Permission { host: String, kind: crate::store::permissions::Kind, request: crate::engine::PermissionAsk },
@ -366,9 +378,11 @@ impl Tab {
/// What the session file keeps for this tab.
pub fn saved(&self) -> crate::store::session::SavedTab {
let url = self.pending_url.clone().unwrap_or_else(|| self.shown_url());
let kept = crate::store::session::kept_url(&url);
crate::store::session::SavedTab {
url: self.pending_url.clone().unwrap_or_else(|| self.shown_url()),
title: self.display_title(),
title: if kept == url { self.display_title() } else { String::new() },
url: kept.to_owned(),
pinned: self.pinned,
muted: self.muted,
history: self.back_forward.iter().map(Url::to_string).collect(),
@ -381,6 +395,17 @@ impl Tab {
mod tests {
use super::*;
// S063 (D-049): a page's navigations never reach Nacre's pages, except "Open anyway" of the certificate warning.
#[test]
fn web_content_cannot_open_internal_pages() {
for url in ["nacre://passwords", "NACRE://passwords", "nacre:passwords", " nacre://settings", "Nacre://allowcert/x", "nacre://newtab"] {
assert!(!web_may_open(url), "{url}");
}
for url in [ALLOW_CERT, "https://example.com/nacre://passwords", "about:blank", "data:text/html,x", "http://nacre/"] {
assert!(web_may_open(url), "{url}");
}
}
// S059: a close left without an answer must not keep the tab closing (Reload and navigation ignored) for good.
#[test]
fn unanswered_close_goes_ahead() {

View file

@ -221,6 +221,9 @@ pub fn draw(root: &mut egui::Ui, f: &mut Frame) {
if !std::mem::take(&mut f.ui.pages.capture_shown) {
f.ui.pages.capture_key = None;
}
if !std::mem::take(&mut f.ui.pages.passwords_shown) && f.ui.pages.passwords_open {
f.ui.pages.close_passwords(&ctx);
}
}
/// The page itself: a web page's framebuffer, or one of Nacre's pages.

View file

@ -52,6 +52,10 @@ pub struct PageState {
pub reauth_error: String,
pub unlock: String,
pub unlock_error: String,
/// The master password (or, without one, a click) was given since the passwords page was last shown (S063).
pub passwords_open: bool,
/// The passwords page was drawn this frame.
pub passwords_shown: bool,
pub generated: String,
pub gen_passphrase: bool,
pub script_edit: Option<(u64, String)>,
@ -66,6 +70,27 @@ pub struct PageState {
pub ntp_customize: bool,
}
impl PageState {
/// The passwords page left the screen (another tab, page or window state, the tab closed): it asks again next
/// time, and nothing it showed or was typed into it stays in memory (S063).
pub fn close_passwords(&mut self, ctx: &egui::Context) {
self.passwords_open = false;
self.passwords_search.clear();
self.revealed.clear();
self.login_edit = None;
self.address_edit = None;
self.card_edit = None;
self.reauth.clear();
self.reauth_error.clear();
self.unlock.clear();
self.unlock_error.clear();
self.generated.clear();
self.master = (String::new(), String::new());
// Text fields keep their text for undo.
ctx.data_mut(|d| d.remove_by_type::<egui::text_edit::TextEditState>());
}
}
pub fn draw(ui: &mut Ui, f: &mut Frame, page: &Page) {
match page {
Page::NewTab => newtab::draw(ui, f),

View file

@ -18,7 +18,9 @@ pub fn draw(ui: &mut Ui, f: &mut Frame) {
page_column(ui, "passwords", 900.0, |ui| {
let subtitle = if f.incognito() { "Incognito windows have their own empty store that is thrown away when they close." } else { "" };
page_header(ui, "Passwords and autofill", subtitle, |_| {});
if f.shared.vault.borrow().locked() {
f.ui.pages.passwords_shown = true;
// Every time the page opens it asks first, even when the store is unlocked for filling in (S063).
if f.shared.vault.borrow().locked() || !f.ui.pages.passwords_open {
unlock(ui, f);
return;
}
@ -58,6 +60,15 @@ fn unlock(ui: &mut Ui, f: &mut Frame) {
super::super::widgets::icon(ui, rect.center(), icons::LOCK, theme::ICON_LARGE, true, p.on_secondary_container);
ui.add_space(theme::GAP_L);
ui.label(RichText::new("Your passwords are locked").font(theme::font(theme::TITLE, Weight::Medium)).color(p.on_surface));
// Without a master password there is nothing to ask: a click shows them (no page can click it, D-049).
if f.shared.vault.borrow().mode() != Mode::Master {
ui.label(RichText::new("Set a master password to keep others from seeing them.").font(theme::font(theme::BODY, Weight::Regular)).color(p.on_surface_variant));
ui.add_space(theme::GAP_L);
if button(ui, ButtonKind::Filled, Some(icons::LOCK_OPEN), "Show passwords").clicked() {
f.ui.pages.passwords_open = true;
}
return;
}
ui.label(RichText::new("Enter your master password to see and fill them.").font(theme::font(theme::BODY, Weight::Regular)).color(p.on_surface_variant));
ui.add_space(theme::GAP_L);
let r = password_field(ui, &mut f.ui.pages.unlock, "Master password", 280.0);
@ -65,9 +76,14 @@ fn unlock(ui: &mut Ui, f: &mut Frame) {
let go = button(ui, ButtonKind::Filled, Some(icons::LOCK_OPEN), "Unlock").clicked() || (r.lost_focus() && ui.input(|i| i.key_pressed(egui::Key::Enter)));
if go {
let pw = std::mem::take(&mut f.ui.pages.unlock);
match f.shared.vault.borrow_mut().unlock(&pw) {
Ok(()) => f.ui.pages.unlock_error.clear(),
Err(_) => f.ui.pages.unlock_error = "That master password is not right. Try again.".into(),
let mut vault = f.shared.vault.borrow_mut();
let ok = if vault.locked() { vault.unlock(&pw).is_ok() } else { vault.check_password(&pw) };
drop(vault);
if ok {
f.ui.pages.unlock_error.clear();
f.ui.pages.passwords_open = true;
} else {
f.ui.pages.unlock_error = "That master password is not right. Try again.".into();
}
r.request_focus();
}