1
votes

I am trying to use std.ip which is a part of varnish 4.0 to to return the client IP which should be the first valid IP address in the X-Forwarded-For header, if the example in the documentation is correct.

varnishtest "Test v4 vcl X-Forwarded-For Header logic"

server s1 {
   rxreq
   expect req.http.X-Real-IP == "2.1.1.1"
   expect req.http.X-Forwarded-For == "2.1.1.1, 3.3.3.3, 3.3.3.3, 127.0.0.1"
   txresp
} -start

varnish v1 -vcl+backend {
   sub vcl_recv {
      set req.http.X-Real-IP = std.ip(req.http.X-Forwarded-For, "0.0.0.0");
   }
} -start

client c1 {
   txreq -url "/" -hdr "X-Forwarded-For: 2.1.1.1, 3.3.3.3, 3.3.3.3"
   rxresp
}

client c1 -run

The above dies an ugly death:

...
***  v1    0.9 debug| Assert error in vwk_thread(), waiter/cache_waiter_kqueue.c line 115:\n
***  v1    0.9 debug|   Condition(read(vwk->pipe[0], &c, 1) == 1) not true.\n
...

And does not behave as I would want by returning the first IP address.

Updated

Alternatively I have found that the following does work for the same purposes, But still could not get std.ip to work:

varnish v1 -vcl+backend {
   sub vcl_recv {
      set req.http.X-Real-IP = regsub(req.http.X-Forwarded-For, "\s*,.*$", "");
   }
} -start

As a follow up question. This vcl_recv{...} logic actually lives in my default.vcl file, where all my backends, probes are defined. But when I try to test that code by including into a varnishtest file as follows:

varnish v1 -vcl+backend {
        include "/path/to/file.vcl";
} -start

The test does not get the expect statements in the server s1. If someone could give some clarity on the following I'd be much ablighed:

  1. Why does std.ip(req.http.X-Forwarded-For, "0.0.0.0") not behave as expected?
  2. How can I test an included default.vcl with expect statements in the set server s1?

Thank you.

1

1 Answers

1
votes

You need to add the line

import std;

at the top of your vcl file