Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9132339

Browse files
committed
parse cookie values containing the equal sign
1 parent e1b81d5 commit 9132339

File tree

3 files changed

+61
-29
lines changed

3 files changed

+61
-29
lines changed

src/Symfony/Component/HttpFoundation/HeaderUtils.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,30 +193,43 @@ public static function makeDisposition(string $disposition, string $filename, st
193193
return $disposition.'; '.self::toString($params, ';');
194194
}
195195

196-
private static function groupParts(array $matches, string $separators): array
196+
private static function groupParts(array $matches, string $separators, bool $first = true): array
197197
{
198198
$separator = $separators[0];
199199
$partSeparators = substr($separators, 1);
200200

201201
$i = 0;
202202
$partMatches = [];
203+
$previousMatchWasSeparator = false;
203204
foreach ($matches as $match) {
204-
if (isset($match['separator']) && $match['separator'] === $separator) {
205+
if (!$first && $previousMatchWasSeparator && isset($match['separator']) && $match['separator'] === $separator) {
206+
$previousMatchWasSeparator = true;
207+
$partMatches[$i][] = $match;
208+
} elseif (isset($match['separator']) && $match['separator'] === $separator) {
209+
$previousMatchWasSeparator = true;
205210
++$i;
206211
} else {
212+
$previousMatchWasSeparator = false;
207213
$partMatches[$i][] = $match;
208214
}
209215
}
210216

211217
$parts = [];
212218
if ($partSeparators) {
213219
foreach ($partMatches as $matches) {
214-
$parts[] = self::groupParts($matches, $partSeparators);
220+
$parts[] = self::groupParts($matches, $partSeparators, false);
215221
}
216222
} else {
217223
foreach ($partMatches as $matches) {
218224
$parts[] = self::unquote($matches[0][0]);
219225
}
226+
227+
if (!$first && 2 < \count($parts)) {
228+
$parts = [
229+
$parts[0],
230+
implode($separator, \array_slice($parts, 1)),
231+
];
232+
}
220233
}
221234

222235
return $parts;

src/Symfony/Component/HttpFoundation/Tests/CookieTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,12 @@ public function testFromString()
227227

228228
$cookie = Cookie::fromString('foo', true);
229229
$this->assertEquals(Cookie::create('foo', null, 0, '/', null, false, false, false, null), $cookie);
230+
231+
$cookie = Cookie::fromString('foo_cookie=foo=1&bar=2&baz=3; expires=Tue, 22-Sep-2020 06:27:09 GMT; path=/');
232+
$this->assertEquals(Cookie::create('foo_cookie', 'foo=1&bar=2&baz=3', strtotime('Tue, 22-Sep-2020 06:27:09 GMT'), '/', null, false, false, true, null), $cookie);
233+
234+
$cookie = Cookie::fromString('foo_cookie=foo==; expires=Tue, 22-Sep-2020 06:27:09 GMT; path=/');
235+
$this->assertEquals(Cookie::create('foo_cookie', 'foo==', strtotime('Tue, 22-Sep-2020 06:27:09 GMT'), '/', null, false, false, true, null), $cookie);
230236
}
231237

232238
public function testFromStringWithHttpOnly()

src/Symfony/Component/HttpFoundation/Tests/HeaderUtilsTest.php

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,33 +16,46 @@
1616

1717
class HeaderUtilsTest extends TestCase
1818
{
19-
public function testSplit()
19+
/**
20+
* @dataProvider provideHeaderToSplit
21+
*/
22+
public function testSplit(array $expected, string $header, string $separator)
23+
{
24+
$this->assertSame($expected, HeaderUtils::split($header, $separator));
25+
}
26+
27+
public function provideHeaderToSplit(): array
2028
{
21-
$this->assertSame(['foo=123', 'bar'], HeaderUtils::split('foo=123,bar', ','));
22-
$this->assertSame(['foo=123', 'bar'], HeaderUtils::split('foo=123, bar', ','));
23-
$this->assertSame([['foo=123', 'bar']], HeaderUtils::split('foo=123; bar', ',;'));
24-
$this->assertSame([['foo=123'], ['bar']], HeaderUtils::split('foo=123, bar', ',;'));
25-
$this->assertSame(['foo', '123, bar'], HeaderUtils::split('foo=123, bar', '='));
26-
$this->assertSame(['foo', '123, bar'], HeaderUtils::split(' foo = 123, bar ', '='));
27-
$this->assertSame([['foo', '123'], ['bar']], HeaderUtils::split('foo=123, bar', ',='));
28-
$this->assertSame([[['foo', '123']], [['bar'], ['foo', '456']]], HeaderUtils::split('foo=123, bar; foo=456', ',;='));
29-
$this->assertSame([[['foo', 'a,b;c=d']]], HeaderUtils::split('foo="a,b;c=d"', ',;='));
30-
31-
$this->assertSame(['foo', 'bar'], HeaderUtils::split('foo,,,, bar', ','));
32-
$this->assertSame(['foo', 'bar'], HeaderUtils::split(',foo, bar,', ','));
33-
$this->assertSame(['foo', 'bar'], HeaderUtils::split(' , foo, bar, ', ','));
34-
$this->assertSame(['foo bar'], HeaderUtils::split('foo "bar"', ','));
35-
$this->assertSame(['foo bar'], HeaderUtils::split('"foo" bar', ','));
36-
$this->assertSame(['foo bar'], HeaderUtils::split('"foo" "bar"', ','));
37-
38-
// These are not a valid header values. We test that they parse anyway,
39-
// and that both the valid and invalid parts are returned.
40-
$this->assertSame([], HeaderUtils::split('', ','));
41-
$this->assertSame([], HeaderUtils::split(',,,', ','));
42-
$this->assertSame(['foo', 'bar', 'baz'], HeaderUtils::split('foo, "bar", "baz', ','));
43-
$this->assertSame(['foo', 'bar, baz'], HeaderUtils::split('foo, "bar, baz', ','));
44-
$this->assertSame(['foo', 'bar, baz\\'], HeaderUtils::split('foo, "bar, baz\\', ','));
45-
$this->assertSame(['foo', 'bar, baz\\'], HeaderUtils::split('foo, "bar, baz\\\\', ','));
29+
return [
30+
[['foo=123', 'bar'], 'foo=123,bar', ','],
31+
[['foo=123', 'bar'], 'foo=123, bar', ','],
32+
[[['foo=123', 'bar']], 'foo=123; bar', ',;'],
33+
[[['foo=123'], ['bar']], 'foo=123, bar', ',;'],
34+
[['foo', '123, bar'], 'foo=123, bar', '='],
35+
[['foo', '123, bar'], ' foo = 123, bar ', '='],
36+
[[['foo', '123'], ['bar']], 'foo=123, bar', ',='],
37+
[[[['foo', '123']], [['bar'], ['foo', '456']]], 'foo=123, bar; foo=456', ',;='],
38+
[[[['foo', 'a,b;c=d']]], 'foo="a,b;c=d"', ',;='],
39+
40+
[['foo', 'bar'], 'foo,,,, bar', ','],
41+
[['foo', 'bar'], ',foo, bar,', ','],
42+
[['foo', 'bar'], ' , foo, bar, ', ','],
43+
[['foo bar'], 'foo "bar"', ','],
44+
[['foo bar'], '"foo" bar', ','],
45+
[['foo bar'], '"foo" "bar"', ','],
46+
47+
[[['foo_cookie', 'foo=1&bar=2&baz=3'], ['expires', 'Tue, 22-Sep-2020 06:27:09 GMT'], ['path', '/']], 'foo_cookie=foo=1&bar=2&baz=3; expires=Tue, 22-Sep-2020 06:27:09 GMT; path=/', ';='],
48+
[[['foo_cookie', 'foo=='], ['expires', 'Tue, 22-Sep-2020 06:27:09 GMT'], ['path', '/']], 'foo_cookie=foo==; expires=Tue, 22-Sep-2020 06:27:09 GMT; path=/', ';='],
49+
50+
// These are not a valid header values. We test that they parse anyway,
51+
// and that both the valid and invalid parts are returned.
52+
[[], '', ','],
53+
[[], ',,,', ','],
54+
[['foo', 'bar', 'baz'], 'foo, "bar", "baz', ','],
55+
[['foo', 'bar, baz'], 'foo, "bar, baz', ','],
56+
[['foo', 'bar, baz\\'], 'foo, "bar, baz\\', ','],
57+
[['foo', 'bar, baz\\'], 'foo, "bar, baz\\\\', ','],
58+
];
4659
}
4760

4861
public function testCombine()

0 commit comments

Comments
 (0)