Aurelien DARRAGON | b015b3e | 2022-12-30 18:59:24 +0100 | [diff] [blame] | 1 | varnishtest "Test RFC 7239 forwarded header support (forwarded option and related converters)" |
| 2 | #REQUIRE_VERSION=2.8 |
| 3 | |
| 4 | # This config tests the HTTP forwarded option and RFC7239 related converters. |
| 5 | |
| 6 | feature ignore_unknown_macro |
| 7 | |
| 8 | #test: converters, parsing and header injection logic |
| 9 | haproxy h1 -conf { |
| 10 | global |
| 11 | # WT: limit false-positives causing "HTTP header incomplete" due to |
| 12 | # idle server connections being randomly used and randomly expiring |
| 13 | # under us. |
| 14 | tune.idle-pool.shared off |
| 15 | |
| 16 | defaults |
| 17 | mode http |
| 18 | timeout connect "${HAPROXY_TEST_TIMEOUT-5s}" |
| 19 | timeout client "${HAPROXY_TEST_TIMEOUT-5s}" |
| 20 | timeout server "${HAPROXY_TEST_TIMEOUT-5s}" |
| 21 | |
| 22 | frontend fe1 |
| 23 | bind "fd@${fe1}" |
| 24 | http-request set-src hdr(x-src) |
| 25 | http-request set-dst hdr(x-dst) |
| 26 | http-request set-header host %[str(vtest)] |
| 27 | use_backend be1 if { path /req1 } |
| 28 | use_backend be2 if { path /req2 } |
| 29 | use_backend be3 if { path /req3 } |
| 30 | use_backend be4 if { path /req4 } |
| 31 | |
| 32 | frontend fe2 |
| 33 | bind "fd@${fe2}" |
| 34 | http-request return status 200 hdr forwarded "%[req.hdr(forwarded)]" |
| 35 | |
| 36 | backend be1 |
| 37 | option forwarded |
| 38 | server s1 ${h1_fe2_addr}:${h1_fe2_port} |
| 39 | |
| 40 | backend be2 |
| 41 | option forwarded for-expr src for_port-expr str(id) by by_port-expr int(10) |
| 42 | server s1 ${h1_fe2_addr}:${h1_fe2_port} |
| 43 | |
| 44 | backend be3 |
| 45 | acl valid req.hdr(forwarded),rfc7239_is_valid |
| 46 | http-request return status 200 if valid |
| 47 | http-request return status 400 |
| 48 | |
| 49 | backend be4 |
| 50 | http-request set-var(req.fnode) req.hdr(forwarded),rfc7239_field(for) |
| 51 | http-request return status 200 hdr nodename "%[var(req.fnode),rfc7239_n2nn]" hdr nodeport "%[var(req.fnode),rfc7239_n2np]" |
| 52 | |
| 53 | } -start |
| 54 | |
| 55 | #test: "default" and "no option forwarded" |
| 56 | haproxy h2 -conf { |
| 57 | global |
| 58 | # WT: limit false-positives causing "HTTP header incomplete" due to |
| 59 | # idle server connections being randomly used and randomly expiring |
| 60 | # under us. |
| 61 | tune.idle-pool.shared off |
| 62 | |
| 63 | defaults |
| 64 | mode http |
| 65 | timeout connect "${HAPROXY_TEST_TIMEOUT-5s}" |
| 66 | timeout client "${HAPROXY_TEST_TIMEOUT-5s}" |
| 67 | timeout server "${HAPROXY_TEST_TIMEOUT-5s}" |
| 68 | option forwarded |
| 69 | |
| 70 | frontend fe1 |
| 71 | bind "fd@${fe1h2}" |
| 72 | use_backend default if { path /default } |
| 73 | use_backend override if { path /override } |
| 74 | use_backend disabled if { path /disabled } |
| 75 | |
| 76 | backend default |
| 77 | server s1 ${h1_fe2_addr}:${h1_fe2_port} |
| 78 | |
| 79 | backend override |
| 80 | option forwarded host-expr str(override) |
| 81 | server s1 ${h1_fe2_addr}:${h1_fe2_port} |
| 82 | |
| 83 | backend disabled |
| 84 | no option forwarded |
| 85 | server s1 ${h1_fe2_addr}:${h1_fe2_port} |
| 86 | |
| 87 | } -start |
| 88 | |
| 89 | client c1 -connect ${h1_fe1_sock} { |
| 90 | txreq -req GET -url /req1 \ |
| 91 | -hdr "x-src: 127.0.0.1" |
| 92 | rxresp |
| 93 | expect resp.status == 200 |
| 94 | expect resp.http.forwarded == "proto=http;for=127.0.0.1" |
| 95 | |
| 96 | txreq -req GET -url /req2 \ |
| 97 | -hdr "x-src: 127.0.0.2" \ |
| 98 | -hdr "x-dst: 127.0.0.3" |
| 99 | rxresp |
| 100 | expect resp.status == 200 |
| 101 | expect resp.http.forwarded == "by=\"127.0.0.3:10\";for=\"127.0.0.2:_id\"" |
| 102 | |
| 103 | txreq -req GET -url /req3 \ |
| 104 | -hdr "forwarded: for=\"unknown:132\";host=\"[::1]:65535\";by=\"_obfs:_port\";proto=https" |
| 105 | rxresp |
| 106 | expect resp.status == 200 |
| 107 | |
| 108 | txreq -req GET -url /req3 \ |
| 109 | -hdr "forwarded: for=\"127.0.0.1\";host=v.test" |
| 110 | rxresp |
| 111 | expect resp.status == 200 |
| 112 | |
| 113 | txreq -req GET -url /req3 \ |
| 114 | -hdr "forwarded: fore=\"unknown:132\"" |
| 115 | rxresp |
| 116 | expect resp.status == 400 |
| 117 | |
| 118 | txreq -req GET -url /req3 \ |
| 119 | -hdr "forwarded: proto=http;proto=http" |
| 120 | rxresp |
| 121 | expect resp.status == 400 |
| 122 | |
| 123 | txreq -req GET -url /req3 \ |
| 124 | -hdr "forwarded: \"" |
| 125 | rxresp |
| 126 | expect resp.status == 400 |
| 127 | |
| 128 | txreq -req GET -url /req3 \ |
| 129 | -hdr "forwarded: by=[::1]" |
| 130 | rxresp |
| 131 | expect resp.status == 400 |
| 132 | |
| 133 | txreq -req GET -url /req3 \ |
| 134 | -hdr "forwarded: by=\"[::1]\"" |
| 135 | rxresp |
| 136 | expect resp.status == 200 |
| 137 | |
| 138 | txreq -req GET -url /req3 \ |
| 139 | -hdr "forwarded: by=\"[::1]:\"" |
| 140 | rxresp |
| 141 | expect resp.status == 400 |
| 142 | |
| 143 | txreq -req GET -url /req3 \ |
| 144 | -hdr "forwarded: by=\"[::1]:3\"" |
| 145 | rxresp |
| 146 | expect resp.status == 200 |
| 147 | |
| 148 | txreq -req GET -url /req4 \ |
| 149 | -hdr "forwarded: proto=http;for=\"[::1]:_id\"" |
| 150 | rxresp |
| 151 | expect resp.status == 200 |
| 152 | expect resp.http.nodename == "::1" |
| 153 | expect resp.http.nodeport == "_id" |
| 154 | } -run |
| 155 | |
| 156 | client c2 -connect ${h2_fe1h2_sock} { |
| 157 | txreq -req GET -url /default |
| 158 | rxresp |
| 159 | expect resp.status == 200 |
| 160 | expect resp.http.forwarded != <undef> |
| 161 | |
| 162 | txreq -req GET -url /override |
| 163 | rxresp |
| 164 | expect resp.status == 200 |
| 165 | expect resp.http.forwarded == "host=\"override\"" |
| 166 | |
| 167 | txreq -req GET -url /disabled |
| 168 | rxresp |
| 169 | expect resp.status == 200 |
| 170 | expect resp.http.forwarded == <undef> |
| 171 | } -run |