Enriching EventContext

What is the best way to enrich EventContext? For security reason we need to a lot of checks during the middleware, for example to check for CSRF, sessions/login, and so on. It would be really helpful to be able to pass more data down the next function/middleware. This cannot be done directly with headers in a safe way. What's the best way to do so? what's a safe way to enrich the context? Related: https://developers.cloudflare.com/pages/functions/api-reference/ lists some fields that are not described.
API reference · Cloudflare Pages docs
Learn about the APIs used within Pages Functions.
10 Replies
Hello, I’m Allie!
req.cf should be passed around without being modified
Paolo of Lost Pages
Paolo of Lost PagesOP12mo ago
yep, I'm not touching request. I think it's immutable too (at least it seems to be immutable for workers)
Hello, I’m Allie!
No, I mean that it shouldn't be modified by Pages, so you should be able to use that As long as you don't wipe out anything that is already on req.cf it should be fine to tack additional stuff on
Paolo of Lost Pages
Paolo of Lost PagesOP12mo ago
yes but there is a risk in clobbering future properties
Hello, I’m Allie!
I mean, if you use a random-enough property name, you shouldn't have any issues
Paolo of Lost Pages
Paolo of Lost PagesOP12mo ago
where is the .cf property? also maybe it would be safer to add a property to context.env
Hello, I’m Allie!
context.request.cf
Paolo of Lost Pages
Paolo of Lost PagesOP12mo ago
it does work but it also is a bodge purpleblob from the cloudflare instrumentation" does not feel like a good place to add details 🙂 is there a place to file a feature request?
Hello, I’m Allie!
GitHub
GitHub - cloudflare/workers-sdk: ⛅️ Home to Wrangler, the CLI for C...
⛅️ Home to Wrangler, the CLI for Cloudflare Workers® - GitHub - cloudflare/workers-sdk: ⛅️ Home to Wrangler, the CLI for Cloudflare Workers®
Paolo of Lost Pages
Paolo of Lost PagesOP12mo ago
thank you so much for the support
Want results from more Discord servers?
Add your server