Skip to content

DsnParser accepts a quoted DSN #6789

Open
@TimWolla

Description

@TimWolla

This request is halfway between a bug report and a feature request. That's why I am not strictly following either template:

A few days ago I had a hard to debug issue when setting up a new Symfony application. In the Symfony Doctrine config I used an environment variable as the source for the DBAL URL:

doctrine:
    dbal:
        url: '%env(resolve:DATABASE_URL)%'

The DATABASE_URL was configured as:

DATABASE_URL="sqlite:///%kernel.project_dir%/var/data.db"

What I did not realize was that the tool I used to configure the environment variables includes the double quotes " as-is in the value of the environment variable.

As a result internally the DSN was interpreted as if the following code was used:

$dsnParser = new DsnParser();
var_dump($dsnParser->parse('"sqlite:///tmp/data.db"'));

which results in the following output:

array(1) {
  ["dbname"]=>
  string(22) "sqlite:///tmp/data.db""
}

As a result, Symfony / DBAL attempted to use the pdo_mysql driver with the dbname of sqlite:////path/to/symfony/application/var/data.db".

It took me quite some time of debugging through Symfony’s Doctrine integration and DBAL itself to find the issue. It did not help that the resulting dbname did not include the leading quote, but only the trailing one, since the string looks correct at a first glance.


As to what I expect here: The ideal solution would probably be throwing a MalformedDsnException if the DSN is surrounded by double or single quotes, since this likely indicates some user error rather than a valid DB name.

Alternatively, the following change probably would've simplified the debugging for me, because it would not have stripped the leading quote from the resulting dbname, making it more obvious that something is wrong:

--- i/src/Tools/DsnParser.php
+++ w/src/Tools/DsnParser.php
@@ -132,7 +132,7 @@ final class DsnParser
     private function normalizeDatabaseUrlPath(string $urlPath): string
     {
         // Trim leading slash from URL path.
-        return substr($urlPath, 1);
+        return preg_replace('/^\//', '', $urlPath);
     }
 
     /**

I'm leaving the choice of what to do to you as the maintainer. If you decide not to change anything, that's also fine with me. I simply wanted to write this down somewhere in case there is something to improve and also for search engines to come across, possibly helping someone having the same issue.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions