A silent CSP bug that broke our homepage layout (and the fix)

2026-11-29来源 dev.toEnglish

I spent part of this week chasing a layout bug that only showed up in real browsers, never in local dev previews, and never threw a single console error. Here's what was actually going on, and the fix.

The symptom

On the homepage of BrightLoop (a small agency I run, building websites, AI chatbots, and business automation for small businesses, in EN/DE/AZ, on Cloudflare Workers), I had a handful of decorative floating cards positioned over a hero section. Each card had its position set with an inline style attribute, computed server-side per card:

<div class="hero-card" style="top: 12%; left: 68%; transform: rotate(-4deg);"> ... </div> 

Locally, in dev, they rendered exactly where they should. Deployed, in every real browser, they all collapsed to the same spot — top-left, no rotation, stacked on top of each other.

The investigation

First instinct: broken CSS cascade, or a stale build. Neither. The markup shipped with the correct style attribute, visible in the rendered HTML source. DevTools confirmed the attribute was present on the element — and showed it crossed out, overridden by nothing, just... not applied.

The actual cause: our Content-Security-Policy header.

Content-Security-Policy: style-src 'self' 

No 'unsafe-inline'. That one directive silently drops any inline style="..." attribute in a conforming browser. No warning banner, no visible error in the Network tab — CSP violations for styles usually only show up in the browser console, and only if you're actually looking at it at that moment, on that page, in that browser. In a quick local check without the production CSP header active, everything looked fine, which is exactly why this slipped through: the HTML was correct, the CSS was correct, only the policy enforcement was the missing piece, and it's invisible unless you test under the real header.

Why we had that CSP in the first place

'unsafe-inline' for styles defeats a meaningful chunk of what CSP is for. Inline styles aren't typically an XSS vector the way inline <script> is, but once you allow unsafe-inline for one directive, it's easy to end up loosening others "just this once," and the policy stops meaning much. We wanted a strict policy and weren't willing to punch a hole in it for a handful of decorative cards.

The fix

Instead of computing per-card positions server-side and inlining them, we moved to a fixed set of static CSS classes, each with its own position/rotation baked into the stylesheet:

.hero-card--1 { top: 12%; left: 68%; transform: rotate(-4deg); } .hero-card--2 { top: 38%; left: 8%; transform: rotate(3deg); } .hero-card--3 { top: 64%; left: 52%; transform: rotate(-2deg); } 
<div class="hero-card hero-card--1">...</div> 

Same visual result, zero inline styles, strict CSP stays intact. The only real constraint this adds is that the number of distinct positions is now fixed at build time rather than computed per-item — which was true in practice anyway, since the layout only ever needed a handful of fixed slots.

The actual lesson

The bug wasn't really about CSS positioning. It was about testing under the conditions that will actually run in production. A local dev server without the production security headers active will hide an entire category of bugs that only a deployed-with-real-headers pass will catch. If you're running a strict CSP, it's worth deliberately testing at least once against the deployed version with DevTools open and the console tab visible, specifically looking for suppressed style/script warnings, rather than assuming "it rendered correctly in dev" means anything once real headers are in front of it.

One nice side effect of fixing this properly rather than loosening the policy: it forced a small architectural cleanup — that hero section also generalized from three hardcoded cards to rendering any number of items (we needed it anyway once a team member's own service needed to show up at equal footing alongside the core ones).

If you're curious what we're building: brightloop.live

原文:https://www.a8q9.cn/en/article/a-silent-csp-bug-that-broke-our-homepage-layout-and-the-fix-cdb71c.html

相关文章