HttpWebRequest Cookie weirdness - cookies

I'm sure I must be doing something wrong. But can't for the life of me figure out what is going on.
I have a problem where it seems that the HttpWebRequest class in the framework is not correctly parsing the cookies from a web response.
I'm using Fiddler to see what is going on and after making a request, the headers of the response look as such:
HTTP/1.1 200 Ok
Connection: close
Date: Wed, 14 Jan 2009 18:20:31 GMT
Server: Microsoft-IIS/6.0
P3P: policyref="/w3c/p3p.xml", CP="CAO DSP IND COR ADM CONo CUR CUSi DEV PSA PSD DELi OUR COM NAV PHY ONL PUR UNI"
Set-Cookie: user=v.5,0,EX01E508801E$97$2E401000t$1BV6$A1$EC$104$A1$EC$104$A1$EC$104$21O001000$1E31!90$7CP$AE$3F$F3$D8$19o$BC$1Cd$23; Domain=.thedomain.com; path=/
Set-Cookie: minfo=v.4,EX019ECD28D6k$A3$CA$0C$CE$A2$D6$AD$D4!2$8A$EF$E8n$91$96$E1$D7$C8$0F$98$AA$ED$DC$40V$AB$9C$C1$9CF$C9$C1zIF$3A$93$C6$A7$DF$A1$7E$A7$A1$A8$BD$A6$94c$D5$E8$2F$F4$AF$A2$DF$80$89$BA$BBd$F6$2C$B6$A8; expires=Sunday, 31-Dec-2014 23:59:59 GMT; Domain=.thedomain.com; path=/
Set-Cookie: accttype=v.2,3,1,EX017E651B09k$A3$CA$0C$DB$A2$CB$AD$D9$8A$8C$EF$E8t$91$90$E1$DC$C89$98$AA$E0$DC$40O$A8$A4$C1$9C; expires=Sunday, 31-Dec-2014 23:59:59 GMT; Domain=.thedomain.com; path=/
Set-Cookie: tpid=v.1,20001; expires=Sunday, 31-Dec-2014 23:59:59 GMT; Domain=.thedomain.com; path=/
Set-Cookie: MC1=GUID=541977e04a341a2a4f4cdaaf49615487; expires=Sunday, 31-Dec-2014 23:59:59 GMT; Domain=.thedomain.com; path=/
Set-Cookie: linfo=v.4,EQC|0|0|255|1|0||||||||0|0|0||0|0|0|-1|-1; expires=Sunday, 31-Dec-2014 23:59:59 GMT; Domain=.thedomain.com; path=/
Set-Cookie: group=v.1,0; expires=Sunday, 31-Dec-2014 23:59:59 GMT; Domain=.thedomain.com; path=/
Content-Type: text/html
But when I look at the response.Cookies, I see far more cookies that I am expecting, with values of different cookies being split up into different cookies.
Manually getting the headers seems to result in more wierdness
eg: the code
foreach(string cookie in response.Headers.GetValues("Set-Cookie"))
{
Console.WriteLine("Cookie found: " + cookie);
}
produces the output:
Cookie found: user=v.5
Cookie found: 0
Cookie found: EX01E508801E$97$2E401000t$1BV6$A1$EC$104$A1$EC$104$A1$EC$104$21O00
1000$1E31!90$7CP$AE$3F$F3$D8$19o$BC$1Cd$23; Domain=.thedomain.com; path=/
Cookie found: minfo=v.4
Cookie found: EX019ECD28D6k$A3$CA$0C$CE$A2$D6$AD$D4!2$8A$EF$E8n$91$96$E1$D7$C8$0
F$98$AA$ED$DC$40V$AB$9C$C1$9CF$C9$C1zIF$3A$93$C6$A7$DF$A1$7E$A7$A1$A8$BD$A6$94c$
D5$E8$2F$F4$AF$A2$DF$80$89$BA$BBd$F6$2C$B6$A8; expires=Sunday
Cookie found: 31-Dec-2014 23:59:59 GMT; Domain=.thedomain.com; path=/
Cookie found: accttype=v.2
Cookie found: 3
Cookie found: 1
Cookie found: EX017E651B09k$A3$CA$0C$DB$A2$CB$AD$D9$8A$8C$EF$E8t$91$90$E1$DC$C89
$98$AA$E0$DC$40O$A8$A4$C1$9C; expires=Sunday
Cookie found: 31-Dec-2014 23:59:59 GMT; Domain=.thedomain.com; path=/
Cookie found: tpid=v.1
Cookie found: 20001; expires=Sunday
Cookie found: 31-Dec-2014 23:59:59 GMT; Domain=.thedomain.com; path=/
Cookie found: MC1=GUID=541977e04a341a2a4f4cdaaf49615487; expires=Sunday
Cookie found: 31-Dec-2014 23:59:59 GMT; Domain=.thedomain.com; path=/
Cookie found: linfo=v.4
Cookie found: EQC|0|0|255|1|0||||||||0|0|0||0|0|0|-1|-1; expires=Sunday
Cookie found: 31-Dec-2014 23:59:59 GMT; Domain=.thedomain.com; path=/
Cookie found: group=v.1
Cookie found: 0; expires=Sunday
Cookie found: 31-Dec-2014 23:59:59 GMT; Domain=.thedomain.com; path=/
as you can see - the first cookie in the list raw response:
Set-Cookie: user=v.5,0,EX01E508801
is getting split into:
Cookie found: user=v.5
Cookie found: 0
Cookie found: EX01E508801E$..........
So - what's going on here?
Am I wrong?
Is the HttpWebRequest class incorrectly parsing the http headers?
Is the webserver that it spitting out the requests producing invalid http headers?

Commas in cookie values are to be avoided - you may need to encode them differently.
The original netscape spec for cookies was a little vague on this issue, stating
NAME=VALUE :
This string is a sequence of characters excluding semi-colon, comma
and white space. If there is a need to
place such data in the name or value,
some encoding method such as URL style
%XX encoding is recommended, though no
encoding is defined or required.
recommended, but not required!

It seems that the server sends incorrect cookie values. IMHO commas are not allowed in cookie values. They need to be encoded with %2C.

you may want to use the integer overload of GetValues(i); you should only get back 7 strings then

Related

how to get some of the cookies from the response header's Set-Cookie

need to get some cookie out from the Set-Cookie and pass the cookie in next request header's cookie. The sample Set-Cookie is like:
Set-Cookie:
AWSALB=8KRpAv...hpOJQm; Expires=Thu, 29 Dec 2022 13:45:03 GMT; Path=/
AWSALBCORS=8KRpAv...pOJQm; Expires=Thu, 29 Dec 2022 13:45:03 GMT; Path=/; SameSite=None; Secure
OTHER_BROWSER=1qpa...5a4jn; Domain=google.com; Path=/; Secure; SameSite=None
When trying to get the Set-Cookie
HttpURLConnection connection = url..openConnection();
... ...
String response_header_setCookie = connection.getHeaderField("Set-Cookie");
// the response_header_setCookie has only OTHER_BROWSER=1qpa...5a4jn; Domain=google.com; Path=/; Secure; SameSite=None
It returns "the latest" added one only.
Question:
How to selectively extract some of the cookies from "Set-Cookie" (i.e. by the cookie name AWSALB)?
How get the cookie part only (cut off the Domain/path etc.)? Is it safe to use the first ";"? theSingleCookieValue.substring( 0, theSingleCookieValue.indexOf(";")+1);

Find all cookies used in a website, not on single page

Are there any ways like online tools or browser extensions to find all the cookies used on a website? I already know we can get all the cookies on each page but I want to know if I can get this done for the whole website. What I am basically looking for is to get the list of all the cookies used and give the customer the ability to choose which cookies can be stored.
You'll only get to know about cookies on each page you visit - every page will see a cookie with a / path, but cookies using other paths won't show up until you visit the pages they correspond to. e.g. say you have an editor that saves preferences in a cookie when you visit /edit, you can't tell that cookie exists until you visit that path. So that means you do indeed need to scan every page.
One way to do that is to use a tool like nikto. By default, nikto performs a very thorough and invasive scan of a server (so you should only use it on your own servers or with explicit permission in that mode), but you can limit what it does, which also makes it much faster and less aggressive:
nikto -Display 2 -Plugins cookies -host stackoverflow.com
-Display 2 means "only display cookies", -Plugins cookies means "only perform a cookie scan". This produces a list of cookies set on every path that nikto finds:
- Nikto v2.1.6
---------------------------------------------------------------------------
+ / sent cookie: prov=3968c6ce-2180-aff2-8e0e-ed7591b64a77; domain=.stackoverflow.com; expires=Fri, 01-Jan-2055 00:00:00 GMT; path=/; HttpOnly
+ / sent cookie: prov=9c9ba76f-6571-425b-1199-393f2f5f88fd; domain=.stackoverflow.com; expires=Fri, 01-Jan-2055 00:00:00 GMT; path=/; HttpOnly
+ Target IP: 151.101.193.69
+ Target Hostname: stackoverflow.com
+ Target Port: 80
+ Start Time: 2020-01-28 09:11:57 (GMT1)
---------------------------------------------------------------------------
+ Server: No banner retrieved
+ / sent cookie: prov=8408954e-060f-bf74-174b-6f2c5f400da8; domain=.stackoverflow.com; expires=Fri, 01-Jan-2055 00:00:00 GMT; path=/; HttpOnly
+ Root page / redirects to: https://stackoverflow.com/
+ /bWqYtKqo.htm sent cookie: prov=dd9d867b-000c-c538-dd61-6c2dac87137c; domain=.stackoverflow.com; expires=Fri, 01-Jan-2055 00:00:00 GMT; path=/; HttpOnly
+ /bWqYtKqo.mediawiki sent cookie: prov=4ca68e7b-fbfd-1513-0ba0-46bf8ff7859e; domain=.stackoverflow.com; expires=Fri, 01-Jan-2055 00:00:00 GMT; path=/; HttpOnly
+ /bWqYtKqo.csp sent cookie: prov=87cf9cdf-ee98-8092-0f6f-5839f6c8208a; domain=.stackoverflow.com; expires=Fri, 01-Jan-2055 00:00:00 GMT; path=/; HttpOnly
+ /bWqYtKqo.pl sent cookie: prov=c61a9f98-1bed-e4c1-4f17-09d0ecded9fd; domain=.stackoverflow.com; expires=Fri, 01-Jan-2055 00:00:00 GMT; path=/; HttpOnly
+ /bWqYtKqo.asp sent cookie: prov=43daff48-2158-85c2-2871-d60d787d8c33; domain=.stackoverflow.com; expires=Fri, 01-Jan-2055 00:00:00 GMT; path=/; HttpOnly
...
You can choose different output formats via the -Format option, including machine-readable ones suitable for scripting, like CSV.

Cookies not being saved

I have two subdomains, local-api.domain.com and local-web.domain.com
local-web.domain.com has a page (local-web.domain.com/test/authtest) that calls out via AJAX to a login service (local-api.domain.com/authentication/login) on local-api.domain.com. The login checks the user's posted credentials, and if they're valid then logs the user in via ASP.Net forms auth. Here is a sample raw response that comes back from the service:
HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
Set-Cookie: token=dsaaflkdaflkxEfrLEUH2Bsfdsjfdksfjdsklfj; expires=Sat, 11 Jan 2014 00:16:04 GMT; domain=.domain.com; path=/; httponly
Access-Control-Allow-Origin: http://local-web.domain.com
Access-Control-Allow-Credentials: true
Set-Cookie: .ASPXAUTH=E18F1521FFF70FDFD60444F6EA791D28DDF1010F907D35DD13CDA7E2698CE9DCFB50A25853A5BCFEA0E21820A0760D8412D517548F59344EDDA052DD6D7BD7DDB1D47D011F2EFE3B58B6B2690B370D54C560FC6FA3B0990190E0CB8A8B4CC80BEA925CA928256C78C502E74444566785C95EDC399777B3CB0D2AAFFD219B3ED5; domain=.domain.com; path=/; HttpOnly
Set-Cookie: Visitor=acfbc21b-6259-4000-809d-7dbc72db8309; domain=.domain.com; expires=Sat, 10-Jan-2015 00:16:04 GMT; path=/; HttpOnly
Set-Cookie: Visit=78406825-adf1-4224-af57-0350136a5fc6; domain=.domain.com; path=/; HttpOnly
Set-Cookie: Culture=en; domain=.domain.com; expires=Sat, 10-Jan-2015 00:16:04 GMT; path=/; HttpOnly
Date: Fri, 10 Jan 2014 00:16:04 GMT
Content-Length: 122
{"token":"dsaaflkdaflkxEfrLEUH2Bsfdsjfdksfjdsklfj","firstName":"Steve","lastName":"Smith"}
However, when I reload the page; I find that the cookie that was set in the response is not there. Further investigation with Chrome Developer Tools finds that the cookie doesn't even get saved after the login response; even though there is a Set-Cookie header.
I'm not sure what I'm doing wrong here. Going by similar questions on the site and their responses; I believe I have everything set up properly for the cookie to be saved and resent across my subdomains. Been Googling for last hour but haven't found anything. Any ideas?
As reproduced on Charles Proxy application,
Replacing httponly with HttpOnly on the conflict cookie did work fine.
I think that's the issue.

PayPal respond to my postback multiple times

PayPal keeps sending the verifications repeatedly to myIPNNotificationURL for the same transaction even if the transaction is VERIFIED and COMPLETED
Inside my IPN handler URL I'm using Coldfusion CFHTTP to send the postback.
<cfhttp url="https://www.sandbox.paypal.com/cgi-bin/webscr?#loc.parameters#" method="get" resolveURL="false"></cfhttp>
This is my dump of the CFHTTP
Charset UTF-8
ErrorDetail [empty string]
Filecontent VERIFIED
Header HTTP/1.1 200 OK Content-Type: text/html; charset=UTF-8 Connection: close Set-Cookie: c9MWDuvPtT9GIMyPc3jwol1VSlO=TidDQytX4Sb52iFHJKvjfI2whU4adLlGiAPXsPFEMmDmeS2Gsg2Eb7BNm3BjrA2Gue9mW-aj4vTUqQgz6oDgnabKNi7ofv3-I5JSyQeDuVt1UmStDFX5k4BbJ2zaV0vgqxnT9G%7coTXCj1bknWC4bbsgnuwe8gNbfVI6zAwERp8Q5qY5kgyEjNl-y_ztsX81v3pMhATPl635-0%7c9yK10cUCi5fnPGd4soVpbcOX5PMUX8Dci5UFV83sk8tpib4MnSIDumavWGAUX7Ih9AEG80%7c1310056764; domain=.paypal.com; path=/; Secure; HttpOnly Set-Cookie: cookie_check=yes; expires=Sun, 04-Jul-2021 16:39:24 GMT; domain=.paypal.com; path=/; Secure; HttpOnly Set-Cookie: navcmd=_notify-validate; domain=.paypal.com; path=/; Secure; HttpOnly Set-Cookie: navlns=0.0; expires=Wed, 02-Jul-2031 16:39:24 GMT; domain=.paypal.com; path=/; Secure; HttpOnly Set-Cookie: Apache=10.72.109.11.1310056764157096; path=/; expires=Sat, 29-Jun-41 16:39:24 GMT Date: Thu, 07 Jul 2011 16:39:24 GMT Server: Apache
Mimetype text/html
Responseheader
struct
Connection close
Content-Type text/html; charset=UTF-8
Date Thu, 07 Jul 2011 16:39:24 GMT
Explanation OK
Http_Version HTTP/1.1
Server Apache
Set-Cookie
struct
1 c9MWDuvPtT9GIMyPc3jwol1VSlO=TidDQytX4Sb52iFHJKvjfI2whU4adLlGiAPXsPFEMmDmeS2Gsg2Eb7BNm3BjrA2Gue9mW-aj4vTUqQgz6oDgnabKNi7ofv3-I5JSyQeDuVt1UmStDFX5k4BbJ2zaV0vgqxnT9G%7coTXCj1bknWC4bbsgnuwe8gNbfVI6zAwERp8Q5qY5kgyEjNl-y_ztsX81v3pMhATPl635-0%7c9yK10cUCi5fnPGd4soVpbcOX5PMUX8Dci5UFV83sk8tpib4MnSIDumavWGAUX7Ih9AEG80%7c1310056764; domain=.paypal.com; path=/; Secure; HttpOnly
2 cookie_check=yes; expires=Sun, 04-Jul-2021 16:39:24 GMT; domain=.paypal.com; path=/; Secure; HttpOnly
3 navcmd=_notify-validate; domain=.paypal.com; path=/; Secure; HttpOnly
4 navlns=0.0; expires=Wed, 02-Jul-2031 16:39:24 GMT; domain=.paypal.com; path=/; Secure; HttpOnly
5 Apache=10.72.109.11.1310056764157096; path=/; expires=Sat, 29-Jun-41 16:39:24 GMT
Status_Code 200
Statuscode 200 OK
Text YES
Also I've use the (IPN) simulator from Paypal and it gives me this error message: IPN delivery failed. HTTP error code 404: Not Found
If they are sending multiple requests to you're myIPNNotificationURL, meaning they're hitting that URL multiple times for the transaction. Luckily those transaction IDs are unique. The key here is recognizing when you have a duplicate so you can treat it as such.
You could write a script that would store these into a stateful object (database or application scope) for a period of time so that you could recognize duplicates.
Once you know you have a dupe, you can ignore it. I don't know how or what they're sending you as far as post values, if you post them I can whip up some sample code.
Sorry guys the issue was with my framework.

Safari not accepting Cookies while FF and IE does

i have a problem with the safari browser and our set-cookie.
Safari is ignoring our set-cookies completely while FF and IE accept and send the cookies.
The Cookie setting page is not a redirection, direct HTTP 200 with set-cookie.
Is there a special character or malformed set-cookie that causes Safari to ignore the cookies completely?
The following Cookies are sent:
CURL output:
Set-Cookie: ASP.NET_SessionId=rdmpn1b4eckozzjns0voon33; path=/
Set-Cookie: SHOPPERID=jZlotLr6HESiqoB/3F0brg==; expires=Wed, 28-Jul-2060 01:09:04 GMT;path=/
Set-Cookie: FVISIT=2010?N7??28??; expires=Wed, 28-Jul-2060 01:09:04 GMT; path=/
Set-Cookie: STOCKMCD=Direct=2010/07/28 10:09:04; expires=Tue, 26-Oct-2010 01:09:04 GMT; path=/
Safari Developer Tools:
Set-Cookie:ASP.NET_SessionId=xqf3eui1r2fce4e30ogh2145; path=/, SHOPPERID=C/lG3XGVPEa7QgGcsqt3yg==; expires=Wed, 28-Jul-2060 01:15:26 GMT; path=/, FVISIT=2010N728รบ; expires=Wed, 28-Jul-2060 01:15:26 GMT; path=/, STOCKMCD=Direct=2010/07/28 10:15:26; expires=Tue, 26-Oct-2010 01:15:26 GMT; path=/
With default privacy settings Safari rejects cookies from domains other than user visited. For example, if page from example.com contains resources from example.net, example.net is not allowed to set cookies.
Take a look at the answers to HTTP headers encoding/decoding in Java. It looks as if the Set-Cookie header being sent has an character without a valid encoding. It seems that IE and Firefox are less strict than Safari.