Skip to content

Commit 161d3cd

Browse files
committed
[JENKINS-75735] Hook payload is discarded if not recognized and comes from Bitbucket Cloud and Data Center instances
Change test cases to better reflect the header sent in the request of a webhook Add payload examples provided by Alexey P. from moveworkforward support team.
1 parent 44b1a90 commit 161d3cd

File tree

13 files changed

+712
-37
lines changed

13 files changed

+712
-37
lines changed

docs/USER_GUIDE.adoc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ For Bitbucket Data Center only it is possible chose which webhooks implementatio
119119

120120
- Native implementation will configure the webhooks provided by default with the Server, so it will always be available.
121121

122-
- Plugin implementation relies on the configuration available via specific APIs provided by the https://marketplace.atlassian.com/apps/1215474/post-webhooks-for-bitbucket?tab=overview&hosting=datacenter[Post Webhooks for Bitbucket] plugin itself. To get it worked plugin must be already pre-installed on the server instance. This provider allows custom settings managed by the _ignore committers_ trait. _Note: This specific implementation will be moved to an individual repository as soon as https://issues.jenkins.io/browse/JENKINS-74913[JENKINS-74913] is implemented._
122+
- Plugin implementation (*deprecated*) relies on the configuration available via specific APIs provided by the https://marketplace.atlassian.com/apps/1215474/post-webhooks-for-bitbucket?tab=overview&hosting=datacenter[Post Webhooks for Bitbucket] plugin itself. To get it worked plugin must be already pre-installed on the server instance. This provider allows custom settings managed by the _ignore committers_ trait. _Note: This specific implementation will be moved to an individual repository as soon as https://issues.jenkins.io/browse/JENKINS-74913[JENKINS-74913] is implemented._
123123

124124
image::images/screenshot-14.png[]
125125

@@ -131,7 +131,7 @@ image::images/screenshot-18.png[]
131131
IMPORTANT: In order to have the auto-registering process working fine the Jenkins base URL must be
132132
properly configured in _Manage Jenkins_ » _System_
133133

134-
=== Webhooks signature
134+
=== Signature verification for incoming webhooks
135135

136136
Once Jenkins is configured to receive payloads, it will listen for any delivery that's sent to the endpoint you configured. For security reasons, you should only process deliveries from Bitbucket.
137137
To ensure your self-hosted server only processes deliveries from Bitbucket, you need to:

src/main/java/com/cloudbees/jenkins/plugins/bitbucket/endpoints/BitbucketCloudEndpoint.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public BitbucketCloudEndpoint(boolean enableCache, int teamCacheDuration, int re
9595
* credentials to use for auto-management of hooks.
9696
* @param enableHookSignature {@code true} hooks that comes Bitbucket Data
9797
* Center are signed.
98-
* @param credentialsId The {@link StringCredentials#getId()} of the
98+
* @param hookSignatureCredentialsId The {@link StringCredentials#getId()} of the
9999
* credentials to use for verify the signature of payload.
100100
*/
101101
@DataBoundConstructor

src/main/java/com/cloudbees/jenkins/plugins/bitbucket/endpoints/BitbucketServerEndpoint.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public BitbucketServerEndpoint(@CheckForNull String displayName, @NonNull String
124124
* credentials to use for auto-management of hooks.
125125
* @param enableHookSignature {@code true} hooks that comes Bitbucket Data
126126
* Center are signed.
127-
* @param credentialsId The {@link StringCredentials#getId()} of the
127+
* @param hookSignatureCredentialsId The {@link StringCredentials#getId()} of the
128128
* credentials to use for verify the signature of payload.
129129
*/
130130
@DataBoundConstructor

src/main/java/com/cloudbees/jenkins/plugins/bitbucket/hooks/BitbucketSCMSourcePushHookReceiver.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,16 +107,20 @@ public HttpResponse doNotify(StaplerRequest2 req) throws IOException {
107107
return HttpResponses.error(HttpServletResponse.SC_BAD_REQUEST, "X-Event-Key HTTP header invalid: " + eventKey);
108108
}
109109

110-
String bitbucketKey = req.getHeader("X-Bitbucket-Type");
110+
String bitbucketKey = req.getHeader("X-Bitbucket-Type"); // specific header from Plugin implementation
111111
String serverURL = req.getParameter("server_url");
112112

113113
BitbucketType instanceType = null;
114114
if (bitbucketKey != null) {
115115
instanceType = BitbucketType.fromString(bitbucketKey);
116116
}
117-
if (instanceType == null && serverURL != null) {
118-
LOGGER.log(Level.FINE, "server_url request parameter found. Bitbucket Native Server webhook incoming.");
119-
instanceType = BitbucketType.SERVER;
117+
if (serverURL != null) {
118+
if (instanceType == null) {
119+
LOGGER.log(Level.FINE, "server_url request parameter found. Bitbucket Native Server webhook incoming.");
120+
instanceType = BitbucketType.SERVER;
121+
} else {
122+
LOGGER.log(Level.FINE, "X-Bitbucket-Type header / server_url request parameter found. Bitbucket Plugin Server webhook incoming.");
123+
}
120124
} else {
121125
LOGGER.log(Level.FINE, "X-Bitbucket-Type header / server_url request parameter not found. Bitbucket Cloud webhook incoming.");
122126
instanceType = BitbucketType.CLOUD;
@@ -179,7 +183,7 @@ private HttpResponseException checkSignature(@NonNull StaplerRequest2 req, @NonN
179183
String requestId = ObjectUtils.firstNonNull(req.getHeader("X-Request-UUID"), req.getHeader("X-Request-Id"));
180184
String hookSignatureCredentialsId = endpoint.getHookSignatureCredentialsId();
181185
LOGGER.log(Level.WARNING, "No credentials {0} found to verify the signature of incoming webhook {1} request {2}", new Object[] { hookSignatureCredentialsId, hookId, requestId });
182-
return HttpResponses.error(HttpServletResponse.SC_FORBIDDEN, "No credentials " + hookSignatureCredentialsId + " found to verify the signature");
186+
return HttpResponses.error(HttpServletResponse.SC_FORBIDDEN, "No credentials " + hookSignatureCredentialsId + " found in Jenkins to verify the signature");
183187
}
184188
return null;
185189
}

0 commit comments

Comments
 (0)