Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 | 1x 52x 50x 49x 43x 6x 37x 1x 63x 63x 12x 12x 12x 11x 11x 11x 11x 6x 6x 1x 5x 6x 2x 3x 3x 2x 1x 5x 11x 11x 4x 1x 52x 63x 63x 63x 5x 5x 4x 3x 3x 1x 2x 47x 52x 52x 52x 63x 4x 4x 4x 4x 4x 4x 1x 51x 51x 51x 1x 51x 51x 51x 51x 51x 51x 51x 51x 51x 12x 8x 4x 39x 36x 102x 6x 30x 51x 63x 5x 51x 2x 2x 1x 51x | import { env } from "cloudflare:workers";
import { defineMiddleware } from "astro:middleware";
import { DEFAULT_LOCAL_API_URL } from "./lib/constants";
// Deploy-version marker — evaluated once when the Worker script loads, which
// happens on every deploy (Cloudflare instantiates a new isolate per version).
// Prefix the edge cache key with this so any HTML cached against a prior
// deploy (which references now-404 asset hashes like
// `/_astro/AuthModal.OLD.js`) is not served after a redeploy. Without this,
// every deploy leaves users with broken hydration for up to `s-maxage`
// (15 min) until the cache entries naturally expire.
const DEPLOY_VERSION = Date.now().toString(36);
// Determine if this request should use the Cloudflare edge cache.
// Only GET requests for HTML pages — not API routes, not static assets.
function isCacheableRequest(method: string, pathname: string): boolean {
if (method !== "GET") return false;
if (pathname.startsWith("/api/")) return false;
if (pathname.startsWith("/_astro/")) return false;
if (
/\.(js|css|woff2?|ttf|otf|eot|svg|png|jpg|jpeg|gif|webp|ico|xml|txt|json)$/.test(
pathname,
)
)
return false;
return true;
}
export const onRequest = defineMiddleware(async (context, next) => {
const url = new URL(context.request.url);
// Local dev: proxy /cdn-cgi/image/* requests to the actual image URL.
// Cloudflare Image Resizing only works on their edge, not in wrangler dev.
// Format: /cdn-cgi/image/params.../https://host/api/images/path
// or: /cdn-cgi/image/params.../path (relative)
//
// Target URL MUST be validated against env.IMAGE_URL + env.API_URL — the
// path is attacker-controlled (from page src), so without allowlisting a
// request to /cdn-cgi/image/w=1/http://169.254.169.254/ would exfiltrate
// cloud metadata, and /cdn-cgi/image/w=1/http://localhost:7001/api/admin/
// would proxy internal endpoints. In production CF handles /cdn-cgi/*
// before the Worker so this code is dev-only, but a misconfigured
// wrangler route would make it exploitable in prod.
if (url.pathname.startsWith("/cdn-cgi/image/")) {
const rest = url.pathname.slice("/cdn-cgi/image/".length);
// Strip the resize params (everything before the first "/" after params)
// Params look like: onerror=redirect,width=800,height=500,format=webp/...
const slashAfterParams = rest.indexOf("/");
if (slashAfterParams !== -1) {
const imageTarget = rest.slice(slashAfterParams + 1);
const allowedImageOrigin = env.IMAGE_URL || "";
const allowedApiUrl = env.API_URL || DEFAULT_LOCAL_API_URL;
// If it starts with http, it's an absolute URL — fetch only if it
// matches the configured image host or API host.
if (imageTarget.startsWith("http")) {
let targetOrigin: string;
try {
targetOrigin = new URL(imageTarget).origin;
} catch {
return new Response(null, { status: 400 });
}
const allowedOrigins = [
allowedImageOrigin && new URL(allowedImageOrigin).origin,
new URL(allowedApiUrl).origin,
].filter(Boolean) as string[];
if (!allowedOrigins.includes(targetOrigin)) {
return new Response(null, { status: 403 });
}
try {
const imgRes = await fetch(imageTarget);
return new Response(imgRes.body, {
status: imgRes.status,
headers: {
"Content-Type": imgRes.headers.get("Content-Type") || "image/jpeg",
"Cache-Control": "public, max-age=3600",
},
});
} catch {
return new Response(null, { status: 404 });
}
}
// Relative path — proxy through the local API
const imgPath = imageTarget.startsWith("/api/images/")
? imageTarget
: `/api/images/${imageTarget}`;
try {
const imgRes = await fetch(`${allowedApiUrl}${imgPath}`);
return new Response(imgRes.body, {
status: imgRes.status,
headers: {
"Content-Type": imgRes.headers.get("Content-Type") || "image/jpeg",
"Cache-Control": "public, max-age=3600",
},
});
} catch {
return new Response(null, { status: 404 });
}
}
}
// --- SSR session probe ---
// The marketplace is SSR on every navigation (not SPA). Without this, every
// hydration island has to fetch /api/homeowner/auth/get-session after hydrate
// before it can render the logged-in state, causing a visible "logged-out
// flash" for 0.8-1.7s. We forward the session cookie server-side so the
// Astro layout can pass `initialUser` to islands and they render correctly
// on first paint.
const cookieHeader = context.request.headers.get("cookie") || "";
const hasSessionCookie = /ho\.session_token=/.test(cookieHeader);
const apiUrlForSession = env.API_URL || DEFAULT_LOCAL_API_URL;
if (hasSessionCookie) {
try {
const sessionRes = await fetch(
`${apiUrlForSession}/api/homeowner/auth/get-session`,
{ headers: { cookie: cookieHeader } },
);
if (sessionRes.ok) {
const data = (await sessionRes.json()) as
| { user?: HomeownerLocalUser & { emailVerified?: boolean } }
| null;
if (data?.user) {
context.locals.homeownerUser = {
id: data.user.id,
name: data.user.name,
email: data.user.email,
image: data.user.image ?? null,
emailVerified: data.user.emailVerified ?? false,
};
} else {
context.locals.homeownerUser = null;
}
}
} catch {
// Non-fatal: fall back to client-side probe.
}
} else {
context.locals.homeownerUser = null;
}
const cacheable = isCacheableRequest(
context.request.method,
url.pathname,
);
// --- Edge Cache: check for cached HTML response ---
// Cloudflare Workers don't use the CDN cache layer automatically.
// We explicitly use the Cache API to cache SSR responses at the edge PoP.
// Skip entirely when a session cookie is present — otherwise one user's
// personalized HTML (e.g. UserMenu with their name) could be served to
// another user or to anonymous traffic.
const hasCache = typeof caches !== "undefined";
const edgeCacheable = cacheable && !hasSessionCookie;
let cache: Cache | undefined;
let cacheKey: Request | undefined;
if (edgeCacheable && hasCache) {
cache = (caches as unknown as { default: Cache }).default;
// Include DEPLOY_VERSION in the cache key so each deploy starts with a
// clean edge cache namespace. Old entries for the prior version
// naturally expire via s-maxage and never get served.
const cacheUrl = new URL(url.toString());
cacheUrl.searchParams.set("__v", DEPLOY_VERSION);
// Construct the cache key with ONLY method + versioned URL. Forwarding
// `context.request.headers` verbatim fragmented the cache by
// Accept-Encoding / Accept / User-Agent, driving the hit rate toward
// zero for anonymous traffic (each browser-family + locale combination
// got its own cache entry). It also included the Cookie header in the
// key — Cloudflare strips it in practice, but relying on undocumented
// behavior is fragile, and tests under miniflare would not strip.
cacheKey = new Request(cacheUrl.toString(), { method: "GET" });
const cached = await cache.match(cacheKey);
if (cached) {
// Responses returned from the Web Cache API have IMMUTABLE headers.
// If we return this object directly, anything downstream that tries
// to add/modify headers (Astro's RenderContext, the Cloudflare
// adapter, CSP middleware, etc.) throws:
// TypeError: Can't modify immutable headers.
// Wrap the body in a fresh Response with a mutable Headers copy so
// downstream code can modify headers normally.
return new Response(cached.body, {
status: cached.status,
statusText: cached.statusText,
headers: new Headers(cached.headers),
});
}
}
const response = await next();
// Early Hints: Cloudflare converts Link headers to 103 Early Hints automatically
// This tells the browser to preconnect to the image domain before the HTML arrives
const imageUrl = env.IMAGE_URL;
if (imageUrl) {
response.headers.append(
"Link",
`<${imageUrl}>; rel=preconnect; crossorigin`,
);
}
// Add security headers
response.headers.set("X-Content-Type-Options", "nosniff");
response.headers.set("X-Frame-Options", "SAMEORIGIN");
response.headers.set("X-XSS-Protection", "1; mode=block");
response.headers.set("Referrer-Policy", "strict-origin-when-cross-origin");
// CSP allows connections to all decorrocket.com subdomains (api, api-preview, etc.),
// the runtime-configured API origin (env.API_URL — required for Conductor workspaces
// where the API runs on a non-default port like 7021/8021), and the canonical
// localhost fallback. The wildcard subdomains cover prod/preview; env.API_URL
// covers any runtime port; the literal fallbacks cover edge cases where env is
// not yet bound (e.g. early-error responses). See:
// https://github.com/withastro/astro/issues/11555
const allowedOrigins = [
"https://*.decorrocket.com", // All production/preview subdomains
"https://*.interioring.com", // All production subdomains (new brand)
env.API_URL, // Runtime API origin (Conductor port slot, e.g. http://localhost:7021)
DEFAULT_LOCAL_API_URL, // Canonical localhost fallback (legacy port 7001)
"http://localhost:8887", // Worktree API development
]
.filter(Boolean)
.join(" ");
// Content Security Policy for marketplace pages
// More permissive than API since we need to load styles, scripts, and images
const csp = [
"default-src 'self'",
// Allow inline styles for Tailwind/Astro
"style-src 'self' 'unsafe-inline'",
// Allow inline scripts for Astro hydration + Cloudflare Web Analytics (auto-injected via observability)
"script-src 'self' 'unsafe-inline' https://static.cloudflareinsights.com",
// Allow images from self, API (for R2 images), and data URIs
`img-src 'self' data: blob: https: ${allowedOrigins}`,
// Allow fonts from self (self-hosted via fontsource)
"font-src 'self'",
// Allow connections to API, WhatsApp, and Cloudflare Analytics beacon
`connect-src 'self' https://wa.me https://cloudflareinsights.com https://*.ingest.us.sentry.io ${allowedOrigins}`,
// Allow form actions to self
"form-action 'self'",
// Allow framing only by self
"frame-ancestors 'self'",
// Allow YouTube embeds
"frame-src 'self' https://www.youtube-nocookie.com https://www.youtube.com",
// Block object embeds
"object-src 'none'",
// Base URI restriction
"base-uri 'self'",
].join("; ");
response.headers.set("Content-Security-Policy", csp);
// Permissions Policy (formerly Feature Policy)
response.headers.set(
"Permissions-Policy",
"camera=(), microphone=(), geolocation=()",
);
// Add caching headers
if (
url.pathname.startsWith("/_astro/") ||
url.pathname.match(
/\.(js|css|woff2?|ttf|otf|eot|svg|png|jpg|jpeg|gif|webp|ico)$/,
)
) {
// Static assets with fingerprinted filenames — cache forever when the
// asset actually exists. For 4xx/5xx (typically a stale HTML reference
// to an old hash from a prior deploy, e.g. `/_astro/client.OLD.js`),
// use short no-store caching so the browser re-fetches the HTML on
// next nav and picks up the current asset hashes. Without this guard,
// a 1-year immutable cache pins the 404 in the browser and the user
// sees a broken site until they hard-refresh.
if (response.status >= 200 && response.status < 400) {
response.headers.set(
"Cache-Control",
"public, max-age=31536000, immutable",
);
} else {
response.headers.set("Cache-Control", "no-store, max-age=0");
}
} else if (
context.request.method === "GET" &&
!url.pathname.startsWith("/api/")
) {
// Listing pages only (not detail pages): cache 30 min at edge
if (
["/pros", "/projects", "/rooms"].some(
(p) => url.pathname === p || url.pathname === `${p}/`,
)
) {
response.headers.set(
"Cache-Control",
"public, max-age=0, s-maxage=1800, stale-while-revalidate=3600",
);
} else {
// Default SSR pages (homepage, how-it-works, etc.): 15 min edge cache
response.headers.set(
"Cache-Control",
"public, max-age=0, s-maxage=900, stale-while-revalidate=1800",
);
}
}
// Prevent downstream/CDN caching of personalized pages. /account/* is
// always opted out — even without a session cookie — so the bfcache
// can't restore a stale signed-in render after sign-out (issue #462).
// The cookie-based branch still covers other personalized surfaces
// (e.g. /projects with the user's avatar in UserMenu).
const isAccountPath =
url.pathname === "/account" || url.pathname.startsWith("/account/");
if (hasSessionCookie || isAccountPath) {
response.headers.set("Cache-Control", "private, no-store");
}
// --- Edge Cache: store successful HTML responses ---
if (edgeCacheable && cache && cacheKey && response.status === 200) {
// Astro 6 + @astrojs/cloudflare 13 expose the ExecutionContext
// as `Astro.locals.cfContext` (replacing the old runtime.ctx).
const waitUntil = context.locals?.cfContext?.waitUntil?.bind(
context.locals.cfContext,
);
if (waitUntil) {
waitUntil(cache.put(cacheKey, response.clone()));
}
}
return response;
});
|