This fixes issue #3489: when forwarding a received TLV with
set-proxy-v2-tlv-fmt(<id>) "%[fc_pp_tlv(<id>)]", the value is cut at
the first NULL byte. fc_pp_tlv returns a string, and _lf_text_len()
copies strings with strlcpy2(). The "bin" option did not help since it
only applied to binary samples.
With the patch, when "bin" is set, strings are copied according to thier
length, like binary samples. Without "bin", nothing changes. CBOR
encoding is untouched.
Note: lf_text()/lf_rawtext() used to pass the remaining buffer size as
the length of NUL-terminated fields (relying on strlcpy2() to stop).
With "bin" they now pass strnlen(src, size), otherwise "%{+bin}o%f"
would read past the end of the string (caught with ASAN on an earlier
version of this patch).
Tested on master
c2852d5b7: new reg-test plus reg-tests/log pass, full
reg-tests 299 pass / 0 fail (no SSL/Lua build). An earlier version was
also tested with ASAN + SSL/Lua (394 pass / 0 fail).
One open question: since TLV values are binary by nature, should
set-proxy-v2-tlv-fmt enable "bin" by default? I did not do it because
it changes the behavior of existing configurations, but I can send a
follow-up patch if you think it makes sense.
It may be backported to 3.0, where "bin" was introduced.
Signed-off-by: Hyeonggeun Oh <hyeonggeun.oh@plaintexting.com>
In this case, we fetch the TLV with the type 0x20 as a string and set as the value
of a newly created TLV that also has the type 0x20.
+ Note that <fmt> is a log-format string: by default, a value is emitted as text
+ and stops at the first NULL byte. To forward a binary TLV value which may
+ contain NULL bytes, enable the "bin" option globally for the format:
+
+ server srv1 192.168.1.1:80 send-proxy-v2 \
+ set-proxy-v2-tlv-fmt(0xE1) "%{+bin}o%[fc_pp_tlv(0xE1)]"
+
proxy-v2-options <option>[,<option>]*
May be used in the following contexts: tcp, http
(intended purpose is for the RFC5424 structured-data log formats)
* bin: try to preserve binary data, this can be useful with sample
expressions that output binary data in order to preserve the original
- data. Be careful however, because it can obviously generate non-
- printable chars, including NULL-byte, which most syslog endpoints
+ data. Except with "cbor", strings are then emitted with their full
+ length, including the NULL bytes they may contain (e.g.
+ "fc_pp_tlv"). Be careful however, because it can obviously generate
+ non-printable chars, including NULL-byte, which most syslog endpoints
don't expect. Thus it is mainly intended for use with set-var-fmt,
rings and binary-capable log endpoints.
This option can only be set globally (with %o), it will be ignored
--- /dev/null
+varnishtest "Check that binary TLV values are forwarded unmodified with +bin"
+
+# A TLV value is length-delimited binary and may embed NULL bytes. When it is
+# forwarded with "%{+bin}o%[fc_pp_tlv(<id>)]", all bytes must be preserved.
+# Without +bin, the value is still emitted as text (up to the first NULL byte).
+# NULL-terminated text fields must keep their length with +bin.
+
+feature ignore_unknown_macro
+
+#REGTEST_TYPE=bug
+
+haproxy h1 -conf {
+ global
+ .if feature(THREAD)
+ thread-groups 1
+ .endif
+
+ defaults
+ mode http
+ timeout connect "${HAPROXY_TEST_TIMEOUT-5s}"
+ timeout client "${HAPROXY_TEST_TIMEOUT-5s}"
+ timeout server "${HAPROXY_TEST_TIMEOUT-5s}"
+
+ listen sender
+ bind "fd@${feS}"
+ server relay ${h1_feF_addr}:${h1_feF_port} send-proxy-v2 set-proxy-v2-tlv-fmt(0xE1) "%{+bin}o%[bin(4100420000430a)]"
+
+ listen relay
+ bind "fd@${feF}" accept-proxy
+ server receiver ${h1_feR_addr}:${h1_feR_port} send-proxy-v2 set-proxy-v2-tlv-fmt(0xE1) "%{+bin}o%[fc_pp_tlv(0xE1)]" set-proxy-v2-tlv-fmt(0xE2) "%[fc_pp_tlv(0xE1)]"
+
+ listen receiver
+ bind "fd@${feR}" accept-proxy
+ http-request set-var-fmt(txn.fe) "%{+bin}o%f"
+ http-request return status 200 hdr tlv-bin "%[fc_pp_tlv(0xE1),hex]" hdr tlv-txt "%[fc_pp_tlv(0xE2),hex]" hdr fe-len "%[var(txn.fe),length]"
+} -start
+
+client c1 -connect ${h1_feS_sock} {
+ txreq -url "/"
+ rxresp
+ expect resp.status == 200
+ expect resp.http.tlv-bin == "4100420000430A"
+ expect resp.http.tlv-txt == "41"
+ expect resp.http.fe-len == "8"
+} -run
return NULL;
len = ret - dst;
}
+ else if (ctx->options & LOG_OPT_BIN) {
+ /* binary-safe copy: a string may embed NULL bytes (e.g.
+ * fc_pp_tlv), they must be preserved like for binary
+ * samples.
+ */
+ if (len >= size)
+ len = size ? size - 1 : 0;
+ memcpy(dst, src, len);
+ }
else {
if (++len > size)
len = size;
return _lf_text_len(dst, src, len, size, ctx);
}
-/* lf_text_len() helper when <src> is null-byte terminated */
+/* lf_text_len() helper when <src> is null-byte terminated. With the "bin"
+ * option, the length is trusted by _lf_text_len(), so the real one is passed.
+ */
static inline char *lf_text(char *dst, const char *src, size_t size, struct lf_buildctx *ctx)
{
- return lf_text_len(dst, src, size, size, ctx);
+ size_t len = (src && (ctx->options & LOG_OPT_BIN)) ? strnlen(src, size) : size;
+
+ return lf_text_len(dst, src, len, size, ctx);
}
-/* lf_rawtext_len() helper when <src> is null-byte terminated */
+/* lf_rawtext_len() helper when <src> is null-byte terminated. With the "bin"
+ * option, the length is trusted by _lf_text_len(), so the real one is passed.
+ */
static inline char *lf_rawtext(char *dst, const char *src, size_t size, struct lf_buildctx *ctx)
{
- return lf_rawtext_len(dst, src, size, size, ctx);
+ size_t len = (src && (ctx->options & LOG_OPT_BIN)) ? strnlen(src, size) : size;
+
+ return lf_rawtext_len(dst, src, len, size, ctx);
}
/*