Skip to content
Draft
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

## 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 @@ -315,6 +315,26 @@ 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();

Assert.assertThrows(
RegistryAuthenticationFailedException.class,
() -> authenticator.authenticatePull(Credential.from("<token>", "oauth2_token")));
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

## 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

## 3.5.2

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,10 @@ private void verifyGoals(Path projectRoot, String profilesString, String... expe
verifier.verifyErrorFreeLog();
Path logFile = Paths.get(verifier.getBasedir()).resolve(verifier.getLogFileName());
List<String> log = Files.readAllLines(logFile, StandardCharsets.UTF_8);
if (log.size() != 0 && log.get(0).startsWith("Picked up JAVA_TOOL_OPTIONS:")) {
log.remove(0);
}
log.removeIf(
line ->
line.startsWith("Picked up JAVA_TOOL_OPTIONS:")
|| line.startsWith("NOTE: Picked up JDK_JAVA_OPTIONS:"));

Assert.assertEquals(Arrays.asList(expectedGoals), log);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,10 @@ private static String getSyncMapJson(Path projectRoot, String module, String pom
throws VerificationException, IOException {
Path logFile = runBuild(projectRoot, module, pomXml);
List<String> outputLines = Files.readAllLines(logFile, StandardCharsets.UTF_8);
if (outputLines.size() != 0 && outputLines.get(0).startsWith("Picked up JAVA_TOOL_OPTIONS:")) {
outputLines.remove(0);
}
outputLines.removeIf(
line ->
line.startsWith("Picked up JAVA_TOOL_OPTIONS:")
|| line.startsWith("NOTE: Picked up JDK_JAVA_OPTIONS:"));
Assert.assertEquals(3, outputLines.size()); // we expect ["\n", "<marker>", "<sync-json>"]
Assert.assertEquals("BEGIN JIB JSON: SYNCMAP/1", outputLines.get(1));
return outputLines.get(2); // this is the JSON output
Expand Down
Loading