Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions tailcat.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@ func (p NodePublic) MarshalBinary() ([]byte, error) {

// UnmarshalBinary implements encoding.BinaryUnmarshaler for CBOR deserialization.
func (p *NodePublic) UnmarshalBinary(x []byte) error {
if len(x) != key.NodePublicRawLen {
return fmt.Errorf("invalid node public key length %d, want %d", len(x), key.NodePublicRawLen)
}
p.NodePublic = key.NodePublicFromRaw32(go4mem.B(x))
return nil
}
Expand Down
36 changes: 36 additions & 0 deletions tailcat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package tailcat

import (
"context"
"encoding/base64"
"errors"
"fmt"
"io"
Expand All @@ -16,6 +17,7 @@ import (
"testing"
"time"

"github.com/fxamacker/cbor/v2"
"github.com/google/go-cmp/cmp"
"go4.org/mem"
"tailscale.com/tailcfg"
Expand Down Expand Up @@ -422,6 +424,40 @@ func TestConnBlob(t *testing.T) {
}
}

func TestParseConnBlobMalformedPublicKey(t *testing.T) {
for name, keyBytes := range map[string][]byte{
"short": make([]byte, key.NodePublicRawLen-1),
"long": make([]byte, key.NodePublicRawLen+1),
} {
t.Run(name, func(t *testing.T) {
raw, err := cbor.Marshal(map[string][]byte{"p": keyBytes})
if err != nil {
t.Fatal(err)
}
cb := ConnBlob("tc" + base64.RawURLEncoding.EncodeToString(raw))
assertParseError := func(name string, parse func() error) {
t.Helper()
defer func() {
if r := recover(); r != nil {
t.Fatalf("%s panicked: %v", name, r)
}
}()
if err := parse(); err == nil {
t.Errorf("%s unexpectedly accepted malformed public key", name)
}
}
assertParseError("ParseConnBlob", func() error {
_, err := ParseConnBlob(cb)
return err
})
assertParseError("ParseConnBlobRaw", func() error {
_, err := ParseConnBlobRaw(cb)
return err
})
})
}
}

// TestFetchDERPMapMemoryCache verifies the default in-memory DERP map
// cache: a second fetch of the same URL within the freshness window
// makes no network request.
Expand Down
Loading