From a187deaec3309d66f8853638e0f74e124b08bbcf Mon Sep 17 00:00:00 2001 From: Mansur Akhmadi <10739460+mansur-akhmadi@users.noreply.github.com> Date: Fri, 13 Nov 2026 23:57:46 +0700 Subject: [PATCH] caddyhttp: expose negotiated TLS curve as placeholder and log field Add {http.request.tls.curve} with the name of the negotiated key exchange and a numeric curve field to the tls object of access logs. Both are empty or omitted when CurveID is 0. Closes #8065 --- modules/caddyhttp/app.go | 1 + modules/caddyhttp/marshalers.go | 3 ++ modules/caddyhttp/marshalers_test.go | 41 ++++++++++++++++++++++++++++ modules/caddyhttp/replacer.go | 6 ++++ modules/caddyhttp/replacer_test.go | 18 +++++++++++- 5 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 modules/caddyhttp/marshalers_test.go diff --git a/modules/caddyhttp/app.go b/modules/caddyhttp/app.go index 1c42212b2..3218b0531 100644 --- a/modules/caddyhttp/app.go +++ b/modules/caddyhttp/app.go @@ -96,6 +96,7 @@ func init() { // `{http.request.tls.proto_mutual}` | The negotiated next protocol was advertised by the server // `{http.request.tls.server_name}` | The server name requested by the client, if any // `{http.request.tls.ech}` | Whether ECH was offered by the client and accepted by the server +// `{http.request.tls.curve}` | The negotiated key exchange mechanism (curve or group), if any // `{http.request.tls.client.fingerprint}` | The SHA256 checksum of the client certificate // `{http.request.tls.client.public_key}` | The public key of the client certificate. // `{http.request.tls.client.public_key_sha256}` | The SHA256 checksum of the client's public key. diff --git a/modules/caddyhttp/marshalers.go b/modules/caddyhttp/marshalers.go index 15fa3e8bc..2f030f554 100644 --- a/modules/caddyhttp/marshalers.go +++ b/modules/caddyhttp/marshalers.go @@ -78,6 +78,9 @@ func (t LoggableTLSConnState) MarshalLogObject(enc zapcore.ObjectEncoder) error enc.AddString("proto", t.NegotiatedProtocol) enc.AddString("server_name", t.ServerName) enc.AddBool("ech", t.ECHAccepted) + if t.CurveID != 0 { + enc.AddUint16("curve", uint16(t.CurveID)) + } if len(t.PeerCertificates) > 0 { enc.AddString("client_common_name", t.PeerCertificates[0].Subject.CommonName) enc.AddString("client_serial", t.PeerCertificates[0].SerialNumber.String()) diff --git a/modules/caddyhttp/marshalers_test.go b/modules/caddyhttp/marshalers_test.go new file mode 100644 index 000000000..61c51dd3f --- /dev/null +++ b/modules/caddyhttp/marshalers_test.go @@ -0,0 +1,41 @@ +// Copyright 2015 Matthew Holt and The Caddy Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package caddyhttp + +import ( + "crypto/tls" + "testing" + + "go.uber.org/zap/zapcore" +) + +func TestLoggableTLSConnStateCurve(t *testing.T) { + for _, tc := range []struct { + curve tls.CurveID + expected any + }{ + {curve: tls.X25519MLKEM768, expected: uint16(4588)}, + {curve: tls.X25519, expected: uint16(29)}, + {curve: 0, expected: nil}, + } { + enc := zapcore.NewMapObjectEncoder() + if err := LoggableTLSConnState(tls.ConnectionState{CurveID: tc.curve}).MarshalLogObject(enc); err != nil { + t.Fatalf("curve %d: unexpected error: %v", tc.curve, err) + } + if actual := enc.Fields["curve"]; actual != tc.expected { + t.Errorf("curve %d: expected %v (%T), got %v (%T)", tc.curve, tc.expected, tc.expected, actual, actual) + } + } +} diff --git a/modules/caddyhttp/replacer.go b/modules/caddyhttp/replacer.go index 6fbe90778..a4da40afe 100644 --- a/modules/caddyhttp/replacer.go +++ b/modules/caddyhttp/replacer.go @@ -622,6 +622,12 @@ func getTLSReplacement(state *tls.ConnectionState, key string) (any, bool) { return state.ServerName, true case "ech": return state.ECHAccepted, true + case "curve": + // CurveID is 0 when no curve-based key exchange was recorded + if state.CurveID == 0 { + return "", true + } + return state.CurveID.String(), true } return nil, false } diff --git a/modules/caddyhttp/replacer_test.go b/modules/caddyhttp/replacer_test.go index 11814c7e1..5a5507c9d 100644 --- a/modules/caddyhttp/replacer_test.go +++ b/modules/caddyhttp/replacer_test.go @@ -47,7 +47,7 @@ func TestTLSPlaceholdersWithoutTLS(t *testing.T) { req = req.WithContext(context.WithValue(req.Context(), VarsCtxKey, map[string]any{})) repl := NewTestReplacer(req) for _, field := range []string{ - "version", "cipher_suite", "resumed", "proto", "proto_mutual", "server_name", "ech", + "version", "cipher_suite", "resumed", "proto", "proto_mutual", "server_name", "ech", "curve", "client.fingerprint", "client.public_key", "client.public_key_sha256", "client.issuer", "client.serial", "client.subject", "client.certificate_pem", "client.certificate_der_base64", "client.san.dns_names", "client.san.emails", "client.san.ips", "client.san.uris", @@ -66,6 +66,17 @@ func TestTLSPlaceholdersWithoutTLS(t *testing.T) { } } +func TestTLSCurvePlaceholderWithoutKeyExchange(t *testing.T) { + req := httptest.NewRequest(http.MethodGet, "/", nil) + req.TLS = &tls.ConnectionState{Version: tls.VersionTLS12} + req = req.WithContext(context.WithValue(req.Context(), VarsCtxKey, map[string]any{})) + repl := NewTestReplacer(req) + value, known := repl.Get("http.request.tls.curve") + if !known || caddy.ToString(value) != "" { + t.Fatalf("Get(%q) = %v, %v; want empty, known", "http.request.tls.curve", value, known) + } +} + func TestUnknownTLSPlaceholdersRemainLiteral(t *testing.T) { for _, state := range []*tls.ConnectionState{nil, {ServerName: "example.com"}} { req := httptest.NewRequest(http.MethodGet, "/", nil) @@ -123,6 +134,7 @@ eqp31wM9il1n+guTNyxJd+FzVAH+hCZE5K+tCgVDdVFUlDEHHbS/wqb2PSIoouLV HandshakeComplete: true, ServerName: "example.com", CipherSuite: tls.TLS_AES_256_GCM_SHA384, + CurveID: tls.X25519MLKEM768, PeerCertificates: []*x509.Certificate{cert}, NegotiatedProtocol: "h2", NegotiatedProtocolIsMutual: true, @@ -252,6 +264,10 @@ eqp31wM9il1n+guTNyxJd+FzVAH+hCZE5K+tCgVDdVFUlDEHHbS/wqb2PSIoouLV get: "http.request.tls.cipher_suite", expect: "TLS_AES_256_GCM_SHA384", }, + { + get: "http.request.tls.curve", + expect: "X25519MLKEM768", + }, { get: "http.request.tls.proto", expect: "h2",