Skip to content

Commit

Permalink
Allow Travel Addresses with Port
Browse files Browse the repository at this point in the history
  • Loading branch information
bbengfort committed Apr 24, 2024
1 parent 761d2ba commit 7964c9c
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 9 deletions.
22 changes: 13 additions & 9 deletions pkg/openvasp/traddr/traddr.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ repository per their ISC license.
package traddr

import (
"fmt"
"net"
"net/url"
"strings"
Expand Down Expand Up @@ -57,8 +58,8 @@ func Encode(uri string) (_ string, err error) {
return "", ErrMissingQueryString
}

if !u.ValidTLD() {
return "", ErrInvalidTLD
if err = u.ValidTLD(); err != nil {
return "", err
}

return scheme + checkEncode([]byte(uri)), nil
Expand Down Expand Up @@ -90,8 +91,8 @@ func Decode(traddr string) (_ string, err error) {
return "", ErrMissingQueryString
}

if !u.ValidTLD() {
return "", ErrInvalidTLD
if err = u.ValidTLD(); err != nil {
return "", err
}

return string(url), nil
Expand Down Expand Up @@ -126,7 +127,7 @@ func Parse(rawURL string) (_ *URL, err error) {
}

// If the scheme and host is missing, try parsing with an empty scheme
if u.Scheme == "" && u.Host == "" {
if (u.Scheme == "" && u.Host == "") || u.Hostname() == "" {
if u, err = url.Parse("//" + rawURL); err != nil {
return nil, err
}
Expand All @@ -135,18 +136,21 @@ func Parse(rawURL string) (_ *URL, err error) {
return &URL{*u}, nil
}

func (u *URL) ValidTLD() bool {
func (u *URL) ValidTLD() error {
// localhost is a valid TLD
hostname := u.Hostname()
if hostname == "localhost" {
return true
return nil
}

// Check if the hostname is an IP address
if net.ParseIP(hostname) != nil {
return true
return nil
}

// Otherwise validate the TLD
return tld.FromDomainName(hostname).IsValid()
if !tld.FromDomainName(hostname).IsValid() {
return fmt.Errorf("%q is an %w", hostname, ErrInvalidTLD)
}
return nil
}
121 changes: 121 additions & 0 deletions pkg/openvasp/traddr/traddr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,42 @@ func TestMake(t *testing.T) {
"https://beneficiary.com/x/12345?t=i&foo=bar&color=red",
"taGw1e4cjuujwyHBV51aspbLBUbcDhebW7ss8iF6dLEn19WfyQZt6HaUTStSE2YcadkFG",
},
{
"https://trisa.beneficiary.com",
"ta6HcFqwh5y4D3r7z66JyKqvSAekyXnXc2KwdXSUec",
},
{
"https://trisa.beneficiary.com/x/12345",
"ta4uMjxqDq4t7nefCdrm4ssCTewVbvBLzmUnVcYe4SXqGafPTPewG",
},
{
"https://trisa.beneficiary.com/x/12345?t=i",
"ta4uMjxqDq4t7nefCdrm4ssCTewVbvBLzmUnVcYe4SXqGafPTPewG",
},
{
"trisa.beneficiary.com:4443",
"ta3dgYSWEoAJPfU2Wdr2hvQmXem4dHbfKuBvkbKGfBrzdb61C",
},
{
"https://beneficiary.com:8000",
"ta21hULpYqwBirCCHHU13UMT1hhy3syAAPXJem8cJ",
},
{
"trisa.beneficiary.com:4443/x/12345",
"ta2wijJqYhcj5zriQyV5SVWybf71AyHEXgRCa7WWJ8XQk9g1NodLEHbKoHdg",
},
{
"https://beneficiary.com:8000/x/12345",
"takLQA9MSCjHr3kkHh4fnC5rWNbrySUJhTwQHZsML2anv95kkqe",
},
{
"trisa.beneficiary.com:4443/x/12345?t=i",
"ta2wijJqYhcj5zriQyV5SVWybf71AyHEXgRCa7WWJ8XQk9g1NodLEHbKoHdg",
},
{
"https://beneficiary.com:8000/x/12345?t=i",
"takLQA9MSCjHr3kkHh4fnC5rWNbrySUJhTwQHZsML2anv95kkqe",
},
}

for i, tc := range testCases {
Expand Down Expand Up @@ -82,6 +118,30 @@ func TestDecodeURL(t *testing.T) {
"taGw1e4cjuujwyHBV51aspbLBUbcDhebW7ss8iF6dLEn19WfyQZt6HaUTStSE2YcadkFG",
"https://beneficiary.com/x/12345?color=red&foo=bar&t=i",
},
{
"ta6HcFqwh5y4D3r7z66JyKqvSAekyXnXc2KwdXSUec",
"https://trisa.beneficiary.com?t=i",
},
{
"ta4uMjxqDq4t7nefCdrm4ssCTewVbvBLzmUnVcYe4SXqGafPTPewG",
"https://trisa.beneficiary.com/x/12345?t=i",
},
{
"ta3dgYSWEoAJPfU2Wdr2hvQmXem4dHbfKuBvkbKGfBrzdb61C",
"https://trisa.beneficiary.com:4443?t=i",
},
{
"ta21hULpYqwBirCCHHU13UMT1hhy3syAAPXJem8cJ",
"https://beneficiary.com:8000?t=i",
},
{
"ta2wijJqYhcj5zriQyV5SVWybf71AyHEXgRCa7WWJ8XQk9g1NodLEHbKoHdg",
"https://trisa.beneficiary.com:4443/x/12345?t=i",
},
{
"takLQA9MSCjHr3kkHh4fnC5rWNbrySUJhTwQHZsML2anv95kkqe",
"https://beneficiary.com:8000/x/12345?t=i",
},
}

for i, tc := range testCases {
Expand All @@ -104,6 +164,18 @@ func TestEncoding(t *testing.T) {
nil,
"happy case",
},
{
"trisa.beneficiary.com:8000?t=i",
"ta3dgYSWEoAJPfU2Wdr2hvQmXem4dHbfKyYrisbBTnXjL2Kub",
nil,
"happy case with port",
},
{
"trisa.beneficiary.com:8000/x/12345?t=i",
"ta2wijJqYhcj5zriQyV5SVWybf71AyHEXjdvtJEwNdQYykjqYkVT6dUXAfTf",
nil,
"happy case with port and path",
},
{
"api.testing.21analytics.xyz/transfers/01H11RHY53MBMEQB1VJ4KJF39Z?t=i",
"ta2SRCMhxtdtKVfoV2MTziaN7F9WD7WKyGvynQa7w3as7ziTr583ZRU1DeKarHjFrLSxuQAZLciZMqry4Yk797qKErWYnCmj8sLaH",
Expand Down Expand Up @@ -164,6 +236,18 @@ func TestDecoding(t *testing.T) {
nil,
"happy case",
},
{
"ta3dgYSWEoAJPfU2Wdr2hvQmXem4dHbfKyYrisbBTnXjL2Kub",
"trisa.beneficiary.com:8000?t=i",
nil,
"happy case with port",
},
{
"ta2wijJqYhcj5zriQyV5SVWybf71AyHEXjdvtJEwNdQYykjqYkVT6dUXAfTf",
"trisa.beneficiary.com:8000/x/12345?t=i",
nil,
"happy case with port and path",
},
{
"ta2SRCMhxtdtKVfoV2MTziaN7F9WD7WKyGvynQa7w3as7ziTr583ZRU1DeKarHjFrLSxuQAZLciZMqry4Yk797qKErWYnCmj8sLaH",
"api.testing.21analytics.xyz/transfers/01H11RHY53MBMEQB1VJ4KJF39Z?t=i",
Expand Down Expand Up @@ -225,3 +309,40 @@ func TestDecoding(t *testing.T) {
}
}
}

func TestValidTLD(t *testing.T) {
t.Run("Valid", func(t *testing.T) {
tests := []string{
"https://beneficiary.com",
"beneficiary.org",
"trisa.beneficiary.co",
"https://beneficiary.co.uk:8000",
"beneficiary.us:8000",
"trisa.beneficiary.net:8000",
"testnet.trisa.beneficiary.io:8000",
}

for i, tc := range tests {
u, _ := traddr.Parse(tc)
require.NoError(t, u.ValidTLD(), "received unexpected error for test case %d", i)
}
})

t.Run("Invalid", func(t *testing.T) {
tests := []string{
"https://beneficiary.bear",
"beneficiary.bear",
"trisa.beneficiary.bear",
"https://beneficiary.bear:8000",
"beneficiary.bear:8000",
"trisa.beneficiary.bear:8000",
"testnet.trisa.beneficiary.bear:8000",
}

for i, tc := range tests {
u, _ := traddr.Parse(tc)
require.ErrorIs(t, u.ValidTLD(), traddr.ErrInvalidTLD, "did not receive expected error for test case %d", i)
}

})
}

0 comments on commit 7964c9c

Please sign in to comment.