Skip to content

Commit

Permalink
Fix password parsing error when URI contains @ #2254
Browse files Browse the repository at this point in the history
Original pull request: #2255
  • Loading branch information
coolbeevip authored and mp911de committed Nov 22, 2022
1 parent 2177a8d commit 1f581c2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/main/java/io/lettuce/core/RedisURI.java
Original file line number Diff line number Diff line change
Expand Up @@ -760,8 +760,8 @@ private static RedisURI buildRedisUriFromUri(URI uri) {

String userInfo = uri.getUserInfo();

if (isEmpty(userInfo) && isNotEmpty(uri.getAuthority()) && uri.getAuthority().indexOf('@') > 0) {
userInfo = uri.getAuthority().substring(0, uri.getAuthority().indexOf('@'));
if (isEmpty(userInfo) && isNotEmpty(uri.getAuthority()) && uri.getAuthority().lastIndexOf('@') > 0) {
userInfo = uri.getAuthority().substring(0, uri.getAuthority().lastIndexOf('@'));
}

if (isNotEmpty(userInfo)) {
Expand Down Expand Up @@ -1121,7 +1121,7 @@ private static Builder configureStandalone(URI uri) {
if (isNotEmpty(uri.getAuthority())) {
String authority = uri.getAuthority();
if (authority.indexOf('@') > -1) {
authority = authority.substring(authority.indexOf('@') + 1);
authority = authority.substring(authority.lastIndexOf('@') + 1);
}

builder = Builder.redis(authority);
Expand Down Expand Up @@ -1158,7 +1158,7 @@ private static RedisURI.Builder configureSentinel(URI uri) {
if (builder == null && isNotEmpty(uri.getAuthority())) {
String authority = uri.getAuthority();
if (authority.indexOf('@') > -1) {
authority = authority.substring(authority.indexOf('@') + 1);
authority = authority.substring(authority.lastIndexOf('@') + 1);
}

String[] hosts = authority.split(",");
Expand Down
21 changes: 21 additions & 0 deletions src/test/java/io/lettuce/core/RedisURIUnitTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@

import static org.assertj.core.api.Assertions.*;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.LinkedHashMap;
import java.util.Map;
Expand Down Expand Up @@ -189,6 +192,24 @@ void socketAltUriTest() {
assertThat(redisURI).hasToString("redis-socket:///var/tmp/other-socket?database=2");
}

@Test
void escapeCharacterParsingTest() throws UnsupportedEncodingException {
String password = "abc@#d";
String translatedPassword = URLEncoder.encode(password, StandardCharsets.UTF_8.name());

// redis sentinel
String uri = "redis-sentinel://"+translatedPassword+"@h1:1234,h2:1234,h3:1234/0?sentinelMasterId=masterId";
RedisURI redisURI = RedisURI.create(uri);
assertThat(redisURI.getSentinels().get(0).getHost()).isEqualTo("h1");
assertThat(String.valueOf(redisURI.getCredentialsProvider().resolveCredentials().block().getPassword())).isEqualTo(password);

// redis standalone
uri = "redis://"+translatedPassword+"@h1:1234/0";
redisURI = RedisURI.create(uri);
assertThat(redisURI.getHost()).isEqualTo("h1");
assertThat(String.valueOf(redisURI.getCredentialsProvider().resolveCredentials().block().getPassword())).isEqualTo(password);
}

@Test
void timeoutParsingTest() {

Expand Down

0 comments on commit 1f581c2

Please sign in to comment.