Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Signed Webhook event verification fails when payload is a JSON String and api request body is map of string #722

Open
sauravkumar-dev opened this issue Mar 9, 2022 · 5 comments
Labels
status: waiting for feedback waiting for feedback from the submitter type: getting started question while getting started

Comments

@sauravkumar-dev
Copy link

sauravkumar-dev commented Mar 9, 2022

Issue Summary
We are trying to validate signed webhook events in java with POST API.
We are sending mail and configured https://webhook.site/73452d78-3dea-478b-a005-d87371972190 as public webhoo url.
We are getting payload, timestamp and signature.
but while verifying above on public key its failing.

Steps to Reproduce
Enable Signed Event Webhook Requests on SendGrid website
Copy the public from SendGrid and paste it in the java method
Configure Mail Settings to enable webhooks and configure to send the event to our endpoint
Send an email using SendGrid
The webhook event is received and we try to validate the request against the public key
We retrieve the info in headers X-Twilio-Email-Event-Webhook-Signature and X-Twilio-Email-Event-Webhook-Timestamp
The validation always fails.

Pls find the code which i am using.

public Boolean isValidSignature(List<Map<String,Object>> emailActivities, Map<String, String> headers) throws JsonProcessingException {
String sandGridPublicKey=;
boolean isValid = false;
Security.addProvider(new BouncyCastleProvider());
String payload= new ObjectMapper().writeValueAsString(emailActivities) + "\r\n";
try {

	        String signature = headers.get(EventWebhookHeader.SIGNATURE.toString().toLowerCase());
	        String timeStamp = headers.get(EventWebhookHeader.TIMESTAMP.toString().toLowerCase());
	        EventWebhook ew = new EventWebhook();
	        ECPublicKey ellipticCurvePublicKey = ew.ConvertPublicKeyToECDSA(sandGridPublicKey);
	        isValid = ew.VerifySignature(ellipticCurvePublicKey, payload, signature, timeStamp);
	        System.out.println(isValid);
	         
	    } catch (Exception e) {
	        e.printStackTrace();
	    }

	    return isValid;
	}

Payload as below:

[
{
"email": "[email protected]",
"event": "processed",
"send_at": 0,
"sg_event_id": "cHJvY2Vzc2VkLTIyODEyNzM5LTllOEljeGljUUt5cl8yWDNwZ0hnRlEtMA",
"sg_message_id": "9e8IcxicQKyr_2X3pgHgFQ.filterdrecv-75ff7b5ffb-96rhp-1-6227379B-A1.0",
"smtp-id": "9e8IcxicQKyr_2X3pgHgFQ@geopod-ismtpd-4-0",
"timestamp": 1646737307
}]
@childish-sambino
Copy link
Contributor

Looks like it's not the raw payload which is needed to properly validate the event. Example here:

final byte[] requestBody = req.bodyAsBytes();

@childish-sambino childish-sambino added status: waiting for feedback waiting for feedback from the submitter type: getting started question while getting started labels Mar 11, 2022
@Kaushik612
Copy link

Hi, I am also facing the same issue, anyone knows how to get the right payload if we are using Spring boot? The example provided is using Spark

@Kaushik612
Copy link

@sauravkumar-dev Were you able to figure out a solution to this issue?

@sauravkumar-dev
Copy link
Author

sauravkumar-dev commented Apr 5, 2022

@Kaushik612 Yes, I was figure out the issues and its working for me now.
Below is the solution
String payload= new ObjectMapper().writeValueAsString(payloadFromSendgrid)+"\n\r";
payload=payload.replace("},{" , "},\n\r{");

@childish-sambino
Copy link
Contributor

@Kaushik612 Was the above comment able to solve your issue? If not, are you able to get the raw payload from the Spring Boot implementation?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: waiting for feedback waiting for feedback from the submitter type: getting started question while getting started
Projects
None yet
Development

No branches or pull requests

3 participants