@cryptotaxi247 / kubo / commits / b90d7bd7b

Remove unneccesary split in IpnsValidator

License: MIT Signed-off-by: Dirk McCormick <dirkmdev@gmail.com>

Dirk McCormick committed Jan 21, 2018 at 09:51 UTC b90d7bd7b06f7a91accdfcd212803819ba318223
2 files changed +68 -27
namesys/ipns_validate_test.go
+67 -24
@@ -1,6 +1,7 @@
1 package namesys
2
3 import (
4 + "io"
5 "testing"
6 "time"
7
@@ -18,10 +19,7 @@ func TestValidation(t *testing.T) {
19
20 // Generate a key for signing the records
21 r := u.NewSeededRand(15) // generate deterministic keypair
21 - priv, pubk, err := ci.GenerateKeyPairWithReader(ci.RSA, 1024, r)
22 - if err != nil {
23 - t.Fatal(err)
24 - }
22 + priv, ipnsPath := genKeys(t, r)
23
24 // Create entry with expiry in one hour
25 ts := time.Now()
@@ -30,64 +28,109 @@ func TestValidation(t *testing.T) {
28 t.Fatal(err)
29 }
30
33 - // Get IPNS record path
34 - pubkb, err := pubk.Bytes()
31 + val, err := proto.Marshal(entry)
32 if err != nil {
33 t.Fatal(err)
34 }
38 - pubkh := u.Hash(pubkb).B58String()
39 - ipnsPath := "/ipns/" + pubkh
35
41 - val, err := proto.Marshal(entry)
36 + // Create the record
37 + rec, err := record.MakePutRecord(priv, ipnsPath, val, true)
38 if err != nil {
39 t.Fatal(err)
40 }
41
46 - // Create the record
47 - r1, err := record.MakePutRecord(priv, ipnsPath, val, true)
48 -
42 // Validate the record
50 - err = validator.VerifyRecord(r1)
43 + err = validator.VerifyRecord(rec)
44 if err != nil {
45 t.Fatal(err)
46 }
47
48 +
49 // Create IPNS record path with a different key
56 - _, pubk2, err := ci.GenerateKeyPairWithReader(ci.RSA, 1024, r)
50 + _, ipnsWrongAuthor := genKeys(t, r)
51 + wrongAuthorRec, err := record.MakePutRecord(priv, ipnsWrongAuthor, val, true)
52 if err != nil {
53 t.Fatal(err)
54 }
60 - pubkb2, err := pubk2.Bytes()
55 +
56 + // Record should fail validation because path doesn't match author
57 + err = validator.VerifyRecord(wrongAuthorRec)
58 + if err != ErrInvalidAuthor {
59 + t.Fatal("ValidateIpnsRecord should have returned ErrInvalidAuthor")
60 + }
61 +
62 +
63 + // Create IPNS record path with extra path components after author
64 + extraPath := ipnsPath + "/some/path"
65 + extraPathRec, err := record.MakePutRecord(priv, extraPath, val, true)
66 if err != nil {
67 t.Fatal(err)
68 }
64 - pubkh2 := u.Hash(pubkb2).B58String()
65 - ipnsWrongPath := "/ipns/" + pubkh2
69
67 - r2, err := record.MakePutRecord(priv, ipnsWrongPath, val, true)
70 + // Record should fail validation because path has extra components after author
71 + err = validator.VerifyRecord(extraPathRec)
72 + if err != ErrInvalidAuthor {
73 + t.Fatal("ValidateIpnsRecord should have returned ErrInvalidAuthor")
74 + }
75
69 - // Record should fail validation because path doesn't match author
70 - err = validator.VerifyRecord(r2)
76 +
77 + // Create unsigned IPNS record
78 + unsignedRec, err := record.MakePutRecord(priv, ipnsPath, val, false)
79 + if err != nil {
80 + t.Fatal(err)
81 + }
82 +
83 + // Record should fail validation because IPNS records require signature
84 + err = validator.VerifyRecord(unsignedRec)
85 + if err != ErrInvalidAuthor {
86 + t.Fatal("ValidateIpnsRecord should have returned ErrInvalidAuthor")
87 + }
88 +
89 +
90 + // Create unsigned IPNS record with no author
91 + unsignedRecNoAuthor, err := record.MakePutRecord(priv, ipnsPath, val, false)
92 + if err != nil {
93 + t.Fatal(err)
94 + }
95 + noAuth := ""
96 + unsignedRecNoAuthor.Author = &noAuth
97 +
98 + // Record should fail validation because IPNS records require author
99 + err = validator.VerifyRecord(unsignedRecNoAuthor)
100 if err != ErrInvalidAuthor {
101 t.Fatal("ValidateIpnsRecord should have returned ErrInvalidAuthor")
102 }
103
104 +
105 // Create expired entry
76 - expired, err := CreateRoutingEntryData(priv, path.Path("foo"), 1, ts.Add(-1*time.Hour))
106 + expiredEntry, err := CreateRoutingEntryData(priv, path.Path("foo"), 1, ts.Add(-1*time.Hour))
107 if err != nil {
108 t.Fatal(err)
109 }
80 - valExp, err := proto.Marshal(expired)
110 + valExp, err := proto.Marshal(expiredEntry)
111 if err != nil {
112 t.Fatal(err)
113 }
114
115 // Create record with the expired entry
86 - r3, err := record.MakePutRecord(priv, ipnsPath, valExp, true)
116 + expiredRec, err := record.MakePutRecord(priv, ipnsPath, valExp, true)
117
118 // Record should fail validation because entry is expired
89 - err = validator.VerifyRecord(r3)
119 + err = validator.VerifyRecord(expiredRec)
120 if err != ErrExpiredRecord {
121 t.Fatal("ValidateIpnsRecord should have returned ErrExpiredRecord")
122 }
123 }
124 +
125 +func genKeys(t *testing.T, r io.Reader) (ci.PrivKey, string) {
126 + priv, pubk, err := ci.GenerateKeyPairWithReader(ci.RSA, 1024, r)
127 + if err != nil {
128 + t.Fatal(err)
129 + }
130 + pubkb, err := pubk.Bytes()
131 + if err != nil {
132 + t.Fatal(err)
133 + }
134 + p := "/ipns/" + u.Hash(pubkb).B58String()
135 + return priv, p
136 +}
namesys/publisher.go
+1 -3
@@ -5,7 +5,6 @@ import (
5 "context"
6 "errors"
7 "fmt"
8 - "strings"
8 "time"
9
10 pb "github.com/ipfs/go-ipfs/namesys/pb"
@@ -319,8 +318,7 @@ func ValidateIpnsRecord(r *record.ValidationRecord) error {
318 // need to do that here
319
320 // Author in key must match author in record
322 - parts := strings.Split(r.Key, "/")
323 - pid, err := peer.IDB58Decode(parts[0])
321 + pid, err := peer.IDB58Decode(r.Key)
322 if err != nil {
323 return ErrInvalidAuthor
324 }