Skip to content

Commit

Permalink
[http-client] Decode credentials for Basic Authentication (#5223)
Browse files Browse the repository at this point in the history
Co-authored-by: draker94 <noreply@github.com>
  • Loading branch information
draker94 and web-flow authored Jul 18, 2024
1 parent 3d313ab commit 0239ff6
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Optional;
Expand Down Expand Up @@ -161,7 +162,8 @@ private static URI getRequestUri(ClassicHttpRequest request) throws IOException
private static void configureBasicAuth(boolean usePreemptiveBasicAuthIfAvailable, String plainUserInfo,
HttpHost host, HttpClientContext internalContext)
{
UserInfo userInfo = UriUtils.parseUserInfo(plainUserInfo);
String plainUserInfoDecoded = URLDecoder.decode(plainUserInfo, StandardCharsets.UTF_8);
UserInfo userInfo = UriUtils.parseUserInfo(plainUserInfoDecoded);
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(userInfo.user(),
userInfo.password().toCharArray());
HttpHost normalizedHost = RoutingSupport.normalize(host, DefaultSchemePortResolver.INSTANCE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.Base64;
Expand Down Expand Up @@ -84,8 +85,10 @@ class HttpClientTests
private static final URI URI_TO_GO = URI.create(VIVIDUS_ORG);

private static final String USER = "user";
private static final String PASSWORD = "pass";
private static final String PASSWORD = "pass%5E";
private static final String PASSWORD_DECODED = URLDecoder.decode(PASSWORD, StandardCharsets.UTF_8);
private static final String BASIC_AUTH = USER + ":" + PASSWORD;
private static final String BASIC_AUTH_DECODED = URLDecoder.decode(BASIC_AUTH, StandardCharsets.UTF_8);
private static final String SCHEME = "https";
private static final String HOST = "www.vividus.org";
private static final HttpHost HTTP_HOST = new HttpHost(SCHEME, HOST, 443);
Expand Down Expand Up @@ -164,7 +167,7 @@ void shouldDoHttpGetWithPreemptiveAuthEnabled() throws IOException, Authenticati
var authScheme = authExchange.getAuthScheme();
var authResponse = authScheme.generateAuthResponse(null, null, null);
var expectedAuthResponse = "Basic " + Base64.getEncoder().encodeToString(
BASIC_AUTH.getBytes(StandardCharsets.UTF_8));
BASIC_AUTH_DECODED.getBytes(StandardCharsets.UTF_8));
assertEquals(expectedAuthResponse, authResponse);
assertNull(httpClientContext.getCredentialsProvider());
}
Expand Down Expand Up @@ -202,7 +205,7 @@ void shouldDoHttpHead() throws IOException
assertInstanceOf(BasicCredentialsProvider.class, credentialsProvider);
var credentials = credentialsProvider.getCredentials(new AuthScope(HTTP_HOST), null);
assertEquals(USER, credentials.getUserPrincipal().getName());
assertArrayEquals(PASSWORD.toCharArray(), credentials.getPassword());
assertArrayEquals(PASSWORD_DECODED.toCharArray(), credentials.getPassword());
}

@Test
Expand Down
4 changes: 4 additions & 0 deletions vividus-tests/src/main/resources/story/integration/HTTP.story
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,7 @@ Then JSON element from `${response-as-bytes}` by JSON path `$` is equal to `{
"Content-Type": [ "application/x-www-form-urlencoded; charset=UTF-8" ]
}
}`ignoring extra fields

Scenario: Verify using of encoded unsafe characters in user info for Basic Authentication
When I execute HTTP GET request for resource with URL `https://username:password%5E@httpbin.org/basic-auth/username/password^`
Then response code is equal to `200`

0 comments on commit 0239ff6

Please sign in to comment.