-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #89 from ucan-wg/fix/prevent-int-overflow
fix: prevent overflow of int values
- Loading branch information
Showing
16 changed files
with
514 additions
and
16 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
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,49 @@ | ||
package limits | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/ipld/go-ipld-prime" | ||
"github.com/ipld/go-ipld-prime/must" | ||
) | ||
|
||
const ( | ||
// MaxInt53 represents the maximum safe integer in JavaScript (2^53 - 1) | ||
MaxInt53 = 9007199254740991 | ||
// MinInt53 represents the minimum safe integer in JavaScript (-2^53 + 1) | ||
MinInt53 = -9007199254740991 | ||
) | ||
|
||
func ValidateIntegerBoundsIPLD(node ipld.Node) error { | ||
switch node.Kind() { | ||
case ipld.Kind_Int: | ||
val := must.Int(node) | ||
if val > MaxInt53 || val < MinInt53 { | ||
return fmt.Errorf("integer value %d exceeds safe bounds", val) | ||
} | ||
case ipld.Kind_List: | ||
it := node.ListIterator() | ||
for !it.Done() { | ||
_, v, err := it.Next() | ||
if err != nil { | ||
return err | ||
} | ||
if err := ValidateIntegerBoundsIPLD(v); err != nil { | ||
return err | ||
} | ||
} | ||
case ipld.Kind_Map: | ||
it := node.MapIterator() | ||
for !it.Done() { | ||
_, v, err := it.Next() | ||
if err != nil { | ||
return err | ||
} | ||
if err := ValidateIntegerBoundsIPLD(v); err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
|
||
return nil | ||
} |
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,82 @@ | ||
package limits | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/ipld/go-ipld-prime/datamodel" | ||
"github.com/ipld/go-ipld-prime/fluent/qp" | ||
"github.com/ipld/go-ipld-prime/node/basicnode" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestValidateIntegerBoundsIPLD(t *testing.T) { | ||
buildMap := func() datamodel.Node { | ||
nb := basicnode.Prototype.Any.NewBuilder() | ||
qp.Map(1, func(ma datamodel.MapAssembler) { | ||
qp.MapEntry(ma, "foo", qp.Int(MaxInt53+1)) | ||
})(nb) | ||
return nb.Build() | ||
} | ||
|
||
buildList := func() datamodel.Node { | ||
nb := basicnode.Prototype.Any.NewBuilder() | ||
qp.List(1, func(la datamodel.ListAssembler) { | ||
qp.ListEntry(la, qp.Int(MinInt53-1)) | ||
})(nb) | ||
return nb.Build() | ||
} | ||
|
||
tests := []struct { | ||
name string | ||
input datamodel.Node | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "valid int", | ||
input: basicnode.NewInt(42), | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "max safe int", | ||
input: basicnode.NewInt(MaxInt53), | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "min safe int", | ||
input: basicnode.NewInt(MinInt53), | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "above MaxInt53", | ||
input: basicnode.NewInt(MaxInt53 + 1), | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "below MinInt53", | ||
input: basicnode.NewInt(MinInt53 - 1), | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "nested map with invalid int", | ||
input: buildMap(), | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "nested list with invalid int", | ||
input: buildList(), | ||
wantErr: true, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
err := ValidateIntegerBoundsIPLD(tt.input) | ||
if tt.wantErr { | ||
require.Error(t, err) | ||
require.Contains(t, err.Error(), "exceeds safe bounds") | ||
} else { | ||
require.NoError(t, 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
Oops, something went wrong.