Skip to content

Commit ed0cb99

Browse files
committed
Changes the parser to support more characters
1 parent f08b3be commit ed0cb99

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

lib/connection_config.js

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77

88
'use strict';
99

10-
const { URL } = require('url');
1110
const ClientConstants = require('./constants/client');
1211
const Charsets = require('./constants/charsets');
12+
const { URL } = require('url');
1313
let SSLProfiles = null;
1414

1515
const validOptions = {
@@ -249,13 +249,31 @@ class ConnectionConfig {
249249
}
250250

251251
static parseUrl(url) {
252-
const parsedUrl = new URL(url);
252+
// First, split the url. Take the mysql:// part. The username is after that.
253+
// Then, split at the : - usernames cannot contain a :
254+
let user = url.split("mysql://")[1];
255+
if (user) user = user.split(":")[0];
256+
else throw new Error("Invalid connection string");
257+
258+
// Now, get the password. Remove the mysql://<USER>: part from the connection string,
259+
// Continue until the last @ - that indicates the host is after it.
260+
let password = url.substring(8 + user.length + 1);
261+
if (password) {
262+
const passwordParts = password.split("@");
263+
password = passwordParts.splice(0, passwordParts.length-1).join("@");
264+
} else throw new Error("Invalid connection string");
265+
266+
// Now, let node handle the rest of the url parsing (for the host, port and database)
267+
// Also, remove the username and password and replace them with placeholders.
268+
// This way, we do not get an error when the username or password contains special characters.
269+
const parsedUrl = new URL(url.replace(user, "_PLACEHOLDER_").replace(password, "_PLACEHOLDER_"));
270+
253271
const options = {
254272
host: parsedUrl.hostname,
255273
port: parsedUrl.port,
256274
database: parsedUrl.pathname.slice(1),
257-
user: decodeURIComponent(parsedUrl.username),
258-
password: decodeURIComponent(parsedUrl.password)
275+
user,
276+
password
259277
};
260278
parsedUrl.searchParams.forEach((value, key) => {
261279
try {

0 commit comments

Comments
 (0)