Skip to content

Commit 0a98bff

Browse files
Le0Developerpedrosousa
authored andcommitted
[WAF] Add HMAC example based on URI path prefix (#24591)
--------- Co-authored-by: Pedro Sousa <[email protected]>
1 parent dcb9ad6 commit 0a98bff

File tree

1 file changed

+44
-2
lines changed

1 file changed

+44
-2
lines changed

src/content/docs/waf/custom-rules/use-cases/configure-token-authentication.mdx

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ You will need to append this parameter to the URL you are protecting:
166166
The authentication token parameter (`verify=<VALUE>` in the example) must be the last parameter in the query string.
167167
:::
168168

169-
### Testing the generated token parameter
169+
### Test the generated token parameter
170170

171171
If you are on an Enterprise plan, you can test if URLs are being generated correctly on the origin server by doing the following:
172172

@@ -175,7 +175,7 @@ If you are on an Enterprise plan, you can test if URLs are being generated corre
175175

176176
---
177177

178-
## Protecting several paths using the same secret
178+
## Protect several paths using the same secret
179179

180180
You can use the same secret key to protect several URI paths.
181181

@@ -184,3 +184,45 @@ This is illustrated in the previous example, where `http.request.uri` is passed
184184
Since `http.request.uri` includes the path to the asset and that value is extracted for each request, the validation function evaluates all request URIs to `downloads.example.com` using the same secret key.
185185

186186
Note that while you can use the same secret key to authenticate several paths, you must generate an HMAC token for each unique message you want to authenticate.
187+
188+
## Protect an entire URI path prefix with a single signature
189+
190+
You can protect an entire fixed-length URI path prefix with a single HMAC signature (it would also use the same secret). To achieve this, supply a URI path prefix (instead of the full URI path) and the original query string as the [`MessageMAC`](/ruleset-engine/rules-language/functions/#messagemac) argument for the [`is_timed_hmac_valid_v0()`](/ruleset-engine/rules-language/functions/#hmac-validation) function.
191+
192+
Use the [`substring()`](/ruleset-engine/rules-language/functions/#substring) function to obtain the prefix from the full URI path.
193+
194+
In the following example, the URI path prefix requiring a single HMAC signature is always 51 characters long (`x` is a character placeholder):
195+
196+
```txt
197+
/case-studies/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/
198+
```
199+
200+
In this case, you would need to use a different HMAC signature for every different URI path prefix of length 51.
201+
202+
If you wanted to block requests for case study files failing the HMAC validation, you could create a custom rule similar to the following:
203+
204+
<Example>
205+
206+
Rule expression:
207+
208+
```txt
209+
(http.host eq "downloads.example.com" and starts_with(http.request.uri.path, "/case-studies") and not is_timed_hmac_valid_v0("mysecrettoken", concat(substring(http.request.uri.path, 0, 51), "?", http.request.uri.query), 10800, http.request.timestamp.sec, 1))
210+
```
211+
212+
Action:
213+
214+
- Block
215+
216+
</Example>
217+
218+
Example URI paths of valid incoming requests:
219+
220+
```txt
221+
/case-studies/12345678-90ab-4cde-f012-3456789abcde/foobar-report.pdf?1755877101-5WOroVcDINdl2%2BQZxZFHJcJ6l%2Fep4HGIrX3DtSXzWO0%3D
222+
/case-studies/12345678-90ab-4cde-f012-3456789abcde/acme-corp.pdf?1755877101-5WOroVcDINdl2%2BQZxZFHJcJ6l%2Fep4HGIrX3DtSXzWO0%3D
223+
/case-studies/768bf477-22d5-4545-857d-b155510119ff/another-company-report.pdf?1755878057-jeMS5S1F3MIgxvL61UmiX4vODiWtuLfcPV6q%2B0Y3Rig%3D
224+
```
225+
226+
The first two URI paths can use the same HMAC signature because they share the same 51-character prefix (`/case-studies/12345678-90ab-4cde-f012-3456789abcde/`) that is validated by the custom rule.
227+
228+
The third URI path needs a different HMAC signature, since the prefix is different.

0 commit comments

Comments
 (0)