Commit 75a10eef authored by Leif Ryge's avatar Leif Ryge

more strict validation of .onion addresses

parent ed310561
......@@ -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
......
......@@ -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",
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment