S023: request headers (DNT, Sec-GPC, Save-Data) and third-party cookie blocking in the vendored servo-net

New vendor/servo-net/nacre.rs holds embedder-set state; http_loader and
websocket_loader read it. Settings rows are working switches; privacy mode
turns blocking on. Loopback smoke tests/smoke/privacy-net.txt, unit tests
for site matching. Patch list in DESIGN.md, D-017.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
astar76 2026-11-12 15:34:32 -06:00
commit f5cb6f76f8
3 changed files with 357 additions and 34 deletions

View file

@ -396,6 +396,14 @@ impl App {
pub fn refresh_hub_config(&self) {
let settings = self.shared.settings.borrow();
*self.hub.config.borrow_mut() = HubConfig::from_settings(&settings, &self.shared.permissions.borrow(), &self.shared.user_agent.borrow(), self.shared.http_allowed.borrow().clone());
// Request headers and the cookie policy live in the vendored servo-net (S023).
net::nacre::set_extra_request_headers(&crate::hub::privacy_headers(&settings));
net::nacre::set_block_third_party_cookies(settings.privacy.block_third_party || self.privacy_mode_on(&settings));
}
/// Privacy mode: on everywhere, or in incognito windows when the user chose that.
fn privacy_mode_on(&self, s: &Settings) -> bool {
s.privacy.mode || (self.shared.incognito && s.privacy.mode_incognito)
}
/// (Re)build the scripts every page gets. Applies on each page's next load.
@ -405,7 +413,7 @@ impl App {
ucm.remove_script(script);
}
let s = self.shared.settings.borrow().clone();
let privacy = s.privacy.mode || (self.shared.incognito && s.privacy.mode_incognito);
let privacy = self.privacy_mode_on(&s);
let options = inject::Options {
token: &self.hub.token,
privacy_mode: privacy,

View file

@ -125,6 +125,19 @@ const TRACKING_PARAMS: &[&str] = &[
];
const TRACKING_PREFIXES: &[&str] = &["utm_", "pk_", "mtm_", "hsa_"];
/// Headers every request carries for the privacy settings: Do Not Track with Global Privacy Control, and
/// Save-Data. Applied in the vendored servo-net (S023).
pub fn privacy_headers(settings: &Settings) -> Vec<(&'static str, &'static str)> {
let mut headers = Vec::new();
if settings.privacy.dnt {
headers.extend([("DNT", "1"), ("Sec-GPC", "1")]);
}
if settings.privacy.save_data {
headers.push(("Save-Data", "on"));
}
headers
}
/// The URL without tracking parameters, if it had any.
pub fn strip_tracking(url: &Url) -> Option<Url> {
url.query()?;
@ -304,6 +317,8 @@ impl Hub {
impl WebViewDelegate for Hub {
fn notify_url_changed(&self, webview: WebView, url: Url) {
// Covers pages shown without a request (history) and downloads that kept the old page.
net::nacre::set_top_level_url(webview.id(), &servo::ServoUrl::from_url(url.clone()));
self.push(Event::UrlChanged(webview.id(), url));
}
fn notify_page_title_changed(&self, webview: WebView, title: Option<String>) {
@ -331,6 +346,7 @@ impl WebViewDelegate for Hub {
self.push(Event::History(webview.id(), entries, current));
}
fn notify_closed(&self, webview: WebView) {
net::nacre::forget_webview(webview.id());
self.push(Event::Closed(webview.id()));
}
fn notify_crashed(&self, webview: WebView, reason: String, backtrace: Option<String>) {
@ -419,6 +435,51 @@ impl servo::GamepadDelegate for Gamepads {
mod tests {
use super::*;
#[test]
fn privacy_headers_follow_the_settings_and_reach_requests() {
let mut settings = Settings::default();
settings.privacy.dnt = true;
settings.privacy.save_data = false;
assert_eq!(privacy_headers(&settings), vec![("DNT", "1"), ("Sec-GPC", "1")]);
settings.privacy.dnt = false;
settings.privacy.save_data = true;
assert_eq!(privacy_headers(&settings), vec![("Save-Data", "on")]);
// servo-net adds them to a request unless the page set the header itself.
net::nacre::set_extra_request_headers(&[("DNT", "1"), ("Sec-GPC", "1"), ("Bad Name", "x")]);
let mut headers = HeaderMap::new();
headers.insert("Sec-GPC", HeaderValue::from_static("0"));
net::nacre::append_extra_headers(&mut headers);
assert_eq!(headers.len(), 2);
assert_eq!(headers["dnt"], "1");
assert_eq!(headers["sec-gpc"], "0");
net::nacre::set_extra_request_headers(&[]);
}
#[test]
fn third_party_matching_keeps_same_site_subdomains() {
use net::nacre::{is_third_party, site_of_host};
let url = |s: &str| servo::ServoUrl::parse(s).unwrap();
assert_eq!(site_of_host("www.Example.com"), "example.com");
assert_eq!(site_of_host("a.b.example.co.uk"), "example.co.uk");
assert_eq!(site_of_host("site.nacre.test"), "nacre.test");
assert_eq!(site_of_host("127.0.0.1"), "127.0.0.1");
assert_eq!(site_of_host("[::1]"), "[::1]");
assert_eq!(site_of_host("localhost"), "localhost");
let top = site_of_host("www.example.com");
assert!(!is_third_party(&top, &url("https://login.example.com/x")));
assert!(!is_third_party(&top, &url("http://example.com:8080/")));
assert!(is_third_party(&top, &url("https://tracker.net/p.gif")));
assert!(is_third_party(&top, &url("https://example.co.uk/")));
// Different users' sites under one public suffix are different sites.
let top = site_of_host("alice.github.io");
assert!(is_third_party(&top, &url("https://bob.github.io/")));
assert!(!is_third_party(&top, &url("https://alice.github.io/repo")));
// IP addresses only match themselves; URLs without a host are never third-party.
let top = site_of_host("127.0.0.1");
assert!(is_third_party(&top, &url("http://127.0.0.2/")));
assert!(!is_third_party(&top, &url("data:text/plain,x")));
}
#[test]
fn images_follow_the_site_answer_then_the_default() {
let mut settings = Settings::default();

View file

@ -717,7 +717,7 @@ fn performance(ui: &mut Ui, f: &mut Frame, s: &mut Settings) {
switch_row(ui, "Preload pages", "Looks up the addresses of links on a page ahead of time", &mut s.web.preload);
switch_row(ui, "Smooth scrolling", "Animated scrolling (off in low-memory mode)", &mut s.web.smooth_scroll);
switch_row(ui, "Still new tab background", "No animation on the new tab page", &mut s.perf.still_ntp);
limited_row(ui, "Save data", "Tells pages to be lighter and stops videos preloading", "Servo can't send the Save-Data header", |ui| switch(ui, &mut s.privacy.save_data));
switch_row(ui, "Save data", "Sends Save-Data so pages can be lighter, and stops videos preloading", &mut s.privacy.save_data);
row(ui, "Hardware acceleration", "Use the GPU. Turn off if pages show up black or blank; Off uses software rendering (restart to apply)", |ui| segmented(ui, &mut s.web.gpu, AutoOnOff::ALL));
});
}
@ -753,9 +753,9 @@ fn privacy(ui: &mut Ui, f: &mut Frame, s: &mut Settings) {
card(ui, |ui| {
row(ui, "Privacy mode", "Hides your browser's fingerprint (canvas, WebGL, screen and hardware details)", |ui| switch(ui, &mut s.privacy.mode));
switch_row(ui, "Privacy mode in incognito windows", "", &mut s.privacy.mode_incognito);
limited_row(ui, "Send Do Not Track and Global Privacy Control", "Pages see navigator.globalPrivacyControl", "Servo can't add the request headers yet", |ui| switch(ui, &mut s.privacy.dnt));
switch_row(ui, "Send Do Not Track and Global Privacy Control", "Asks every site not to sell or share your data (DNT and Sec-GPC headers)", &mut s.privacy.dnt);
switch_row(ui, "Remove tracking parameters", "Strips utm_, fbclid, gclid and similar from links before they load", &mut s.privacy.strip_params);
unavailable_row(ui, "Block third-party cookies", "Servo 0.7 has no cookie policy setting");
switch_row(ui, "Block third-party cookies", "Sites inside a page (ads, trackers) can't send or store cookies; always on in privacy mode", &mut s.privacy.block_third_party);
switch_row(ui, "Google sign-in", "Looks like Firefox on accounts.google.com so Google lets you sign in", &mut s.privacy.google_signin);
});
section(ui, "Site data");