You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
Copy file name to clipboardExpand all lines: examples/official-site/sqlpage/migrations/67_hmac_function.sql
+6-3Lines changed: 6 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -52,7 +52,7 @@ SET actual_signature = sqlpage.header(''X-Webhook-Signature'');
52
52
SELECT
53
53
''redirect'' as component,
54
54
''/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;
56
56
57
57
-- If we reach here, the signature is valid - process the order
58
58
INSERT INTO orders (order_data) VALUES ($body);
@@ -89,7 +89,7 @@ SET expected = sqlpage.hmac(
89
89
''sha256''
90
90
);
91
91
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'');
93
93
94
94
-- serve the file
95
95
```
@@ -98,7 +98,10 @@ WHERE $expected IS DISTINCT FROM $token OR $expires_at < datetime(''now'');
98
98
99
99
- **Keep your secret key safe**: If your secret leaks, anyone can forge signatures and access protected pages
100
100
- **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.
0 commit comments