Why can't I set session cookies? [Sovled] - javascript

I set session cookies but it creates new cookies. I'm tired of this Do you know how to fix it?
Code:
document.cookie = ".ROBLOSECURITY=cookie; expires=session; path=/";

Let's check documentation
;domain=domain (e.g., 'example.com' or 'subdomain.example.com'). If
not specified, this defaults to the host portion of the current
document location. Contrary to earlier specifications, leading dots in
domain names are ignored, but browsers may decline to set the cookie
containing such dots. If a domain is specified, subdomains are always
included.
Note: The domain must match the domain of the JavaScript
origin. Setting cookies to foreign domains will be silently ignored.
Your first cookie with domain www.roblox.com will be accessible only at www.roblox.com/... page but .roblox.com's cookie may be accessed by JS from all roblox.com subdomains.
Here is a good answer
So as #smac89 wrote in comment, You should add domain when create new cookie
document.cookie = ".ROBLOSECURITY=cookie; expires=session; path=/; domain=.roblox.com"

There is no syntax for what you want.
You can either not set the expiration value, the cookie will expire at the end of the session, or choose an arbitrarily large value.
Be aware that some browsers have problems with dates past 2038 (when the Unix epoch time exceeds a 32-bit integer).
See : https://stackoverflow.com/a/532660/1901857

Related

Is the order in which cookies show up in document.cookie deterministic?

It's possible to have two cookies with the same name but different values in document.cookie if a subdomain and a domain set the same cookie.
For example, if I:
Visit subdomain.domain.com first, which sets a my_cookie=foocookie.
And then visit domain.com second, which sets a my_cookie=boocookie.
document.cookie in Chrome 81 will show my_cookie=foo; my_cookie=boo on subdomain.domain.com, in the order in which the cookies were set with the most recent cookie last.
My question is, can this ordering be relied on (and is there a rfc that talks about this)?
Interestingly found this in an RFC about not relying on cookie orders between different subdomians:
reference: https://tools.ietf.org/rfc/rfc6265.txt
4.2.2. Semantics

Setting a cookie on .github.io domain in JavaScript

While on a page on subdomain.foo.com, I know it is possible to set a cookie in JavaScript with a domain=foo.com clause (or domain=.foo.com according to earlier specifications), and have that cookie apply to all subdomains.
When I open up the developer console in Chrome on a GitHub Pages page (say, yelp.github.io) and try this using a domain=github.io clause, the cookie doesn't get set (document.cookie yields the empty string). I only seem to be able to get the cookie to set if I omit the domain clause, or use domain=yelp.github.io.
I can understand why GitHub would want to restrict cookie scope this way for security reasons, but I'm not sure how this is actually working or what's behind the behavior I'm seeing. Is there something special about the github.io domain? Is there a security policy being applied I'm not aware of? Or am I just doing it wrong?
document.cookie = 'foo=1; domain=github.io'
According to the relevant specification (RFC 6265), cookies are rejected for public suffixes if set from a subdomain:
If the user agent is configured to reject "public suffixes" and the domain-attribute is a public suffix:
If the domain-attribute is identical to the canonicalized request-host:
Let the domain-attribute be the empty string.
Otherwise:
Ignore the cookie entirely and abort these steps.
NOTE: A "public suffix" is a domain that is controlled by a
public registry, such as "com", "co.uk", and "pvt.k12.wy.us".
This step is essential for preventing attacker.com from
disrupting the integrity of example.com by setting a cookie
with a Domain attribute of "com". Unfortunately, the set of
public suffixes (also known as "registry controlled domains")
changes over time. If feasible, user agents SHOULD use an
up-to-date public suffix list, such as the one maintained by
the Mozilla project at http://publicsuffix.org/.
github.io is on the public suffix list.

Javascript - Security of document.cookie

I have an application that creates Cloud Front cookies using the AWS CloudFront API and Lambda. Unfortunately I can't set cookies using the standard HTTP response format and have to use document.cookie to set cookies to my users' browsers from an HTML page. The cookie includes a policy to grant access to content, a signature to confirm authenticity of the cookie, and key-pair ID. A back-end script on Lambda creates the cookie and sends it to the requester as a payload, which then gets passed as a variable to document.cookie.
I've read a lot about securing cookies (HttpOnly, session cookie, secure flag, etc.) and I'm trying to understand the security risks of document.cookie. Is there a difference between setting cookies through Http response and document.cookie in the context of security? Would it be possible for a malicious user to insert their own policy into the cookie as the cookie is created client-side, giving them access to other content despite the page being read only?
Here's some code for reference:
payload = data["Payload"]
jsoned = JSON.parse(payload)
cookie = jsoned['cookie']
redirectUrl = jsoned['redirectUrl']
document.cookie = 'CloudFront-Policy' + "=" + cookie['CloudFront-Policy'] + "; path=/mydirectory";
document.cookie = 'CloudFront-Key-Pair-Id' + "=" + cookie['CloudFront-Key-Pair-Id'] + "; path=/mydirectory"
document.cookie = 'CloudFront-Signature' + "=" + cookie['CloudFront-Signature'] + "; path=/mydirectory"
My first time posting to this. Thanks for the help in advance.
-Ken
Is there a difference between setting cookies through Http response and document.cookie in the context of security?
Not really. An HTTP cookie can be set with httponly, but that's a only very weak mitigation against XSS, not really a proper security measure in itself.
Would it be possible for a malicious user to insert their own policy into the cookie as the cookie is created client-side
Yes, but it already was for the HTTP cookie; they're both stored client-side and thus within reach of an untrusted client.
This is what the signature is for, right? If it's correctly implemented it should prevent tampering with the content it signs.
Nothing of "direct" value should ever be stored in a cookie, period.
All validation / processing of the cookie's value should occur server-side (regarding any sensitive information) and the only thing a cookie should contain is some sort of guid (or perhaps a couple of guid's.) And all "client-side" id's that are stored in a cookie, should be encoded in a manner to both prevent tampering & detect tampering on the server side.
In reference to the comments, I stand by this statement ...
"Any information given to the client, should be considered compromised."
... and will expand my answer ... You have no idea what "client application" will be used as it doesn't have to be a "browser" (Postman / custom apps can interact with your website directly, with the intention of directly examining everything you send) as well as proxies (or worse malicious man-in-the-middle apps), network sniffers, etc ... so that being said, both the "client side application / 'loaded page'" && any other data (including cookies) should be considered compromised from the perspective that you should 'not' consider any aspect guaranteed with respect to a future client response.
i.e. Here is an example of a vulnerability...
you have a site that uses the value in a cookie to restrict (using client-side javascript) the options in a drop-down list (or some other functionality)
this would be a bad practice, as the user can attack this many different ways...
"modify" the cookie values
"edit" the client side javascript in many ways
"manually" submit any "response" to your web application endpoints
essentially spoof any value to any input
So in summary, anything given to the client should be considered "insecure", and you need to handle any "return values" from the client as "compromised / malicious".

Mobile browser(chrome,safari) cookies not deleted on closing session

Mobile browser(chrome, safari) not killing cookies with expire time set to '0', Same cookie persists when browser is reopened...
Cookies must be deleted with the same parameters as they were set with. If the value argument is an empty string, or FALSE, and all other arguments match a previous call to setcookie, then the cookie with the specified name will be deleted from the remote client. This is internally achieved by setting value to 'deleted' and expiration time to one year in past.
From the official manual: http://php.net/manual/en/function.setcookie.php

Cant read my cookie from the root of my site

I successfully set a cookie with javascript on one page like this:
..
I went to this article and took the code from it:
UPDATE :
**http://techpatterns.com/downloads/javascript_cookies.php**
The code works.. but I can set and read my cookie from one page only, when I go to the document root , the cookie isnt available there anymore..
I set my cookie when i am in a subfolder of my directory
I am also trying to set it this way:
document.cookie =
"landing_page_ref=" + encodeURIComponent("FBLND1") +
"; path=/; " ;
but i dont know where i am wrong
Session cookies (which are deleted when the browser is closed) are created by not specifying an explicit expiration time.
function setSessionCookie(c_name,value,exdays) {
document.cookie=c_name + "=" + escape(value);
}
That said, I'd use a robust cookie library to handle cookies rather than trying to roll-my-own.
There's no way to set a cookie to expire based on closing the browser and have an expiration time. That functionality is determined by the user's browser. If they have it set up to clear their cookies upon closing, then it will delete your cookie regardless of expiration time.
Your best bet would be setting the cookie to a relatively short lifetime (say 30 minutes or so) and refreshing that cookie on each page view. That would allow you to expire the cookie after 30 minutes of inactivity on your site. It's not quite the same thing, but as there's no way to enforce what you're looking for, it's a close second.
There are two type of cookies. persistent and session. Use session cookie for it. These cookies expire whenever you close your browser. To convert a persistent cookie to a session cookie just skip expire time.

Categories