Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix password parsing error when redis-sentinel URI contains @ #2255

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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