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

Ensure wildcard subject object IDs are not used with non-empty relations #47

Merged
merged 2 commits into from Dec 20, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 30 additions & 0 deletions proto/authzed/api/v1/00_handwritten_validation.go
Expand Up @@ -9,6 +9,9 @@ func (m *CheckPermissionRequest) HandwrittenValidate() error {
reason: "alphanumeric value is required",
}
}
if m.GetSubject() != nil {
jzelinskie marked this conversation as resolved.
Show resolved Hide resolved
return m.GetSubject().HandwrittenValidate()
}

return nil
}
Expand Down Expand Up @@ -39,6 +42,19 @@ func (m *RelationshipFilter) HandwrittenValidate() error {
reason: "alphanumeric value is required",
}
}
if m.GetOptionalSubjectFilter() != nil {
return m.GetOptionalSubjectFilter().HandwrittenValidate()
}
return nil
}

func (m *SubjectFilter) HandwrittenValidate() error {
if m.GetOptionalSubjectId() == "*" && m.GetOptionalRelation() != nil && m.GetOptionalRelation().GetRelation() != "" {
return SubjectFilterValidationError{
field: "OptionalRelation",
reason: "optionalrelation must be empty on subject if object ID is a wildcard",
}
}
return nil
}

Expand All @@ -49,6 +65,16 @@ func (m *RelationshipUpdate) HandwrittenValidate() error {
return nil
}

func (m *SubjectReference) HandwrittenValidate() error {
if m.GetObject() != nil && m.GetObject().GetObjectId() == "*" && m.GetOptionalRelation() != "" {
return SubjectReferenceValidationError{
field: "OptionalRelation",
reason: "optionalrelation must be empty on subject if object ID is a wildcard",
}
}
return nil
}

func (m *Relationship) HandwrittenValidate() error {
if m.GetResource() != nil && m.GetResource().GetObjectId() == "*" {
return ObjectReferenceValidationError{
Expand All @@ -57,6 +83,10 @@ func (m *Relationship) HandwrittenValidate() error {
}
}

if m.GetSubject() != nil {
return m.GetSubject().HandwrittenValidate()
}

return nil
}

Expand Down
23 changes: 23 additions & 0 deletions proto/authzed/api/validation_test/tuples_test.go
Expand Up @@ -389,3 +389,26 @@ func TestV1CoreObjectValidity(t *testing.T) {
}
}
}

func TestWildcardSubjectRelation(t *testing.T) {
subjObjRef := &v1.ObjectReference{
ObjectType: "somenamespace",
ObjectId: "*",
}
subRef := &v1.SubjectReference{
Object: subjObjRef,
OptionalRelation: "somerelation",
}
require.Error(t, subRef.HandwrittenValidate())
}

func TestWildcardSubjectRelationEmpty(t *testing.T) {
subjObjRef := &v1.ObjectReference{
ObjectType: "somenamespace",
ObjectId: "*",
}
subRef := &v1.SubjectReference{
Object: subjObjRef,
}
require.NoError(t, subRef.HandwrittenValidate())
}