Skip to content

Commit bdcd847

Browse files
committed
Fix unhandled NumberFormatException in webhook timestamp parsing
When parsing the Stripe-Signature header, getTimestamp() calls Long.parseLong() on the timestamp value without catching NumberFormatException. If a malformed header contains a non-numeric timestamp value (e.g., 't=not_a_number'), the exception propagates up uncaught, rather than being wrapped in a SignatureVerificationException as documented. This commit catches NumberFormatException and returns -1, which causes verifyHeader() to throw the expected SignatureVerificationException with the message 'Unable to extract timestamp and signatures from header'. Also adds a test case for malformed timestamp values.
1 parent 1afcc9c commit bdcd847

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

src/main/java/com/stripe/net/Webhook.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,11 @@ private static long getTimestamp(String sigHeader) {
176176
for (String item : items) {
177177
String[] itemParts = item.split("=", 2);
178178
if (itemParts[0].equals("t")) {
179-
return Long.parseLong(itemParts[1]);
179+
try {
180+
return Long.parseLong(itemParts[1]);
181+
} catch (NumberFormatException e) {
182+
return -1;
183+
}
180184
}
181185
}
182186

src/test/java/com/stripe/net/WebhookTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,21 @@ public void testMalformedHeader() throws SignatureVerificationException {
160160
assertEquals("Unable to extract timestamp and signatures from header", exception.getMessage());
161161
}
162162

163+
@Test
164+
public void testMalformedTimestampValue() throws SignatureVerificationException {
165+
// Test with non-numeric timestamp value - should throw SignatureVerificationException,
166+
// not NumberFormatException
167+
final String sigHeader = "t=not_a_number,v1=somesignature";
168+
169+
Throwable exception =
170+
assertThrows(
171+
SignatureVerificationException.class,
172+
() -> {
173+
Webhook.Signature.verifyHeader(payload, sigHeader, secret, 0, null);
174+
});
175+
assertEquals("Unable to extract timestamp and signatures from header", exception.getMessage());
176+
}
177+
163178
@Test
164179
public void testNoSignaturesWithExpectedScheme()
165180
throws SignatureVerificationException, NoSuchAlgorithmException, InvalidKeyException {

0 commit comments

Comments
 (0)