Skip to content

Commit b096934

Browse files
authored
Merge pull request #281 from onflow/josh/remove-bad-entitle-guidance
Revert bad entitlement guidance
2 parents f47f221 + ac1b494 commit b096934

2 files changed

Lines changed: 9 additions & 11 deletions

File tree

docs/anti-patterns.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,11 @@ Accidentally exposed fields can be a security hole.
8383
### Solution
8484

8585
When writing your smart contract, look at every field and function and make sure
86-
they require access through an [entitlement](./language/access-control.md#entitlements) (`access(E)`),
87-
or use a non-public [access modifier](./language/access-control.md) like `access(self)`, `access(contract)`, or `access(account)`,
88-
unless you are making a deliberate design decision to allow completely open and unrestricted access to read that field or call that function.
86+
that any functions that you don't want every user to be able to access require access through an [entitlement](./language/access-control.md#entitlements) (`access(E)`),
87+
or use a non-public [access modifier](./language/access-control.md) like `access(self)`, `access(contract)`, or `access(account)`.
88+
Declaring a function as `access(all)` is a deliberate design decision to allow completely open and unrestricted access to read that field or call that function and should not be taken lightly.
8989

90-
The only functions that should be `access(all)` are `view` functions and the only fields that can be `access(all)` are basic types like numbers or addresses.
90+
The only functions that should be `access(all)` are `view` functions and functions the everyone should be able to access and the only fields that should be `access(all)` are basic types like numbers or addresses.
9191
Complex fields like arrays, dictionaries, structs, resources, or capabilities should always be `access(self)`.
9292

9393
## Capability-Typed public fields are a security hole

docs/security-best-practices.md

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Some practices listed below might overlap with advice in the [Cadence Anti-Patte
1212

1313
Do not use the `access(all)` modifier on fields and functions unless absolutely necessary. Prefer `access(self)`, `access(contract)`, `access(account)`, or `access(SomeEntitlement)`. Unintentionally declaring fields or functions as `access(all)` can expose vulnerabilities in your code.
1414

15-
When writing definitions for contracts, structs, or resources, start by declaring all your fields and functions as `access(self)`. If there is a function that needs to be accessible by external code, only declare it as `access(all)` if it is a `view` function:
15+
When writing definitions for contracts, structs, or resources, start by declaring all your fields and functions as `access(self)`. If there is a function that needs to be accessible by external code, only declare it as `access(all)` if it is a `view` function or if you definitely want it to be accessible by anyone in the network.
1616

1717
```cadence
1818
/// Simplified Bank Account implementation
@@ -30,7 +30,7 @@ access(all) resource BankAccount {
3030
}
3131
```
3232

33-
If there are any functions that modify state that also need to be callable from external code, use [entitlements] for the access modifiers for those functions:
33+
If there are any functions that modify privileged state that also need to be callable from external code, use [entitlements] for the access modifiers for those functions:
3434

3535
```cadence
3636
/// Simplified Vault implementation
@@ -39,7 +39,6 @@ access(all) resource BankAccount {
3939
4040
/// Declare Entitlements for state-modifying functions
4141
access(all) entitlement Owner
42-
access(all) entitlement Depositor
4342
4443
/// Fields should default to access(self) just to be safe
4544
access(self) var balance: UFix64
@@ -58,10 +57,9 @@ access(all) resource BankAccount {
5857
return <-create BankAccount(balance: amount)
5958
}
6059
61-
/// This is also state-modifying, so it should also be restricted with entitlements
62-
/// In this case, we can use two entitlements to be more specific
63-
/// about who can access (Owner OR Depositor)
64-
access(Owner | Depositor) fun depositToAccount(_ from: @BankAccount) {
60+
/// This is also state-modifying, but we intend for it to be callable by anyone
61+
/// so we can make it access(all)
62+
access(all) fun depositToAccount(_ from: @BankAccount) {
6563
self.updateBalance(self.balance + from.getBalance())
6664
destroy from
6765
}

0 commit comments

Comments
 (0)