Skip to content

Fix giveOwnership() always-nil issue and nil dereference in removeOwned()#181

Merged
joshuahannan merged 3 commits into
mainfrom
fix/report-findings-1-2
Apr 9, 2026
Merged

Fix giveOwnership() always-nil issue and nil dereference in removeOwned()#181
joshuahannan merged 3 commits into
mainfrom
fix/report-findings-1-2

Conversation

@joshuahannan

@joshuahannan joshuahannan commented Apr 7, 2026

Copy link
Copy Markdown
Contributor
  • Capture `previousOwner` before calling `seal()` in `giveOwnership` — previously `getOwner()` was called after `seal()` set `currentlyOwned = false`, causing `previousOwner` to always be emitted as `nil` in the `OwnershipGranted` event
  • Capture `uuid` before calling `seal()` in `Manager.removeOwned` — previously `uuid` was read after `seal()` could invalidate the capability, causing a force-unwrap panic on `id!` when the capability was invalid; replaced with an explicit panic message via `??`
  • Restrict `parents`, `pendingOwner`, `acctOwner`, and `currentlyOwned` fields on `OwnedAccount` from `access(all)` to `access(self)` — public getter methods and interface conformances already provide all necessary read access
  • Restrict `factories` field on `CapabilityFactory.Manager` from `access(all)` to `access(self)` — external callers use the `Getter` interface which already exposes `getFactory` and `getSupportedTypes`

@joshuahannan joshuahannan changed the title Fix TOCTOU in giveOwnership and nil dereference in removeOwned Fix giveOwnership() always-nil issue and nil dereference in removeOwned() Apr 7, 2026
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

access(all) resource Manager: Getter {
/// Mapping of Factories indexed on Type of Capability they retrieve
access(all) let factories: {Type: {CapabilityFactory.Factory}}
access(self) let factories: {Type: {CapabilityFactory.Factory}}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: IMO this change is unnecessarily restrictive and prevents things like borrowing a reference to the dictionary/directly iterating values

…let (#182)

- Replace \`self.factory.borrow()!\` force-unwrap in \`getCapability\`
and \`getPublicCapability\` with \`if let\` — an invalidated factory
capability now returns \`nil\` instead of panicking
- Replace \`self.filter.borrow()!\` force-unwrap in \`getCapability\`
with an optional chain — an invalidated filter fails closed (returns
\`false\`) rather than panicking and bricking the entire
\`ChildAccount\`
@joshuahannan joshuahannan merged commit 86a1d03 into main Apr 9, 2026
1 check passed
@joshuahannan joshuahannan deleted the fix/report-findings-1-2 branch April 9, 2026 19:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants