Prevent adding empty peers at runtime

Fixes #1182
This commit is contained in:
Neil Alexander
2026-02-23 22:55:06 +00:00
parent a4b522288d
commit 82df3ea9a5
2 changed files with 24 additions and 1 deletions

View File

@@ -158,6 +158,9 @@ const ErrLinkNoSuitableIPs = linkError("peer has no suitable addresses")
const ErrLinkToSelf = linkError("node cannot connect to self")
func (l *links) add(u *url.URL, sintf string, linkType linkType) error {
if _, err := l.dialerFor(u); err != nil {
return err
}
var retErr error
phony.Block(l, func() {
// Generate the link info and see whether we think we already
@@ -591,6 +594,14 @@ func (l *links) listen(u *url.URL, sintf string, local bool) (*Listener, error)
}
func (l *links) connect(ctx context.Context, u *url.URL, info linkInfo, options linkOptions) (net.Conn, error) {
dialer, err := l.dialerFor(u)
if err != nil {
return nil, err
}
return dialer.dial(ctx, u, info, options)
}
func (l *links) dialerFor(u *url.URL) (linkProtocol, error) {
var dialer linkProtocol
switch strings.ToLower(u.Scheme) {
case "tcp":
@@ -610,7 +621,7 @@ func (l *links) connect(ctx context.Context, u *url.URL, info linkInfo, options
default:
return nil, ErrLinkUnrecognisedSchema
}
return dialer.dial(ctx, u, info, options)
return dialer, nil
}
func (l *links) handler(linkType linkType, options linkOptions, conn net.Conn, success func(), local bool) error {

View File

@@ -39,3 +39,15 @@ func TestDuplicatePeerFromAPI(t *testing.T) {
t.Fatalf("Adding peer should have failed on second attempt")
}
}
func TestAddEmptyPeer(t *testing.T) {
cfg := config.GenerateConfig()
c, err := New(cfg.Certificate, nil)
if err != nil {
t.Fatal(err)
}
u, _ := url.Parse("")
if err := c.AddPeer(u, ""); err == nil {
t.Fatalf("Expected error on empty URL: %s", err)
}
}