Skip to content

Commit 47fba88

Browse files
committed
deprecate finding deep items in request parameters
1 parent b630972 commit 47fba88

14 files changed

+176
-24
lines changed

src/Symfony/Component/HttpFoundation/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
CHANGELOG
22
=========
33

4+
2.8.0
5+
-----
6+
7+
* Finding deep items in `ParameterBag::get()` is deprecated since version 2.8 and
8+
will be removed in 3.0.
9+
410
2.6.0
511
-----
612

src/Symfony/Component/HttpFoundation/ParameterBag.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,29 +78,35 @@ public function add(array $parameters = array())
7878
/**
7979
* Returns a parameter by name.
8080
*
81-
* @param string $path The key
81+
* Note: Finding deep items is deprecated since version 2.8, to be removed in 3.0.
82+
*
83+
* @param string $key The key
8284
* @param mixed $default The default value if the parameter key does not exist
8385
* @param bool $deep If true, a path like foo[bar] will find deeper items
8486
*
8587
* @return mixed
8688
*
8789
* @throws \InvalidArgumentException
8890
*/
89-
public function get($path, $default = null, $deep = false)
91+
public function get($key, $default = null, $deep = false)
9092
{
91-
if (!$deep || false === $pos = strpos($path, '[')) {
92-
return array_key_exists($path, $this->parameters) ? $this->parameters[$path] : $default;
93+
if (true === $deep) {
94+
@trigger_error('Using paths to find deeper items in '.__METHOD__.' is deprecated since version 2.8 and will be removed in 3.0. Filter the returned value in your own code instead.', E_USER_DEPRECATED);
95+
}
96+
97+
if (!$deep || false === $pos = strpos($key, '[')) {
98+
return array_key_exists($key, $this->parameters) ? $this->parameters[$key] : $default;
9399
}
94100

95-
$root = substr($path, 0, $pos);
101+
$root = substr($key, 0, $pos);
96102
if (!array_key_exists($root, $this->parameters)) {
97103
return $default;
98104
}
99105

100106
$value = $this->parameters[$root];
101107
$currentKey = null;
102-
for ($i = $pos, $c = strlen($path); $i < $c; ++$i) {
103-
$char = $path[$i];
108+
for ($i = $pos, $c = strlen($key); $i < $c; ++$i) {
109+
$char = $key[$i];
104110

105111
if ('[' === $char) {
106112
if (null !== $currentKey) {

src/Symfony/Component/HttpFoundation/Request.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,8 @@ public static function getHttpMethodParameterOverride()
714714
* It is better to explicitly get request parameters from the appropriate
715715
* public property instead (query, attributes, request).
716716
*
717+
* Note: Finding deep items is deprecated since version 2.8, to be removed in 3.0.
718+
*
717719
* @param string $key the key
718720
* @param mixed $default the default value
719721
* @param bool $deep is parameter deep in multidimensional array
@@ -722,6 +724,10 @@ public static function getHttpMethodParameterOverride()
722724
*/
723725
public function get($key, $default = null, $deep = false)
724726
{
727+
if (true === $deep) {
728+
@trigger_error('Using paths to find deeper items in '.__METHOD__.' is deprecated since version 2.8 and will be removed in 3.0. Filter the returned value in your own code instead.', E_USER_DEPRECATED);
729+
}
730+
725731
if ($this !== $result = $this->query->get($key, $this, $deep)) {
726732
return $result;
727733
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ public function testGetDoesNotUseDeepByDefault()
8686
}
8787

8888
/**
89+
* @group legacy
8990
* @dataProvider getInvalidPaths
9091
* @expectedException \InvalidArgumentException
9192
*/
@@ -106,6 +107,9 @@ public function getInvalidPaths()
106107
);
107108
}
108109

110+
/**
111+
* @group legacy
112+
*/
109113
public function testGetDeep()
110114
{
111115
$bag = new ParameterBag(array('foo' => array('bar' => array('moo' => 'boo'))));

src/Symfony/Component/Security/Http/Authentication/DefaultAuthenticationFailureHandler.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Component\Security\Core\Exception\AuthenticationException;
1818
use Symfony\Component\Security\Core\Security;
1919
use Symfony\Component\Security\Http\HttpUtils;
20+
use Symfony\Component\Security\Http\ParameterBagUtils;
2021

2122
/**
2223
* Class with the default authentication failure handling logic.
@@ -82,7 +83,7 @@ public function setOptions(array $options)
8283
*/
8384
public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
8485
{
85-
if ($failureUrl = $request->get($this->options['failure_path_parameter'], null, true)) {
86+
if ($failureUrl = ParameterBagUtils::getRequestParameterValue($request, $this->options['failure_path_parameter'])) {
8687
$this->options['failure_path'] = $failureUrl;
8788
}
8889

src/Symfony/Component/Security/Http/Authentication/DefaultAuthenticationSuccessHandler.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
1515
use Symfony\Component\HttpFoundation\Request;
1616
use Symfony\Component\Security\Http\HttpUtils;
17+
use Symfony\Component\Security\Http\ParameterBagUtils;
1718

1819
/**
1920
* Class with the default authentication success handling logic.
@@ -108,7 +109,7 @@ protected function determineTargetUrl(Request $request)
108109
return $this->options['default_target_path'];
109110
}
110111

111-
if ($targetUrl = $request->get($this->options['target_path_parameter'], null, true)) {
112+
if ($targetUrl = ParameterBagUtils::getRequestParameterValue($request, $this->options['target_path_parameter'])) {
112113
return $targetUrl;
113114
}
114115

src/Symfony/Component/Security/Http/Firewall/LogoutListener.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use Symfony\Component\Security\Http\HttpUtils;
2525
use Symfony\Component\Security\Http\Logout\LogoutHandlerInterface;
2626
use Symfony\Component\Security\Http\Logout\LogoutSuccessHandlerInterface;
27+
use Symfony\Component\Security\Http\ParameterBagUtils;
2728

2829
/**
2930
* LogoutListener logout users.
@@ -98,7 +99,7 @@ public function handle(GetResponseEvent $event)
9899
}
99100

100101
if (null !== $this->csrfTokenManager) {
101-
$csrfToken = $request->get($this->options['csrf_parameter'], null, true);
102+
$csrfToken = ParameterBagUtils::getRequestParameterValue($request, $this->options['csrf_parameter']);
102103

103104
if (false === $this->csrfTokenManager->isTokenValid(new CsrfToken($this->options['intention'], $csrfToken))) {
104105
throw new LogoutException('Invalid CSRF token.');

src/Symfony/Component/Security/Http/Firewall/SimpleFormAuthenticationListener.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
2727
use Symfony\Component\Security\Core\Security;
2828
use Symfony\Component\Security\Http\HttpUtils;
29+
use Symfony\Component\Security\Http\ParameterBagUtils;
2930
use Symfony\Component\Security\Http\Session\SessionAuthenticationStrategyInterface;
3031
use Psr\Log\LoggerInterface;
3132

@@ -101,19 +102,19 @@ protected function requiresAuthentication(Request $request)
101102
protected function attemptAuthentication(Request $request)
102103
{
103104
if (null !== $this->csrfTokenManager) {
104-
$csrfToken = $request->get($this->options['csrf_parameter'], null, true);
105+
$csrfToken = ParameterBagUtils::getRequestParameterValue($request, $this->options['csrf_parameter']);
105106

106107
if (false === $this->csrfTokenManager->isTokenValid(new CsrfToken($this->options['intention'], $csrfToken))) {
107108
throw new InvalidCsrfTokenException('Invalid CSRF token.');
108109
}
109110
}
110111

111112
if ($this->options['post_only']) {
112-
$username = trim($request->request->get($this->options['username_parameter'], null, true));
113-
$password = $request->request->get($this->options['password_parameter'], null, true);
113+
$username = trim(ParameterBagUtils::getParameterBagValue($request->request, $this->options['username_parameter']));
114+
$password = ParameterBagUtils::getParameterBagValue($request->request, $this->options['password_parameter']);
114115
} else {
115-
$username = trim($request->get($this->options['username_parameter'], null, true));
116-
$password = $request->get($this->options['password_parameter'], null, true);
116+
$username = trim(ParameterBagUtils::getRequestParameterValue($request, $this->options['username_parameter']));
117+
$password = ParameterBagUtils::getRequestParameterValue($request, $this->options['password_parameter']);
117118
}
118119

119120
$request->getSession()->set(Security::LAST_USERNAME, $username);

src/Symfony/Component/Security/Http/Firewall/UsernamePasswordFormAuthenticationListener.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
2020
use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface;
2121
use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
22+
use Symfony\Component\Security\Http\ParameterBagUtils;
2223
use Symfony\Component\Security\Http\Session\SessionAuthenticationStrategyInterface;
2324
use Symfony\Component\Security\Http\HttpUtils;
2425
use Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface;
@@ -76,19 +77,19 @@ protected function requiresAuthentication(Request $request)
7677
protected function attemptAuthentication(Request $request)
7778
{
7879
if (null !== $this->csrfTokenManager) {
79-
$csrfToken = $request->get($this->options['csrf_parameter'], null, true);
80+
$csrfToken = ParameterBagUtils::getRequestParameterValue($request, $this->options['csrf_parameter']);
8081

8182
if (false === $this->csrfTokenManager->isTokenValid(new CsrfToken($this->options['intention'], $csrfToken))) {
8283
throw new InvalidCsrfTokenException('Invalid CSRF token.');
8384
}
8485
}
8586

8687
if ($this->options['post_only']) {
87-
$username = trim($request->request->get($this->options['username_parameter'], null, true));
88-
$password = $request->request->get($this->options['password_parameter'], null, true);
88+
$username = trim(ParameterBagUtils::getParameterBagValue($request->request, $this->options['username_parameter']));
89+
$password = ParameterBagUtils::getParameterBagValue($request->request, $this->options['password_parameter']);
8990
} else {
90-
$username = trim($request->get($this->options['username_parameter'], null, true));
91-
$password = $request->get($this->options['password_parameter'], null, true);
91+
$username = trim(ParameterBagUtils::getRequestParameterValue($request, $this->options['username_parameter']));
92+
$password = ParameterBagUtils::getRequestParameterValue($request, $this->options['password_parameter']);
9293
}
9394

9495
$request->getSession()->set(Security::LAST_USERNAME, $username);
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Security\Http;
13+
14+
use Symfony\Component\HttpFoundation\ParameterBag;
15+
use Symfony\Component\HttpFoundation\Request;
16+
use Symfony\Component\PropertyAccess\Exception\AccessException;
17+
use Symfony\Component\PropertyAccess\Exception\InvalidArgumentException;
18+
use Symfony\Component\PropertyAccess\PropertyAccess;
19+
20+
/**
21+
* @internal
22+
*/
23+
final class ParameterBagUtils
24+
{
25+
private static $propertyAccessor;
26+
27+
/**
28+
* Returns a "parameter" value.
29+
*
30+
* Paths like foo[bar] will be evaluated to find deeper items in nested data structures.
31+
*
32+
* @param ParameterBag $parameters The parameter bag
33+
* @param string $path The key
34+
*
35+
* @return mixed
36+
*
37+
* @throws InvalidArgumentException when the given path is malformed
38+
*/
39+
public static function getParameterBagValue(ParameterBag $parameters, $path)
40+
{
41+
if (false === $pos = strpos($path, '[')) {
42+
return $parameters->get($path);
43+
}
44+
45+
$root = substr($path, 0, $pos);
46+
47+
if (null === $value = $parameters->get($root)) {
48+
return;
49+
}
50+
51+
if (null === self::$propertyAccessor) {
52+
self::$propertyAccessor = PropertyAccess::createPropertyAccessor();
53+
}
54+
55+
try {
56+
return self::$propertyAccessor->getValue($value, substr($path, $pos));
57+
} catch (AccessException $e) {
58+
return;
59+
}
60+
}
61+
62+
/**
63+
* Returns a request "parameter" value.
64+
*
65+
* Paths like foo[bar] will be evaluated to find deeper items in nested data structures.
66+
*
67+
* @param Request $request The request
68+
* @param string $path The key
69+
*
70+
* @return mixed
71+
*
72+
* @throws InvalidArgumentException when the given path is malformed
73+
*/
74+
public static function getRequestParameterValue(Request $request, $path)
75+
{
76+
if (false === $pos = strpos($path, '[')) {
77+
return $request->get($path);
78+
}
79+
80+
$root = substr($path, 0, $pos);
81+
82+
if (null === $value = $request->get($root)) {
83+
return;
84+
}
85+
86+
if (null === self::$propertyAccessor) {
87+
self::$propertyAccessor = PropertyAccess::createPropertyAccessor();
88+
}
89+
90+
try {
91+
return self::$propertyAccessor->getValue($value, substr($path, $pos));
92+
} catch (AccessException $e) {
93+
return;
94+
}
95+
}
96+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy