fix Read call in APIAddr
* don't assume that Read fills the buffer. * don't succeed if the API file is too large. License: MIT Signed-off-by: Steven Allen <steven@stebalien.com>
Steven Allen committed
Mar 7, 2018 at 18:51 UTC
fda2428d36cd17126a747254ac2cc2aa536aef3a
1 file changed
+12
-4
repo/fsrepo/fsrepo.go
+12
-4
@@ -324,13 +324,21 @@ func APIAddr(repoPath string) (ma.Multiaddr, error) {
324
325
// read up to 2048 bytes. io.ReadAll is a vulnerability, as
326
// someone could hose the process by putting a massive file there.
327
- buf := make([]byte, 2048)
328
- n, err := f.Read(buf)
329
- if err != nil && err != io.EOF {
327
+ //
328
+ // NOTE(@stebalien): @jbenet probably wasn't thinking straight when he
329
+ // wrote that comment but I'm leaving the limit here in case there was
330
+ // some hidden wisdom. However, I'm fixing it such that:
331
+ // 1. We don't read too little.
332
+ // 2. We don't truncate and succeed.
333
+ buf, err := ioutil.ReadAll(io.LimitReader(f, 2048))
334
+ if err != nil {
335
return nil, err
336
}
337
+ if len(buf) == 2048 {
338
+ return nil, fmt.Errorf("API file too large, must be <2048 bytes long: %s", apiFilePath)
339
+ }
340
333
- s := string(buf[:n])
341
+ s := string(buf)
342
s = strings.TrimSpace(s)
343
return ma.NewMultiaddr(s)
344
}