Securing Mechanic webhooks
​Mechanic webhooks are configured properly for CORS, which makes them suitable for submissions from your online store's frontend.
These webhooks do not include any authentication, so you may want to add some verification in your own implementation.

Generating signatures

By establishing a shared secret value, stored in server-side Shopify theme code and in server-side Mechanic task code, the webhook request data can be "signed" with a signature value, uniquely determined by the shared secret combined with the request data. This prevents would-be attackers from modifying the webhook data and re-using the same signature, because any modification to the webhook data would invalidate the signature.
One might use something like this in the Shopify theme code:
1
<script>
2
$.post(
3
'https://usemechanic.com/webhook/000000000',
4
{
5
customer_id: {{ customer.id | json }},
6
customer_id_signature: {{ customer.id | hmac_sha256: "some-secret-value" | json }}
7
}
8
);
9
</script>
Copied!
... and then something like this, in the corresponding Mechanic task code:
1
{% assign customer_id = event.data.customer_id %}
2
{% assign customer_id_signature = event.data.customer_id_signature %}
3
​
4
{% assign expected_customer_id_signature = customer_id | hmac_sha256: "some-secret-value" %}
5
​
6
{% if expected_customer_id_signature != customer_id_signature %}
7
{% error "Customer ID signature did not match." %}
8
{% endif %}
Copied!

Preventing replay attacks

The approach above prevents would-be attackers from passing off altered data as legitimate, but it does not prevent manually repeated submissions of the same data.

Using idempotent task code

The best approach for preventing this is to ensure that the Mechanic task code is fully idempotent, such that it only performs the desired work once, no matter how many times it is invoked.
One way to solve this is to leverage the Mechanic cache to remember that a given request has already been processed. Here's an example:
1
{% assign cache_key = event.data | json | sha256 %}
2
​
3
{% if cache[cache_key] %}
4
{% log "we've already seen this request; skipping it" %}
5
{% break %}
6
{% endif %}
7
​
8
{% action "cache", "set", cache_key, true %}
9
​
10
[... proceed with processing the event]
Copied!
In this way, later submissions of the same webhook data will be ignored.

Adding time to the request signature

To prevent a replay of the same request much later, add a rounded representation of the current time to the signature calculation. Here's an example, on the theme code side:
1
{% assign time_rounded = "now" | date: "%s" | times: 1.0 | divided_by: 60 | round %}
2
{% assign signature_data = customer.id | append: time_rounded %}
3
{% assign signature = hmac_sha256: "some-secret-value" %}
Copied!
And here's how this might be validated in task code:
1
{% assign customer_id = event.data.customer_id %}
2
{% assign customer_id_signature = event.data.customer_id_signature %}
3
​
4
{% assign time_rounded = "now" | date: "%s" | times: 1.0 | divided_by: 60 | round %}
5
{% assign expected_customer_id_signature = customer_id | append: time_rounded | hmac_sha256: "some-secret-value" %}
6
​
7
{% if expected_customer_id_signature != customer_id_signature %}
8
{% error "Customer ID signature did not match." %}
9
{% endif %}
Copied!
In this way, the signature will only be valid within a 60-minute interval.
Bear in mind, with this strategy, timeliness becomes important! A queue delay could push the event past the rounding, resulting in a new time value, and invalidating the request signature.
Last modified 2mo ago
Copy link
Edit on GitHub