--- /dev/null
+#!/usr/bin/env python3
+# SPDX-License-Identifier: GPL-2.0-or-later
+
+"""Fake QUIC backend answering haproxy's Initial with a long header packet
+using an unsupported version. A single datagram is enough to reach
+quic_rx_pkt_parse() on the backend side of the connection: it must be
+dropped instead of being parsed with a QUIC_PACKET_TYPE_UNKNOWN type, which
+resolves to a NULL encryption level.
+"""
+import argparse
+import socket
+import sys
+
+
+def main():
+ ap = argparse.ArgumentParser()
+ ap.add_argument("--port", type=int, required=True)
+ ap.add_argument("--timeout", type=float, default=10.0)
+ args = ap.parse_args()
+
+ s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
+ s.settimeout(args.timeout)
+ s.bind(("127.0.0.1", args.port))
+ print("READY", flush=True)
+
+ try:
+ data, peer = s.recvfrom(65535)
+ except socket.timeout:
+ print("no Initial packet received", flush=True)
+ return 1
+
+ if not data[0] & 0x80:
+ print("not a long header packet", flush=True)
+ return 1
+
+ dcid_len = data[5]
+ scid_off = 6 + dcid_len
+ scid_len = data[scid_off]
+ hap_scid = data[scid_off + 1:scid_off + 1 + scid_len]
+
+ payload = b"\x00" * 40
+ pkt = (bytes([0xC0]) + (0x1A2A3A4A).to_bytes(4, "big") +
+ bytes([len(hap_scid)]) + hap_scid + b"\x00" +
+ bytes([0x40 | (len(payload) >> 8), len(payload) & 0xFF]) + payload)
+ s.sendto(pkt, peer)
+ s.close()
+ print("sent %d bytes long header packet with unsupported version" % len(pkt), flush=True)
+ return 0
+
+
+if __name__ == "__main__":
+ sys.exit(main())
--- /dev/null
+varnishtest "QUIC backend must drop packets with an unsupported version"
+# SPDX-License-Identifier: GPL-2.0-or-later
+
+#REGTEST_TYPE=bug
+
+feature ignore_unknown_macro
+feature cmd "python3 --version"
+
+# QUIC backend connections are not supported with USE_QUIC_OPENSSL_COMPAT.
+feature cmd "$HAPROXY_PROGRAM -cc 'feature(QUIC) && !feature(QUIC_OPENSSL_COMPAT) && !feature(OPENSSL_WOLFSSL)'"
+
+# Reserve the UDP port used by the fake QUIC backend.
+server s_backend {} -start
+server s_backend -break
+
+process p1 "python3 ${testdir}/unknown_version_backend.py --port ${s_backend_port}" -start
+process p1 -expect-text 0 0 "READY"
+
+haproxy h1 -conf {
+ global
+ expose-experimental-directives
+
+ defaults
+ mode http
+ timeout connect "${HAPROXY_TEST_TIMEOUT-5s}"
+ timeout client "${HAPROXY_TEST_TIMEOUT-5s}"
+ timeout server "${HAPROXY_TEST_TIMEOUT-5s}"
+
+ frontend fe
+ bind "fd@${fe}"
+ default_backend be
+
+ backend be
+ # Fail on the first attempt: do not depend on ICMP port unreachable
+ # to make connection retries fail fast once the fake backend exited.
+ retries 0
+ server qsrv quic4@127.0.0.1:${s_backend_port} ssl verify none alpn h3
+} -start
+
+# The fake backend answers the Initial with a long header packet using an
+# unsupported version: the request must fail, but haproxy used to abort on
+# BUG_ON(!qel || !pktns) in qc_enc_level_alloc() while parsing it.
+client c1 -connect ${h1_fe_sock} {
+ txreq
+ rxresp
+ expect resp.status == 503
+} -run
+
+process p1 -wait