Skip to content

Commit

Permalink
CacheDB: Allow "@" in URL passwords
Browse files Browse the repository at this point in the history
(cherry picked from commit 1df9afd)
  • Loading branch information
liviuchircu committed Nov 3, 2023
1 parent 2a44eb4 commit 5a8f6e4
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
10 changes: 9 additions & 1 deletion cachedb/cachedb_id.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ static int parse_cachedb_url(struct cachedb_id* id, const str* url)

enum state st;
unsigned int len, i, ipv6_flag=0, multi_hosts=0;
char* begin;
char* begin, *last_at;
char* prev_token,*start_host=NULL,*start_prev=NULL,*ptr;

prev_token = 0;
Expand All @@ -106,6 +106,8 @@ static int parse_cachedb_url(struct cachedb_id* id, const str* url)
if (dupl_string(&id->initial_url,url->s,url->s+url->len) < 0)
goto err;

last_at = q_memrchr(url->s, '@', url->len);

for(i = 0; i < len; i++) {
switch(st) {
case ST_SCHEME:
Expand Down Expand Up @@ -159,6 +161,9 @@ static int parse_cachedb_url(struct cachedb_id* id, const str* url)
case ST_USER_HOST:
switch(url->s[i]) {
case '@':
if (&url->s[i] < last_at)
break;

st = ST_HOST;
multi_hosts = 0;
if (dupl_string(&id->username, begin, url->s + i) < 0) goto err;
Expand Down Expand Up @@ -195,6 +200,9 @@ static int parse_cachedb_url(struct cachedb_id* id, const str* url)
case ST_PASS_PORT:
switch(url->s[i]) {
case '@':
if (&url->s[i] < last_at)
break;

st = ST_HOST;
id->username = prev_token;
if (dupl_string(&id->password, begin, url->s + i) < 0) goto err;
Expand Down
32 changes: 32 additions & 0 deletions cachedb/test/test_cachedb.c
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,38 @@ static void test_cachedb_url(void)

/* special chars in password */

CDB_PARSE("redis:group1://user:,pwd,foo,@h1,h2,h3:6379/d");
ok(db->flags == CACHEDB_ID_MULTIPLE_HOSTS);
ok(!strcmp(db->username, "user"));
ok(!strcmp(db->password, ",pwd,foo,"));
ok(!strcmp(db->host, "h1,h2,h3:6379"));
ok(!strcmp(db->database, "d"));
ok(db->port == 0);

CDB_PARSE("redis:group1://user:@,pwd,foo,@h1,h2,h3:6379/d");
ok(db->flags == CACHEDB_ID_MULTIPLE_HOSTS);
ok(!strcmp(db->username, "user"));
ok(!strcmp(db->password, "@,pwd,foo,"));
ok(!strcmp(db->host, "h1,h2,h3:6379"));
ok(!strcmp(db->database, "d"));
ok(db->port == 0);

CDB_PARSE("redis:group1://user:,pwd,@foo,@h1,h2,h3:6379/d");
ok(db->flags == CACHEDB_ID_MULTIPLE_HOSTS);
ok(!strcmp(db->username, "user"));
ok(!strcmp(db->password, ",pwd,@foo,"));
ok(!strcmp(db->host, "h1,h2,h3:6379"));
ok(!strcmp(db->database, "d"));
ok(db->port == 0);

CDB_PARSE("redis:group1://user:,pwd,foo,@@h1,h2,h3:6379/d");
ok(db->flags == CACHEDB_ID_MULTIPLE_HOSTS);
ok(!strcmp(db->username, "user"));
ok(!strcmp(db->password, ",pwd,foo,@"));
ok(!strcmp(db->host, "h1,h2,h3:6379"));
ok(!strcmp(db->database, "d"));
ok(db->port == 0);

CDB_PARSE("redis:group1://:,pwd,foo,@h1,h2,h3:6379/d");
ok(db->flags == CACHEDB_ID_MULTIPLE_HOSTS);
ok(!strcmp(db->username, ""));
Expand Down

0 comments on commit 5a8f6e4

Please sign in to comment.