Files
sqlpage--sqlpage/tests/webhook_hmac_validation.sql
Ophir LOJKINE 625deb973b Add and document sqlpage.hmac function (#1033)
* feat: Add sqlpage.hmac function for cryptographic signing

Co-authored-by: contact <contact@ophir.dev>

* feat: Add sqlpage.hmac function for secure data signing

Co-authored-by: contact <contact@ophir.dev>

* Test HMAC function with RFC vectors and update tests

Co-authored-by: contact <contact@ophir.dev>

* feat: Add sqlpage.hmac() function for secure signatures

Co-authored-by: contact <contact@ophir.dev>

* feat: Add base64 output option to hmac function

Co-authored-by: contact <contact@ophir.dev>

* Refactor hmac function for cleaner output formatting

Co-authored-by: contact <contact@ophir.dev>

* Add webhook HMAC signature validation tests

Co-authored-by: contact <contact@ophir.dev>

* Refactor HMAC function and update SQL examples for clarity and consistency

- Changed function parameters to remove Option types for data and key in the HMAC function.
- Improved SQL documentation and examples for HMAC usage, including clearer descriptions and updated error handling.
- Enhanced test cases for webhook HMAC validation to ensure accurate signature checks and responses.
- Removed obsolete test file for HMAC with null values.

* Update HMAC validation logic to handle NULL values in SQL queries

- Modified conditions in SQL queries to check for NULL values alongside signature mismatches.
- Enhanced documentation on NULL handling for HMAC checks to improve clarity and portability.

* remove debug logging from ci

---------

Co-authored-by: Cursor Agent <cursoragent@cursor.com>
2025-10-01 21:07:36 +02:00

17 lines
955 B
SQL

-- Webhook HMAC signature validation example
-- This simulates receiving a webhook with HMAC signature in header
-- Redirect to error page if signature is invalid
-- test this with: curl localhost:8080/tests/webhook_hmac_validation.sql -H 'X-Webhook-Signature: 260b3b5ead84843645588af82d5d2c3fe24c598a950d36c45438c3a5f5bb941c' -H 'Content-Type: application/json' --data-raw '{"order_id":12345,"total":"99.99"}' -v
SET body = sqlpage.request_body();
SET secret = sqlpage.environment_variable('WEBHOOK_SECRET');
SET expected_signature = sqlpage.hmac($body, $secret, 'sha256');
SET actual_signature = sqlpage.header('X-Webhook-Signature');
SELECT
'redirect' as component,
'/error.sql?err=bad_webhook_signature' as link
WHERE $actual_signature != $expected_signature OR $actual_signature IS NULL;
-- If we reach here, signature is valid - return success
SELECT 'json' as component, 'jsonlines' as type;
select 'Webhook signature is valid !' as msg;