Skip to content

Commit cf519b5

Browse files
committed
Prevent refreshed zcap expiry from exceeding parent expiry, if any.
1 parent 76cbee7 commit cf519b5

2 files changed

Lines changed: 20 additions & 6 deletions

File tree

lib/refreshedZcapCache.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@ bedrock.events.on('bedrock.init', async () => {
1717
REFRESHED_ZCAP_CACHE = new LruCache(cfg.caches.refreshedZcap);
1818
});
1919

20-
export async function getRefreshedZcap({profileId, capability}) {
20+
export async function getRefreshedZcap({
21+
profileId, capability, capabilityChain
22+
}) {
2123
const key = _createCacheKey({profileId, capability});
22-
const fn = () => _getUncached({profileId, capability});
24+
const fn = () => _getUncached({profileId, capability, capabilityChain});
2325
return REFRESHED_ZCAP_CACHE.memoize({key, fn});
2426
}
2527

@@ -50,7 +52,7 @@ async function _getZcapPolicy({profileId, delegateId}) {
5052
}
5153
}
5254

53-
async function _getUncached({profileId, capability}) {
55+
async function _getUncached({profileId, capability, capabilityChain}) {
5456
// get the policy for the profile + controller (delegate)
5557
const {controller: delegateId} = capability;
5658
const {policy} = await _getZcapPolicy({profileId, delegateId});
@@ -92,10 +94,19 @@ async function _getUncached({profileId, capability}) {
9294

9395
// compute new `expires` from policy, defaulting to max delegation TTL
9496
const now = Date.now();
95-
const expires = new Date(
97+
let expires = new Date(
9698
now + (policy.refresh?.constraints?.maxDelegationTtl ??
9799
authorizeZcapInvocationOptions.maxDelegationTtl));
98100

101+
// ensure policy computed expiry doesn't exceed parent capability
102+
const parentZcap = capabilityChain.at(-2);
103+
if(parentZcap.expires !== undefined) {
104+
const parentExpires = new Date(parentZcap.expires);
105+
if(expires > parentExpires) {
106+
expires = parentExpires;
107+
}
108+
}
109+
99110
return profileAgents.refreshCapability({
100111
capability, profileSigner, now, expires
101112
});

lib/zcaps.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,11 @@ bedrock.events.on('bedrock-express.configure.routes', app => {
151151
middleware.verifyRefreshableZcapDelegation(),
152152
asyncHandler(async (req, res) => {
153153
const {profileId} = req.params;
154-
const {body: capability} = req;
155-
const zcap = await getRefreshedZcap({profileId, capability});
154+
const {body: capability, verifyRefreshableZcapDelegation} = req;
155+
const {capabilityChain} = verifyRefreshableZcapDelegation;
156+
const zcap = await getRefreshedZcap({
157+
profileId, capability, capabilityChain
158+
});
156159
res.json(zcap);
157160
}));
158161

0 commit comments

Comments
 (0)