Commit88f929c9Recorded18 Oct 2026Repositorysigil-quic

Bound TLS duplicate tracking to eight KiB bitmaps

Changed
 README.md                         |  4 ++++
 scripts/inject-seen-spy.mjs       | 19 +++++++++++++++++++
 scripts/native-tls-tests.mjs      |  9 +++++++--
 scripts/seen-budget-tests.sgl     | 29 +++++++++++++++++++++++++++++
 scripts/seen-spy.sgl              | 13 +++++++++++++
 src/sigil/quic/tls/codec.sgl      | 13 ++++++++-----
 src/sigil/quic/tls/extensions.sgl | 13 ++++++++-----
 test/test-tls-seen.sgl            | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 8 files changed, 149 insertions(+), 12 deletions(-)
Diff
README.mdmodified
@@ -60,6 +60,10 @@ ticket ages/binders and removal of identities, while rejecting new or reordered
60
identities and a repeated early-data offer. No PSK or early-data selection is sent.
61
An optional CertificateRequest is declined with an empty Certificate. Bounded
62
NewSessionTicket messages are checked and discarded; TLS KeyUpdate is rejected.
+63
+64
Extension and client key-share duplicate tracking use fixed 8 KiB bitmaps for
+65
the complete uint16 identifier space. Marking each ID takes constant time;
+66
the existing wire-size bounds and duplicate rejection remain in force.
67
Incoming SNI values outside the supported DNS syntax are treated as
68
unrecognized: the server continues with its configured certificate and omits
69
the SNI acknowledgement. Empty host names, duplicate name types and malformed
scripts/inject-seen-spy.mjsadded
@@ -0,0 +1,19 @@
+1
// Remote-only allocation observation at the duplicate-tracker sites.
+2
// Each wrapper returns the actual buffer; canonical inputs stay untouched.
+3
import { readFileSync, writeFileSync } from 'node:fs';
+4
+5
const [kind, input, output] = process.argv.slice(2);
+6
if (!['codec', 'extensions'].includes(kind) || !input || !output || input === output) {
+7
throw new Error('Usage: inject-seen-spy.mjs codec|extensions INPUT OUTPUT');
+8
}
+9
let source = readFileSync(input, 'utf8');
+10
const sites = [...source.matchAll(/\(make-bytevector (65536|8192) 0\)/g)];
+11
if (sites.length !== (kind === 'codec' ? 2 : 1) || source.includes('seen-spy') ||
+12
source.split(' (import ').length !== 2) {
+13
throw new Error('Expected canonical TLS duplicate-tracker sites');
+14
}
+15
source = source.replace(/\(make-bytevector (65536|8192) 0\)/g,
+16
(_, size) => `(seen-spy-buffer ${size} 0)`);
+17
source = source.replace(' (import ',
+18
' (import (sigil quic tests seen spy)\n ');
+19
writeFileSync(output, ';;; TEST ONLY: observed duplicate-tracker allocations.\n' + source);
scripts/native-tls-tests.mjsmodified
@@ -11,6 +11,7 @@ const source = join(output, 'src', 'sigil', 'quic', 'tests');
11
mkdirSync(source, { recursive: true });
12
const files = [
13
'test-tls-codec.sgl', 'test-tls-extensions.sgl',
+14
'test-tls-seen.sgl',
15
'test-tls-authentication.sgl', 'test-tls-handshake.sgl',
16
'test-tls-input-bounds.sgl',
17
'test-tls-alerts.sgl',
@@ -54,7 +55,8 @@ const pathResponse = process.argv.includes('--path-response');
55
const cidZero = process.argv.includes('--cid-zero');
56
const cidSequence = process.argv.includes('--cid-sequence');
57
const handlerReadiness = process.argv.includes('--handler-readiness');
−57
if ([cleanupFaults, secretCleanup, secretRetention, handshakeCleanup, packetRetention, factoryCleanup, eventCleanup, constructionCleanup, pathResponse, cidZero, cidSequence, handlerReadiness].filter(Boolean).length > 1) {
+58
const seenBudget = process.argv.includes('--seen-budget');
+59
if ([cleanupFaults, secretCleanup, secretRetention, handshakeCleanup, packetRetention, factoryCleanup, eventCleanup, constructionCleanup, pathResponse, cidZero, cidSequence, handlerReadiness, seenBudget].filter(Boolean).length > 1) {
60
throw new Error('Select one focused test suite');
61
}
62
if (cleanupFaults) {
@@ -93,6 +95,9 @@ if (cidSequence) {
95
if (handlerReadiness) {
96
files.splice(0, files.length, 'test-handler-readiness.sgl');
97
}
+98
if (seenBudget) {
+99
files.splice(0, files.length, 'test-tls-seen.sgl', '../scripts/seen-budget-tests.sgl');
+100
}
101
const bodies = files.map(file => {
102
const text = readFileSync(join(root, 'test', file), 'utf8');
103
const boundary = text.indexOf('\n\n');
@@ -129,7 +134,7 @@ writeFileSync(join(source, 'native.sgl'), `(define-library (sigil quic tests nat
134
(sigil quic tls extensions)
135
(sigil quic tls authentication)
136
(sigil quic tls transcript)
−132
(sigil quic tls vectors)${secretCleanup || secretRetention ? '\n (sigil quic tests secret spy)' : ''}${handshakeCleanup ? '\n (sigil quic tests handshake spy)' : ''}${packetRetention ? '\n (sigil quic tests packet spy)' : ''}${factoryCleanup ? '\n (sigil quic tests factory spy)' : ''}${eventCleanup ? '\n (sigil quic tests event spy)' : ''}${constructionCleanup ? '\n (sigil quic tests construction spy)' : ''})
+137
(sigil quic tls vectors)${secretCleanup || secretRetention ? '\n (sigil quic tests secret spy)' : ''}${handshakeCleanup ? '\n (sigil quic tests handshake spy)' : ''}${packetRetention ? '\n (sigil quic tests packet spy)' : ''}${factoryCleanup ? '\n (sigil quic tests factory spy)' : ''}${eventCleanup ? '\n (sigil quic tests event spy)' : ''}${constructionCleanup ? '\n (sigil quic tests construction spy)' : ''}${seenBudget ? '\n (sigil quic tests seen spy)' : ''})
138
(export main)
139
(begin
140
${bodies.map(body => body.split('\n').map(line => ' ' + line).join('\n')).join('\n\n')}
scripts/seen-budget-tests.sgladded
@@ -0,0 +1,29 @@
+1
(import (sigil test)
+2
(sigil io)
+3
(sigil quic tls codec)
+4
(sigil quic tls extensions)
+5
(sigil quic tests seen spy))
+6
+7
(define seen-budget-entries (list (cons 0 #u8(1)) (cons 65535 #u8(2))))
+8
(define seen-budget-wire #u8(0 10 0 0 0 1 1 255 255 0 1 2))
+9
+10
(define (seen-budget! procedure expected)
+11
(seen-spy-reset!)
+12
(let* ((result (procedure)) (allocations (seen-spy-allocations)))
+13
(unless (and (= 1 (length allocations))
+14
(= (vector-ref (car allocations) 0) (vector-ref (car allocations) 1)))
+15
(error "Allocation fixture did not observe the actual single tracker"))
+16
(println "SEEN-TRACKER requested=~a actual=~a"
+17
(vector-ref (car allocations) 0) (vector-ref (car allocations) 1))
+18
(assert-true
+19
(and (equal? expected result) (<= (vector-ref (car allocations) 1) 8192)))))
+20
+21
(test-group "TLS duplicate-tracker allocation budget"
+22
(test "ql10-seen: extension encoder tracker fits eight KiB"
+23
(seen-budget! (lambda () (encode-extensions seen-budget-entries)) seen-budget-wire))
+24
(test "ql10-seen: extension decoder tracker fits eight KiB"
+25
(seen-budget! (lambda () (decode-extensions seen-budget-wire)) seen-budget-entries))
+26
(test "ql10-seen: key-share encoder tracker fits eight KiB"
+27
(seen-budget! (lambda () (encode-client-key-shares seen-budget-entries)) seen-budget-wire))
+28
(test "ql10-seen: key-share decoder tracker fits eight KiB"
+29
(seen-budget! (lambda () (decode-client-key-shares seen-budget-wire)) seen-budget-entries)))
scripts/seen-spy.sgladded
@@ -0,0 +1,13 @@
+1
;;; Records only sizes of actual returned buffers, never buffer references.
+2
(define-library (sigil quic tests seen spy)
+3
(export seen-spy-reset! seen-spy-allocations seen-spy-buffer)
+4
(begin
+5
(define captured '())
+6
+7
(define (seen-spy-reset!) (set! captured '()))
+8
(define (seen-spy-allocations) (reverse captured))
+9
+10
(define (seen-spy-buffer size fill)
+11
(let ((buffer (make-bytevector size fill)))
+12
(set! captured (cons (vector size (bytevector-length buffer)) captured))
+13
buffer))))
src/sigil/quic/tls/codec.sglmodified
@@ -107,13 +107,16 @@
107
;; Unknown types survive decoding; placement/negotiation checks are made
108
;; by the handshake state machine. Duplicates are always malformed.
109
(define (mark-extension! seen type)
−110
(when (= (bytevector-u8-ref seen type) 1)
−111
(error "TLS: duplicate extension" type))
−112
(bytevector-u8-set! seen type 1))
+110
(let* ((index (quotient type 8)) (mask (expt 2 (modulo type 8)))
+111
(bits (bytevector-u8-ref seen index)))
+112
(unless (= (bitwise-and bits mask) 0)
+113
(error "TLS: duplicate extension" type))
+114
(bytevector-u8-set! seen index (+ bits mask))))
115
116
(define (tls-read-extensions! reader)
117
(let* ((input (make-tls-reader (tls-read-vector! reader 2 0 65535)))
−116
(seen (make-bytevector 65536 0)))
+118
;; One bit per uint16 identifier keeps the fixed budget at 8 KiB.
+119
(seen (make-bytevector 8192 0)))
120
(let loop ((entries '()))
121
(if (= (tls-remaining input) 0)
122
(reverse entries)
@@ -131,7 +134,7 @@
134
(define (encode-extensions entries)
135
(unless (list? entries)
136
(error "TLS: extensions must be a proper list"))
−134
(let ((seen (make-bytevector 65536 0))
+137
(let ((seen (make-bytevector 8192 0))
138
(size 0))
139
(for-each
140
(lambda (entry)
src/sigil/quic/tls/extensions.sglmodified
@@ -1,6 +1,7 @@
1
;;; TLS extension payloads used by the QUIC TLS 1.3 handshake.
2
(define-library (sigil quic tls extensions)
−3
(import (sigil io)
+3
(import (sigil math)
+4
(sigil io)
5
(sigil quic bytes)
6
(sigil quic tls alert)
7
(sigil quic tls codec))
@@ -185,12 +186,14 @@
186
187
(define (decode-client-key-shares wire)
188
(let ((entries (decode-list-vector wire 2 0 65535 read-key-share!))
−188
(seen (make-bytevector 65536 0)))
+189
(seen (make-bytevector 8192 0)))
190
(for-each
191
(lambda (entry)
−191
(when (= (bytevector-u8-ref seen (car entry)) 1)
−192
(error "TLS key_share: duplicate group"))
−193
(bytevector-u8-set! seen (car entry) 1))
+192
(let* ((group (car entry)) (index (quotient group 8))
+193
(mask (expt 2 (modulo group 8))) (bits (bytevector-u8-ref seen index)))
+194
(unless (= (bitwise-and bits mask) 0)
+195
(error "TLS key_share: duplicate group"))
+196
(bytevector-u8-set! seen index (+ bits mask))))
197
entries)
198
entries))
199
test/test-tls-seen.sgladded
@@ -0,0 +1,61 @@
+1
(import (sigil test)
+2
(sigil quic bytes)
+3
(sigil quic tls codec)
+4
(sigil quic tls extensions))
+5
+6
(define seen-check-types '(0 1 7 8 255 256 32767 65535))
+7
+8
(define (seen-check-entries)
+9
(map (lambda (type) (cons type (bytevector (modulo type 256)))) seen-check-types))
+10
+11
(define (seen-check-range count)
+12
(let loop ((type 0) (entries '()))
+13
(if (= type count)
+14
(reverse entries)
+15
(loop (+ type 1) (cons (cons type #u8()) entries)))))
+16
+17
(test-group "Bounded TLS uint16 duplicate tracking"
+18
(test "ql10-seen-control: extension IDs at bit byte and namespace boundaries stay distinct"
+19
(let ((entries (seen-check-entries)))
+20
(assert-equal entries (decode-extensions (encode-extensions entries)))))
+21
(test "ql10-seen-control: extension encoder rejects duplicates at every boundary"
+22
(for-each
+23
(lambda (type)
+24
(assert-error (encode-extensions (list (cons type #u8()) (cons type #u8(1))))))
+25
seen-check-types))
+26
(test "ql10-seen-control: extension decoder rejects duplicates at every boundary"
+27
(for-each
+28
(lambda (type)
+29
(assert-error
+30
(decode-extensions
+31
(tls-vector (bytes-append (tls-uint type 2) #u8(0 0)
+32
(tls-uint type 2) #u8(0 0)) 2 0 65535))))
+33
seen-check-types))
+34
(test "ql10-seen-control: largest empty-extension block survives and the next entry is rejected"
+35
(let* ((wire (encode-extensions (seen-check-range 16383)))
+36
(entries (decode-extensions wire)))
+37
(assert-equal 65534 (bytevector-length wire))
+38
(assert-equal 16383 (length entries))
+39
(assert-equal 0 (caar entries))
+40
(assert-equal 16382 (car (list-ref entries 16382)))
+41
(assert-error (encode-extensions (seen-check-range 16384)))))
+42
(test "ql10-seen-control: key-share group IDs at all boundaries stay distinct"
+43
(let ((entries (seen-check-entries)))
+44
(assert-equal entries (decode-client-key-shares (encode-client-key-shares entries)))))
+45
(test "ql10-seen-control: key-share encoder rejects duplicate group IDs"
+46
(for-each
+47
(lambda (type)
+48
(assert-error (encode-client-key-shares (list (cons type #u8(1)) (cons type #u8(2))))))
+49
seen-check-types))
+50
(test "ql10-seen-control: key-share decoder rejects duplicate group IDs"
+51
(for-each
+52
(lambda (type)
+53
(let ((wire (encode-key-share type #u8(1))))
+54
(assert-error (decode-client-key-shares (tls-vector (bytes-append wire wire) 2 0 65535)))))
+55
seen-check-types))
+56
(test "ql10-seen-control: independent calls start empty and X25519 framing remains valid"
+57
(let ((extensions (seen-check-entries))
+58
(shares (list (cons 29 (make-bytevector 32 7)) (cons 65535 #u8(1)))))
+59
(do ((round 0 (+ round 1))) ((= round 16))
+60
(assert-equal extensions (decode-extensions (encode-extensions extensions)))
+61
(assert-equal shares (decode-client-key-shares (encode-client-key-shares shares)))))))