Skip to content

Commit fc68024

Browse files
committed
style: fix golangci-lint issues in QUIC SNI code
1 parent c2dadf8 commit fc68024

3 files changed

Lines changed: 29 additions & 27 deletions

File tree

internal/proxy/quic_sni.go

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ func ExtractQUICSNI(packet []byte) string {
128128
}
129129

130130
// Derive Initial secrets.
131-
clientSecret, err := deriveQUICClientSecret(dcid, salt, version)
131+
clientSecret, err := deriveQUICClientSecret(dcid, salt)
132132
if err != nil {
133133
return ""
134134
}
@@ -317,7 +317,7 @@ func ExtractQUICCryptoData(packet []byte) (data []byte, offset uint64) {
317317
return nil, 0
318318
}
319319

320-
clientSecret, err := deriveQUICClientSecret(dcid, salt, version)
320+
clientSecret, err := deriveQUICClientSecret(dcid, salt)
321321
if err != nil {
322322
return nil, 0
323323
}
@@ -404,10 +404,10 @@ func extractFirstCryptoFrame(frames []byte) ([]byte, uint64) {
404404
}
405405
pos += n
406406

407-
switch {
408-
case frameType == 0x00: // PADDING
409-
case frameType == 0x01: // PING
410-
case frameType == 0x02 || frameType == 0x03: // ACK
407+
switch frameType {
408+
case 0x00: // PADDING
409+
case 0x01: // PING
410+
case 0x02, 0x03: // ACK
411411
_, vn := readQUICVarint(frames[pos:])
412412
if vn == 0 {
413413
return nil, 0
@@ -449,7 +449,7 @@ func extractFirstCryptoFrame(frames []byte) ([]byte, uint64) {
449449
pos += vn
450450
}
451451
}
452-
case frameType == 0x06: // CRYPTO
452+
case 0x06: // CRYPTO
453453
cryptoOffset, vn := readQUICVarint(frames[pos:])
454454
if vn == 0 {
455455
return nil, 0
@@ -466,7 +466,7 @@ func extractFirstCryptoFrame(frames []byte) ([]byte, uint64) {
466466
result := make([]byte, int(dataLen))
467467
copy(result, frames[pos:pos+int(dataLen)])
468468
return result, cryptoOffset
469-
case frameType == 0x1c || frameType == 0x1d: // CONNECTION_CLOSE
469+
case 0x1c, 0x1d: // CONNECTION_CLOSE
470470
_, vn := readQUICVarint(frames[pos:])
471471
if vn == 0 {
472472
return nil, 0
@@ -535,15 +535,15 @@ func extractCryptoData(frames []byte) []byte {
535535
}
536536
pos += n
537537

538-
switch {
539-
case frameType == 0x00:
538+
switch frameType {
539+
case 0x00:
540540
// PADDING frame: single-byte type, no payload. The type byte
541541
// was already consumed above.
542542

543-
case frameType == 0x01:
543+
case 0x01:
544544
// PING frame: single-byte type, no payload.
545545

546-
case frameType == 0x02 || frameType == 0x03:
546+
case 0x02, 0x03:
547547
// ACK frame: skip it. Parse enough to find the length.
548548
// Largest Acknowledged (varint)
549549
_, vn := readQUICVarint(frames[pos:])
@@ -593,7 +593,7 @@ func extractCryptoData(frames []byte) []byte {
593593
}
594594
}
595595

596-
case frameType == 0x06:
596+
case 0x06:
597597
// CRYPTO frame: offset(varint) + length(varint) + data
598598
offset, vn := readQUICVarint(frames[pos:])
599599
if vn == 0 {
@@ -615,7 +615,7 @@ func extractCryptoData(frames []byte) []byte {
615615
}
616616
pos += int(dataLen)
617617

618-
case frameType == 0x1c || frameType == 0x1d:
618+
case 0x1c, 0x1d:
619619
// CONNECTION_CLOSE frame: error_code(varint) + frame_type(varint,
620620
// only for 0x1c) + reason_phrase_length(varint) + reason_phrase.
621621
_, vn := readQUICVarint(frames[pos:])
@@ -685,13 +685,14 @@ func readQUICVarint(buf []byte) (uint64, int) {
685685
}
686686

687687
// deriveQUICClientSecret derives the TLS 1.3 client Initial secret from
688-
// the DCID and salt per RFC 9001 Section 5.2.
689-
func deriveQUICClientSecret(dcid, salt []byte, version uint32) ([]byte, error) {
688+
// the DCID and salt per RFC 9001 Section 5.2. Both QUIC v1 and v2 use the
689+
// same label for initial secret derivation, so the version is only reflected
690+
// in the caller's choice of salt.
691+
func deriveQUICClientSecret(dcid, salt []byte) ([]byte, error) {
690692
// Step 1: initial_secret = HKDF-Extract(salt, dcid)
691693
h := hkdf.Extract(sha256.New, dcid, salt)
692694

693695
// Step 2: client_in = HKDF-Expand-Label(initial_secret, "client in", "", 32)
694-
// Both QUIC v1 and v2 use the same label for initial secret derivation.
695696
return hkdfExpandLabel(h, "client in", 32)
696697
}
697698

internal/proxy/quic_sni_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ func buildQUICInitialFromPlaintext(t *testing.T, dcid, plaintext []byte, version
286286
ivLabel = "quicv2 iv"
287287
}
288288

289-
clientSecret, err := deriveQUICClientSecret(dcid, salt, version)
289+
clientSecret, err := deriveQUICClientSecret(dcid, salt)
290290
if err != nil {
291291
t.Fatalf("deriveQUICClientSecret: %v", err)
292292
}
@@ -414,7 +414,7 @@ func buildQUICInitial(t *testing.T, hostname string, version uint32) []byte {
414414
}
415415

416416
// Derive keys.
417-
clientSecret, err := deriveQUICClientSecret(dcid, salt, version)
417+
clientSecret, err := deriveQUICClientSecret(dcid, salt)
418418
if err != nil {
419419
t.Fatalf("deriveQUICClientSecret: %v", err)
420420
}

internal/proxy/server_test.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4486,8 +4486,9 @@ func TestSNIAccumulatorPartialDataCannotExtractSNI(t *testing.T) {
44864486
// sole CRYPTO frame sits at the given offset with the provided handshake
44874487
// data. This lets tests simulate quic-go fragmenting a ClientHello across
44884488
// several Initial packets. dcid must be identical across packets that share
4489-
// the same connection so decryption uses the same keys.
4490-
func buildQUICInitialWithCrypto(t *testing.T, dcid []byte, offset uint64, data []byte, version uint32) []byte {
4489+
// the same connection so decryption uses the same keys. Always builds a
4490+
// QUIC v1 packet since all current callers only exercise v1 fragmentation.
4491+
func buildQUICInitialWithCrypto(t *testing.T, dcid []byte, offset uint64, data []byte) []byte {
44914492
t.Helper()
44924493

44934494
var crypto []byte
@@ -4496,7 +4497,7 @@ func buildQUICInitialWithCrypto(t *testing.T, dcid []byte, offset uint64, data [
44964497
crypto = append(crypto, encodeQUICVarint(uint64(len(data)))...)
44974498
crypto = append(crypto, data...)
44984499

4499-
return buildQUICInitialFromPlaintext(t, dcid, crypto, version)
4500+
return buildQUICInitialFromPlaintext(t, dcid, crypto, quicVersionV1)
45004501
}
45014502

45024503
// TestExtractQUICCryptoDataReturnsOffsetAndData verifies that
@@ -4507,7 +4508,7 @@ func TestExtractQUICCryptoDataReturnsOffsetAndData(t *testing.T) {
45074508
dcid := []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}
45084509
payload := []byte("first-crypto-chunk")
45094510

4510-
packet := buildQUICInitialWithCrypto(t, dcid, 0, payload, quicVersionV1)
4511+
packet := buildQUICInitialWithCrypto(t, dcid, 0, payload)
45114512
got, offset := ExtractQUICCryptoData(packet)
45124513
if offset != 0 {
45134514
t.Errorf("offset = %d, want 0", offset)
@@ -4517,7 +4518,7 @@ func TestExtractQUICCryptoDataReturnsOffsetAndData(t *testing.T) {
45174518
}
45184519

45194520
// Non-zero offset packet.
4520-
packet2 := buildQUICInitialWithCrypto(t, dcid, 42, []byte("later-chunk"), quicVersionV1)
4521+
packet2 := buildQUICInitialWithCrypto(t, dcid, 42, []byte("later-chunk"))
45214522
got2, offset2 := ExtractQUICCryptoData(packet2)
45224523
if offset2 != 42 {
45234524
t.Errorf("offset = %d, want 42", offset2)
@@ -4675,8 +4676,8 @@ func TestQUICSNIAccumulationAcrossTwoPackets(t *testing.T) {
46754676
part2 := hs[splitAt:]
46764677

46774678
dcid := []byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88}
4678-
packet1 := buildQUICInitialWithCrypto(t, dcid, 0, part1, quicVersionV1)
4679-
packet2 := buildQUICInitialWithCrypto(t, dcid, uint64(splitAt), part2, quicVersionV1)
4679+
packet1 := buildQUICInitialWithCrypto(t, dcid, 0, part1)
4680+
packet2 := buildQUICInitialWithCrypto(t, dcid, uint64(splitAt), part2)
46804681

46814682
// Sanity: the first packet alone should NOT produce an SNI via the
46824683
// single-packet path.
@@ -4721,7 +4722,7 @@ func TestQUICSNIAccumulationFallsBackAfterPacketBudget(t *testing.T) {
47214722
destIP := net.ParseIP("10.77.0.2")
47224723

47234724
for i := 0; i < maxSNIAccumulatorPackets; i++ {
4724-
packet := buildQUICInitialWithCrypto(t, dcid, uint64(1000+i*16), []byte("gap-bytes-only"), quicVersionV1)
4725+
packet := buildQUICInitialWithCrypto(t, dcid, uint64(1000+i*16), []byte("gap-bytes-only"))
47254726
if _, err := env.udpConn.WriteTo(wrapInSOCKS5UDP(packet, destIP), env.bindAddr); err != nil {
47264727
t.Fatalf("send packet %d: %v", i, err)
47274728
}

0 commit comments

Comments
 (0)