Skip to content
This repository was archived by the owner on Oct 5, 2023. It is now read-only.

Commit a3b4935

Browse files
committed
fix: change CID breaking logic when parsing ipld.ErrNotFound
1 parent a3354f0 commit a3b4935

File tree

2 files changed

+16
-26
lines changed

2 files changed

+16
-26
lines changed

errors.go

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,6 @@ func parseErrNotFoundWithFallbackToError(msg error) error {
4747
return msg
4848
}
4949

50-
// Use a string to move it into RODATA
51-
// print("".join("\\x01" if chr(i) not in string.ascii_letters + string.digits else "\\x00" for i in range(ord('z')+1)))
52-
const notAsciiLetterOrDigitsLUT = "\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x01\x01\x01\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x01\x01\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
53-
54-
func notAsciiLetterOrDigits(r rune) bool {
55-
if r > 'z' {
56-
return true
57-
}
58-
59-
return notAsciiLetterOrDigitsLUT[r] > 0
60-
}
61-
6250
//lint:ignore ST1008 this function is not using the error as a mean to return failure but it massages it to return the correct type
6351
func parseErrNotFound(msg string) (error, bool) {
6452
if msg == "" {
@@ -76,6 +64,12 @@ func parseErrNotFound(msg string) (error, bool) {
7664
return nil, false
7765
}
7866

67+
// Assume CIDs break on:
68+
// - Whitespaces: " \t\n\r\v\f"
69+
// - Semicolon: ";" this is to parse ipld.ErrNotFound wrapped in multierr
70+
// - Double Quotes: "\"" this is for parsing %q and %#v formating
71+
const cidBreakSet = " \t\n\r\v\f;\""
72+
7973
//lint:ignore ST1008 using error as values
8074
func parseIPLDErrNotFound(msg string) (error, bool) {
8175
// The patern we search for is:
@@ -99,9 +93,9 @@ func parseIPLDErrNotFound(msg string) (error, bool) {
9993
c = cid.Undef
10094
postIndex = len("node")
10195
} else {
102-
// Assume that CIDs only contain a-zA-Z0-9 characters.
103-
// This is true because go-ipld-format use go-cid#Cid.String which use base{3{2,6},58}.
104-
postIndex = strings.IndexFunc(msgPostKey, notAsciiLetterOrDigits)
96+
postIndex = strings.IndexFunc(msgPostKey, func(r rune) bool {
97+
return strings.ContainsAny(string(r), cidBreakSet)
98+
})
10599
if postIndex < 0 {
106100
postIndex = len(msgPostKey)
107101
}

errors_test.go

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package httpapi
33
import (
44
"errors"
55
"fmt"
6-
"strings"
76
"testing"
87

98
"github.com/ipfs/go-cid"
@@ -36,12 +35,17 @@ func TestParseIPLDNotFound(t *testing.T) {
3635
t.Errorf("expected empty string to give no error; got %T %q", err, err.Error())
3736
}
3837

39-
for _, wrap := range [...]string{
38+
cidBreaks := make([]string, len(cidBreakSet))
39+
for i, v := range cidBreakSet {
40+
cidBreaks[i] = "%w" + string(v)
41+
}
42+
43+
for _, wrap := range append(cidBreaks,
4044
"",
4145
"merkledag: %w",
4246
"testing: %w the test",
4347
"%w is wrong",
44-
} {
48+
) {
4549
for _, err := range [...]error{
4650
errors.New("ipld: could not find "),
4751
errors.New("ipld: could not find Bad_CID"),
@@ -79,11 +83,3 @@ func TestBlockstoreNotFoundMatchingIPLDErrNotFound(t *testing.T) {
7983
doParseIpldNotFoundTest(t, err)
8084
}
8185
}
82-
83-
func TestNotAsciiLetterOrDigits(t *testing.T) {
84-
for i := rune(0); i <= 256; i++ {
85-
if notAsciiLetterOrDigits(i) != !strings.ContainsAny(string(i), "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") {
86-
t.Errorf("%q is incorrectly identified", i)
87-
}
88-
}
89-
}

0 commit comments

Comments
 (0)