Skip to content

Commit

Permalink
PR(IDENTITY-FIX): Fix Panic On Private Doc Update
Browse files Browse the repository at this point in the history
This was  bug that caused a panic when a request without an identity tried
to update a document that was private (registered with an identity).

The test that asserts the fix is in the next few commits. The test name
is called: `TestACP_CreateWithIdentityAndUpdateWithoutIdentity_CanNotUpdate`
  • Loading branch information
shahzadlone committed Mar 11, 2024
1 parent 9973a69 commit bcd75fc
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 1 deletion.
2 changes: 2 additions & 0 deletions client/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,8 @@ type Collection interface {
// Get returns the document with the given DocID.
//
// Returns an ErrDocumentNotFound if a document matching the given DocID is not found.
//
// Returns an ErrDocumentNotAccessible if DocID is inaccessible.
Get(
ctx context.Context,
identity immutable.Option[string],
Expand Down
1 change: 1 addition & 0 deletions client/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ var (
ErrFieldNotObject = errors.New("trying to access field on a non object type")
ErrValueTypeMismatch = errors.New("value does not match indicated type")
ErrDocumentNotFound = errors.New("no document for the given ID exists")
ErrDocumentNotAccessible = errors.New("document is not accessible")
ErrPolicyAddFailureACPModuleNotFound = errors.New("failure adding policy because ACP module was not found")
ErrInvalidACPPermToDeleteDocument = errors.New("invalid acp permission to delete the document")
ErrInvalidACPPermToUpdateDocument = errors.New("invalid acp permission to update the document")
Expand Down
5 changes: 5 additions & 0 deletions db/collection_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ func (c *collection) Get(
if err != nil {
return nil, err
}

if doc == nil {
return nil, client.ErrDocumentNotAccessible
}

return doc, c.commitImplicitTxn(ctx, txn)
}

Expand Down
4 changes: 3 additions & 1 deletion db/collection_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,11 @@ func (c *collection) updateIndexedDoc(
if err != nil {
return err
}
// TODO-ACP: https://github.com/sourcenetwork/defradb/issues/2365 - ACP <> Indexing, possibly also check
// and handle the case of when oldDoc == nil (will be nil if inaccessible document).
oldDoc, err := c.get(
ctx,
acpIdentity.NoIdentity, // TODO-ACP: https://github.com/sourcenetwork/defradb/issues/2365 - ACP <> Indexing
acpIdentity.NoIdentity,
txn,
c.getPrimaryKeyFromDocID(doc.ID()), c.Definition().CollectIndexedFields(),
false,
Expand Down

0 comments on commit bcd75fc

Please sign in to comment.