Skip to content

Commit e54ff55

Browse files
committed
quic: truncate CONNECTION_CLOSE frame reason field
1 parent c0daf39 commit e54ff55

4 files changed

Lines changed: 24 additions & 8 deletions

File tree

‎src/quic/data.cc‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
#include <openssl/ssl.h>
88
#include <string_bytes.h>
99
#include <v8.h>
10+
#include <algorithm>
1011
#include "bindingdata.h"
1112
#include "data.h"
1213
#include "defs.h"
14+
#include "simdutf.h"
1315
#include "util.h"
1416

1517
namespace node {
@@ -218,6 +220,9 @@ void Store::MemoryInfo(MemoryTracker* tracker) const {
218220
// ============================================================================
219221

220222
namespace {
223+
224+
constexpr size_t kMaxConnectionCloseReasonLength = 256;
225+
221226
constexpr std::string_view TypeName(QuicError::Type type) {
222227
switch (type) {
223228
case QuicError::Type::APPLICATION:
@@ -332,6 +337,18 @@ QuicError::operator const ngtcp2_ccerr*() const {
332337
return ptr_;
333338
}
334339

340+
ngtcp2_ccerr QuicError::ToNgtcp2ConnectionCloseError() const {
341+
ngtcp2_ccerr error = *ptr_;
342+
error.reason = reason_c_str();
343+
344+
const size_t reasonlen =
345+
std::min(reason_.size(), kMaxConnectionCloseReasonLength);
346+
347+
error.reasonlen = simdutf::trim_partial_utf8(reason_.data(), reasonlen);
348+
349+
return error;
350+
}
351+
335352
std::string QuicError::reason_for_liberr(int liberr) {
336353
return ngtcp2_strerror(liberr);
337354
}

‎src/quic/data.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,8 @@ class QuicError final : public MemoryRetainer {
260260
operator const ngtcp2_ccerr&() const;
261261
operator const ngtcp2_ccerr*() const;
262262

263+
ngtcp2_ccerr ToNgtcp2ConnectionCloseError() const;
264+
263265
// Crypto errors are a subset of transport errors. The error code includes
264266
// the TLS alert code embedded within it.
265267
bool is_crypto_error() const;

‎src/quic/packet.cc‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,9 @@ Packet::Ptr Packet::CreateConnectionClosePacket(
110110
if (!packet) return packet;
111111
ngtcp2_vec vec = *packet;
112112

113+
ngtcp2_ccerr wire_error = error.ToNgtcp2ConnectionCloseError();
113114
ssize_t nwrite = ngtcp2_conn_write_connection_close(
114-
conn, nullptr, nullptr, vec.base, vec.len, error, uv_hrtime());
115+
conn, nullptr, nullptr, vec.base, vec.len, &wire_error, uv_hrtime());
115116
if (nwrite < 0) return Ptr();
116117
packet->Truncate(static_cast<size_t>(nwrite));
117118
return packet;

‎src/quic/session.cc‎

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3680,13 +3680,9 @@ void Session::SendConnectionClose() {
36803680

36813681
ngtcp2_vec vec = *packet;
36823682
Path path(impl_->local_address_, impl_->remote_address_);
3683-
ssize_t nwrite = ngtcp2_conn_write_connection_close(*this,
3684-
&path,
3685-
nullptr,
3686-
vec.base,
3687-
vec.len,
3688-
impl_->last_error_,
3689-
uv_hrtime());
3683+
ngtcp2_ccerr wire_error = impl_->last_error_.ToNgtcp2ConnectionCloseError();
3684+
ssize_t nwrite = ngtcp2_conn_write_connection_close(
3685+
*this, &path, nullptr, vec.base, vec.len, &wire_error, uv_hrtime());
36903686

36913687
if (nwrite < 0) [[unlikely]] {
36923688
return ErrorAndSilentClose();

0 commit comments

Comments
 (0)