Question

Can I decrypt incoming data from webhook?

  • 20 October 2022
  • 1 reply
  • 274 views

I need to decrypt incoming webhook from a form website using express.js. I can get it to work locally. Is this possible by using Zapier? See the following snippet of code.
 

const express = require('express')

const app = express()

// Instantiating formsg-sdk without parameters default to using the package's production public signing key.

const formsg = require('@opengovsg/formsg-sdk')()

const POST_URI = 'zapier_sample'

const formSecretKey = process.env.FORM_SECRET_KEY

app.post(

    '/submissions',

    // Endpoint authentication by verifying signatures

    function (req, res, next) {

        try {

            formsg.webhooks.authenticate(req.get('X-Formsg-Signature'), POST_URI)

            // Continue processing the POST body

            return next()

        } catch (e) {

            return next()

            // return res.status(401).send({ message: 'Unauthorized' })

        }

    },

    // Parse JSON from raw request body

    express.json(),

    // Decrypt the submission

    async function (req, res, next) {

        const submission = HAS_ATTACHMENTS

            ? await formsg.crypto.decryptWithAttachments(formSecretKey, req.body.data)

            : formsg.crypto.decrypt(formSecretKey, req.body.data)

 

        if (submission) {

            console.log('This is submission' + JSON.stringify(submission))

            return res.status(200).send({ message: 'See console for submission' })

            // Continue processing the submission

        } else {

            return res.status(200).send({ message: 'Could not decrypt the submission' })

        }

    }

)

 


This post has been closed for comments. Please create a new post if you need help or have a question about this topic.

1 reply

Userlevel 7
Badge +14

Hi @Nik 

Good question.

https://help.zapier.com/hc/en-us/articles/8496310939021#utilities-in-code-steps-0-9

Limitations with Code steps

  • The environment in which your Code steps run (AWS Lambda) has an I/O limit of 6 MB. The total size of the code and the data processed by the Code step cannot exceed that. If you're hitting this error, try to limit the amount of data returned from your function. For instance, don't return an entire JSON structure, just the keys you need.
  • Free users can run scripts of up to 1 second and 128 MB of RAM. Paid users can run scripts of up to 10 seconds and 256 MB of RAM. Your Zap will hit an error if it exceeds these limits.
  • You cannot require external libraries, or install or import libraries commonly referred to as "npm modules". Only the standard node.js library and the fetch package are available in the Code by Zapier app. fetch is already included in the namespace.