Skip to content

Commit

Permalink
fix: exponential scan (#329)
Browse files Browse the repository at this point in the history
This fixes the problem caused by the attestation validator when more than one attestation is included in the proof chain.
  • Loading branch information
Gozala authored Nov 16, 2023
1 parent 794f72b commit a81c8d5
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 2 deletions.
12 changes: 10 additions & 2 deletions packages/validator/src/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -553,8 +553,16 @@ const verifySession = async (delegation, proofs, config) => {

return await claim(
attestation,
// We omit the delegation otherwise we may end up in an infinite loop
proofs.filter(proof => proof != delegation),
// We only consider attestations otherwise we will end up doing an
// exponential scan if there are other proofs that require attestations.
proofs.filter(isAttestation),
config
)
}

/**
* Checks if the delegation is an attestation.
*
* @param {API.Delegation} proof
*/
const isAttestation = proof => proof.capabilities[0]?.can === 'ucan/attest'
60 changes: 60 additions & 0 deletions packages/validator/test/session.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -447,3 +447,63 @@ test('service can not delegate account resource', async () => {

assert.equal(!result.ok, true)
})

test('redundant proofs have no impact', async () => {
const account = Absentee.from({ id: 'did:mailto:web.mail:alice' })
const pairs = 6
const logins = await Promise.all(
Array(pairs)
.fill(0)
.map((_, n) =>
Delegation.delegate({
issuer: account,
audience: alice,
capabilities: [
{
can: '*',
with: 'ucan:*',
},
],
expiration: Infinity,
nonce: `${n}`,
})
)
)

const expiration = Core.UCAN.now() + 60 * 60 * 24 * 365 // 1 year
const attestations = await Promise.all(
logins.map(login =>
Delegation.delegate({
issuer: w3,
audience: alice,
capabilities: [
{
with: w3.did(),
can: 'ucan/attest',
nb: { proof: login.cid },
},
],
expiration,
})
)
)

const proofs = [...logins, ...attestations]

const request = await echo.invoke({
issuer: alice,
audience: w3,
with: account.did(),
nb: { message: 'hello world' },
proofs,
})

const result = await access(await request.delegate(), {
authority: w3,
capability: echo,
principal: Verifier,
validateAuthorization: () => ({ ok: {} }),
})

assert.ok(result.ok)
})

0 comments on commit a81c8d5

Please sign in to comment.