diff options
author | Aurelien Poscia <aurelien.poscia@sonarsource.com> | 2022-09-16 15:20:09 +0200 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2022-09-19 20:03:08 +0000 |
commit | 1f9bc827e81575d6515061cace65526ca3edf18b (patch) | |
tree | f16fd9d7ffb341854f679ecbcc7860183f9425b8 /server/sonar-webserver-auth | |
parent | 1e1e26d5dc136036fc4cbfde759e4879e098f519 (diff) | |
download | sonarqube-1f9bc827e81575d6515061cace65526ca3edf18b.tar.gz sonarqube-1f9bc827e81575d6515061cace65526ca3edf18b.zip |
SONAR-17271 Request body can be read multiple time & implement support of comments coming from GitHub webhooks
Diffstat (limited to 'server/sonar-webserver-auth')
2 files changed, 16 insertions, 16 deletions
diff --git a/server/sonar-webserver-auth/src/main/java/org/sonar/server/authentication/GithubWebhookAuthentication.java b/server/sonar-webserver-auth/src/main/java/org/sonar/server/authentication/GithubWebhookAuthentication.java index 2fb47dd24d0..6197d4e4c32 100644 --- a/server/sonar-webserver-auth/src/main/java/org/sonar/server/authentication/GithubWebhookAuthentication.java +++ b/server/sonar-webserver-auth/src/main/java/org/sonar/server/authentication/GithubWebhookAuthentication.java @@ -20,7 +20,6 @@ package org.sonar.server.authentication; import com.google.common.annotations.VisibleForTesting; -import java.io.IOException; import java.security.MessageDigest; import java.util.Optional; import javax.servlet.http.HttpServletRequest; @@ -97,28 +96,18 @@ public class GithubWebhookAuthentication { private static String getGithubSignature(HttpServletRequest request, String githubAppId) { String githubSignature = request.getHeader(GITHUB_SIGNATURE_HEADER); - if (isEmpty(githubSignature) ) { + if (isEmpty(githubSignature)) { logAuthenticationProblemAndThrow(format(MSG_UNAUTHENTICATED_GITHUB_CALLS_DENIED, githubAppId)); } return githubSignature; } private static String getBody(HttpServletRequest request) { - Optional<String> body = getBodyInternal(request); - if (body.isEmpty() || isEmpty(body.get())) { - logAuthenticationProblemAndThrow(MSG_NO_BODY_FOUND); - } - return body.get(); - } - - - private static Optional<String> getBodyInternal(HttpServletRequest request) { try { - String body = request.getReader().lines().collect(joining(System.lineSeparator())); - return Optional.of(body); - } catch (IOException e) { - LOG.debug("Unexpected error while trying to get the body of github webhook request", e); - return Optional.empty(); + return request.getReader().lines().collect(joining(System.lineSeparator())); + } catch (Exception e) { + logAuthenticationProblemAndThrow(MSG_NO_BODY_FOUND); + return ""; } } diff --git a/server/sonar-webserver-auth/src/test/java/org/sonar/server/authentication/GithubWebhookAuthenticationTest.java b/server/sonar-webserver-auth/src/test/java/org/sonar/server/authentication/GithubWebhookAuthenticationTest.java index 96b48fee579..f12309fe53d 100644 --- a/server/sonar-webserver-auth/src/test/java/org/sonar/server/authentication/GithubWebhookAuthenticationTest.java +++ b/server/sonar-webserver-auth/src/test/java/org/sonar/server/authentication/GithubWebhookAuthenticationTest.java @@ -119,6 +119,17 @@ public class GithubWebhookAuthenticationTest { assertThatExceptionOfType(AuthenticationException.class) .isThrownBy(() -> githubWebhookAuthentication.authenticate(request)) + .withMessage(MSG_AUTHENTICATION_FAILED); + assertThat(logTester.getLogs(LoggerLevel.WARN)).extracting(LogAndArguments::getFormattedMsg).contains(MSG_AUTHENTICATION_FAILED); + } + + @Test + public void authenticate_withExceptionWhileReadingBody_throws() throws IOException { + HttpServletRequest request = mockRequest(GITHUB_PAYLOAD, GITHUB_SIGNATURE); + when(request.getReader()).thenThrow(new IOException()); + + assertThatExceptionOfType(AuthenticationException.class) + .isThrownBy(() -> githubWebhookAuthentication.authenticate(request)) .withMessage(MSG_NO_BODY_FOUND); assertThat(logTester.getLogs(LoggerLevel.WARN)).extracting(LogAndArguments::getFormattedMsg).contains(MSG_NO_BODY_FOUND); } |