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

Fix: doublehashes can use different hashes #28

Merged
merged 2 commits into from
Oct 27, 2023
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
15 changes: 8 additions & 7 deletions denylist.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,18 +371,19 @@ func (dl *Denylist) parseLine(line string, number uint64) error {
// It can be a Multihash (CIDv0) or a sha256-hex-encoded string.

var mhType uint64
// attempt to parse CID
// attempt to parse a b58btc-encoded multihash
rule = strings.TrimPrefix(rule, "//")
c, err := cid.Decode(rule)
mh, err := multihash.FromB58String(rule)
if err == nil {
prefix := c.Prefix()
if prefix.Version != 0 {
return fmt.Errorf("double-hash is not a raw-multihash (cidv0) (%s:%d)", dl.Filename, number)
dmh, err := multihash.Decode(mh)
if err != nil {
return fmt.Errorf("what appears to be a multihash b58 string cannot be decoded (%s:%d): %w", dl.Filename, number, err)
}
e.Multihash = c.Hash()

e.Multihash = mh
// we use the multihash codec to group double-hashes
// with the same hashing function.
mhType = c.Prefix().MhType
mhType = dmh.Code
} else { // Assume a hex-encoded sha256 string
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@lidel there is a problem here...

some of the badbits hex-encoded strings parse just fine as B58 strings, resulting in valid multihashes with weird code, which then make the whole error checking break for anything later.

I assume the other way around also is an issue: some b58 strings could perhaps be hex-decoded... ? If you have thoughts, they are welcome.

Copy link
Member

Choose a reason for hiding this comment

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

Creating two rules sgtm.

bs, err := hex.DecodeString(rule)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion tester/test.deny
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,5 @@ author: "@hsanjuan"
# (different codec)
# And /ipfs/f01701e20903cf61d46521b05f926ba1634628d0bba8a7ffb5b6d5a3ca310682ca63b5ef0/path
# But not /path2
//QmbK7LDv5NNBvYQzNfm2eED17SNLt1yNMapcUhSuNLgkqz
//gW813G35CnLsy7gRYYHuf63hrz71U1xoLFDVeV7actx6oX