Skip to content

Commit

Permalink
move args int validation to their creation
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiobozzo committed Dec 2, 2024
1 parent 105323b commit 56eab75
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 5 deletions.
5 changes: 5 additions & 0 deletions pkg/args/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/ipld/go-ipld-prime/node/basicnode"
"github.com/ipld/go-ipld-prime/printer"

"github.com/ucan-wg/go-ucan/pkg/policy/limits"
"github.com/ucan-wg/go-ucan/pkg/policy/literal"
)

Expand Down Expand Up @@ -62,6 +63,10 @@ func (a *Args) Add(key string, val any) error {
return err
}

if err := limits.ValidateIntegerBoundsIPLD(node); err != nil {
return fmt.Errorf("value for key %q: %w", key, err)
}

a.Values[key] = node
a.Keys = append(a.Keys, key)

Expand Down
66 changes: 66 additions & 0 deletions pkg/args/args_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/stretchr/testify/require"

"github.com/ucan-wg/go-ucan/pkg/args"
"github.com/ucan-wg/go-ucan/pkg/policy/limits"
"github.com/ucan-wg/go-ucan/pkg/policy/literal"
)

Expand Down Expand Up @@ -185,6 +186,71 @@ func TestInclude(t *testing.T) {
}, maps.Collect(a1.Iter()))
}

func TestArgsIntegerBounds(t *testing.T) {
t.Parallel()

tests := []struct {
name string
key string
val int64
wantErr string
}{
{
name: "valid int",
key: "valid",
val: 42,
},
{
name: "max safe integer",
key: "max",
val: limits.MaxInt53,
},
{
name: "min safe integer",
key: "min",
val: limits.MinInt53,
},
{
name: "exceeds max safe integer",
key: "tooBig",
val: limits.MaxInt53 + 1,
wantErr: "exceeds safe integer bounds",
},
{
name: "below min safe integer",
key: "tooSmall",
val: limits.MinInt53 - 1,
wantErr: "exceeds safe integer bounds",
},
{
name: "duplicate key",
key: "duplicate",
val: 42,
wantErr: "duplicate key",
},
}

a := args.New()
require.NoError(t, a.Add("duplicate", 1)) // tests duplicate key

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := a.Add(tt.key, tt.val)
if tt.wantErr != "" {
require.Error(t, err)
require.Contains(t, err.Error(), tt.wantErr)
} else {
require.NoError(t, err)
val, err := a.GetNode(tt.key)
require.NoError(t, err)
i, err := val.AsInt()
require.NoError(t, err)
require.Equal(t, tt.val, i)
}
})
}
}

const (
argsSchema = "type Args { String : Any }"
argsName = "Args"
Expand Down
5 changes: 0 additions & 5 deletions pkg/policy/selector/selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"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/ucan-wg/go-ucan/pkg/policy/limits"
)

// Selector describes a UCAN policy selector, as specified here:
Expand All @@ -23,10 +22,6 @@ type Selector []segment
// - a resolutionerr error if not being able to resolve to a node
// - nil and no errors, if the selector couldn't match on an optional segment (with ?).
func (s Selector) Select(subject ipld.Node) (ipld.Node, error) {
if err := limits.ValidateIntegerBoundsIPLD(subject); err != nil {
return nil, fmt.Errorf("node contains integer values outside safe bounds: %w", err)
}

return resolve(s, subject, nil)
}

Expand Down

0 comments on commit 56eab75

Please sign in to comment.