X-Hub-Signature Verification on Google Cloud Functions / Firebase Cloud Functions for Facebook Graph API Webhooks

Raj Chaudhary
Jan 6, 2018 · 1 min read

Google Cloud Functions (GCF) / Firebase Cloud Functions automatically parses the request body for all requests where content type is application/json. Converting the parsed request body to a string via JSON.stringify() always fails the X-Hub-Signature verification for Facebook Graph API webhooks. And as of today, there is no way to plug in any middleware into the Express pipeline to get access to the raw request body. Fortunately, the folks at Google added a new rawBody property to the request object to fix this problem. Below code has been tested and works with Facebook Graph API webhooks. All you have to do is pass req.rawBody to the update method on the Hmac object instead of JSON.stringify(req.body).

Resources:

  1. https://stackoverflow.com/questions/42950561/how-can-i-get-the-raw-request-body-in-a-google-cloud-function
  2. https://issuetracker.google.com/issues/36252545
  3. https://cloud.google.com/functions/docs/writing/http#handling_http_requests_file_uploads
  4. https://cloud.google.com/community/tutorials/github-auto-assign-reviewers-cloud-functions#validating-the-request-from-github

Happy Coding!

Google Cloud Platform - Community

A collection of technical articles published or curated by Google Cloud Platform Developer Advocates. The views expressed are those of the authors and don't necessarily reflect those of Google.

Raj Chaudhary

Written by

chasing excellence…

Google Cloud Platform - Community

A collection of technical articles published or curated by Google Cloud Platform Developer Advocates. The views expressed are those of the authors and don't necessarily reflect those of Google.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade