forked from kurin/blazer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix escape/unescape edge cases detected by go-fuzz escape filename in uri for DownloadFileByName
- Loading branch information
Showing
4 changed files
with
130 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -533,3 +533,76 @@ func TestEscapes(t *testing.T) { | |
} | ||
} | ||
} | ||
|
||
func TestUploadDownloadFilenameEscaping(t *testing.T) { | ||
filename := "file%foo.txt" | ||
|
||
id := os.Getenv(apiID) | ||
key := os.Getenv(apiKey) | ||
|
||
if id == "" || key == "" { | ||
t.Skipf("B2_ACCOUNT_ID or B2_SECRET_KEY unset; skipping integration tests") | ||
} | ||
ctx := context.Background() | ||
|
||
// b2_authorize_account | ||
b2, err := AuthorizeAccount(ctx, id, key, UserAgent("blazer-base-test")) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// b2_create_bucket | ||
bname := id + "-" + bucketName | ||
bucket, err := b2.CreateBucket(ctx, bname, "", nil, nil) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
defer func() { | ||
// b2_delete_bucket | ||
if err := bucket.DeleteBucket(ctx); err != nil { | ||
t.Error(err) | ||
} | ||
}() | ||
|
||
if err != nil { | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
armhold
Author
Owner
|
||
t.Fatalf("error unescaping string: %s\n", err) | ||
} | ||
|
||
// b2_get_upload_url | ||
ue, err := bucket.GetUploadURL(ctx) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// b2_upload_file | ||
smallFile := io.LimitReader(zReader{}, 128) | ||
hash := sha1.New() | ||
buf := &bytes.Buffer{} | ||
w := io.MultiWriter(hash, buf) | ||
if _, err := io.Copy(w, smallFile); err != nil { | ||
t.Error(err) | ||
} | ||
smallSHA1 := fmt.Sprintf("%x", hash.Sum(nil)) | ||
file, err := ue.UploadFile(ctx, buf, buf.Len(), filename, "application/octet-stream", smallSHA1, nil) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
defer func() { | ||
// b2_delete_file_version | ||
if err := file.DeleteFileVersion(ctx); err != nil { | ||
t.Error(err) | ||
} | ||
}() | ||
|
||
// b2_download_file_by_name | ||
fr, err := bucket.DownloadFileByName(ctx, filename, 0, 0) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
lbuf := &bytes.Buffer{} | ||
if _, err := io.Copy(lbuf, fr); err != nil { | ||
t.Fatal(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package base | ||
|
||
import ( | ||
"testing" | ||
"fmt" | ||
) | ||
|
||
func TestEncodeDecode(t *testing.T) { | ||
// crashes identified by go-fuzz | ||
origs := []string{ | ||
"&\x020000", | ||
"&\x020000\x9c", | ||
"&\x020\x9c0", | ||
"&\x0230j", | ||
"&\x02\x98000", | ||
"&\x02\x983\xc8j00", | ||
"00\x000", | ||
"00\x0000", | ||
"00\x0000000000000", | ||
"\x11\x030", | ||
} | ||
|
||
for _, orig := range origs { | ||
escaped := escape(orig) | ||
unescaped, err := unescape(escaped) | ||
if err != nil { | ||
t.Errorf("%s: orig: %#v, escaped: %#v, unescaped: %#v\n", err.Error(), orig, escaped, unescaped) | ||
continue | ||
} | ||
|
||
if unescaped != orig { | ||
t.Errorf("expected: %#v, got: %#v", orig, unescaped) | ||
} | ||
} | ||
} | ||
|
||
// hook for go-fuzz: https://github.com/dvyukov/go-fuzz | ||
func Fuzz(data []byte) int { | ||
orig := string(data) | ||
escaped := escape(orig) | ||
|
||
unescaped, err := unescape(escaped) | ||
if err != nil { | ||
return 0 | ||
} | ||
|
||
if unescaped != orig { | ||
panic(fmt.Sprintf("unescaped: \"%#v\", != orig: \"%#v\"", unescaped, orig)) | ||
} | ||
|
||
return 1 | ||
} |
This seems to be checking a function call that never happened.