Skip to content

Commit 8b523be

Browse files
StebalienKubuxu
authored andcommitted
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 <[email protected]>
1 parent ae8826b commit 8b523be

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

repo/fsrepo/fsrepo.go

+12-4
Original file line numberDiff line numberDiff line change
@@ -324,13 +324,21 @@ func APIAddr(repoPath string) (ma.Multiaddr, error) {
324324

325325
// read up to 2048 bytes. io.ReadAll is a vulnerability, as
326326
// 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 {
330335
return nil, err
331336
}
337+
if len(buf) == 2048 {
338+
return nil, fmt.Errorf("API file too large, must be <2048 bytes long: %s", apiFilePath)
339+
}
332340

333-
s := string(buf[:n])
341+
s := string(buf)
334342
s = strings.TrimSpace(s)
335343
return ma.NewMultiaddr(s)
336344
}

0 commit comments

Comments
 (0)