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

Fix Undefined offset: 0 #1894

Closed
wants to merge 3 commits into from
Closed

Fix Undefined offset: 0 #1894

wants to merge 3 commits into from

Conversation

e1111o
Copy link

@e1111o e1111o commented Aug 7, 2017

No description provided.

@sagikazarmark
Copy link
Member

Can you please provide some examples how you met this issue? Examining the code this should not be possible at all. array_filter reindexes the array and the empty check should catch the case when the first index does not exist.

@e1111o
Copy link
Author

e1111o commented Aug 7, 2017

In the fromString method of Cookie/SetCookie.php, the return value of $pieces is as follows.

Array: 2 [
   1 => "path = /"
   2 => "HttpOnly"
]

An error occurred because the 0th array did not exist.
I encountered this error while creating and using the CookieJar class as shown below.

$cookie_jar = new CookieJar ();
$json = $ this-> client-> request ('POST', '[[URL]]',
             'Cookies' => $cookie_jar,
             'Headers' => $headers,
             ...
         ]);

@rtek
Copy link

rtek commented Aug 7, 2017

Related: #1895

http://php.net/manual/en/function.array-filter.php

Iterates over each value in the array passing them to the callback function. If the callback function returns true, the current value from array is returned into the result array. Array keys are preserved.

@Nyholm
Copy link
Member

Nyholm commented Aug 8, 2017

It would be easy to write a test for this. Could you provide a test that is proving the correctness of this patch?

@e1111o
Copy link
Author

e1111o commented Aug 9, 2017

Test cases added.
This is an error when passing the argument from the fromString method to ; path = /; HttpOnly.
The array_filter function removes the 0th index and accesses the 0th index of the $pieces array in the 40th line.

@sagikazarmark
Copy link
Member

Array keys are preserved.

Oh my.... of course it does.

Thanks a lot for the fix. Will merge and tag once the build passes.

Copy link
Member

@Nyholm Nyholm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @e1111o. I like that you added a test. But your test does not prove the correctness of the patch.

Actually, your patch does not do anything with this input date.

https://3v4l.org/nD4l3

@e1111o
Copy link
Author

e1111o commented Aug 9, 2017

@Nyholm Thank you for your care.
Yet, in this case, i think code should be $cookie = '; Path=/; HttpOnly';, not $cookie = 'path=/; Path=/; HttpOnly';

@Nyholm
Copy link
Member

Nyholm commented Aug 9, 2017

I think you miss the point. Im saying that the test you provided will pass with and without you changes to SetCookie.php.

I need you to provide a test that would fail without your changes to SetCookie.php. Such test will prove the correctness of your patch.

@sagikazarmark sagikazarmark added this to the 6.3.1 milestone Sep 13, 2017
@sagikazarmark
Copy link
Member

What's the status of this PR?

@e1111o
Copy link
Author

e1111o commented Sep 14, 2017

Sorry. I have been busy with my company business these days and have not progressed since. I'll commit it soon after the fix.

@curtisdf
Copy link

curtisdf commented Jan 31, 2018

I encountered this bug myself just now.

I've looked at the proposed changes in the aforementioned pull request, but I don't believe they address the real issue. This error is triggered when the first KVP (key-value pair) in a Set-Cookie string is missing. This KVP is pretty vital to the cookie because it provides the cookie's name and value. I'm not familiar with the relevant RFCs or other standards regarding cookie handling, but I would guess this isn't legal. But what action should Guzzle take in this case? It seems to me that a cookie without a name and value is dead on arrival and should be ignored.

The code in question already has a comment indicating that it expects the cookie to have that first KVP. See line 38:

    // The name of the cookie (first kvp) must include an equal sign.

The proposed fix only re-indexes the $pieces array by calling array_values() on it. But this obfuscates the fact that the cookie is malformed. It also doesn't clean up the inline comment, which would be obsoleted by such a fix.

So I believe this pull request should indeed be rejected, even without looking at the tests. I'm considering submitting my own pull request which would fix this more properly, if I can find time today or tomorrow.

curtisdf pushed a commit to curtisdf/guzzle that referenced this pull request Jan 31, 2018
@curtisdf
Copy link

curtisdf commented Jan 31, 2018

I've submitted a pull request. See #1998. I saw that the function in question already returns an empty cookie object if the cookie is missing a name, so I made a trivial fix which also does that if the whole first key-value pair is missing.

For clarification, this bug is only triggered if the $cookie string begins with a leading semicolon (";") and has at least one additional key-value pair behind it.

sagikazarmark added a commit that referenced this pull request Mar 26, 2018
A better fix for #1894 - "Undefined offset: 0"
@sagikazarmark
Copy link
Member

Accepted the fix in #1998

Thanks for your contribution though

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

Successfully merging this pull request may close these issues.

None yet

5 participants