Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support/fuzz: Add xdr.ClaimPredicate JSON fuzzing #3181

Merged
merged 2 commits into from
Nov 5, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added support/fuzz/xdr/jsonclaimpredicate/corpus/0
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
81 changes: 81 additions & 0 deletions support/fuzz/xdr/jsonclaimpredicate/fuzz.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// +build gofuzz

package jsonclaimpredicate

import (
"bytes"
"encoding/json"

"github.com/stellar/go/xdr"
)

// Fuzz is go-fuzz function for fuzzing xdr.ClaimPredicate JSON
// marshaller and unmarshaller. To run:
// 1. Install go-fuzz: https://github.com/dvyukov/go-fuzz
// 2. go-fuzz-build
// 3. go-fuzz
Copy link
Member

@leighmcculloch leighmcculloch Nov 2, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Can you place these commands in a gofuzz.sh file in the repo root in a similar fashion as govet.sh? The shell script can install the go-fuzz tool at a specific version, then run the commands. It'll mean easy for others to get going without having to stumble on this comment, and we can include the fuzz test in a CI run, which I think will be important because the build tag means we don't verify this code compiles anywhere in tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a great idea but I'd like to do it in a separate PR. We won't run fuzzer in Circle-CI, we probably need to use our internal infra for this. There are multiple reasons: go-fuzz runs indefinitely, we need to update the corpus when new files are generated and it's better to run fuzzing on a powerful machine. I'll create a script but when we agree when we want to run fuzzing.

func Fuzz(data []byte) int {
// Ignore malformed ClaimPredicate
var p xdr.ClaimPredicate
err := xdr.SafeUnmarshal(data, &p)
if err != nil {
return -1
}

// Ignore invalid predicates: (and/or length != 2, nil not)
if !validate(p) {
return -1
}

j, err := json.Marshal(p)
if err != nil {
panic(err)
}

var p2 xdr.ClaimPredicate
err = json.Unmarshal(j, &p2)
if err != nil {
panic(err)
}

j2, err := json.Marshal(p2)
if err != nil {
panic(err)
}

if !bytes.Equal(j, j2) {
panic("not equal " + string(j) + " " + string(j2))
}

return 1
}

func validate(p xdr.ClaimPredicate) bool {
switch p.Type {
case xdr.ClaimPredicateTypeClaimPredicateUnconditional:
return true
case xdr.ClaimPredicateTypeClaimPredicateAnd:
and := *p.AndPredicates
if len(and) != 2 {
return false
}
return validate(and[0]) && validate(and[1])
case xdr.ClaimPredicateTypeClaimPredicateOr:
or := *p.OrPredicates
if len(or) != 2 {
return false
}
return validate(or[0]) && validate(or[1])
case xdr.ClaimPredicateTypeClaimPredicateNot:
if *p.NotPredicate == nil {
return false
}
return validate(**p.NotPredicate)
case xdr.ClaimPredicateTypeClaimPredicateBeforeAbsoluteTime:
return *p.AbsBefore >= 0
case xdr.ClaimPredicateTypeClaimPredicateBeforeRelativeTime:
return *p.RelBefore >= 0
}

panic("Invalid type")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❓ What's the motivation for panicing here? Should this return false rather than panicing given that it is not valid?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line should never be executed because Unmarshal called before validate should error on non-existent union arm and we check all available options in the switch above.

}