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

fix: check groupnames #638

Merged
merged 4 commits into from
Feb 14, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
19 changes: 19 additions & 0 deletions packages/core/src/modules/proofs/services/ProofService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,9 @@ export class ProofService {
comment?: string
}
): Promise<ProofProtocolMsgReturnType<RequestPresentationMessage>> {
// Check if Attribute names match Predicate names
this.checkAttributePredicateMatch(proofRequest)

// Assert
proofRecord.assertState(ProofState.ProposalReceived)

Expand Down Expand Up @@ -299,6 +302,9 @@ export class ProofService {
): Promise<ProofProtocolMsgReturnType<RequestPresentationMessage>> {
this.logger.debug(`Creating proof request`)

// Check if Attribute names match Predicate names
this.checkAttributePredicateMatch(proofRequest)

// Assert
connectionRecord?.assertReady()

Expand Down Expand Up @@ -360,6 +366,9 @@ export class ProofService {
}
await validateOrReject(proofRequest)

// Check if Attribute names match Predicate names
this.checkAttributePredicateMatch(proofRequest)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment makes me believe we want them to match.

Maybe something like:

Suggested change
// Check if Attribute names match Predicate names
this.checkAttributePredicateMatch(proofRequest)
// Assert attribute and predicate group names do not match
this.assertAttributePredicateGroupNamesDoNotMatch(proofRequest)

Not sure if doNotMatch is the best, but is is explicit


this.logger.debug('received proof request', proofRequest)

try {
Expand Down Expand Up @@ -1037,6 +1046,16 @@ export class ProofService {

return credentialDefinitions
}

public checkAttributePredicateMatch(proofRequest: ProofRequest) {
for (const attribute of proofRequest.requestedAttributes.keys()) {
for (const predicate of proofRequest.requestedPredicates.keys()) {
if (attribute === predicate) {
throw new AriesFrameworkError(`The proof request contains attributes that match the predicates: ${attribute}`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
throw new AriesFrameworkError(`The proof request contains attributes that match the predicates: ${attribute}`)
throw new AriesFrameworkError(`The proof request contains an attribute group name that matches a predicate group name: ${attribute}`)

}
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For each key it will loop through the keys of the other array. Maybe we can use .includes here?

Suggested change
for (const attribute of proofRequest.requestedAttributes.keys()) {
for (const predicate of proofRequest.requestedPredicates.keys()) {
if (attribute === predicate) {
throw new AriesFrameworkError(`The proof request contains attributes that match the predicates: ${attribute}`)
}
}
}
}
const predicateGroupNames = proofRequest.requestedPredicates.keys()
for (const attribute of proofRequest.requestedAttributes.keys()) {
if (predicateGroupNames.includes(attribute)) {
throw new AriesFrameworkError(`The proof request contains attributes that match the predicates: ${attribute}`)
}
}
}

}

export interface ProofRequestTemplate {
Expand Down
36 changes: 36 additions & 0 deletions packages/core/tests/proofs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,4 +331,40 @@ describe('Present Proof', () => {
presentationMessage: expect.any(PresentationMessage),
})
})

test('Faber starts with proof request to Alice, but the attributes match with the predicates', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
test('Faber starts with proof request to Alice, but the attributes match with the predicates', async () => {
test('Faber starts with proof request to Alice, but an attribute group name matches with a predicate group name so an error is thrown', async () => {

// Age attribute
const attributes = {
age: new ProofAttributeInfo({
name: 'age',
restrictions: [
new AttributeFilter({
credentialDefinitionId: credDefId,
}),
],
}),
}

// Age predicate
const predicates = {
age: new ProofPredicateInfo({
name: 'age',
predicateType: PredicateType.GreaterThanOrEqualTo,
predicateValue: 50,
restrictions: [
new AttributeFilter({
credentialDefinitionId: credDefId,
}),
],
}),
}

await expect(
faberAgent.proofs.requestProof(faberConnection.id, {
name: 'test-proof-request',
requestedAttributes: attributes,
requestedPredicates: predicates,
})
).rejects.toThrowError(`The proof request contains attributes that match the predicates: age`)
})
})