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

Skip to content

[HttpFoundation] Use parse_str() for query strings normalization #26220

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

Merged
merged 1 commit into from
Mar 23, 2018

Conversation

nicolas-grekas
Copy link
Member

@nicolas-grekas nicolas-grekas commented Feb 19, 2018

Q A
Branch? master
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets -
License MIT
Doc PR -

Follow up of #26214 and #26202

The current normalization logic is both too loose and too broad: it changes the order of recursive data structures, while not normalizing keys.
Since the normalization logic varies by query string parser, I'd like to propose a logic that exactly matches the native PHP one, which is exposed to userland via parse_str(). Using this, we accurately remove all useless information, while preserving all the meaningful one.

(The change in overrideGlobals() is a bug fix to me btw, the current logic breaks the interpretation of legitimate query strings.)

@@ -524,9 +524,11 @@ public function __toString()
*/
public function overrideGlobals()
{
$this->server->set('QUERY_STRING', static::normalizeQueryString(http_build_query($this->query->all(), null, '&')));
$qs = $this->query->all();
Copy link
Member

Choose a reason for hiding this comment

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

Could we please rename $qs var as $queryString? 🙏

$this->assertSame($expectedQuery, $request->getQueryStringForPhp(), $msg);
}

public function getQueryStringNormalizationDataForPhp()
Copy link
Member

Choose a reason for hiding this comment

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

Could we add some tests with UTF-8 chars in the query string? You can see those in URLs like https://github.com/symfony/symfony/issues?utf8=✓

Copy link
Member Author

Choose a reason for hiding this comment

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

added

@nicolas-grekas nicolas-grekas force-pushed the request-qs-norm branch 2 times, most recently from e98cdc3 to dbcf2fb Compare February 19, 2018 14:56
@@ -656,6 +658,24 @@ public static function normalizeQueryString($qs)
return implode('&', $parts);
}

/**
* Normalizes a query string using `parse_str()`.
Copy link
Contributor

Choose a reason for hiding this comment

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

should the parse_str implem be further clarified? It doesnt really tell when to use normalizeQueryStringForPhp vs. normalizeQueryString given both document:

* It builds a normalized query string, where keys/value pairs are alphabetized,
* have consistent escaping and unneeded delimiters are removed.

Does/Should ...ForPhp() imply @internal api?

Copy link
Member Author

Choose a reason for hiding this comment

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

When the resulting string is going to be passed to a PHP based server, you should use the ForPhp variant.
It should certainly not be internal, since this has been requested by users. We want to provide them with the tool :)

array('foo[]=Z&foo[]=A', 'foo%5B0%5D=Z&foo%5B1%5D=A', 'keeps order of values'),
array('foo[Z]=B&foo[A]=B', 'foo%5BZ%5D=B&foo%5BA%5D=B', 'keeps order of keys'),

array('utf8=✓', 'utf8=%E2%9C%93', 'encodes UTF-8'),
Copy link
Member

Choose a reason for hiding this comment

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

Since we're testing the query string sorting, maybe you could add here more than one query string param: array('utf8=✓', ... -> array('utf8=✓&utf16=✘', ...

Copy link
Member Author

Choose a reason for hiding this comment

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

to test what? this is unrelated to the behavior of this function, isn't it?

@javiereguiluz javiereguiluz added the ❄️ Feature Freeze Important Pull Requests to finish before the next Symfony "feature freeze" label Mar 12, 2018
@nicolas-grekas
Copy link
Member Author

ping @fabpot I suppose since this is HttpFoundation (failures unrelated)

@nicolas-grekas nicolas-grekas changed the title [HttpFoundation] Add parse_str()-based methods for query strings normalization [HttpFoundation] Use parse_str() for query strings normalization Mar 21, 2018
@fabpot
Copy link
Member

fabpot commented Mar 23, 2018

Thank you @nicolas-grekas.

@fabpot fabpot merged commit 5133536 into symfony:master Mar 23, 2018
fabpot added a commit that referenced this pull request Mar 23, 2018
…malization (nicolas-grekas)

This PR was merged into the 4.1-dev branch.

Discussion
----------

[HttpFoundation] Use parse_str() for query strings normalization

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

Follow up of #26214 and #26202

The current normalization logic is both too loose and too broad: it changes the order of recursive data structures, while not normalizing keys.
Since the normalization logic varies by query string parser, I'd like to propose a logic that exactly matches the native PHP one, which is exposed to userland via `parse_str()`. Using this, we accurately remove all useless information, while preserving all the meaningful one.

(The change in `overrideGlobals()` is a bug fix to me btw, the current logic breaks the interpretation of legitimate query strings.)

Commits
-------

5133536 [HttpFoundation] Use parse_str() for query strings normalization
@nicolas-grekas nicolas-grekas deleted the request-qs-norm branch April 2, 2018 12:07
@fabpot fabpot mentioned this pull request May 7, 2018
fabpot added a commit that referenced this pull request Jun 24, 2020
…oes the same as `parse_str()` but preserves dots in variable names (nicolas-grekas)

This PR was merged into the 5.2-dev branch.

Discussion
----------

[HttpFoundation] add `HeaderUtils::parseQuery()`: it does the same as `parse_str()` but preserves dots in variable names

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Tickets       | -
| License       | MIT
| Doc PR        | -

Inspired by symfony/psr-http-message-bridge#80
/cc @drupol

Related to #9009, #29664, #26220 but also api-platform/core#509 and https://www.drupal.org/project/drupal/issues/2984272
/cc @dunglas @alexpott

Commits
-------

dd81e32 [HttpFoundation] add `HeaderUtils::parseQuery()`: it does the same as `parse_str()` but preserves dots in variable names
inri13666 pushed a commit to oro-flex/batch-bundle that referenced this pull request Dec 28, 2021
 - change verbosity level for oro:platform:update command on CI
 - fixed TestKernel construction, incorrect namespace uses, current work directory
 - misc services marked as public
 - updated jsm serializer version
 - made use of DebugProcessor instead of the deprecated DebugHandler
 - replaced not existing anymore LockHandler to new symfony component
 symfony/lock. Added needed changes to the logic
 - temporary disabled "closed entity manager" in dataProvider for test
 JobStorageTest
 - temporary disabled "closed entity manager" in dataProvider for test
 DoctrineJobRepositoryTest
 - changed container file name in functional_standalone.sh
 - Symfony 4 changed Request::normalizeQueryString() (symfony/symfony#26220). Due to our logic has own filters (query params) which is not compatible with new implementation. We use old approach till we change our logic to use new syntax
inri13666 pushed a commit to oro-flex/message-queue-bundle that referenced this pull request Jan 6, 2022
 - change verbosity level for oro:platform:update command on CI
 - fixed TestKernel construction, incorrect namespace uses, current work directory
 - misc services marked as public
 - updated jsm serializer version
 - made use of DebugProcessor instead of the deprecated DebugHandler
 - replaced not existing anymore LockHandler to new symfony component
 symfony/lock. Added needed changes to the logic
 - temporary disabled "closed entity manager" in dataProvider for test
 JobStorageTest
 - temporary disabled "closed entity manager" in dataProvider for test
 DoctrineJobRepositoryTest
 - changed container file name in functional_standalone.sh
 - Symfony 4 changed Request::normalizeQueryString() (symfony/symfony#26220). Due to our logic has own filters (query params) which is not compatible with new implementation. We use old approach till we change our logic to use new syntax
josh-taylor-k3 pushed a commit to josh-taylor-k3/crm-application-using-symfony that referenced this pull request Nov 4, 2024
 - change verbosity level for oro:platform:update command on CI
 - fixed TestKernel construction, incorrect namespace uses, current work directory
 - misc services marked as public
 - updated jsm serializer version
 - made use of DebugProcessor instead of the deprecated DebugHandler
 - replaced not existing anymore LockHandler to new symfony component
 symfony/lock. Added needed changes to the logic
 - temporary disabled "closed entity manager" in dataProvider for test
 JobStorageTest
 - temporary disabled "closed entity manager" in dataProvider for test
 DoctrineJobRepositoryTest
 - changed container file name in functional_standalone.sh
 - Symfony 4 changed Request::normalizeQueryString() (symfony/symfony#26220). Due to our logic has own filters (query params) which is not compatible with new implementation. We use old approach till we change our logic to use new syntax
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature HttpFoundation ❄️ Feature Freeze Important Pull Requests to finish before the next Symfony "feature freeze" Status: Needs Review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants