Cryptographic VCL features

Fastly provides several functions in VCL for cryptographic- and hashing-related purposes. It is based very heavily on Kristian Lyngstøl's digest vmod for Varnish 3 (which means you can also refer to that documentation for more detail).



In base64 decoding, the output theoretically could be in binary but is interpreted as a string. So if the binary output contains '\0' then it could be truncated.

The time based One-Time Password algorithm initializes the HMAC using the key and appropriate hash type. Then it hashes the message

(<time now in seconds since UNIX epoch> / <interval>) + <offset>

as a 64bit unsigned integer (little endian) and base64 encodes the result.


One-Time Password Validation (Token Authentication)

Use this to validate tokens with a URL format like the following:


Example implementations for token generation in various languages can be found in GitHub.

Example VCL

sub vcl_recv {

  /* make sure there is a token */
  if (req.url !~ "[?&]token=([^&]+)") {
    error 403;

  if (re.group.1 != digest.time_hmac_sha256("RmFzdGx5IFRva2VuIFRlc3Q=", 60, 0) &&
      re.group.1 != digest.time_hmac_sha256("RmFzdGx5IFRva2VuIFRlc3Q=", 60, -1)) {
    error 403;

#FASTLY recv



set resp.http.x-data-sig = digest.hmac_sha256("secretkey",resp.http.x-data);

Base64 decoding

A snippet like this in vcl_error would set the response body to the value of the request header field named x-parrot after base64-decoding the value:

synthetic digest.base64_decode(req.http.x-parrot);

However, if the base64-decoded string contains a NUL byte (0x00), then that byte and any bytes following it will not be included in the response. Keep that in mind if you intend to send a synthetic response that contains binary data. There is currently no way to send a synthetic response containing a NUL byte.