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.
This commit is contained in:
Joshua Moon
2026-11-13 11:33:53 +00:00
committed by GitHub
parent 48741fd17d
commit 5a40bc8bd7
+18 -9
View File
@@ -113,14 +113,23 @@ fn safe_area_probe(
.map_err(|error| anyhow::anyhow!("Failed to create safe-area probe: {error:?}"))?
.dyn_into::<web_sys::HtmlElement>()
.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)
}