Skip to content
Merged
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
1 change: 1 addition & 0 deletions jib-core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ All notable changes to this project will be documented in this file.
### Changed

### Fixed
- fix: do not send OAuth2 refresh tokens over plain HTTP unless `sendCredentialsOverHttp` is set (`-DsendCredentialsOverHttp=true`)

## 0.28.2

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,12 @@ private Response call(
boolean retryOnIoException) // See https://github.com/GoogleContainerTools/jib/issues/3424
throws IOException {
boolean clearAuthorization = !isHttpsProtocol(url) && !sendAuthorizationOverHttp;
if (clearAuthorization && request.hasCredentialBody()) {
throw new SSLException(
"credentials not sent over insecure HTTP connection: "
+ url
+ "; use HTTPS or set the 'sendCredentialsOverHttp' system property to true");
}

HttpHeaders requestHeaders =
clearAuthorization
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,17 @@ public class Request {
/** The HTTP request body. */
@Nullable private final HttpContent body;

/** Whether the HTTP request body carries credentials. */
private final boolean credentialBody;

/** HTTP connection and read timeout. */
@Nullable private final Integer httpTimeout;

public static class Builder {

private final HttpHeaders headers = new HttpHeaders().setAccept("*/*");
@Nullable private HttpContent body;
private boolean credentialBody;
@Nullable private Integer httpTimeout;

public Request build() {
Expand Down Expand Up @@ -96,6 +100,21 @@ public Builder setHttpTimeout(@Nullable Integer httpTimeout) {
*/
public Builder setBody(@Nullable HttpContent httpContent) {
this.body = httpContent;
this.credentialBody = false;
return this;
}

/**
* Sets a body that carries credentials. {@link FailoverHttpClient} sends such a request over
* plain HTTP only if sending credentials over HTTP is enabled, and otherwise throws {@link
* javax.net.ssl.SSLException}.
*
* @param httpContent the body content
* @return this
*/
public Builder setCredentialBody(HttpContent httpContent) {
this.body = httpContent;
this.credentialBody = true;
return this;
}
}
Expand All @@ -107,6 +126,7 @@ public static Builder builder() {
private Request(Builder builder) {
this.headers = builder.headers;
this.body = builder.body;
this.credentialBody = builder.credentialBody;
this.httpTimeout = builder.httpTimeout;
}

Expand All @@ -119,6 +139,10 @@ HttpContent getHttpContent() {
return body;
}

boolean hasCredentialBody() {
return credentialBody;
}

@Nullable
Integer getHttpTimeout() {
return httpTimeout;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ private Authorization authenticate(

if (isOAuth2Auth(credential)) {
String parameters = getAuthRequestParameters(credential, repositoryScopes);
requestBuilder.setBody(
requestBuilder.setCredentialBody(
new BlobHttpContent(Blobs.from(parameters), MediaType.FORM_DATA.toString()));
} else if (credential != null) {
requestBuilder.setAuthorization(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,40 @@ public void testGet_originalRequestHeaderUntouchedWhenClearingHeader() throws IO
"Basic ZmFrZS11c2VybmFtZTpmYWtlLXNlY3JldA==", request.getHeaders().getAuthorization());
}

@Test
public void testPost_doNotSendCredentialBodyOverHttp() throws IOException {
FailoverHttpClient insecureHttpClient = newHttpClient(true, false);
Request request =
Request.builder()
.setCredentialBody(new BlobHttpContent(Blobs.from("crepecake"), "fake.content.type"))
.build();

Mockito.when(mockHttpRequest.execute()).thenThrow(new ConnectException());

SSLException exception =
Assert.assertThrows(
SSLException.class,
() -> insecureHttpClient.post(new URL("https://insecure"), request));
assertThat(exception).hasMessageThat().contains("sendCredentialsOverHttp");

verifyCapturedUrls("https://insecure");
}

@Test
public void testPost_sendCredentialBodyOverHttp() throws IOException {
FailoverHttpClient insecureHttpClient = newHttpClient(true, true);
Request request =
Request.builder()
.setCredentialBody(new BlobHttpContent(Blobs.from("crepecake"), "fake.content.type"))
.build();

try (Response response = insecureHttpClient.post(new URL("http://plain.http"), request)) {
// intentionally empty
}

verifyCapturedUrls("http://plain.http");
}

@Test
public void testShutDown() throws IOException {
FailoverHttpClient secureHttpClient = newHttpClient(false, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.nio.charset.StandardCharsets;
import java.security.GeneralSecurityException;
import java.util.Collections;
import javax.net.ssl.SSLException;
import org.hamcrest.CoreMatchers;
import org.hamcrest.MatcherAssert;
import org.junit.Assert;
Expand Down Expand Up @@ -315,6 +316,29 @@ public void testAuthorizationCleared() throws RegistryAuthenticationFailedExcept
}
}

@Test
public void testAuthenticate_oauth2RefreshTokenNotSentOverHttp()
throws IOException, InterruptedException, GeneralSecurityException, URISyntaxException,
RegistryAuthenticationFailedException {
try (TestWebServer server = new TestWebServer(false)) {
RegistryAuthenticator authenticator =
RegistryAuthenticator.fromAuthenticationMethod(
"Bearer realm=\"" + server.getEndpoint() + "\"",
registryEndpointRequestProperties,
"user-agent",
new FailoverHttpClient(true, false, ignored -> {}))
.get();

RegistryAuthenticationFailedException exception =
Assert.assertThrows(
RegistryAuthenticationFailedException.class,
() -> authenticator.authenticatePull(Credential.from("<token>", "oauth2_token")));
assertThat(exception).hasCauseThat().isInstanceOf(SSLException.class);
assertThat(exception).hasMessageThat().contains("sendCredentialsOverHttp");
assertThat(server.getInputRead()).isEmpty();
}
}

@Test
public void testAuthenticationResponseTemplate_readsToken() throws IOException {
String input = "{\"token\":\"test_value\"}";
Expand Down
1 change: 1 addition & 0 deletions jib-gradle-plugin/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ All notable changes to this project will be documented in this file.
### Changed

### Fixed
- fix: do not send OAuth2 refresh tokens over plain HTTP unless `sendCredentialsOverHttp` is set (`-DsendCredentialsOverHttp=true`)

## 3.5.4

Expand Down
1 change: 1 addition & 0 deletions jib-maven-plugin/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ All notable changes to this project will be documented in this file.
### Changed

### Fixed
- fix: do not send OAuth2 refresh tokens over plain HTTP unless `sendCredentialsOverHttp` is set (`-DsendCredentialsOverHttp=true`)

## 3.5.2

Expand Down
Loading