Skip to content

Commit

Permalink
Ensure wildcard subject object IDs are not used with non-empty relations
Browse files Browse the repository at this point in the history
  • Loading branch information
josephschorr committed Dec 20, 2021
1 parent 4126c5f commit 6f78067
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
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 {
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())
}

0 comments on commit 6f78067

Please sign in to comment.