Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP PHP User Authentication What is Authentication Cookies vs. Session Storage

Sergey Podgornyy
Sergey Podgornyy
20,660 Points

Alena, what are you talking about? O_o

In the video, Alena said

Sessions are prone to cross-site scripting because the sessions are accessible via JavaScript...

WHAT? O_o How Front-End could fetch an information, which is stored on a server? If you said, that JavaScript could do this, so please explain how or provide a peace of code that can perform this action.

Sessions stored on a server in files, or in database (if you are using Redis, for example) and front-end side could not get access to these files without PHPSESSID, which is by default stored in cookies, and cookies are insecure, because you can get values from user cookies. In this case, highly recommended to store all front-end data into localStorage instead of cookies

Samuel Ferree
Samuel Ferree
31,722 Points

if PHPSESSID is by default stored in cookies, and cookies are insecure, would that not mean that by default, sessions are insecure?

Sergey Podgornyy
Sergey Podgornyy
20,660 Points

Samuel, no, that mean, that cookies are insecure, but not sessions. And by the way, Alena said, that's possible to get access to session via JavaScript, what is absolutely wrong

Simon Coates
Simon Coates
28,694 Points

I assumed Alena Holligan meant they could access PHPSESSID (if they could compromise your javascript or some you rely upon) and then use this to access the session (indirectly). The owasp link seemed to indicate the attack was possible, but didn't seem to specify a preferred strategy to deal with it.

Alena Holligan
Alena Holligan
Treehouse Teacher

I did link to a couple full articles that go into explanations in more details since it's not an easy discussion to have in the notes.

Check out:

Sergey Podgornyy
Sergey Podgornyy
20,660 Points

Alena, did you read the links you have provided to me?

first the attacker uses a sniffer to capture a valid token session called β€œSession ID”, then he uses the valid token session to gain unauthorized access to the Web Server.

You could get a session, only if you will hijack Session ID, which is sent with every HTTP request, but you couldn't fetch session directly via JavaScript

The attacker can compromise the session token by using malicious code or programs running at the client-side. The example in figure 3 uses an XSS attack to show the cookie value of the current session; using the same technique it's possible to create a specific JavaScript code that will send the cookie to the attacker. <SCRIPT>alert(document.cookie);</SCRIPT>

The attacker can only compromise the session token (not session), which is stored in cookies. So cookies are insecure, because cookies stored on client-side, so only cookies are accessible via JavaScript, not sessions

If you store JWT in cookie, you do manualy the same, that web server do automaticaly. Webserver store Session ID in cookies, which can be easily grabbed, you store JWT in cookies, which can be easily grabbed. So where is security? Instead of 32 characters Session ID with each HTTP request you send JWT, which is usualy much longer. What about performance?

By the way, JWT ideally should be sent in Authorization: Bearer <token> header

Sergey Podgornyy
Sergey Podgornyy
20,660 Points

Simon, yes, indirecly you can get session, if you will know Session Id, which by default stored in cookies. But in this course Alena explain, how to use JWT instead of session. Unfortuanly, she does the same vulnerability, but instead of storing in cookies and sending with each HTTP request 32 characters Session ID, she created JWT (base64 encoded JSON), which is much longer, then Session ID

I'm a noob, so correct me if I'm wrong, but I believe she said sessions are prone to XSS and cookies are prone to CSRF. Or at least that's what was on the quiz.