Skip to content

Commit

Permalink
Revert "Merge pull request jenkinsci#242 from ababushk/JENKINS_50154_…
Browse files Browse the repository at this point in the history
…unicode_payload"

This reverts commit c1aa272, reversing
changes made to e394f77.
  • Loading branch information
ababushk committed Feb 17, 2021
1 parent c35ebd6 commit 10418c4
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 27 deletions.
5 changes: 0 additions & 5 deletions pom.xml
Expand Up @@ -84,11 +84,6 @@
<artifactId>commons-lang3</artifactId>
<version>3.11</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-text</artifactId>
<version>1.9</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>github-api</artifactId>
Expand Down
Expand Up @@ -2,7 +2,6 @@

import hudson.util.Secret;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.text.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -55,10 +54,7 @@ public String sha1() {
final SecretKeySpec keySpec = new SecretKeySpec(secret.getPlainText().getBytes(UTF_8), HMAC_SHA1_ALGORITHM);
final Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
mac.init(keySpec);

final String unescapedPayload = StringEscapeUtils.unescapeJava(payload);
final String convertedUnicode = new String(unescapedPayload.getBytes("latin1"), UTF_8);
final byte[] rawHMACBytes = mac.doFinal(convertedUnicode.getBytes(UTF_8));
final byte[] rawHMACBytes = mac.doFinal(payload.getBytes(UTF_8));

return Hex.encodeHexString(rawHMACBytes);
} catch (Exception e) {
Expand Down
@@ -1,4 +1,4 @@
package org.jenkinsci.plugins.github.webhook;
package org.jenkinsci.plugins.github.extension;

import hudson.util.Secret;
import org.junit.ClassRule;
Expand All @@ -14,40 +14,28 @@
*
* @author martinmine
*/
public class GHWebhookSignatureTest {
public class CryptoUtilTest {

private static final String SIGNATURE = "85d155c55ed286a300bd1cf124de08d87e914f3a";
private static final String PAYLOAD = "foo";
private static final String SECRET = "bar";

// Taken from real example of Pull Request update webhook payload
private static final String UNICODE_PAYLOAD = "{\"description\":\"foo\\u00e2\\u0084\\u00a2\"}";
private static final String UNICODE_SIGNATURE = "10e3cb05d27049775aeca89d84d9e6123d5ab006";

@ClassRule
public static JenkinsRule jRule = new JenkinsRule();

@Test
public void shouldComputeSHA1Signature() throws Exception {
assertThat("signature is valid", webhookSignature(
PAYLOAD,
PAYLOAD,
Secret.fromString(SECRET)
).sha1(), equalTo(SIGNATURE));
}

@Test
public void shouldMatchSignature() throws Exception {
assertThat("signature should match", webhookSignature(
PAYLOAD,
PAYLOAD,
Secret.fromString(SECRET)
).matches(SIGNATURE), equalTo(true));
}

@Test
public void shouldComputeSHA1SignatureWithUnicodePayload() throws Exception {
assertThat("signature is valid for unicode payload", webhookSignature(
UNICODE_PAYLOAD,
Secret.fromString(SECRET)
).sha1(), equalTo(UNICODE_SIGNATURE));
}
}
}

0 comments on commit 10418c4

Please sign in to comment.