Webapp behind Cloudflare Access cant access Cookies

Hello I have a Webapp which is behind Access. I want to validate the CF_Authorization Cookie but when i try to get document.cookie i only get an empty string. On the same page when i open the DevTools and go to Application > Cookies > DOMAIN.TLD > I see the cookies with the current domain. Do i have to enable anything to get this working?
29 Replies
louis
louisOP11mo ago
Ayaan
Ayaan11mo ago
@Louis please upload your image on external sources(e.g. Imgbb.com) and put link here because the discord media server is having problem now
louis
louisOP11mo ago
one sec
louis
louisOP11mo ago
ImgBB
SCR-20240113-nxve hosted at ImgBB
Image SCR-20240113-nxve hosted in ImgBB
ImgBB
SCR-20240113-nxyj hosted at ImgBB
Image SCR-20240113-nxyj hosted in ImgBB
louis
louisOP11mo ago
i want to access cf_authorization but i cant because document.cookie returns an empty string @Minsu Kim
Ayaan
Ayaan11mo ago
CF_Authorization is HTTP Only cookie in this case, so you cannot access with document.cookie @Louis
louis
louisOP11mo ago
is there any way to get the jwt in JS?
Ayaan
Ayaan11mo ago
You should get this attribute in backend level. You can search how to get clients’ cookie in backend language which you are using If you don't know how to use a backend framework but know some intermediate level of JS, you can create CF workers for this.
louis
louisOP11mo ago
yeah tried that but the problem is that when i protect my backend api i have to use service auth because else i only get a 302 when i try to call it (because of the redirect to the login form of access) and with the service auth policy i dont get the identity in the jwt
Ayaan
Ayaan11mo ago
So your backend is just for api right?
louis
louisOP11mo ago
yes its a worker
Ayaan
Ayaan11mo ago
Front-end is dedicated for your case right?
louis
louisOP11mo ago
yes so the frontend is app.example.com -> Protected with Access with allow rule and access group etc. api.example.com -> Protected with service auth and service token
Ayaan
Ayaan11mo ago
Then I am so sorry for saying this, there is no way to get CF_Authorization
louis
louisOP11mo ago
okay sad thx then i have to find an alternative way
Ayaan
Ayaan11mo ago
Due to Chrome and more browsers’ security policy, It is strictly prohibited for getting http only cookie in front-end side I suggest that you make your API server and host by yourself, And use the reverse proxy trick with your server It means App.example.com -> your frontend App.example.com/api -> Your API server Then the credentials will be shared with API server, which means you can feel free to use credentials
louis
louisOP11mo ago
good point cant i do this with cloudflare worker routes? thank you so much it works @Minsu Kim fyi: i now changed the api to the /api endpoint like you said and added 2 policys to the application. one with access and one with service token because i need the service token for mobile app authentication (we also have a flutter app which authenticates via service token before the user logs in)
Ayaan
Ayaan11mo ago
you can do this but not recommended with free plan. if you using paid plan for workers, yes feel free to use
louis
louisOP11mo ago
yeah i have the paid workers plan with paid u mean for workers not the domain right? so i pay 5$/month for workers subscription
Ayaan
Ayaan11mo ago
Yep workers
louis
louisOP11mo ago
good
Ayaan
Ayaan11mo ago
because reverse proxy means workers proxy your service, user accesses workers. It means all traffic will comes to workers,
louis
louisOP11mo ago
okay
Ayaan
Ayaan11mo ago
then you will be charged for this and asked for pay, but you have subscription, you can do this with workers find some references from GitHub and Google
louis
louisOP11mo ago
alright thank you!
Ayaan
Ayaan11mo ago
I will give you some piece of code that I am using for my service
Ayaan
Ayaan11mo ago
@Louis this code is made with deno, but you can simply migrate to workers if you have some knowledge about how workers works. https://github.com/LuanRT/YouTube.js/blob/main/examples/browser/proxy/deno.ts
GitHub
YouTube.js/examples/browser/proxy/deno.ts at main · LuanRT/YouTube.js
A wrapper around YouTube's internal API — reverse engineering InnerTube - LuanRT/YouTube.js
Ayaan
Ayaan11mo ago
this code is working with __host parameter, but you may want to replace with constant value you can migrate this code to workers by: install wrangler and workerd remove deno import statement in line 1 and port variable in line 3 remove statement that calling serve function in line 84 write down code that exports handler function to run in workers deploy it by wrangler cli, it will be works
louis
louisOP11mo ago
thank you! i will look into it!
Want results from more Discord servers?
Add your server