Skip to content

Commit

Permalink
fix: Make requests with no identity work with "*" target (#3278)
Browse files Browse the repository at this point in the history
## Relevant issue(s)
Resolves #3276 

## Description
- Fix the bug where a request without an identity still wouldn't be able
to access a document even if there was a "*" relationship

## Tasks
- [x] I made sure the code is well commented, particularly
hard-to-understand areas.
- [x] I made sure the repository-held documentation is changed
accordingly.
- [x] I made sure the pull request title adheres to the conventional
commit style (the subset used in the project can be found in
[tools/configs/chglog/config.yml](tools/configs/chglog/config.yml)).
- [x] I made sure to discuss its limitations such as threats to
validity, vulnerability to mistake and misuse, robustness to
invalidation of assumptions, resource requirements, ...

## How has this been tested?
- Added tests

Specify the platform(s) on which this was tested:
- WSL2 (Manjaro)
  • Loading branch information
shahzadlone authored Dec 6, 2024
1 parent 89f9f41 commit b4a3eba
Show file tree
Hide file tree
Showing 13 changed files with 677 additions and 65 deletions.
3 changes: 2 additions & 1 deletion acp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,8 @@ Result:
Error: document not found or not authorized to access
```

Sometimes we might want to give a specific access (form a relationship) not just to one identity, but any identity.
Sometimes we might want to give a specific access (i.e. form a relationship) not just with one identity, but with
any identity (includes even requests with no-identity).
In that case we can specify "*" instead of specifying an explicit `actor`:
```sh
defradb client acp relationship add \
Expand Down
14 changes: 9 additions & 5 deletions internal/db/permission/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,22 @@ func CheckAccessOfDocOnCollectionWithACP(
return true, nil
}

// At this point if the request is not signatured, then it has no access, because:
// the collection has a policy on it, and the acp is enabled/available,
// and the document is not public (is registered with acp).
var identityValue string
if !identity.HasValue() {
return false, nil
// We can't assume that there is no-access just because there is no identity even if the document
// is registered with acp, this is because it is possible that acp has a registered relation targeting
// "*" (any) actor which would mean that even a request without an identity might be able to access
// a document registered with acp. So we pass an empty `did` to accommodate that case.
identityValue = ""
} else {
identityValue = identity.Value().DID
}

// Now actually check using the signature if this identity has access or not.
hasAccess, err := acpSystem.CheckDocAccess(
ctx,
permission,
identity.Value().DID,
identityValue,
policyID,
resourceName,
docID,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,13 @@ func TestACP_OwnerMakesAManagerThatGivesItSelfReadAndWriteAccess_GQL_ManagerCanR

Description: "Test acp, owner makes a manager that gives itself read and write access",

SupportedMutationTypes: immutable.Some([]testUtils.MutationType{
// GQL mutation will return no error when wrong identity is used so test that separately.
testUtils.GQLRequestMutationType,
}),
SupportedMutationTypes: immutable.Some(
[]testUtils.MutationType{
// GQL mutation will return no error when wrong identity is used (only for update requests),
// so test that separately.
testUtils.GQLRequestMutationType,
},
),

Actions: []any{
testUtils.AddPolicy{
Expand Down Expand Up @@ -274,10 +277,13 @@ func TestACP_OwnerMakesManagerButManagerCanNotPerformOperations_GQL_ManagerCantR

Description: "Test acp, owner makes a manager, manager can't read or write",

SupportedMutationTypes: immutable.Some([]testUtils.MutationType{
// GQL mutation will return no error when wrong identity is used so test that separately.
testUtils.GQLRequestMutationType,
}),
SupportedMutationTypes: immutable.Some(
[]testUtils.MutationType{
// GQL mutation will return no error when wrong identity is used (only for update requests),
// so test that separately.
testUtils.GQLRequestMutationType,
},
),

Actions: []any{
testUtils.AddPolicy{
Expand Down Expand Up @@ -442,10 +448,13 @@ func TestACP_ManagerAddsRelationshipWithRelationItDoesNotManageAccordingToPolicy

Description: "Test acp, manager adds relationship with relation it does not manage according to policy, error",

SupportedMutationTypes: immutable.Some([]testUtils.MutationType{
// GQL mutation will return no error when wrong identity is used so test that separately.
testUtils.GQLRequestMutationType,
}),
SupportedMutationTypes: immutable.Some(
[]testUtils.MutationType{
// GQL mutation will return no error when wrong identity is used (only for update requests),
// so test that separately.
testUtils.GQLRequestMutationType,
},
),

Actions: []any{
testUtils.AddPolicy{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -601,10 +601,12 @@ func TestACP_OwnerMakesAManagerThatGivesItSelfReadAndWriteAccess_ManagerCanReadA

Description: "Test acp, owner makes a manager that gives itself read and write access",

SupportedMutationTypes: immutable.Some([]testUtils.MutationType{
testUtils.CollectionNamedMutationType,
testUtils.CollectionSaveMutationType,
}),
SupportedMutationTypes: immutable.Some(
[]testUtils.MutationType{
// GQL mutation will return no error when wrong identity is used with gql (only for update requests),
testUtils.CollectionNamedMutationType,
testUtils.CollectionSaveMutationType,
}),

Actions: []any{
testUtils.AddPolicy{
Expand Down Expand Up @@ -849,10 +851,12 @@ func TestACP_ManagerAddsRelationshipWithRelationItDoesNotManageAccordingToPolicy

Description: "Test acp, manager adds relationship with relation it does not manage according to policy, error",

SupportedMutationTypes: immutable.Some([]testUtils.MutationType{
testUtils.CollectionNamedMutationType,
testUtils.CollectionSaveMutationType,
}),
SupportedMutationTypes: immutable.Some(
[]testUtils.MutationType{
// GQL mutation will return no error when wrong identity is used with gql (only for update requests),
testUtils.CollectionNamedMutationType,
testUtils.CollectionSaveMutationType,
}),

Actions: []any{
testUtils.AddPolicy{
Expand Down Expand Up @@ -1017,10 +1021,12 @@ func TestACP_OwnerMakesManagerButManagerCanNotPerformOperations_ManagerCantReadO

Description: "Test acp, owner makes a manager, manager can't read or write",

SupportedMutationTypes: immutable.Some([]testUtils.MutationType{
testUtils.CollectionNamedMutationType,
testUtils.CollectionSaveMutationType,
}),
SupportedMutationTypes: immutable.Some(
[]testUtils.MutationType{
// GQL mutation will return no error when wrong identity is used with gql (only for update requests),
testUtils.CollectionNamedMutationType,
testUtils.CollectionSaveMutationType,
}),

Actions: []any{
testUtils.AddPolicy{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,13 @@ func TestACP_OwnerGivesUpdateWriteAccessToAnotherActorWithoutExplicitReadPerm_GQ

Description: "Test acp, owner gives write(update) access without explicit read permission, can still update",

SupportedMutationTypes: immutable.Some([]testUtils.MutationType{
// GQL mutation will return no error when wrong identity is used so test that separately.
testUtils.GQLRequestMutationType,
}),
SupportedMutationTypes: immutable.Some(
[]testUtils.MutationType{
// GQL mutation will return no error when wrong identity is used (only for update requests),
// so test that separately.
testUtils.GQLRequestMutationType,
},
),

Actions: []any{
testUtils.AddPolicy{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ func TestACP_OwnerGivesUpdateWriteAccessToAnotherActorWithoutExplicitReadPerm_Ot

Description: "Test acp, owner gives write(update) access without explicit read permission, can still update",

SupportedMutationTypes: immutable.Some([]testUtils.MutationType{
testUtils.CollectionNamedMutationType,
testUtils.CollectionSaveMutationType,
}),
SupportedMutationTypes: immutable.Some(
[]testUtils.MutationType{
// GQL mutation will return no error when wrong identity is used with gql (only for update requests),
testUtils.CollectionNamedMutationType,
testUtils.CollectionSaveMutationType,
}),

Actions: []any{
testUtils.AddPolicy{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,13 @@ func TestACP_OwnerGivesOnlyReadAccessToAnotherActor_GQL_OtherActorCanReadButNotU

Description: "Test acp, owner gives read access to another actor, but the other actor can't update",

SupportedMutationTypes: immutable.Some([]testUtils.MutationType{
// GQL mutation will return no error when wrong identity is used so test that separately.
testUtils.GQLRequestMutationType,
}),
SupportedMutationTypes: immutable.Some(
[]testUtils.MutationType{
// GQL mutation will return no error when wrong identity is used (only for update requests),
// so test that separately.
testUtils.GQLRequestMutationType,
},
),

Actions: []any{
testUtils.AddPolicy{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -465,10 +465,11 @@ func TestACP_OwnerGivesOnlyReadAccessToAnotherActor_OtherActorCanReadButNotUpdat

Description: "Test acp, owner gives read access to another actor, but the other actor can't update",

SupportedMutationTypes: immutable.Some([]testUtils.MutationType{
testUtils.CollectionNamedMutationType,
testUtils.CollectionSaveMutationType,
}),
SupportedMutationTypes: immutable.Some(
[]testUtils.MutationType{
testUtils.CollectionNamedMutationType,
testUtils.CollectionSaveMutationType,
}),

Actions: []any{
testUtils.AddPolicy{
Expand Down
Loading

0 comments on commit b4a3eba

Please sign in to comment.