Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Duplication Of Cookies [rt.cpan.org #75897] #33

Open
oalders opened this issue Mar 30, 2017 · 0 comments
Open

Duplication Of Cookies [rt.cpan.org #75897] #33

oalders opened this issue Mar 30, 2017 · 0 comments

Comments

@oalders
Copy link
Member

oalders commented Mar 30, 2017

Migrated from rt.cpan.org#75897 (status was 'open')

Requestors:

Attachments:

From [email protected] on 2012-03-20 06:24:48:

Bug in HTTP::Cookies version 6.01
Perl Version 5.10.1
Operating System: Ubuntu 10.04

BUG:
In package HTTP::Cookies, the function add_cookie_header() has a bug 
that it copies prevoiusly existing cookies and concatenates them to the 
request header. This sometimes causes duplication of cookies.
The Bug causes error 400 Bad Request, due to the request header being 
too long when trying to log in to a server.This condition does not cause 
an error until and unless the cookies are too long.


Workaround:
Rather than appending the existing cookies to the new cookies we should 
check if the cookie already exists than skip appending it, otherwise 
append it to the new cookies.



My Scenario:
I was trying to log on to an AD-FS server using NTLM Authentication.
We recieve 4 NTLM Request Cookies which are base64 encoded. But during 
Authentication there are 3 redirections which are handled using 
request() in LWP::UserAgent.
This make a indirect call to HTTP::Cookies each time.
Therefore, the same cookies are repeated 3 times.
As the cookies are too long we recieve an HTTP Bad Request, Error 400.



Place of Bug in Code:

    if (@cval) {
        if (my $old = $request->header("Cookie")) {
            unshift(@cval, $old);
        }
        $request->header(Cookie => join("; ", @cval));
    }

Fix:
This patch checks whether the cookie in @oldcookie has alredy been 
included in @cval, if yes than it skips appending it to @cval,

if (@cval) {
	if (my $old = $request->header("Cookie")) {
		my @oldcookie = split(/;/, $old);
		my $cookieflag;
		my $ocookie;
		my $ncookie;
		foreach(@oldcookie){
			$ocookie = $_;
			$ocookie=~ s/^\s*//;
			$ocookie=~ s/\s*$//;
			chomp($ocookie);
			$cookieflag = 1;
			foreach(@cval){
 				$ncookie = $_;
				$ncookie=~ s/^\s*//;
				$ncookie=~ s/\s*$//;
				chomp($ncookie);
				if($ncookie eq $ocookie)
				{	
					$cookieflag=0;
				}
			}
			if($cookieflag==1)
			{    
			unshift(@cval, $ocookie);
			}
		}
	}
	$request->header(Cookie => join("; ", @cval));
    }

From [email protected] on 2016-10-04 19:45:04:

On Tue Mar 20 02:24:48 2012, vaibhavkhunger wrote:
> 
> Bug in HTTP::Cookies version 6.01
> Perl Version 5.10.1
> Operating System: Ubuntu 10.04
> 
> BUG:
> In package HTTP::Cookies, the function add_cookie_header() has a bug 
> that it copies prevoiusly existing cookies and concatenates them to the 
> request header. This sometimes causes duplication of cookies.
> The Bug causes error 400 Bad Request, due to the request header being 
> too long when trying to log in to a server.This condition does not cause 
> an error until and unless the cookies are too long.
> 
> 
> Workaround:
> Rather than appending the existing cookies to the new cookies we should 
> check if the cookie already exists than skip appending it, otherwise 
> append it to the new cookies.
> 
> 
> 
> My Scenario:
> I was trying to log on to an AD-FS server using NTLM Authentication.
> We recieve 4 NTLM Request Cookies which are base64 encoded. But during 
> Authentication there are 3 redirections which are handled using 
> request() in LWP::UserAgent.
> This make a indirect call to HTTP::Cookies each time.
> Therefore, the same cookies are repeated 3 times.
> As the cookies are too long we recieve an HTTP Bad Request, Error 400.
> 
> 
> 
> Place of Bug in Code:
> 
>     if (@cval) {
>         if (my $old = $request->header("Cookie")) {
>             unshift(@cval, $old);
>         }
>         $request->header(Cookie => join("; ", @cval));
>     }
> 
> Fix:
> This patch checks whether the cookie in @oldcookie has alredy been 
> included in @cval, if yes than it skips appending it to @cval,
> 
> if (@cval) {
> 	if (my $old = $request->header("Cookie")) {
> 		my @oldcookie = split(/;/, $old);
> 		my $cookieflag;
> 		my $ocookie;
> 		my $ncookie;
> 		foreach(@oldcookie){
> 			$ocookie = $_;
> 			$ocookie=~ s/^\s*//;
> 			$ocookie=~ s/\s*$//;
> 			chomp($ocookie);
> 			$cookieflag = 1;
> 			foreach(@cval){
>  				$ncookie = $_;
> 				$ncookie=~ s/^\s*//;
> 				$ncookie=~ s/\s*$//;
> 				chomp($ncookie);
> 				if($ncookie eq $ocookie)
> 				{	
> 					$cookieflag=0;
> 				}
> 			}
> 			if($cookieflag==1)
> 			{    
> 			unshift(@cval, $ocookie);
> 			}
> 		}
> 	}
> 	$request->header(Cookie => join("; ", @cval));
>     }

Looks like this is still an issue.  See https://github.com/libwww-perl/WWW-Mechanize/issues/52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant