From 5a40bc8bd78da92ea3b1ed143dd1208a0e4a3567 Mon Sep 17 00:00:00 2001 From: Joshua Moon Date: Fri, 13 Nov 2026 11:33:53 +0000 Subject: [PATCH] gpui_web: Fix safe-area insets under a strict style CSP (#66907) ## Summary gpui web uses exploratory divs to learn about the unusable deadspace on the margins of a device's screen, which it calls safe-area insets. When doing this, it uses html `style` attributes with raw css parameters. In modern web development, is typical to use a `Content-Security-Policy` to block `unsafe-inline` css in html. This feature breaks when such security settings are in use. However, while raw CSS in the style element is not permitted, even with the CSP set to block `unsafe-inline` style attributes, it still permits some direct adjustments via CSSOM with `el.style.setProperty(p, v)`. This workaround is already used by gpui web on the canvas and IME elements. See * https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Content-Security-Policy/style-src * https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Content-Security-Policy/style-src-attr ## Testing I have manually reviewed this change in WebKit, Chromium, and Gecko. All modern web engines support this workaround and document it as safe. ## Self-review - [x] I've reviewed my diff for quality, security, reliability, and performance. - [x] There are no UI changes. - [x] Tests are not relevant for this change. Release Notes: - [GPUI] Fixed safe-area insets reading as zero on pages with a strict style Content-Security-Policy. --- crates/gpui_web/src/viewport.rs | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/crates/gpui_web/src/viewport.rs b/crates/gpui_web/src/viewport.rs index 1e6582af133..e8a75c866fc 100644 --- a/crates/gpui_web/src/viewport.rs +++ b/crates/gpui_web/src/viewport.rs @@ -113,14 +113,23 @@ fn safe_area_probe( .map_err(|error| anyhow::anyhow!("Failed to create safe-area probe: {error:?}"))? .dyn_into::() .map_err(|error| anyhow::anyhow!("Invalid safe-area probe element: {error:?}"))?; - probe - .set_attribute( - "style", - &format!( - "position:fixed;left:0;top:0;width:0;padding:0;border:0;visibility:hidden;\ - pointer-events:none;height:env(safe-area-inset-{edge},0px)" - ), - ) - .map_err(|error| anyhow::anyhow!("Failed to style safe-area probe: {error:?}"))?; + let height = format!("env(safe-area-inset-{edge},0px)"); + // A CSP that blocks inline styles still allows per-property CSSOM writes. + let style = probe.style(); + for (property, value) in [ + ("position", "fixed"), + ("left", "0"), + ("top", "0"), + ("width", "0"), + ("padding", "0"), + ("border", "0"), + ("visibility", "hidden"), + ("pointer-events", "none"), + ("height", height.as_str()), + ] { + style.set_property(property, value).map_err(|error| { + anyhow::anyhow!("Failed to set safe-area probe {property} style: {error:?}") + })?; + } Ok(probe) }