Skip to content

Commit be159ab

Browse files
Merge branch '4.2'
* 4.2: [Routing] dont redirect routes with greedy trailing vars with no explicit slash skip native serialize among child and parent serializable objects [Routing] backport tests from 4.1 [MonologBridge] Remove unused local variable Remove unreachable code Add PackageNameTest to ConfigurationTest also add in the changelog the corresponding entry to this PR Support use of hyphen in asset package name Fix format strings for deprecation notices Remove a harmless duplicate array key from VarDumper [VarDumper] Fixed search bar Remove gendered pronouns Replace gender by eye color in tests [Security] dont do nested calls to serialize()
2 parents d225ed5 + 49e9b30 commit be159ab

11 files changed

+87
-41
lines changed

Authentication/Token/AbstractToken.php

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -136,22 +136,17 @@ public function eraseCredentials()
136136
*/
137137
public function serialize()
138138
{
139-
return serialize(
140-
[
141-
\is_object($this->user) ? clone $this->user : $this->user,
142-
$this->authenticated,
143-
array_map(function ($role) { return clone $role; }, $this->roles),
144-
$this->attributes,
145-
]
146-
);
139+
$serialized = [$this->user, $this->authenticated, $this->roles, $this->attributes];
140+
141+
return $this->doSerialize($serialized, \func_num_args() ? \func_get_arg(0) : null);
147142
}
148143

149144
/**
150145
* {@inheritdoc}
151146
*/
152147
public function unserialize($serialized)
153148
{
154-
list($this->user, $this->authenticated, $this->roles, $this->attributes) = unserialize($serialized);
149+
list($this->user, $this->authenticated, $this->roles, $this->attributes) = \is_array($serialized) ? $serialized : unserialize($serialized);
155150
}
156151

157152
/**
@@ -231,6 +226,19 @@ public function __toString()
231226
return sprintf('%s(user="%s", authenticated=%s, roles="%s")', $class, $this->getUsername(), json_encode($this->authenticated), implode(', ', $roles));
232227
}
233228

229+
/**
230+
* @internal
231+
*/
232+
protected function doSerialize($serialized, $isCalledFromOverridingMethod)
233+
{
234+
if (null === $isCalledFromOverridingMethod) {
235+
$trace = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 3);
236+
$isCalledFromOverridingMethod = isset($trace[2]['function'], $trace[2]['object']) && 'serialize' === $trace[2]['function'] && $this === $trace[2]['object'];
237+
}
238+
239+
return $isCalledFromOverridingMethod ? $serialized : serialize($serialized);
240+
}
241+
234242
private function hasUserChanged(UserInterface $user)
235243
{
236244
if (!($this->user instanceof UserInterface)) {

Authentication/Token/AnonymousToken.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,17 @@ public function getSecret()
5959
*/
6060
public function serialize()
6161
{
62-
return serialize([$this->secret, parent::serialize()]);
62+
$serialized = [$this->secret, parent::serialize(true)];
63+
64+
return $this->doSerialize($serialized, \func_num_args() ? \func_get_arg(0) : null);
6365
}
6466

6567
/**
6668
* {@inheritdoc}
6769
*/
6870
public function unserialize($serialized)
6971
{
70-
list($this->secret, $parentStr) = unserialize($serialized);
72+
list($this->secret, $parentStr) = \is_array($serialized) ? $serialized : unserialize($serialized);
7173
parent::unserialize($parentStr);
7274
}
7375
}

Authentication/Token/PreAuthenticatedToken.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,17 @@ public function eraseCredentials()
7979
*/
8080
public function serialize()
8181
{
82-
return serialize([$this->credentials, $this->providerKey, parent::serialize()]);
82+
$serialized = [$this->credentials, $this->providerKey, parent::serialize(true)];
83+
84+
return $this->doSerialize($serialized, \func_num_args() ? \func_get_arg(0) : null);
8385
}
8486

8587
/**
8688
* {@inheritdoc}
8789
*/
8890
public function unserialize($str)
8991
{
90-
list($this->credentials, $this->providerKey, $parentStr) = unserialize($str);
92+
list($this->credentials, $this->providerKey, $parentStr) = \is_array($str) ? $str : unserialize($str);
9193
parent::unserialize($parentStr);
9294
}
9395
}

Authentication/Token/RememberMeToken.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,19 +94,17 @@ public function getCredentials()
9494
*/
9595
public function serialize()
9696
{
97-
return serialize([
98-
$this->secret,
99-
$this->providerKey,
100-
parent::serialize(),
101-
]);
97+
$serialized = [$this->secret, $this->providerKey, parent::serialize(true)];
98+
99+
return $this->doSerialize($serialized, \func_num_args() ? \func_get_arg(0) : null);
102100
}
103101

104102
/**
105103
* {@inheritdoc}
106104
*/
107105
public function unserialize($serialized)
108106
{
109-
list($this->secret, $this->providerKey, $parentStr) = unserialize($serialized);
107+
list($this->secret, $this->providerKey, $parentStr) = \is_array($serialized) ? $serialized : unserialize($serialized);
110108
parent::unserialize($parentStr);
111109
}
112110
}

Authentication/Token/UsernamePasswordToken.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,17 @@ public function eraseCredentials()
9191
*/
9292
public function serialize()
9393
{
94-
return serialize([$this->credentials, $this->providerKey, parent::serialize()]);
94+
$serialized = [$this->credentials, $this->providerKey, parent::serialize(true)];
95+
96+
return $this->doSerialize($serialized, \func_num_args() ? \func_get_arg(0) : null);
9597
}
9698

9799
/**
98100
* {@inheritdoc}
99101
*/
100102
public function unserialize($serialized)
101103
{
102-
list($this->credentials, $this->providerKey, $parentStr) = unserialize($serialized);
104+
list($this->credentials, $this->providerKey, $parentStr) = \is_array($serialized) ? $serialized : unserialize($serialized);
103105
parent::unserialize($parentStr);
104106
}
105107
}

Exception/AccountStatusException.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,17 @@ public function setUser(UserInterface $user)
4444
*/
4545
public function serialize()
4646
{
47-
return serialize([
48-
$this->user,
49-
parent::serialize(),
50-
]);
47+
$serialized = [$this->user, parent::serialize(true)];
48+
49+
return $this->doSerialize($serialized, \func_num_args() ? \func_get_arg(0) : null);
5150
}
5251

5352
/**
5453
* {@inheritdoc}
5554
*/
5655
public function unserialize($str)
5756
{
58-
list($this->user, $parentData) = unserialize($str);
57+
list($this->user, $parentData) = \is_array($str) ? $str : unserialize($str);
5958

6059
parent::unserialize($parentData);
6160
}

Exception/AuthenticationException.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,33 @@ public function setToken(TokenInterface $token)
3838
$this->token = $token;
3939
}
4040

41+
/**
42+
* {@inheritdoc}
43+
*/
4144
public function serialize()
4245
{
43-
return serialize([
46+
$serialized = [
4447
$this->token,
4548
$this->code,
4649
$this->message,
4750
$this->file,
4851
$this->line,
49-
]);
52+
];
53+
54+
return $this->doSerialize($serialized, \func_num_args() ? \func_get_arg(0) : null);
55+
}
56+
57+
/**
58+
* @internal
59+
*/
60+
protected function doSerialize($serialized, $isCalledFromOverridingMethod)
61+
{
62+
if (null === $isCalledFromOverridingMethod) {
63+
$trace = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 3);
64+
$isCalledFromOverridingMethod = isset($trace[2]['function'], $trace[2]['object']) && 'serialize' === $trace[2]['function'] && $this === $trace[2]['object'];
65+
}
66+
67+
return $isCalledFromOverridingMethod ? $serialized : serialize($serialized);
5068
}
5169

5270
public function unserialize($str)
@@ -57,7 +75,7 @@ public function unserialize($str)
5775
$this->message,
5876
$this->file,
5977
$this->line
60-
) = unserialize($str);
78+
) = \is_array($str) ? $str : unserialize($str);
6179
}
6280

6381
/**

Exception/CustomUserMessageAuthenticationException.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,19 +60,17 @@ public function getMessageData()
6060
*/
6161
public function serialize()
6262
{
63-
return serialize([
64-
parent::serialize(),
65-
$this->messageKey,
66-
$this->messageData,
67-
]);
63+
return serialize([parent::serialize(true), $this->messageKey, $this->messageData]);
64+
65+
return $this->doSerialize($serialized, \func_num_args() ? \func_get_arg(0) : null);
6866
}
6967

7068
/**
7169
* {@inheritdoc}
7270
*/
7371
public function unserialize($str)
7472
{
75-
list($parentData, $this->messageKey, $this->messageData) = unserialize($str);
73+
list($parentData, $this->messageKey, $this->messageData) = \is_array($str) ? $str : unserialize($str);
7674

7775
parent::unserialize($parentData);
7876
}

Exception/UsernameNotFoundException.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,17 @@ public function setUsername($username)
5454
*/
5555
public function serialize()
5656
{
57-
return serialize([
58-
$this->username,
59-
parent::serialize(),
60-
]);
57+
$serialized = [$this->username, parent::serialize(true)];
58+
59+
return $this->doSerialize($serialized, \func_num_args() ? \func_get_arg(0) : null);
6160
}
6261

6362
/**
6463
* {@inheritdoc}
6564
*/
6665
public function unserialize($str)
6766
{
68-
list($this->username, $parentData) = unserialize($str);
67+
list($this->username, $parentData) = \is_array($str) ? $str : unserialize($str);
6968

7069
parent::unserialize($parentData);
7170
}

Tests/Authentication/Token/AbstractTokenTest.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,14 @@ public function __construct($user, array $roles = [])
4343
$this->setUser($user);
4444
}
4545

46+
/**
47+
* {@inheritdoc}
48+
*/
4649
public function serialize()
4750
{
48-
return serialize([$this->credentials, parent::serialize()]);
51+
$serialized = [$this->credentials, parent::serialize(true)];
52+
53+
return $this->doSerialize($serialized, \func_num_args() ? \func_get_arg(0) : null);
4954
}
5055

5156
public function unserialize($serialized)

0 commit comments

Comments
 (0)