Skip to main content
Version: Next

Cadence Security Best Practices

This is an opinionated list of best practices Cadence developers should follow to write more secure Cadence code.

Some practices listed below might overlap with advice in the Cadence Anti-Patterns section, which is a recommended read as well.

References

References are ephemeral values and cannot be stored. If persistence is required, store a capability and borrow it when needed.

When exposing functionality, provide the least access necessary. When creating an authorized reference, create it with only the minimal set of entitlements required to achieve the desired functionality.

Account Storage

Don't trust a users’ account storage. Users have full control over their data and may reorganize it as they see fit. Users may store values in any path, so paths may store values of “unexpected” types. These values may be instances of types in contracts that the user deployed.

Always borrow with the specific type that is expected. Or, check if the value is an instance of the expected type.

Auth Accounts

Access to an AuthAccount gives full access to the account's storage, keys, and contracts. Therefore, avoid using AuthAccount as a function parameter unless absolutely necessary.

It is preferable to use capabilities over direct AuthAccount storage when exposing account data. Using capabilities allows the revocation of access by unlinking and limits the access to a single value with a certain set of functionality – access to an AuthAccount gives full access to the whole storage, as well as key and contract management.

Capabilities

Don't issue and publish capabilities unless really required. Anyone can access capabilities that are published. If public access is needed, follow the principle of least privilege/authority: Make sure that the capability type only grants access to the fields and functions that should be exposed, and nothing else. Ideally, create a capability with a reference type that is unauthorized.

If an entitlement is necessary to access the field or function, ensure it is only used for the particular field or function, and not also by other fields and functions. If needed, introduce a new, fine-grained entitlement.

When linking a capability, the link might already be present. In that case, Cadence will not panic with a runtime error but the link function will return nil.

It is a good practice to check if the link already exists with getLinkTarget before creating it. This function will return nil if the link does not exist.

If it is necessary to handle the case where borrowing a capability might fail, the account.check function can be used to verify that the target exists and has a valid type.

Ensure capabilities cannot be accessed by unauthorized parties. For example, capabilities should not be accessible through a public field, including public dictionaries or arrays. Exposing a capability in such a way allows anyone to borrow it and perform all actions that the capability allows.

Transactions

Audits of Cadence code should also include transactions, as they may contain arbitrary code, just, like in contracts. In addition, they are given full access to the accounts of the transaction’s signers, i.e. the transaction is allowed to manipulate the signers’ account storage, contracts, and keys.

Signing a transaction gives access to the AuthAccount, i.e. full access to the account’s storage, keys, and contracts.

Do not blindly sign a transaction. The transaction could for example change deployed contracts by upgrading them with malicious statements, revoking or adding keys, transferring resources from storage, etc.

Types

Use intersection types and interfaces. Always use the most specific type possible, following the principle of least privilege. Types should always be as restrictive as possible, especially for resource types.

If given a less-specific type, cast to the more specific type that is expected. For example, when implementing the fungible token standard, a user may deposit any fungible token, so the implementation should cast to the expected concrete fungible token type.

Access Control

Declaring a field as access(all) only protects from replacing the field’s value, but the value itself can still be mutated if it is mutable. Remember that containers, like dictionaries, and arrays, are mutable.

Prefer non-public access to a mutable state. That state may also be nested. For example, a child may still be mutated even if its parent exposes it through a field with non-settable access.

Do not use the access(all) modifier on fields unless necessary. Prefer access(self), or access(contract) and access(account) when other types in the contract or account need to have access, and entitlement-based access for other cases.