Functions (2)·续2
Examples:
url_decode("John%20Doe") will return "John Doe"
url_decode("John+Doe") will return "John Doe"
url_decode("%2520") will return "%20"
url_decode("%2520", "r") will return " "
// Using url_decode() with the any() function:
any(url_decode(http.request.body.form.values[*])[*] contains "an xss attack")
// Using the u option to match a specific alphabet
url_decode(http.request.uri.path) matches "(?u)\p{Hangul}+"
uuidv4
uuidv4(source Bytes): String
Generates a random UUIDv4 (Universally Unique Identifier, version 4) based on the given argument (a source of randomness). To obtain an array of random bytes, use the cf.random_seed field.
For example, uuidv4(cf.random_seed) will return a UUIDv4 similar to 49887398-6bcf-485f-8899-f15dbef4d1d5.
Note
You can only use the uuidv4() function in rewrite expressions of Transform Rules.
wildcard_replace
wildcard_replace(source Bytes, wildcard_pattern Bytes, replacement Bytes, flags Bytes optional): String
Replaces a source string, matched by a literal with zero or more * wildcard metacharacters, with a replacement string, returning the result. The replacement string can contain references to wildcard capture groups (for example, ${1} and ${2}), up to eight replacement references.
If there is no match, the function will return source unchanged.
The source parameter must be a field (it cannot be a literal string). Additionally, the entire source value must match the wildcard_pattern parameter (it cannot match only part of the field value).
To enter a literal * character in the wildcard_pattern parameter, you must escape it using \*. Additionally, you must also escape \ using \\. Two unescaped * characters in a row (**) in this parameter are considered invalid and cannot be used. If you need to perform character escaping, it is recommended that you use the raw string syntax for the wildcard_pattern parameter.
To enter a literal $ character in the replacement parameter, you must escape it using $$.
To perform case-sensitive wildcard matching, set the flags parameter to "s".
This function uses lazy matching, that is, it tries to match each * metacharacter with the shortest possible string.
You can only use the wildcard_replace() function once in an expression, and you cannot nest it with the regex_replace() function.
Examples:
- If the full URI is
https://apps.example.com/calendar/admin?expand=true,
wildcard_replace(http.request.full_uri, "https://*.example.com/*/*", "https://example.com/${1}/${2}/${3}")will returnhttps://example.com/apps/calendar/admin?expand=true - If the full URI is
https://example.com/applications/app1,
wildcard_replace(http.request.full_uri, "/applications/*", "/apps/${1}")will returnhttps://example.com/applications/app1(unchanged value, since there is no match for the full URI value; you should use thehttp.request.uri.pathfield for URI path matching). - If the URI path is
/calendar,
wildcard_replace(http.request.uri.path, "/*", "/apps/${1}")will return/apps/calendar. - If the URI path is
/Apps/calendar,
wildcard_replace(http.request.uri.path, "/apps/*", "/${1}")will return/calendar(case-insensitive match by default). - If the URI path is
/Apps/calendar,
wildcard_replace(http.request.uri.path, "/apps/*", "/${1}", "s")will return/Apps/calendar(unchanged value) because there is no case-sensitive match. - If the URI path is
/apps/calendar/login,
wildcard_replace(http.request.uri.path, "/apps/*/login", "/${1}/login")will return/calendar/login.
For more examples of wildcard matching, refer to Wildcard matching.
Note
Currently, you can only use the wildcard_replace() function in rewrite expressions of URL rewrites and target URL expressions of dynamic URL redirects.
Cloudflare Network Firewall Functions
bit_slice
bit_slice(protocol String, offset_start Number, offset_end Number): Number
This function looks for matches on a given slice of bits.
The offset starts on the given protocol header. For example, to match on the first bit of payload for a UDP packet, you must set offset_start to 64.
This is primarily intended for use with ip, udp, and tcp.
The slice (offset_end – offset_start) cannot be longer than 32 bits, but multiple calls can be joined together by using logical expressions.
The bit_slice offset cannot exceed 2,040 bits.
HMAC validation
Note
Access to the HMAC validation function requires a Cloudflare Pro, Business, or Enterprise plan.
Overview
You can validate hash-based message authentication code (HMAC) tokens in a rule expression by using the is_timed_hmac_valid_v0() function, which has this signature:
is_timed_hmac_valid_v0(
<String literal as Key>,
<String field as MessageMAC>,
<Integer literal as ttl>,
<Integer as currentTimeStamp>,
<Optional Integer literal as lengthOfSeparator, default: 0>,
<Optional String literal as flags>
) -> <Bool as result>
The is_timed_hmac_valid_v0() function has these parameter definitions:
KeyString literal- Specifies the secret cryptographic key for validating the HMAC.
MessageMACString- Contains a concatenation of these HMAC elements:
message,separator,timestamp,mac. For a definition and an example, refer to MessageMAC. ttlInteger literal- Defines the time-to-live for the HMAC token, expressed in seconds. Determines how long the token is valid, relative to the time it was issued.
currentTimeStampInteger- Represents the UNIX timestamp when Cloudflare received the request, expressed in seconds. Pass the
http.request.timestamp.secfield as an approximate value to this argument. lengthOfSeparatorInteger literaloptional- Specifies the length of the
separatorbetween thetimestampand themessagein theMessageMAC. Expressed in bytes, with a default value of0. flagsString literaloptional- When you set this optional argument to
's', the function expects the value of the Base64-encodedmacin theMessageMACargument to use the URL-safe character set with no padding. - When you do not set the value of
flagsto's', you must URL encode the Base64 value formacin theMessageMACargument.
Usage
The is_timed_hmac_valid_v0() function uses the supplied Key to generate a message authentication code (MAC) from the message and the timestamp regions of the MessageMAC. When the generated MAC matches the mac region of the MessageMAC and the token has not expired, the HMAC is valid and the function returns true.
For example, the following expression matches requests to downloads.example.com that do not include valid HMAC tokens:
http.host == "downloads.example.com"
and not is_timed_hmac_valid_v0("mysecretkey", http.request.uri, 100000, http.request.timestamp.sec, 8)
For examples of rules that use HMAC validation, refer to Configure token authentication in the WAF documentation.
MessageMAC
A valid MessageMAC satisfies the following regular expression:
(.+)(.*)(\d{10})-(.{43,})
and is composed of these parentheses-delimited expressions:
| Expression | Description | Example |
|---|---|---|
(.+) |
The message to validate. |
/download/cat.jpg |
(.*) |
The separator between message and timestamp, commonly a parameter name. |
&verify= |
(\d{10}) |
The 10-digit UNIX timestamp when the MAC was issued, expressed in seconds. |
1484063137 |
(.{43,}) |
A Base64-encoded version of the mac. When you do not set the value of the urlSafe argument in the HMAC validation function to 's', you must URL-encode the Base64 value for mac. When the Base64 MAC encoding is URL-safe, the mac value contains 43 bytes. Otherwise, the value will be 44 bytes or more, because of URL encoding. |
IaLGSmELTvlhfd0ItdN6PhhHTFhzx73EX8uy%2FcSDiIU%3D |
For details on generating a MessageMAC, refer to HMAC token generation.
HMAC validation examples
Note
When you do not use the optional flags argument for is_timed_hmac_valid_v0(), you must URL-encode the Base64 value for mac in the MessageMAC argument.
For more information, refer to HMAC Validation: Overview.
MessageMAC in a single field
Consider the case where the MessageMAC is contained entirely within a single field, as in this example URI path:
/download/cat.jpg?verify=1484063787-IaLGSmELTvlhfd0ItdN6PhhHTFhzx73EX8uy%2FcSDiIU%3D
Note how the URI maps to the elements of the MessageMAC:
| Element | Value |
|---|---|
message |
/download/cat.jpg |
separator |
?verify= (with length 8) |
timestamp |
1484063787 |
mac |
IaLGSmELTvlhfd0ItdN6PhhHTFhzx73EX8uy%2FcSDiIU%3D |
When the MessageMAC is contained entirely within a single field such as http.request.uri, pass the field name to the MessageMAC argument of the HMAC validation function:
is_timed_hmac_valid_v0(
"mysecretkey",
http.request.uri,
100000,
http.request.timestamp.sec,
8
)
Concatenated MessageMAC argument
To compose a MessageMAC from more than one field, use the concat() function.
This example constructs the value of the MessageMAC argument by concatenating the request URI and two header fields:
is_timed_hmac_valid_v0(
"mysecretkey",
concat(
http.request.uri,
http.request.headers["timestamp"][0],
"-",
http.request.headers["mac"][0]),
100000,
http.request.timestamp.sec,
0
)
Was this helpful?
YesNo
On this page
{"@context":"https://schema.org","@type":"TechArticle","@id":"https://developers.cloudflare.com/ruleset-engine/rules-language/functions/#page","headline":"Functions","description":"Functions available for transforming values in rule expressions.","url":"https://developers.cloudflare.com/ruleset-engine/rules-language/functions/","inLanguage":"en","image":"https://developers.cloudflare.com/ruleset-engine/rules-language/functions/og.png?v=5260fd9806ba8159","dateModified":"2026-10-14","publisher":{"@type":"Organization","name":"Cloudflare","description":"One platform for your apps, agents, and workforce. Build, secure, and scale without managing infrastructure","url":"https://www.cloudflare.com/","sameAs":["https://github.com/cloudflare","https://www.linkedin.com/company/cloudflare","https://x.com/cloudflare"],"logo":{"@type":"ImageObject","url":"https://developers.cloudflare.com/logo.svg"},"address":{"@type":"PostalAddress","streetAddress":"101 Townsend St","addressLocality":"San Francisco","addressRegion":"CA","postalCode":"94107","addressCountry":"US"},"contactPoint":[{"@type":"ContactPoint","contactType":"Customer Support","url":"https://support.cloudflare.com/","availableLanguage":["English"]},{"@type":"ContactPoint","contactType":"Sales","url":"https://www.cloudflare.com/contact/","availableLanguage":["English"]}]},"isPartOf":{"@type":"WebSite","@id":"https://developers.cloudflare.com/#website","name":"Cloudflare Docs","url":"https://developers.cloudflare.com/"}}
description: Learn about comparison, logical operators, and grouping symbols in Cloudflare's Rules language. Understand precedence and how to structure expressions. title: Operators and grouping symbols image: https://developers.cloudflare.com/ruleset-engine/rules-language/operators/og.png?v=6d9b481e4dc42e76
Documentation Index
Fetch the complete documentation index at: https://developers.cloudflare.com/ruleset-engine/llms.txt
Use this file to discover all available pages before exploring further.
Docs