Sphere
Log in Join
☆ 1

Is it safe to put a JWT in a non-HttpOnly cookie if JS needs to read it?

Single-page app, API on a separate subdomain. We need the client to read a claim from the token to decide which UI to render, so the cookie cannot be HttpOnly. It is SameSite=Strict and Secure. How much risk does that actually add, and what is the usual mitigation?

⚑ Report

4 replies

☆ 2
P Paul ·2026-11-14#1

It makes the token available to any script running on your origin, so one XSS becomes a token theft instead of a session abuse. SameSite helps with CSRF, not with XSS.

⚑ Report
☆ 0
N Nick ·2026-11-17#2

So the mitigation has to be on the script side. We have a strict CSP with no inline scripts, but one third-party chat widget needs an exception.

⚑ Report
☆ 0
Y Yuki ·2026-11-18#3

That widget is your weak point. Better pattern: keep the auth cookie HttpOnly and expose the non-sensitive claim from a /me endpoint. The client reads the claim from JSON, not from the token.

⚑ Report
☆ 0
N Nick ·2026-11-18#4

That is the design we went with. The token is now HttpOnly and the UI reads role from /me.

⚑ Report
Log in to reply.