-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The default derives function has very basic loose equality checks on caveat fields. Turns out this doesn't allow the service to be invoked for most of the defined capabilities when the issuer has been delegated a capability (i.e. when not using the service key to self sign the invocation). When using a delegated capability the derives function is called to figure out if you have violated any constraints. Luckily we didn't expose this publically and we have been using the service key to sign invocations so this hasn't come up yet.
- Loading branch information
Showing
5 changed files
with
231 additions
and
11 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { fail, ok } from '@ucanto/validator' | ||
import * as Bytes from 'multiformats/bytes' | ||
import { base58btc } from 'multiformats/bases/base58' | ||
|
||
/** @import * as API from '@ucanto/interface' */ | ||
|
||
/** | ||
* Checks that `with` on claimed capability is the same as `with` | ||
* in delegated capability, or starts with the same string if the delegated | ||
* capability is a wildcard. Note this will ignore `can` field. | ||
* | ||
* @param {API.ParsedCapability} claimed | ||
* @param {API.ParsedCapability} delegated | ||
*/ | ||
export const equalWith = (claimed, delegated) => { | ||
if (delegated.with.endsWith('*')) { | ||
if (!claimed.with.startsWith(delegated.with.slice(0, -1))) { | ||
return fail(`Resource ${claimed.with} does not match delegated ${delegated.with}`) | ||
} | ||
} | ||
if (claimed.with !== delegated.with) { | ||
return fail(`Can not derive ${claimed.can} with ${claimed.with} from ${delegated.with}`) | ||
} | ||
return ok({}) | ||
} | ||
|
||
/** | ||
* @param {unknown} claimed | ||
* @param {unknown} delegated | ||
* @param {string} constraint | ||
*/ | ||
export const equal = (claimed, delegated, constraint) => { | ||
if (String(claimed) !== String(delegated)) { | ||
return fail(`${constraint}: ${claimed} violates ${delegated}`) | ||
} | ||
return ok({}) | ||
} | ||
|
||
/** @param {import('multiformats').Link<unknown, number, number, 0|1>|{digest: Uint8Array}} linkOrDigest */ | ||
const toDigestBytes = (linkOrDigest) => | ||
'multihash' in linkOrDigest | ||
? linkOrDigest.multihash.bytes | ||
: linkOrDigest.digest | ||
|
||
/** | ||
* @template {API.ParsedCapability<API.Ability, API.URI, { content?: API.UnknownLink | { digest: Uint8Array } }>} T | ||
* @param {T} claimed | ||
* @param {T} delegated | ||
* @returns {API.Result<{}, API.Failure>} | ||
*/ | ||
export const equalLinkOrDigestContent = (claimed, delegated) => { | ||
if (delegated.nb.content) { | ||
const delegatedBytes = toDigestBytes(delegated.nb.content) | ||
if (!claimed.nb.content) { | ||
return fail(`content: undefined violates ${base58btc.encode(delegatedBytes)}`) | ||
} | ||
const claimedBytes = toDigestBytes(claimed.nb.content) | ||
if (!Bytes.equals(claimedBytes, delegatedBytes)) { | ||
return fail(`content: ${base58btc.encode(claimedBytes)} violates ${base58btc.encode(delegatedBytes)}`) | ||
} | ||
} | ||
return ok({}) | ||
} | ||
|
||
/** | ||
* @template T | ||
* @param {API.Result<T , API.Failure>} result | ||
* @returns {{error: API.Failure, ok?:undefined}|undefined} | ||
*/ | ||
export const and = (result) => (result.error ? result : undefined) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters