8000 bug #37868 [Lock] MongoDbStore handle duplicate querystring keys in m… · symfony/symfony@c426abe · GitHub
[go: up one dir, main page]

Skip to content

Commit c426abe

Browse files
committed
bug #37868 [Lock] MongoDbStore handle duplicate querystring keys in mongodb uri when stripping (kralos)
This PR was squashed before being merged into the 5.1 branch. Discussion ---------- [Lock] MongoDbStore handle duplicate querystring keys in mongodb uri when stripping | Q | A | ------------- | --- | Branch? | 5.1 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #37864 | License | MIT | Doc PR | Allow duplicate querystring keys when stripping `collection`. `readPreferenceTags` is currently allowed to be specified twice so re-assembling the querystring with `http_build_query` will also strip duplicated `readPreferenceTags`. Use `preg_match` instead. Commits ------- c1ea9ae [Lock] MongoDbStore handle duplicate querystring keys in mongodb uri when stripping
2 parents 44caccb + c1ea9ae commit c426abe

File tree

2 files changed

+17
-19
lines changed

2 files changed

+17
-19
lines changed

src/Symfony/Component/Lock/Store/MongoDbStore.php

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -141,30 +141,21 @@ private function skimUri(string $uri): string
141141
if (false === $parsedUrl = parse_url($uri)) {
142142
throw new InvalidArgumentException(sprintf('The given MongoDB Connection URI "%s" is invalid.', $uri));
143143
}
144-
145-
$query = [];
146-
if (isset($parsedUrl['query'])) {
147-
parse_str($parsedUrl['query'], $query);
148-
}
149-
150-
if (isset($query['collection'])) {
151-
$this->options['collection'] = $query['collection'];
152-
$queryStringPos = strrpos($uri, $parsedUrl['query']);
153-
unset($query['collection']);
154-
$prefix = substr($uri, 0, $queryStringPos);
155-
$newQuery = http_build_query($query, '', '&', PHP_QUERY_RFC3986);
156-
if (empty($newQuery)) {
157-
$prefix = rtrim($prefix, '?');
158-
}
159-
$suffix = substr($uri, $queryStringPos 8000 + \strlen($parsedUrl['query']));
160-
$uri = $prefix.$newQuery.$suffix;
161-
}
162-
163144
$pathDb = ltrim($parsedUrl['path'] ?? '', '/') ?: null;
164145
if (null !== $pathDb) {
165146
$this->options['database'] = $pathDb;
166147
}
167148

149+
$matches = [];
150+
if (preg_match('/^(.*[\?&])collection=([^&#]*)&?(([^#]*).*)$/', $uri, $matches)) {
151+
$prefix = $matches[1];
152+
$this->options['collection'] = $matches[2];
153+
if (empty($matches[4])) {
154+
$prefix = substr($prefix, 0, -1);
155+
}
156+
$uri = $prefix.$matches[3];
157+
}
158+
168159
return $uri;
169160
}
170161

src/Symfony/Component/Lock/Tests/Store/MongoDbStoreTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,5 +187,12 @@ public function provideUriCollectionStripArgs()
187187
yield ['mongodb://localhost/?replicaSet=repl', ['database' => 'test', 'collection' => 'lock'], 'mongodb://localhost/?replicaSet=repl'];
188188
yield ['mongodb://localhost/test?collection=lock&replicaSet=repl', [], 'mongodb://localhost/test?replicaSet=repl'];
189189
yield ['mongodb://localhost/test?replicaSet=repl', ['collection' => 'lock'], 'mongodb://localhost/test?replicaSet=repl'];
190+
191+
yield ['mongodb://localhost/test?readPreferenceTags=dc:foo&collection=lock&readPreferenceTags=dc:bar', [], 'mongodb://localhost/test?readPreferenceTags=dc:foo&readPreferenceTags=dc:bar'];
192+
yield ['mongodb://localhost?foo_collection=x&collection=lock&bar_collection=x#collection=x', ['database' => 'test'], 'mongodb://localhost?foo_collection=x&bar_collection=x#collection=x'];
193+
yield ['mongodb://localhost?collection=lock&foo_collection=x&bar_collection=x#collection=x', ['database' => 'test'], 'mongodb://localhost?foo_collection=x&bar_collection=x#collection=x'];
194+
yield ['mongodb://localhost?foo_collection=x&bar_collection=x&collection=lock#collection=x', ['database' => 'test'], 'mongodb://localhost?foo_collection=x&bar_collection=x#collection=x'];
195+
yield ['mongodb://user:?collection=a@localhost?collection=lock', ['database' => 'test'], 'mongodb://user:?collection=a@localhost'];
196+
yield ['mongodb://user:&collection=a@localhost/?collection=lock', ['database' => 'test'], 'mongodb://user:&collection=a@localhost/'];
190197
}
191198
}

0 commit comments

Comments
 (0)
0