8000 chore: Handle empty policies in the parser by charithe · Pull Request #2530 · cerbos/cerbos · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

chore: Handle empty policies in the parser #2530

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

Merged
merged 1 commit into from
Mar 25, 2025
Merged
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
14 changes: 5 additions & 9 deletions internal/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,11 @@ func UnmarshalBytes[T proto.Message](contents []byte, factory func() T, opts ...
}

if len(f.Docs) == 0 {
if contentLen > 0 {
// Special case for unterminated strings. See test case 20.
return nil, nil, NewUnmarshalError(&sourcev1.Error{
Kind: sourcev1.Error_KIND_PARSE_ERROR,
Message: "invalid document: contents are not valid YAML or JSON",
Position: &sourcev1.Position{Line: 1, Column: 1, Path: "$"},
})
}
return nil, nil, nil
return nil, nil, NewUnmarshalError(&sourcev1.Error{
Kind: sourcev1.Error_KIND_PARSE_ERROR,
Message: "invalid document: contents are not valid YAML or JSON",
Position: &sourcev1.Position{Line: 1, Column: 1, Path: "$"},
})
}

u := &unmarshaler[T]{unmarshalOpts: unmarshalOpts{}}
Expand Down
8 changes: 7 additions & 1 deletion internal/policy/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"github.com/cerbos/cerbos/internal/parser"
)

var errEmptyPolicy = errors.New("policy is empty")

type ValidationError struct {
Err *sourcev1.Error
}
Expand All @@ -30,7 +32,11 @@ func (ve ValidationError) Error() string {
}

func Validate(p *policyv1.Policy, sc parser.SourceCtx) error {
switch pt := p.PolicyType.(type) {
if p == nil {
return errEmptyPolicy
}

switch pt := p.GetPolicyType().(type) {
case *policyv1.Policy_ResourcePolicy:
return validateResourcePolicy(pt.ResourcePolicy, sc)
case *policyv1.Policy_PrincipalPolicy:
Expand Down
Loading
0