Validating Ray ID

Am working on a PR to forward the Cf-Ray value for a logging context, but would like to be able to validate the value I receive first. Any assistance would be appreciated.
9 Replies
Zesh
ZeshOP2w ago
For example if Cf-Ray is 230b030023ae2822-SJC will it consistently be structured like this and contain as many characters as this?
Erisa
Erisa2w ago
No, the structure isn't guaranteed to that degree and if you rely on it then your application may break years down the line
Zesh
ZeshOP2w ago
OK. Do you potentially have a suggestion as to how I can verify that the value is indeed a valid Ray ID provided by CF?
Erisa
Erisa2w ago
I dont believe you can, unless you have an Enterprise product like http logpush setup on the zone to do a rayid lookup in (but even then you'd hardly be able to do it for every single request) Is your code going to be running in the request/response flow of some website, like a web server or such?
Zesh
ZeshOP2w ago
OK, that's informative. SaaS application, yes
Erisa
Erisa2w ago
If the request comes from a Cloudflare IP address then it's probably reasonable to assume the ray id is correct
Erisa
Erisa2w ago
Specifically one of these https://www.cloudflare.com/en-gb/ips/
IP Ranges | Cloudflare
This page is intended to be the definitive source of Cloudflare’s current IP ranges.
Zesh
ZeshOP2w ago
Ah, that could be a good combination. Thank you Erisa!
asuffield
asuffield2w ago
The above is correct, but I'll add - there isn't really such a thing as an "invalid" ray ID, it's more or less just an integer. It has no real meaning beyond being something you can find in logs, if any logs exist.

Did you find this page helpful?