@cryptotaxi247 / kubo / commits / 4bbaa2aa2

Modified keystore to ignore invalid key files inside the keystore directory.

* Has calls the validateName function before checking if we have the file * List filters the returned list of file names by validateName. License: MIT Signed-off-by: matrushka <barisgumustas@gmail.com>

matrushka committed Feb 14, 2018 at 12:31 UTC 4bbaa2aa27b57dff2919cda69819baf49b1c2854
2 files changed +76 -1
keystore/keystore.go
+19 -1
@@ -77,6 +77,10 @@ func (ks *FSKeystore) Has(name string) (bool, error) {
77 return false, err
78 }
79
80 + if err := validateName(name); err != nil {
81 + return false, err
82 + }
83 +
84 return true, nil
85 }
86
@@ -149,5 +153,19 @@ func (ks *FSKeystore) List() ([]string, error) {
153 return nil, err
154 }
155
152 - return dir.Readdirnames(0)
156 + dirs, err := dir.Readdirnames(0)
157 + if err != nil {
158 + return nil, err
159 + }
160 +
161 + var list []string
162 +
163 + for _, name := range dirs {
164 + err := validateName(name)
165 + if err == nil {
166 + list = append(list, name)
167 + }
168 + }
169 +
170 + return list, err
171 }
keystore/keystore_test.go
+57
@@ -4,6 +4,7 @@ import (
4 "fmt"
5 "io/ioutil"
6 "math/rand"
7 + "path/filepath"
8 "sort"
9 "testing"
10
@@ -143,6 +144,62 @@ func TestKeystoreBasics(t *testing.T) {
144 }
145 }
146
147 +func TestInvalidKeyFiles(t *testing.T) {
148 + tdir, err := ioutil.TempDir("", "keystore-test")
149 +
150 + if err != nil {
151 + t.Fatal(err)
152 + }
153 +
154 + ks, err := NewFSKeystore(tdir)
155 + if err != nil {
156 + t.Fatal(err)
157 + }
158 +
159 + key := privKeyOrFatal(t)
160 +
161 + bytes, err := key.Bytes()
162 + if err != nil {
163 + t.Fatal(err)
164 + }
165 +
166 + err = ioutil.WriteFile(filepath.Join(ks.dir, "valid"), bytes, 0644)
167 + if err != nil {
168 + t.Fatal(err)
169 + }
170 +
171 + err = ioutil.WriteFile(filepath.Join(ks.dir, ".invalid"), bytes, 0644)
172 + if err != nil {
173 + t.Fatal(err)
174 + }
175 +
176 + l, err := ks.List()
177 + if err != nil {
178 + t.Fatal(err)
179 + }
180 +
181 + sort.Strings(l)
182 + if len(l) != 1 {
183 + t.Fatal("wrong entry count")
184 + }
185 +
186 + if l[0] != "valid" {
187 + t.Fatal("wrong entries listed")
188 + }
189 +
190 + exist, err := ks.Has("valid")
191 + if !exist {
192 + t.Fatal("should know it has a key named valid")
193 + }
194 + if err != nil {
195 + t.Fatal(err)
196 + }
197 +
198 + if exist, err = ks.Has(".invalid"); err == nil {
199 + t.Fatal("shouldnt be able to put a key with a 'hidden' name")
200 + }
201 +}
202 +
203 func TestNonExistingKey(t *testing.T) {
204 tdir, err := ioutil.TempDir("", "keystore-test")
205 if err != nil {