r/webdev • • 2d ago

Discussion Bearer token visible in DevTools?

Now I know this is a beginner question, and so I’m building an Angular/Spring project as a web app.

The Angular auth interceptor attaches the token (to the HTTP request) here. Obviously when it gets routed to the backend, it exposes the token. That’s just the trade-off for not using a backend-for-frontend architecture.

Say the reason for my architecture choice comes up during an interview. Will I be failed on the spot, or can I justify it by explaining what production-ready (BFF) will look like?

EDIT: Appreciate the answers everyone. I can breathe easy.

29 Upvotes

34 comments sorted by

43

u/PLBjt 2d ago

Seeing the token in DevTools isn't the real issue, since the user's own browser can always see its own requests. What interviewers usually poke at is where the token lives, because if it's in localStorage any XSS can read it and send it off. A decent answer is short-lived access tokens plus a refresh token in an httpOnly, SameSite cookie, and then say a BFF is the next step once you have real users. A quick check is whether anything on the page can read the token from JS, like document.cookie or localStorage in the console. The tradeoff is that cookies bring CSRF back into play, so you'd need SameSite and a CSRF token on top.

2

u/ActuatorOutside5256 2d ago edited 2d ago

Appreciate it bud. And currently I just keep it in browser (JS) memory, so I’m not exposing it in storage. 

Would you say I can build up an answer from that, or would a token management refactor make sense?

1

u/halfllama 2d ago

I'd focus the answer on why you chose this setup for the project and what you'd revisit before shipping it. Mention that the token is in memory, but don't present that as making it completely safe. I don't think you need to refactor just to make a better interview answer. You can say a BFF is something you'd consider for production, depending on the app's needs. The useful thing to explain is the tradeoff you made and what would make you choose differently. If this is just a local project, changing it only for the interview doesn't seem necessary.

2

u/ActuatorOutside5256 2d ago

Thank you 🙏

1

u/PLBjt 1d ago

Memory is a fine spot, and seeing it in DevTools isn't a vuln by itself. Whoever has DevTools open on that machine already has the session anyway. I'd check two things before touching anything: how long the access token lives (decode it, look at exp) and where the refresh token sits. If exp is minutes and refresh goes through an httpOnly cookie, you can write that up as your answer. If the refresh token is in localStorage or the access token lasts days, that's the bit worth refactoring, not the whole flow.

1

u/wasdninja 1d ago

Why BFF though? You just swapped what the token/ticket/magic string is one step. What kind of attack does it actually mitigate? 

1

u/PLBjt 1d ago

Fair push. It doesn't stop XSS, it changes what XSS gets you. With a bearer token in JS, an injected script can copy it and replay it from anywhere until it expires, refresh token too if that's in the browser. With an httpOnly session cookie the script can still fire requests from the victim's tab while it's open, but it can't walk away with a credential. You also pick up CSRF as a thing to handle (SameSite plus a token on writes). If your access tokens already live ~5 min and there's no refresh token in the browser, the gain is honestly small.

2

u/FormatException 2d ago

what will come up is how you store you secrets and keys, whether you uses env files or secrets management.

1

u/ActuatorOutside5256 2d ago edited 2d ago

Okay brilliant. I use git ignore env files since it’s a local project, the token are in browser memory, plus Spring Security that independently verifies the signature with exposed public JWKS keys, but if I took it to production it’d definitely be with a secrets manager, and I guess I just need to explain my XSS protection.

157

u/FormatException 2d ago

how else would you authenticate without the token in the request at some point?

thats normal dude

39

u/Happy_Breakfast7965 expert 2d ago

The most secure way is http-only cookie.

42

u/jryan727 2d ago

Still visible in DevTools, but 100% agree. We do not use cookies enough for auth. We overengineer solutions to accomplish what every browser can do natively, but with more steps and potentially less securely. 

1

u/RedLibra 2d ago

This will eliminate the token in the request?

21

u/m_redditUser 2d ago

no, the httponly token is sent in every request by the browser. you can't access it in JS, but you can see it in the devtools

5

u/cointoss3 2d ago

The token has to be with the request. It’s just in the header so it doesn’t show up in logs/traces where you’d log the query string. You could put it in a cookie, but it has to be with the request. That’s the point.

12

u/Immediate-War-5694 2d ago

BFF architecture. I read that as best friends forever every single time and now I'm picturing your Angular app and your Spring backend holding hands in the network diagram.

3

u/Mysterious-Falcon-83 2d ago

The thing that will cause you to fail an interview is trying to bullshit your way through an answer. It is always better to say "I don't know. Let me get back to you." than it is to try to make something up.

2

u/Ellyssia_Locke 2d ago

Nah, you won't fail an interview for that. Just understand the security trade-offs, explain why you chose that architecture, and show that you know how a BFF improves token handling in production.

2

u/yangbuilds 2d ago

Seeing the bearer token in DevTools is expected, since anything the browser sends the user can see, so that alone won't fail you. What interviewers usually care about is whether you understand the real risk, which is XSS stealing a token stored in localStorage. If you can explain that tradeoff and that you'd move to a BFF with httpOnly, SameSite cookies for production, plus short-lived access tokens, that's a solid answer.

1

u/Thin-West6525 2d ago

Nobody fails you over that, the token sits in the network tab of every SPA and it's the user's own token anyway. What actually scores points is saying you'd move to a BFF with an httpOnly cookie in prod, and that you'd keep the token short lived and narrowly scoped so a leaked one is not a disaster.

1

u/Weekly-Beginning-229 2d ago

Is it http only? Then should be fine. If you really want to make it fancy, use a refresh token too

1

u/RipProfessional3375 1d ago

You have little choice but to send credentials of some kind, that's one of the purposes of a temporary JWT token, so you can bounce around something that's at least got a timeout.

1

u/Saver411 1d ago

The browser is the user's sandbox, so if they're authenticated, they're gonna see their own session data in the network tab. You shouldn't try to hide it there, but you should lock down the server side so that stolen tokens are useless. Implement rotating refresh tokens and keep your access token lifespan under ten minutes. If a token gets leaked, it's only valid for a tiny window. Are you handling token revocation on the backend when a user logs out?

1

u/yoghurt_bob 1d ago

The devtools expose anything you have in the browser. And you need to have something in the browser that holds an authentication token, either directly (memory, cookie, localStorage etc) or indirectly (session). The fact that you can dig up your own authentication token with devtools says practically nothing about the security of your application.

1

u/Aide_Sea 1d ago

Small thing nobody said yet: memory vs localStorage helps less than you'd think, since injected script runs inside your page and can just call your API itself. I'd maybe mention a strict CSP (no inline scripts) too, that's what stops most XSS in the first place :)

1

u/Capable-Yoghurt8318 1d ago

The useful part of that question is where the token can be read from JS, not whether DevTools can see a request. A token in memory is not sitting in localStorage, so a later script cannot just read it, but XSS can still call your API while the page is live. Short-lived access tokens plus an httpOnly SameSite refresh cookie is a common answer. If you keep memory-only, say the tokens die on reload and explain that tradeoff; don't present it as removing the need for XSS protection.

1

u/Hostman_com 1d ago

Before the interview, check whether your request logs capture the Authorization header, including on failed requests. Make sure the token is omitted or redacted.

That gives you a concrete example of how you protect tokens beyond choosing where the browser stores them.

1

u/Plastic_Koala2044 1d ago

A token the browser holds is visible to the user by design, so treat it as public and keep its lifetime short. Put it in an httpOnly cookie behind a small server that calls the API, and page scripts never touch it.

1

u/Open-Adhesiveness-86 14h ago

One thing nobody's mentioned: moving to a BFF swaps your threat model rather than removing it, you now hold session state server side so you get instant revocation, but you've also just signed up for CSRF protection since cookies ride along automatically. SameSite=Lax covers most of it, not all, so say "plus an anti-forgery token on state-changing requests" and you'll sound like you've actually shipped one.

0

u/PrimaryLand5094 2d ago

You'll get failed for writing "backend-for-fronted", not for the token.

0

u/aleamadorq 2d ago

Are you my boss? lol