Skip to content

Commit 2e1fa30

Browse files
committed
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.
1 parent 317784d commit 2e1fa30

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

examples/official-site/sqlpage/migrations/67_hmac_function.sql

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ SET actual_signature = sqlpage.header(''X-Webhook-Signature'');
5252
SELECT
5353
''redirect'' as component,
5454
''/error.sql?err=bad_webhook_signature'' as link
55-
WHERE $actual_signature IS DISTINCT FROM $expected_signature;
55+
WHERE $actual_signature != $expected_signature OR $actual_signature IS NULL;
5656
5757
-- If we reach here, the signature is valid - process the order
5858
INSERT INTO orders (order_data) VALUES ($body);
@@ -89,7 +89,7 @@ SET expected = sqlpage.hmac(
8989
''sha256''
9090
);
9191
SELECT ''redirect'' AS component, ''/error.sql?err=expired'' AS link
92-
WHERE $expected IS DISTINCT FROM $token OR $expires_at < datetime(''now'');
92+
WHERE $expected != $token OR $token IS NULL OR $expires_at < datetime(''now'');
9393
9494
-- serve the file
9595
```
@@ -98,7 +98,10 @@ WHERE $expected IS DISTINCT FROM $token OR $expires_at < datetime(''now'');
9898
9999
- **Keep your secret key safe**: If your secret leaks, anyone can forge signatures and access protected pages
100100
- **The signature is case-sensitive**: Even a single wrong letter means the signature won''t match
101-
- **NULL handling**: Always use `IS DISTINCT FROM`, not `=` to check for hmac matches. In SQL `SELECT ''redirect'' as component WHERE sqlpage.hmac(...) != $signature` will not redirect if `$signature` is NULL (the signature is absent). Use `SELECT ''redirect'' as component WHERE sqlpage.hmac(...) IS DISTINCT FROM $signature` instead.
101+
- **NULL handling**: Always use `IS DISTINCT FROM`, not `=` to check for hmac matches.
102+
- `SELECT ''redirect'' as component WHERE sqlpage.hmac(...) != $signature` will not redirect if `$signature` is NULL (the signature is absent).
103+
- `SELECT ''redirect'' as component WHERE sqlpage.hmac(...) IS DISTINCT FROM $signature` checks for both NULL and non-NULL values (but is not available in all SQL dialects).
104+
- `SELECT ''redirect'' as component WHERE sqlpage.hmac(...) != $signature OR $signature IS NULL` is the most portable solution.
102105
'
103106
);
104107

tests/webhook_hmac_validation.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ SET actual_signature = sqlpage.header('X-Webhook-Signature');
1010
SELECT
1111
'redirect' as component,
1212
'/error.sql?err=bad_webhook_signature' as link
13-
WHERE $actual_signature IS DISTINCT FROM $expected_signature;
13+
WHERE $actual_signature != $expected_signature OR $actual_signature IS NULL;
1414

1515
-- If we reach here, signature is valid - return success
1616
SELECT 'json' as component, 'jsonlines' as type;

0 commit comments

Comments
 (0)