Skip to main content

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.

Authorized references (references with the auth keyword) allow downcasting, e.g. a restricted type to its unrestricted type and should only be used in some specific cases.

When exposing functionality, provide the least access necessary. Do not use authorized references, as they can be downcasted, potentially allowing a user to gain access to supposedly restricted functionality. For example, the fungible token standard provides an interface to get the balance of a vault, without exposing the withdrawal functionality.

Be aware that the subtype or unrestricted type could expose functionality that was not intended to be exposed. Do not use authorized references when exposing functionality. For example, the fungible token standard provides an interface to get the balance of a vault, without exposing the withdrawal 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 store anything under the public capability storage unless strictly required. Anyone can access your public capability using AuthAccount.getCapability. If something needs to be stored under /public/, make sure only read functionality is provided by restricting its type using either a resource interface or struct interface.

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 restricted 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 pub/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 pub/access(all) modifier on fields and functions unless necessary. Prefer priv/access(self), or access(contract) and access(account) when other types in the contract or account need to have access.