Skip to content

Commit 653f8a5

Browse files
committed
[GHWebhookSignature] Process escaped Unicode characters in a JSON payload
[GHWebhookSignatureTest] Add test to check signature generation for unicode payloads [GHWebhookSignatureTest] Fix test data We should've pass all those \uXXXX to function, but Java was keeping them as unicode characters inside [GHWebhookSignature] Use modules available via pom.xml to perform unescape [GHWebhookSignatureTest] Explain test data choice [GHWebhookSignatureTest] Remove escaped unicode from comments
1 parent bb56162 commit 653f8a5

2 files changed

Lines changed: 20 additions & 4 deletions

File tree

src/main/java/org/jenkinsci/plugins/github/webhook/GHWebhookSignature.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import hudson.util.Secret;
44
import org.apache.commons.codec.binary.Hex;
5+
import org.apache.commons.lang3.StringEscapeUtils;
56
import org.apache.commons.lang3.StringUtils;
67
import org.slf4j.Logger;
78
import org.slf4j.LoggerFactory;
@@ -54,7 +55,10 @@ public String sha1() {
5455
final SecretKeySpec keySpec = new SecretKeySpec(secret.getPlainText().getBytes(UTF_8), HMAC_SHA1_ALGORITHM);
5556
final Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
5657
mac.init(keySpec);
57-
final byte[] rawHMACBytes = mac.doFinal(payload.getBytes(UTF_8));
58+
59+
final String unescapedPayload = StringEscapeUtils.unescapeJava(payload);
60+
final String convertedUnicode = new String(unescapedPayload.getBytes("latin1"), UTF_8);
61+
final byte[] rawHMACBytes = mac.doFinal(convertedUnicode.getBytes(UTF_8));
5862

5963
return Hex.encodeHexString(rawHMACBytes);
6064
} catch (Exception e) {

src/test/java/org/jenkinsci/plugins/github/webhook/GHWebhookSignatureTest.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,34 @@ public class GHWebhookSignatureTest {
2020
private static final String PAYLOAD = "foo";
2121
private static final String SECRET = "bar";
2222

23+
// Taken from real example of Pull Request update webhook payload
24+
private static final String UNICODE_PAYLOAD = "{\"description\":\"foo\\u00e2\\u0084\\u00a2\"}";
25+
private static final String UNICODE_SIGNATURE = "10e3cb05d27049775aeca89d84d9e6123d5ab006";
26+
2327
@ClassRule
2428
public static JenkinsRule jRule = new JenkinsRule();
2529

2630
@Test
2731
public void shouldComputeSHA1Signature() throws Exception {
2832
assertThat("signature is valid", webhookSignature(
29-
PAYLOAD,
33+
PAYLOAD,
3034
Secret.fromString(SECRET)
3135
).sha1(), equalTo(SIGNATURE));
3236
}
3337

3438
@Test
3539
public void shouldMatchSignature() throws Exception {
3640
assertThat("signature should match", webhookSignature(
37-
PAYLOAD,
41+
PAYLOAD,
3842
Secret.fromString(SECRET)
3943
).matches(SIGNATURE), equalTo(true));
4044
}
41-
}
45+
46+
@Test
47+
public void shouldComputeSHA1SignatureWithUnicodePayload() throws Exception {
48+
assertThat("signature is valid for unicode payload", webhookSignature(
49+
UNICODE_PAYLOAD,
50+
Secret.fromString(SECRET)
51+
).sha1(), equalTo(UNICODE_SIGNATURE));
52+
}
53+
}

0 commit comments

Comments
 (0)