Skip to main content

HTTP Macros

CEL provides a set of predefined macros in addition to the ext macros that can also be used in policy expressions. For convenience, the following custom macros are also supported:

NameReturn TypeDescription
hasReqHeader(string)boolReturns true or false if the provided header key is present on the request.
getReqHeader(string)listReturns a list of header values for the provided key on the request.
hasQueryParam(string)boolReturns true or false if the specified query parameter key is part of the request URL.
getQueryParam(string)listReturns a list of the query parameter values from the request URL for the specified key.
hasReqCookie(string)boolReturns true or false if a cookie exists on the request with the specified name.
getReqCookie(string)cookieReturns the cookie struct for the specified cookie name, if it exists on the request. If there are multiple cookies of the same name, the first from the ordering specified in the Cookie header will be returned.
hasResHeader(string)boolReturns true or false if the provided header key is present on the response.
getResHeader(string)listReturns a list of header values for the provided key on the response.
hasResCookie(string)boolReturns true or false if a cookie exists on the response with the specified name.
getResCookie(string)cookieReturns the cookie struct for the specified cookie name, if it exists on the response. If there are multiple cookies of the same name, the cookie with the longest path will be returned.
inCidrRange(ip string, cidr string)boolReturns true or false if the provided IP address falls within the provided CIDR range. Returns false if the provided CIDR range is invalid.
inCidrRanges(ip string, cidrs list)boolReturns true or false if the provided IP address falls within any of the provided CIDR ranges. Ignores any provided CIDR ranges that are invalid.
rand.double()doubleReturns a random double between 0 and 1.
rand.int(min int, max int)intReturns a random int between the provided min and max values. Only supports positive integers and min must be larger than the provided max. By default, min is 0 and max is 1.

hasReqHeader(string)

Returns true or false if the provided header key is present on the request. Header keys must be written in canonical format.

# snippet
---
expressions:
- hasReqHeader('X-Version-Id')

getReqHeader(string)

Returns a list of header values for the provided key on the request. Header keys must be written in canonical format.

# snippet
---
expressions:
- getReqHeader('User-Agent').exists(v, v.matches('(?i)google-images'))

hasQueryParam(string)

Returns true or false if the specified query parameter key is part of the request URL.

# snippet
---
expressions:
- hasQueryParam('q')

getQueryParam(string)

Returns a list of the query parameter values from the request URL for the specified key.

# snippet
---
expressions:
- size(getQueryParam('q')) == 0

hasReqCookie(string)

Returns true or false if a cookie exists on the request with the specified name.

# snippet
---
expressions:
- hasReqCookie('session')

getReqCookie(string)

Returns the cookie struct for the specified cookie name, if it exists on the request. If there are multiple cookies of the same name, the first from the ordering specified in the Cookie header will be returned.

# snippet
---
expressions:
- getReqCookie('session').secure

hasResHeader(string)

Returns true or false if the provided header key is present on the response. Header keys must be written in canonical format.

# snippet
---
expressions:
- hasResHeader('Content-Type')

getResHeader(string)

Returns a list of header values for the provided key on the response. Header keys must be written in canonical format.

# snippet
---
expressions:
- size(getResHeader('Content-Type').filter(v, v.matches('application/json')))
> 0

hasResCookie(string)

Returns true or false if a cookie exists on the response with the specified name.

# snippet
---
expressions:
- hasResCookie('_device_id')

getResCookie(string)

Returns the cookie struct for the specified cookie name, if it exists on the response. If there are multiple cookies of the same name, the cookie with the longest path will be returned.

# snippet
---
expressions:
- getResCookie('_device_id').value == 'mobile-phone-14'

inCidrRange(ip string, cidr string)

Returns true or false if the provided IP address falls within the provided CIDR range. Returns false if the provided CIDR range is invalid.

# snippet
---
expressions:
- inCidrRange(conn.client_ip, '66.249.66.1/24')

inCidrRanges(ip string, cidrs list)

Returns true or false if the provided IP address falls within any of the provided CIDR ranges. Ignores any provided CIDR ranges that are invalid.

# snippet
---
expressions:
- inCidrRanges(conn.client_ip, ['66.249.66.1/24', '2001:4860::/32'])

rand.double()double

Returns a random double between 0 and 1.

# snippet
---
expressions:
- rand.double() >= 0.5

rand.int(min int, max int)int

Returns a random int between the provided min and max values. Only supports positive integers and min must be larger than the provided max. By default, min is 0 and max is 1.

The following is an example of using rand.int with the default values:

# snippet
---
expressions:
- rand.int() == 1

The following is an example of using rand.int with custom values:

# snippet
---
expressions:
- rand.int(0, 10) >= 5