Skip to content

Commit 3004e93

Browse files
authored
Only trim the leading slash from the DSN if a host is also available (#6790)
| Q | A |------------- | ----------- | Type | improvement | Fixed issues | #6789 #### Summary Previously the DsnParser unconditionally removed the first character from the path, without verifying that it is actually a slash and that it is dealing with a reasonably well-formed URI. This causes confusing behavior when a malformed URI that survives `parse_url()` is passed to `parse()`, since DsnParser would return the input as the `dbname`, but with the first character missing. This can lead to hard-to-debug situations when the DSN is coming from an environment variable, where quoting characters are accidentally included in the value itself, instead of being interpreted by the tool setting the env, since the `dbname` almost looks like the intended input (but still having the trailing quote). see #6789
1 parent a11db88 commit 3004e93

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

src/Tools/DsnParser.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,11 @@ private function parseDatabaseUrlPath(array $url, array $params): array
109109
return $params;
110110
}
111111

112-
$url['path'] = $this->normalizeDatabaseUrlPath($url['path']);
112+
if (isset($params['host'])) {
113+
// Only normalize the path if a host is also available. Otherwise we might trim leading slashes
114+
// from a pure dbname.
115+
$url['path'] = $this->normalizeDatabaseUrlPath($url['path']);
116+
}
113117

114118
// If we do not have a known DBAL driver, we do not know any connection URL path semantics to evaluate
115119
// and therefore treat the path as a regular DBAL connection URL path.
@@ -131,6 +135,8 @@ private function parseDatabaseUrlPath(array $url, array $params): array
131135
*/
132136
private function normalizeDatabaseUrlPath(string $urlPath): string
133137
{
138+
assert($urlPath[0] === '/');
139+
134140
// Trim leading slash from URL path.
135141
return substr($urlPath, 1);
136142
}

tests/Tools/DsnParserTest.php

+8
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,14 @@ public static function databaseUrls(): iterable
172172
'dbname' => 'baz',
173173
],
174174
],
175+
'quoted URL' => [
176+
'"sqlite:////tmp/dbname.sqlite"',
177+
['dbname' => '"sqlite:////tmp/dbname.sqlite"'],
178+
],
179+
'absolute path' => [
180+
'/tmp/dbname.sqlite',
181+
['dbname' => '/tmp/dbname.sqlite'],
182+
],
175183
];
176184
}
177185

0 commit comments

Comments
 (0)