Cadence Anti-Patterns

This is an opinionated list of issues that can be improved if they are found in Cadence code intended for production.

Security and Robustness

Avoid using AuthAccount as a function parameter

Problem

Some may choose to authenticate or perform operations for their users by using the users' account addresses. In order to do this, a commonly seen case would be to pass the user's AuthAccount object as a parameter to a contract function to use for querying the account or storing objects directly. This is problematic, as the AuthAccount object allows access to ALL private areas of the account, for example, all of the user's storage, authorized keys, etc., which provides the opportunity for bad actors to take advantage of.

Example:

1
...
2
// BAD CODE
3
// DO NOT COPY
4
5
// Imagine this code is in a contract that uses AuthAccount to authenticate users
6
// To transfer NFTs
7
8
// They could deploy the contract with an Ethereum-style access control list functionality
9
10
pub fun transferNFT(id: UInt64, owner: AuthAccount) {
11
assert(owner(id) == owner.address)
12
13
transfer(id)
14
}
15
16
// But they could upgrade the function to have the same signature
17
// so it looks like it is doing the same thing, but they could also drain a little bit
18
// of FLOW from the user's vault, a totally separate piece of the account that
19
// should not be accessible in this function
20
// BAD
21
22
pub fun transferNFT(id: UInt64, owner: AuthAccount) {
23
assert(owner(id) == owner.address)
24
25
transfer(id)
26
27
// Sneakily borrow a reference to the user's Flow Token Vault
28
// and withdraw a bit of FLOW
29
// BAD
30
let vaultRef = owner.borrow<&FlowToken.Vault>(/storage/flowTokenVault)!
31
let stolenTokens <- vaultRef.withdraw(amount: 0.1)
32
33
// deposit the stolen funds in the contract owners vault
34
// BAD
35
contractVault.deposit(from: <-stolenTokens)
36
}
37
...

Solution

Projects should find other ways to authenticate users, such as using resources and capabilities as authentication objects. They should also expect to perform most storage and linking operations within transaction bodies rather than inside contract utility functions.

There are some scenarios where using an AuthAccount object is necessary, such as a cold storage multi-sig, but those cases are extremely rare and AuthAccount usage should still be avoided unless absolutely necessary.

Auth references and capabilities should be avoided

Problem

Authorized references allow downcasting restricted types to their unrestricted type and should be avoided unless necessary. The type that is being restricted could expose functionality that was not intended to be exposed. If the auth keyword is used on local variables they will be references. References are ephemeral and cannot be stored. This prevents any reference casting to be stored under account storage. Additionally, if the auth keyword is used to store a public capability, serious harm could happen since the value could be downcasted to a type that has functionality and values altered.

Example

A commonly seen pattern in NFT smart contracts is including a public borrow function that borrows an auth reference to an NFT (eg. NBA Top Shot). This allows anyone to access the stored metadata or extra fields that weren't part of the NFT standard. While generally safe in most scenarios, not all NFTs are built the same. Some NFTs may have privileged functions that shouldn't be exposed by this method, so please be cautious and mindful when imitating NFT projects that use this pattern.

Another Example

When we create a public capability for our FungibleToken.Vault we do not use an auth capability:

1
// GOOD: Create a public capability to the Vault that only exposes
2
// the balance field through the Balance interface
3
signer.link<&FlowToken.Vault{FungibleToken.Balance}>(
4
/public/flowTokenBalance,
5
target: /storage/flowTokenVault
6
)

If we were to use an authorized type for the capability, like so:

1
// BAD: Create an Authorized public capability to the Vault that only exposes
2
// the balance field through the Balance interface
3
// Authorized referenced can be downcasted to their unrestricted types, which is dangerous
4
signer.link<auth &FlowToken.Vault{FungibleToken.Balance}>(
5
/public/flowTokenBalance,
6
target: /storage/flowTokenVault
7
)

Then anyone in the network could take that restricted reference that is only supposed to expose the balance field and downcast it to expose the withdraw field and steal all our money!

1
// Exploit of the auth capability to expose withdraw
2
let balanceRef = getAccount(account)
3
.getCapability<auth &FlowToken.Vault{FungibleToken.Balance}>(/public/flowTokenBalance)
4
.borrow()!
5
6
let fullVaultRef = balanceRef as! &FlowToken.Vault
7
8
// Withdraw the newly exposed funds
9
let stolenFunds <- fullVaultRef.withdraw(amount: 1000000)

Events from resources may not be unique

Problem

Public functions in a contract can be called by anyone, e.g. any other contract or any transaction. If that function creates a resource, and that resource has functions that emit events, that means any account can create an instance of that resource and emit those events. If those events are meant to indicate actions taken using a single instance of that resource (eg. admin object, registry), or instances created through a particular workflow, it's possible that events from other instances may be mixed in with the ones you're querying for - making the event log search and management more cumbersome.

Solution

To fix this, if there should be only a single instance of the resource, it should be created and link()ed to a public path in an admin account's storage during the contracts's initializer.

Access Control

Public functions and fields should be avoided

Problem

Be sure to keep track of access modifiers when structuring your code, and make public only what should be public. Accidentally exposed fields can be a security hole.

Solution

When writing your smart contract, look at every field and function and make sure that they are all access(self), access(contract), or access(account), unless otherwise needed.

Capability-Typed public fields are a security hole

This is a specific case of "Public Functions And Fields Should Be Avoided", above.

Problem

The values of public fields can be copied. Capabilities are value types, so if they are used as a public field, anyone can copy it from the field and call the functions that it exposes. This almost certainly is not what you want if a capability has been stored as a field on a contract or resource in this way.

Solution

For public access to a capability, place it in an accounts public area so this expectation is explicit.

Array or dictionary fields should be private

This anti-pattern has been addressed with FLIP #703

Problem

This is a specific case of "Public Functions And Fields Should Be Avoided", above. Public array or dictionary fields are not directly over-writable, but their members can be accessed and overwritten if the field is public. This could potentially result in security vulnerabilities for the contract if these fields are mistakenly made public.

Ex:

1
pub contract Array {
2
// array is intended to be initialized to something constant
3
pub let shouldBeConstantArray: [Int]
4
}

Anyone could use a transaction like this to modify it:

1
import Array from 0x01
2
3
transaction {
4
execute {
5
Array.shouldbeConstantArray[0] = 1000
6
}
7
}

Solution

Make sure that any array or dictionary fields in contracts, structs, or resources are access(contract) or access(self) unless they need to be intentionally made public.

1
pub contract Array {
2
// array is inteded to be initialized to something constant
3
access(self) let shouldBeConstantArray: [Int]
4
}

Public admin resource creation functions are unsafe

This is a specific case of "Public Functions And Fields Should Be Avoided", above.

Problem

A public function on a contract that creates a resource can be called by any account. If that resource provides access to admin functions then the creation function should not be public.

Solution

To fix this, a single instance of that resource should be created in the contract's init() method, and then a new creation function can be potentially included within the admin resource, if necessary. The admin resource can then be link()ed to a private path in an admin's account storage during the contract's initializer.

Example

1
// Pseudo-code
2
3
// BAD
4
pub contract Currency {
5
pub resource Admin {
6
pub fun mintTokens()
7
}
8
9
// Anyone in the network can call this function
10
// And use the Admin resource to mint tokens
11
pub fun createAdmin(): @Admin {
12
return <-create Admin()
13
}
14
}
15
16
// This contract makes the admin creation private and in the initializer
17
// so that only the one who controls the account can mint tokens
18
// GOOD
19
pub contract Currency {
20
pub resource Admin {
21
pub fun mintTokens()
22
23
// Only an admin can create new Admins
24
pub fun createAdmin(): @Admin {
25
return <-create Admin()
26
}
27
}
28
29
init() {
30
// Create a single admin resource
31
let firstAdmin <- create Admin()
32
33
// Store it in private account storage in `init` so only the admin can use it
34
self.account.save(<-firstAdmin, to: /storage/currencyAdmin)
35
}
36
}

Do not modify smart contract state or emit events in public struct initializers

This is another example of the risks of having publicly accessible parts to your smart contract.

Problem

Data structure definitions in Cadence currently must be declared as public so that they can be used by anyone. Structs do not have the same restrictions that resources have on them, which means that anyone can create a new instance of a struct without going through any authorization.

Solution

Any contract state-modifying operations related to the creation of structs should be contained in restricted resources instead of the initializers of structs.

Example

This used to be a bug in the NBA Top Shot smart contract, so we'll use that as an example. Before, when it created a new play, it would initialize the play record with a struct, which increments the number that tracks the play IDs and emits an event:

1
// Simplified Code
2
// BAD
3
//
4
pub contract TopShot {
5
6
// The Record that is used to track every unique play ID
7
pub var nextPlayID: UInt32
8
9
pub struct Play {
10
11
pub let playID: UInt32
12
13
init() {
14
15
self.playID = TopShot.nextPlayID
16
17
// Increment the ID so that it isn't used again
18
TopShot.nextPlayID = TopShot.nextPlayID + 1
19
20
emit PlayCreated(id: self.playID, metadata: metadata)
21
}
22
}
23
}

This is a risk because anyone can create the Play struct as many times as they want, which could increment the nextPlayID field to the max UInt32 value, effectively preventing new plays from being created. It also would emit bogus events.

This bug was fixed by instead updating the contract state in the admin function that creates the plays.

1
// Update contract state in admin resource functions
2
// GOOD
3
//
4
pub contract TopShot {
5
6
// The Record that is used to track every unique play ID
7
pub var nextPlayID: UInt32
8
9
pub struct Play {
10
11
pub let playID: UInt32
12
13
init() {
14
self.playID = TopShot.nextPlayID
15
}
16
}
17
18
pub resource Admin {
19
20
// Protected within the private admin resource
21
pub fun createPlay() {
22
// Create the new Play
23
var newPlay = Play()
24
25
// Increment the ID so that it isn't used again
26
TopShot.nextPlayID = TopShot.nextPlayID + UInt32(1)
27
28
emit PlayCreated(id: newPlay.playID, metadata: metadata)
29
30
// Store it in the contract storage
31
TopShot.playDatas[newPlay.playID] = newPlay
32
}
33
}
34
}