more strict validation of .onion addresses

This commit is contained in:
Leif Ryge 2015-08-29 20:22:48 +00:00
parent ed310561bd
commit 75a10eef9b
2 changed files with 8 additions and 3 deletions

View File

@ -168,12 +168,15 @@ func addressStringToBytes(p Protocol, s string) ([]byte, error) {
case P_TOR:
fields := strings.Split(s, ".onion")
if len(fields) != 2 {
return nil, fmt.Errorf("failed to parse ipfs addr: %s not a Tor .onion address.", s)
if len(fields) != 2 || len(fields[1]) != 0 {
return nil, fmt.Errorf("failed to parse tor addr: %s does not end with .onion", s)
}
b, err := base32.StdEncoding.DecodeString(strings.ToUpper(fields[0]))
if err != nil {
return nil, fmt.Errorf("failed to parse ipfs addr: %s %s", s, err)
return nil, fmt.Errorf("failed to parse tor addr: %s %s", s, err)
}
if len(b) != 10 {
return nil, fmt.Errorf("failed to parse tor addr: %s decoded to %s bytes, expected 10", s, len(b))
}
return b, nil
case P_IPFS: // ipfs

View File

@ -27,6 +27,8 @@ func TestConstructFails(t *testing.T) {
"/tcp/65536",
"/tor/9imaq4ygg2iegci7.onion",
"/tor/aaimaq4ygg2iegci7.onion",
"/tor/timaq4ygg2iegci7.onionxxx",
"/tor/timaq4yg.onion",
"/udp/1234/sctp",
"/udp/1234/udt/1234",
"/udp/1234/utp/1234",