The 125/100 Bonus-Gate Paradox: Securing Firebase, Cloud Run, and WebMCP
Why a single unhashed inline script costs 45 points on Mozilla HTTP Observatory—and how Middleware #1 CSPRNG nonces, static SHA-256 hashes, and WebMCP origin isolation keep modern Google Cloud and Firebase apps at Grade A+.
Most engineering teams assume web security headers degrade gracefully: miss one directive, lose five points, fix it next sprint.
On Mozilla HTTP Observatory, the math is non-linear—and it catches thousands of otherwise thoughtful Firebase Hosting, Cloud Run, and Astro deployments off guard.
1. Why One Inline Script Costs 45 Points
Mozilla HTTP Observatory starts every site at 100 points and offers +25 to +35 bonus points for defense-in-depth isolation (Strict-Transport-Security preload, Cross-Origin-Opener-Policy, Cross-Origin-Resource-Policy, strict Referrer-Policy, and strict Content-Security-Policy).
Here is the catch: bonus points are strictly gated behind a baseline score of 90+ with zero major penalties.
- Adding
'unsafe-inline'toscript-src—often pasted in to unblock a five-line dark-mode toggle or analytics bootstrap—triggers an immediate-20point deduction. - Dropping from
100to80crosses below the90bonus gate, revoking all+25bonus points simultaneously. - A single unhashed inline
<script>tag drops a125/100(Grade A+) deployment straight to80/100(Grade B+)—a 45-point swing from one line of configuration.
| Posture | Baseline | Bonus Gate | Final Score | Grade |
|---|---|---|---|---|
| External ES Modules + CSPRNG Nonce / SHA-256 | 100 |
Unlocked (+25) |
125 / 100 |
A+ |
Single 'unsafe-inline' in script-src |
80 (-20) |
Revoked (+0) |
80 / 100 |
B+ |
(Note: Mozilla Observatory assigns 0 penalty to 'unsafe-inline' when confined strictly to style-src, though external stylesheets eliminate inline style tokens altogether.)
2. The “Middleware #1” Invariant on Cloud Run
On Google Cloud Run or Node SSR backends, the most common silent regression is middleware ordering. When a health check, static asset handler, or domain router mounts above the security middleware and exits early, matching responses ship with zero CSP or HSTS headers.
The fix is structural: mount a zero-dependency Middleware #1 at the top of the HTTP server, generating a fresh 128-bit OS CSPRNG nonce (crypto.randomBytes(16).toString('base64')) before any route executes:
import crypto from 'node:crypto';
export function applyObservatory125Headers(res) {
const nonce = crypto.randomBytes(16).toString('base64');
res.setHeader('Strict-Transport-Security', 'max-age=31536000; includeSubDomains; preload');
res.setHeader('Cross-Origin-Opener-Policy', 'same-origin');
res.setHeader('Cross-Origin-Resource-Policy', 'same-origin');
res.setHeader('Cross-Origin-Embedder-Policy', 'credentialless');
res.setHeader('Referrer-Policy', 'strict-origin-when-cross-origin');
res.setHeader('X-Frame-Options', 'SAMEORIGIN');
res.setHeader('X-Content-Type-Options', 'nosniff');
res.setHeader('Permissions-Policy', 'tools=(self)');
res.setHeader('Origin-Agent-Cluster', '?1');
res.setHeader(
'Content-Security-Policy',
[
"default-src 'none'",
`script-src 'self' 'nonce-${nonce}' 'strict-dynamic'`,
"style-src 'self'",
"img-src 'self' data:",
"font-src 'self'",
"connect-src 'self'",
"frame-ancestors 'self'",
"base-uri 'self'",
"form-action 'self'",
"object-src 'none'"
].join('; ')
);
return nonce;
}
3. Static Edge Hosting (firebase.json) Without SSR
Static sites deployed to Firebase Hosting have no per-request Node process to mint dynamic nonces. Instead of falling back to 'unsafe-inline', use two deterministic patterns:
- Externalize Client Scripts: Compile TypeScript/JavaScript into external ES modules (
<script type="module" src="/app.mjs"></script>) covered cleanly byscript-src 'self'. - Pre-Deploy SHA-256 Verification: When a tiny synchronous theme script must stay in
<head>, compute its exact UTF-8 SHA-256 digest during build verification ('sha256-...') and fail the build in CI if any unhashed inline<script>appears indist/.
4. Hardening In-Browser AI Agents (WebMCP)
As browsers introduce client-side tool registration (navigator.modelContext / document.modelContext), CSP and origin headers gain a second responsibility: preventing cross-origin tool hijacking.
Three rules keep WebMCP deployments at 125/100:
- Bundle Tool Registrations Externally: Register
modelContext.registerTool(...)inside compiled ES modules—never inside unhashed inline<script>blocks. - Lock Tool Delegation to Same-Origin: Declare
Permissions-Policy: tools=(self)infirebase.jsonand Cloud Run Middleware #1 so embedded third-party frames cannot invoke or shadow registered browser tools. - Preserve Agent Cluster Isolation: Set
Origin-Agent-Cluster: ?1so the browser isolates the origin’s execution process cleanly.
Security that relies on developer memory eventually drifts. Security compiled into Middleware #1 and verified in CI stays at Grade A+ on every deploy.
Share, Adapt, or Build Upon This Work
Ideas grow stronger when shared openly across the workbench. If this essay sparked a useful direction for your team, keynote, or research, feel free to reference it using the citation below—or reach out anytime to collaborate:
You have reached the end of this reflection.
Honor your finite cognitive budget: look away from the display, allow your thoughts to synthesize in diffuse mode, or capture reflections in the client-side scratchpad below.
Work Together or License Tools
Personal tools and essays are free. Commercial licenses, fonts, archival prints, and written architecture reviews live in the Studio Store.
Technical Storytelling
Clear keynotes, essays, and visual explainers for complex engineering work.
Architecture Second Opinions
Candid written trade-off reviews for AI workflows, privacy, and cloud costs.
Calm UX & Security Audits
High-contrast legibility, low-clutter interfaces, and Grade A+ security headers.
Studio Licenses & Fonts
White-label Solvency Engine, PocketGull v4.0 fonts, and signed archival prints.
Looking for business strategy or field photography journals?
While philgear.dev hosts deep technical architectures and engineering treatises, sister hubs publish specialized dispatches.
Enterprise Advisory & Systems Practice
Strategic technical leadership, fractional CTO advisory, sovereign cloud roadmaps, and business engineering case studies.
Pacific Northwest Optical Field Journal
High-resolution wilderness galleries, optical telemetry, solar and astronomical wayfinding, and conservation essays.
Discussion & Collaborative Feedback
Software and ideas grow through honest conversation and mutual learning.
Have a question on implementation, spotted an architectural trade-off, or want to discuss ideas? Send a direct note.
Explore repositories, open issues, or contribute improvements across open-source codebases.