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

#267: Added intents depth and size limit #543

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Consensus Breaking Changes

* (shield) The depth of nesting for functions is limited to 100 levels

### Features (non-breaking)

### Bug Fixes
Expand Down
47 changes: 47 additions & 0 deletions shield/internal/validation/functions_nesting_analyzer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package validation

import (
"github.com/warden-protocol/wardenprotocol/shield/ast"
)

func AnalyzeFunctionsNesting(node *ast.Expression, depth int) int {
switch n := node.Value.(type) {
case *ast.Expression_Identifier:
return depth
case *ast.Expression_ArrayLiteral:
return analyzeElements(n.ArrayLiteral.Elements, depth)
case *ast.Expression_CallExpression:
return analyzeCallExpression(n.CallExpression, depth)
case *ast.Expression_PrefixExpression:
return analyzePrefixExpression(n.PrefixExpression, depth)
case *ast.Expression_InfixExpression:
return analyzeInfixExpression(n.InfixExpression, depth)
default:
return depth
}
}

func analyzeElements(elements []*ast.Expression, depth int) int {
var currentMaxDepth = depth
for _, elem := range elements {
possibleMaxDepth := AnalyzeFunctionsNesting(elem, depth)
currentMaxDepth = max(currentMaxDepth, possibleMaxDepth)
}
return currentMaxDepth
}

func analyzePrefixExpression(prefix *ast.PrefixExpression, depth int) int {
newMaxDepth := AnalyzeFunctionsNesting(prefix.Right, depth)
return newMaxDepth
}

func analyzeInfixExpression(infix *ast.InfixExpression, depth int) int {
maxDepthLeft := AnalyzeFunctionsNesting(infix.Left, depth)
maxDepthRight := AnalyzeFunctionsNesting(infix.Right, depth)

return max(maxDepthLeft, maxDepthRight)
}

func analyzeCallExpression(call *ast.CallExpression, depth int) int {
return analyzeElements(call.Arguments, depth+1)
}
16 changes: 16 additions & 0 deletions shield/internal/validation/validation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package validation

import (
"fmt"
"github.com/warden-protocol/wardenprotocol/shield/ast"
)

func Validate(root *ast.Expression, maxNestingDepth int) error {
maxDepth := AnalyzeFunctionsNesting(root, 0)

if maxDepth > maxNestingDepth {
return fmt.Errorf("max allowed functions nesting depth is %d. Got %d", maxNestingDepth, maxDepth)
}

return nil
}
81 changes: 81 additions & 0 deletions shield/internal/validation/validation_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package validation

import (
"testing"

"github.com/stretchr/testify/require"
"github.com/warden-protocol/wardenprotocol/shield/ast"
"github.com/warden-protocol/wardenprotocol/shield/internal/lexer"
"github.com/warden-protocol/wardenprotocol/shield/internal/parser"
)

func parseExpression(t *testing.T, input string) *ast.Expression {
l := lexer.New(input)
p := parser.New(l)
expression := p.Parse()

err := p.Errors()
if len(err) != 0 {
require.FailNow(t, "Parser finished with errors", err)
}

require.NotNil(t, expression)
return expression
}

func TestDepthAnalyzer(t *testing.T) {
testCases := []struct {
expression string
expectedDepth int
}{
{"foo1(foo2([foo2(), 11, 12]), false || true, [false, [10, 11, 12]])", 3},
{"foo2() && foo1(foo2(), false || true, [false, [10, 11, 12]])", 2},
{"foo2() ", 1},
{"true", 0},
}

for _, tc := range testCases {
expression := parseExpression(t, tc.expression)
maxDepth := AnalyzeFunctionsNesting(expression, 0)

require.Equal(t, tc.expectedDepth, maxDepth)
}
}

func TestValidatorShouldFail(t *testing.T) {
testCases := []struct {
expression string
expectedDepth int
}{
{"foo1(foo2([foo2(), 11, 12]), false || true, [false, [10, 11, 12]])", 2},
{"foo2() && foo1(foo2(), false || true, [false, [10, 11, 12]])", 1},
{"foo2() ", 0},
}

for _, tc := range testCases {
expression := parseExpression(t, tc.expression)
err := Validate(expression, tc.expectedDepth)

require.Error(t, err)
}
}

func TestValidatorShouldSuccess(t *testing.T) {
testCases := []struct {
expression string
expectedDepth int
}{
{"foo1(foo2([foo2(), 11, 12]))", 3},
{"foo1(foo2([foo2(), 11, 12]))", 4},
{"foo2() ", 1},
{"foo2() ", 2},
{"true", 0},
}

for _, tc := range testCases {
expression := parseExpression(t, tc.expression)
err := Validate(expression, tc.expectedDepth)

require.NoError(t, err)
}
}
8 changes: 8 additions & 0 deletions shield/shield.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,15 @@ import (
"github.com/warden-protocol/wardenprotocol/shield/internal/metadata"
"github.com/warden-protocol/wardenprotocol/shield/internal/parser"
"github.com/warden-protocol/wardenprotocol/shield/internal/preprocess"
"github.com/warden-protocol/wardenprotocol/shield/internal/validation"
"github.com/warden-protocol/wardenprotocol/shield/object"
)

type Environment = env.Environment

// TODO AT: Move to Env or Config?
const MaxNestingDepth = 100

// Parse parses the input string and returns the root node of the AST.
// In case of syntax errors, it returns an error.
func Parse(input string) (*ast.Expression, error) {
Expand All @@ -26,6 +30,10 @@ func Parse(input string) (*ast.Expression, error) {
return nil, fmt.Errorf("parser errors: %v", p.Errors())
}

if err := validation.Validate(root, MaxNestingDepth); err != nil {
return nil, fmt.Errorf("parser validation error: %v", err)
}

return root, nil
}

Expand Down
Loading