Signed-off-by: Hyeonggeun Oh <[email protected]>
---
doc/configuration.txt | 13 +++++-
.../proxy_protocol_send_generic_bin.vtc | 45 +++++++++++++++++++
src/log.c | 25 +++++++++--
3 files changed, 77 insertions(+), 6 deletions(-)
create mode 100644 reg-tests/connection/proxy_protocol_send_generic_bin.vtc
diff --git a/doc/configuration.txt b/doc/configuration.txt
index cc84f9ae5..2e5de22d6 100644
--- a/doc/configuration.txt
+++ b/doc/configuration.txt
@@ -20876,6 +20876,13 @@ set-proxy-v2-tlv-fmt(<id>) <fmt>
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
@@ -31126,8 +31133,10 @@ Supported item flags are (may be enabled/disabled from
item's arguments):
(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
diff --git a/reg-tests/connection/proxy_protocol_send_generic_bin.vtc
b/reg-tests/connection/proxy_protocol_send_generic_bin.vtc
new file mode 100644
index 000000000..450c6b801
--- /dev/null
+++ b/reg-tests/connection/proxy_protocol_send_generic_bin.vtc
@@ -0,0 +1,45 @@
+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
diff --git a/src/log.c b/src/log.c
index 050f5b3d5..7db6a4cef 100644
--- a/src/log.c
+++ b/src/log.c
@@ -2706,6 +2706,15 @@ static inline char *_lf_text_len(char *dst, const char
*src,
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;
@@ -2791,16 +2800,24 @@ static char *lf_rawtext_len(char *dst, const char *src,
size_t len, size_t 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);
}
/*
--
2.52.0