My Angular app uses an interceptor to attach a bearer token to requests sent to a Spring backend. The token is kept in JavaScript memory, but it is visible in the browser's network tools. Is that a security problem, and how would I explain the choice in an interview?
Is it a problem if a bearer token shows up in browser developer tools?
Answers
Seeing your own token in the network tools is expected. The browser has to send the credential, so the person controlling that browser can inspect it. That alone doesn't tell you whether the app is insecure.
The more useful questions are how the token gets there, how long it lasts, what it can access, and what happens if someone steals it. Use HTTPS, validate tokens on the backend, and keep their lifetime and permissions appropriate for the job.
Reply to this answer
Memory is a reasonable choice if you want to avoid leaving the access token in persistent browser storage. The trade-off is that a reload loses the in-memory state, so you need a plan for restoring the session or asking the user to sign in again.
It doesn't solve cross-site scripting, though. Injected code may be able to reach a token in memory depending on how the app handles it, and it can make authenticated requests while the page is open even if it can't extract the token. Preventing XSS still matters. A restrictive content security policy can help as one layer, along with handling untrusted content safely.
Reply to this answer
A backend-for-frontend changes where the access token lives. The browser sends a session cookie to your backend, and that backend holds and uses the token when calling the relevant API. That can keep the bearer token out of browser JavaScript and reduce the chance of an injected script taking it away for later use.
It doesn't make XSS harmless. Malicious code can still act through the user's session while it runs. Cookies also need care: use HttpOnly and Secure, choose an appropriate SameSite setting, and address cross-site request forgery for cookie-authenticated requests. The session cookie itself is still a credential, even though JavaScript can't read an HttpOnly cookie.
3d!parcelmarble_472:
So a BFF is useful when keeping the API token out of the browser is worth the extra server-side session management, not simply because DevTools can see requests?
Reply to this answer
You could also use a short-lived access token in memory and an HttpOnly cookie for a refresh credential. That lets the app obtain another access token without storing the refresh credential in JavaScript-accessible storage. Cookie settings and CSRF protection still need thought, and a short lifetime only limits the window after a token leaks. It doesn't prevent misuse during that window.
Reply to this answer
For an interview, explain what you built and why, then be clear about its limits. You can discuss XSS, token lifetime, backend validation, and when a BFF might be worth adding. No one can promise how a particular interviewer will score it. If you don't know something, say so rather than making up an answer.
Reply to this answer
Add your answer
Answers are reviewed before publication.
_j69!plovrani.3:
So memory reduces persistence, but I shouldn't describe it as protection against all injected scripts?
q.584!tavdori:
Right. Describe what it avoids and what an attacker could still do in an active session.