Skip to content

Commit 0972db6

Browse files
authored
Implement SVCB (miekg#1067)
* Implement SVCB * Fix serialization and deserialization of double quotes * More effort (?) 4 months old commit * DEBUG * _ * Presentation format serialization/deserialization * _ Remove generated * Progress on presentation format parse & write * _ * Finish parsing presentation format * Regenerate * Pack unpack * Move to svcb.go Scan_rr.go and types.go should be untouched now * 🐛 Thanks ghedo * Definitions * TypeHTTPSSVC * Generated and isDuplicate * Goodbye lenient functions Now private key=value pairs have to be defined as structs too. They are no longer automatically named as KeyNNNNN * Encode/decode * Experimental svc * Read method * Implement some of the methods, use trick... to report where the error is while reading it. This should be applied to EDNS too. Todo: Find if case can only contain e := new(SVC_ALPN) and rest moved out Also fix two compile errors * Add SVC_LOCAL methods, reorder, remove alpn value, bugs * Errors * Alpn, make it build * Correct testsuite * Fully implement parser Change from keeping a state variable to reading in one iteration until the key=value pair is fully consumed * Simplify and document EDNS should be simplified too * Attempt to fix fuzzer And Alpn bug * A bug and change type values to match @ghedo's implementation * IP bug Also there are two ip duplicating patterns, one with copy, one with append. Maybe change it to be consistent. * Check for strictly increasing keys as required * Don't panic on invalid alpn * Redundant check, don't modify original array * Size calculation * Fix the fuzzer, match the style * 65535 is reserved too, don't delay errors * Check keyNNN, check for aliasform having values * IPvNHint is an array * Fix ipvNHint * Rename everything * Unrecognized keys according to the updated specification * Skip zero-length structs in generators. Fix CI * Doc cleanup * Off by one * Add parse tests * Check if private key doesn't collide with known key, invalid tests * Disallow IPv4 as IPv6. More tests. Related miekg#1107 * Style fixes * More consistency, more tests * 🐛 Deep copy as in the documentation a := make([]net.IP, 1) a[0] = net.ParseIP("1.1.1.1").To4() b := append(make([]net.IP, 0, 1), a...) b[0] = net.ParseIP("3.1.1.1").To4() fmt.Println(a[0][0]) * Make tests readable * Move valid parse tests to different file * 🐛 One of previous commits not fully committed * Test binary single value encoding/decoding and full encode/decode * Add worst-case grows to builders, 🐛 Wrong visible character range, redundant tests * Testing improvements And don't convert to IPv4 twice * Doc update only * Document worst case allocations and ipv6 can be at most of length 39, not 40 * Redundant IP copy, consistent IPv6 behavior, fix deep copy * isDuplicate for SVCB * Optimizations * echoconfig * Svc => SVCB * Fix CI * Regenerate after REBASE (2) Rebased twice on 15th and 20th May * Rename svc, use escapeByte. * Fix parsing whitespaces between quotes, rename ECHOHOConfig * resolve Remove svcbFieldLen Use reverseInt Uppercase SVCB Rename key_value "invalid" => bad Alpn comments > 65535 check Unneeded slices * a little more read => parse IP array meaning Force pushed because forgot to change read in svcb_test.go * HTTPSSVC -> HTTPS * Use new values * mandatory code MikeBishop/dns-alt-svc#205 * Resolve comments Rename svcb-pairs Remove SVCB_PRIVATE ranges Comment on SVCB_KEY65535 ParseError return l.token rename svcbKeyToString and svcbStringToKey privatize SVCBKeyToString, SVCBStringToKey * Refactor 1 Rename sorted, originalPairs Use append instead of copy Use svcb_RESERVED instead of 65535, with it now being private "type SVCBKey uint16" * Refactor 2 svcbKeyToString as method svcbStringToKey updated after key 0 :bug: mandatory has missing key Rename str idx < 0 * Refactor 3 Use l.token as z var key, value string Comment wrap 0: Sentences with '.' keyValue => kv * Refactor 4 * Refactor 5 len() int * Refactor 6 * Refactor 7 * Test remove parsing * Error messages * Rewrite two estimate comments * parse shouldn't modify original array :bug: * Remove two unneeded comments * Address review comments Push 2 because can't build fuzzer python Push 3 to try again * Simplify argument duplication as per tmthrgd's suggestion And add the relevant test Force push edit: Make sorting code fit into one line * Rewrite ECHConfig and address the review * Remove the optional tab * Add To4() Check * More cleanup and fix mandatory not sorting bug
1 parent cec9156 commit 0972db6

12 files changed

+1230
-0
lines changed

duplicate_generate.go

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

duplicate_test.go

+33
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,39 @@ func TestDuplicateTXT(t *testing.T) {
3434
}
3535
}
3636

37+
func TestDuplicateSVCB(t *testing.T) {
38+
a1, _ := NewRR(`example.com. 3600 IN SVCB 1 . ipv6hint=1::3:3:3:3 key65300=\254\032\030\000\ \043,\;`)
39+
a2, _ := NewRR(`example.com. 3600 IN SVCB 1 . ipv6hint=1:0::3:3:3:3 key65300="\254\ \030\000 +\,;"`)
40+
41+
if !IsDuplicate(a1, a2) {
42+
t.Errorf("expected %s/%s to be duplicates, but got false", a1.String(), a2.String())
43+
}
44+
45+
a2, _ = NewRR(`example.com. 3600 IN SVCB 1 . ipv6hint=1::3:3:3:3 key65300="\255\ \030\000 +\,;"`)
46+
47+
if IsDuplicate(a1, a2) {
48+
t.Errorf("expected %s/%s not to be duplicates, but got true", a1.String(), a2.String())
49+
}
50+
51+
a1, _ = NewRR(`example.com. 3600 IN SVCB 1 . ipv6hint=1::3:3:3:3`)
52+
53+
if IsDuplicate(a1, a2) {
54+
t.Errorf("expected %s/%s not to be duplicates, but got true", a1.String(), a2.String())
55+
}
56+
57+
a2, _ = NewRR(`example.com. 3600 IN SVCB 1 . ipv4hint=1.1.1.1`)
58+
59+
if IsDuplicate(a1, a2) {
60+
t.Errorf("expected %s/%s not to be duplicates, but got true", a1.String(), a2.String())
61+
}
62+
63+
a1, _ = NewRR(`example.com. 3600 IN SVCB 1 . ipv4hint=1.1.1.1,1.0.2.1`)
64+
65+
if IsDuplicate(a1, a2) {
66+
t.Errorf("expected %s/%s not to be duplicates, but got true", a1.String(), a2.String())
67+
}
68+
}
69+
3770
func TestDuplicateOwner(t *testing.T) {
3871
a1, _ := NewRR("www.example.org. IN A 127.0.0.1")
3972
a2, _ := NewRR("www.example.org. IN A 127.0.0.1")

msg_generate.go

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

msg_helpers.go

+60
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"encoding/binary"
77
"encoding/hex"
88
"net"
9+
"sort"
910
"strings"
1011
)
1112

@@ -612,6 +613,65 @@ func packDataNsec(bitmap []uint16, msg []byte, off int) (int, error) {
612613
return off, nil
613614
}
614615

616+
func unpackDataSVCB(msg []byte, off int) ([]SVCBKeyValue, int, error) {
617+
var xs []SVCBKeyValue
618+
var code uint16
619+
var length uint16
620+
var err error
621+
for off < len(msg) {
622+
code, off, err = unpackUint16(msg, off)
623+
if err != nil {
624+
return nil, len(msg), &Error{err: "overflow unpacking SVCB"}
625+
}
626+
length, off, err = unpackUint16(msg, off)
627+
if err != nil || off+int(length) > len(msg) {
628+
return nil, len(msg), &Error{err: "overflow unpacking SVCB"}
629+
}
630+
e := makeSVCBKeyValue(SVCBKey(code))
631+
if e == nil {
632+
return nil, len(msg), &Error{err: "bad SVCB key"}
633+
}
634+
if err := e.unpack(msg[off : off+int(length)]); err != nil {
635+
return nil, len(msg), err
636+
}
637+
if len(xs) > 0 && e.Key() <= xs[len(xs)-1].Key() {
638+
return nil, len(msg), &Error{err: "SVCB keys not in strictly increasing order"}
639+
}
640+
xs = append(xs, e)
641+
off += int(length)
642+
}
643+
return xs, off, nil
644+
}
645+
646+
func packDataSVCB(pairs []SVCBKeyValue, msg []byte, off int) (int, error) {
647+
pairs = append([]SVCBKeyValue(nil), pairs...)
648+
sort.Slice(pairs, func(i, j int) bool {
649+
return pairs[i].Key() < pairs[j].Key()
650+
})
651+
prev := svcb_RESERVED
652+
for _, el := range pairs {
653+
if el.Key() == prev {
654+
return len(msg), &Error{err: "repeated SVCB keys are not allowed"}
655+
}
656+
prev = el.Key()
657+
packed, err := el.pack()
658+
if err != nil {
659+
return len(msg), err
660+
}
661+
off, err = packUint16(uint16(el.Key()), msg, off)
662+
if err != nil {
663+
return len(msg), &Error{err: "overflow packing SVCB"}
664+
}
665+
off, err = packUint16(uint16(len(packed)), msg, off)
666+
if err != nil || off+len(packed) > len(msg) {
667+
return len(msg), &Error{err: "overflow packing SVCB"}
668+
}
669+
copy(msg[off:off+len(packed)], packed)
670+
off += len(packed)
671+
}
672+
return off, nil
673+
}
674+
615675
func unpackDataDomainNames(msg []byte, off, end int) ([]string, int, error) {
616676
var (
617677
servers []string

parse_test.go

+64
Original file line numberDiff line numberDiff line change
@@ -1633,6 +1633,70 @@ func TestParseCSYNC(t *testing.T) {
16331633
}
16341634
}
16351635

1636+
func TestParseSVCB(t *testing.T) {
1637+
svcbs := map[string]string{
1638+
`example.com. 3600 IN SVCB 0 cloudflare.com.`: `example.com. 3600 IN SVCB 0 cloudflare.com.`,
1639+
`example.com. 3600 IN SVCB 65000 cloudflare.com. alpn=h2 ipv4hint=3.4.3.2`: `example.com. 3600 IN SVCB 65000 cloudflare.com. alpn="h2" ipv4hint="3.4.3.2"`,
1640+
`example.com. 3600 IN SVCB 65000 cloudflare.com. key65000=4\ 3 key65001="\" " key65002 key65003= key65004="" key65005== key65006==\"\" key65007=\254 key65008=\032`: `example.com. 3600 IN SVCB 65000 cloudflare.com. key65000="4\ 3" key65001="\"\ " key65002="" key65003="" key65004="" key65005="=" key65006="=\"\"" key65007="\254" key65008="\ "`,
1641+
}
1642+
for s, o := range svcbs {
1643+
rr, err := NewRR(s)
1644+
if err != nil {
1645+
t.Error("failed to parse RR: ", err)
1646+
continue
1647+
}
1648+
if rr.String() != o {
1649+
t.Errorf("`%s' should be equal to\n`%s', but is `%s'", s, o, rr.String())
1650+
}
1651+
}
1652+
}
1653+
1654+
func TestParseBadSVCB(t *testing.T) {
1655+
header := `example.com. 3600 IN HTTPS `
1656+
evils := []string{
1657+
`0 . no-default-alpn`, // aliasform
1658+
`65536 . no-default-alpn`, // bad priority
1659+
`1 ..`, // bad domain
1660+
`1 . no-default-alpn=1`, // value illegal
1661+
`1 . key`, // invalid key
1662+
`1 . key=`, // invalid key
1663+
`1 . =`, // invalid key
1664+
`1 . ==`, // invalid key
1665+
`1 . =a`, // invalid key
1666+
`1 . ""`, // invalid key
1667+
`1 . ""=`, // invalid key
1668+
`1 . "a"`, // invalid key
1669+
`1 . "a"=`, // invalid key
1670+
`1 . key1=`, // we know that key
1671+
`1 . key65535`, // key reserved
1672+
`1 . key065534`, // key can't be padded
1673+
`1 . key65534="f`, // unterminated value
1674+
`1 . key65534="`, // unterminated value
1675+
`1 . key65534=\2`, // invalid numberic escape
1676+
`1 . key65534=\24`, // invalid numberic escape
1677+
`1 . key65534=\256`, // invalid numberic escape
1678+
`1 . key65534=\`, // invalid numberic escape
1679+
`1 . key65534=""alpn`, // zQuote ending needs whitespace
1680+
`1 . key65534="a"alpn`, // zQuote ending needs whitespace
1681+
`1 . ipv6hint=1.1.1.1`, // not ipv6
1682+
`1 . ipv6hint=1:1:1:1`, // not ipv6
1683+
`1 . ipv6hint=a`, // not ipv6
1684+
`1 . ipv4hint=1.1.1.1.1`, // not ipv4
1685+
`1 . ipv4hint=::fc`, // not ipv4
1686+
`1 . ipv4hint=..11`, // not ipv4
1687+
`1 . ipv4hint=a`, // not ipv4
1688+
`1 . port=`, // empty port
1689+
`1 . echconfig=YUd`, // bad base64
1690+
}
1691+
for _, o := range evils {
1692+
_, err := NewRR(header + o)
1693+
if err == nil {
1694+
t.Error("failed to reject invalid RR: ", header+o)
1695+
continue
1696+
}
1697+
}
1698+
}
1699+
16361700
func TestParseBadNAPTR(t *testing.T) {
16371701
// Should look like: mplus.ims.vodafone.com. 3600 IN NAPTR 10 100 "S" "SIP+D2U" "" _sip._udp.mplus.ims.vodafone.com.
16381702
naptr := `mplus.ims.vodafone.com. 3600 IN NAPTR 10 100 S SIP+D2U _sip._udp.mplus.ims.vodafone.com.`

0 commit comments

Comments
 (0)