Access control
Access control allows making certain parts of a program accessible/visible and making other parts inaccessible/invisible.
In Cadence, access control consists of:
- Access control on objects in account storage, using capability security.
A user is not able to access an object unless they own the object or have a reference to that object. This means that nothing is truly public by default.
Other accounts can not read or write the objects in an account unless the owner of the account has granted them access by providing references to the objects.
This kind of access control is covered in the pages on capabilities and capability management.
- Access control within contracts and objects,
using access modifiers (
access
keyword).
This page covers the second part of access control, using access modifiers.
All declarations, such as function, composite types, and fields,
must be prefixed with an access modifier, using the access
keyword.
The access modifier determines where the declaration is accessible/visible. Fields can only be assigned to and mutated from within the same or inner scope.
For example, to make a function publicly accessible (access(all)
is explained below):
_10access(all) fun test() {}
There are five levels of access control:
-
Public access means the declaration is accessible/visible in all scopes. This includes the current scope, inner scopes, and the outer scopes.
A declaration is made publicly accessible using the
access(all)
modifier.For example, a public field in a type can be accessed on an instance of the type in an outer scope.
-
Entitled access means the declaration is only accessible/visible to the owner of the object, or to references that are authorized to the required entitlements.
A declaration is made accessible through entitlements by using the
access(E)
syntax, whereE
is a set of one or more entitlements, or a single entitlement mapping.A reference is considered authorized to an entitlement if that entitlement appears in the
auth
portion of the reference type.For example, an
access(E, F)
field on a resourceR
can only be accessed by an owned (@R
-typed) value, or a reference toR
that is authorized to theE
andF
entitlements (auth(E, F) &R
). -
Account access means the declaration is only accessible/visible in the scope of the entire account where it is defined. This means that other contracts in the account are able to access it.
A declaration is made accessible by code in the same account, for example other contracts, by using the
access(account)
keyword. -
Contract access means the declaration is only accessible/visible in the scope of the contract that defined it. This means that other declarations that are defined in the same contract can access it, but not other contracts in the same account.
A declaration is made accessible by code in the same contract by using the
access(contract)
keyword. -
Private access means the declaration is only accessible/visible in the current and inner scopes.
A declaration is made accessible by code in the same containing type by using the
access(self)
keyword.For example, an
access(self)
field can only be accessed by functions of the type is part of, not by code in an outer scope.
To summarize the behavior for variable declarations, constant declarations, and fields:
Declaration kind | Access modifier | Accessible in | Assignable in | Mutable in |
---|---|---|---|---|
let | access(self) | Current and inner | None | Current and inner |
let | access(contract) | Current, inner, and containing contract | None | Current and inner |
let | access(account) | Current, inner, and other contracts in same account | None | Current and inner |
let | access(all) | All | None | Current and inner |
let | access(E) | All with required entitlements | None | Current and inner |
var | access(self) | Current and inner | Current and inner | Current and inner |
var | access(contract) | Current, inner, and containing contract | Current and inner | Current and inner |
var | access(account) | Current, inner, and other contracts in same account | Current and inner | Current and inner |
var | access(all) | All | Current and inner | Current and inner |
var | access(E) | All with required entitlements | Current and inner | Current and inner |
Declarations of composite types must be public. However, even though the declarations/types are publicly visible, resources can only be created, and events can only be emitted from inside the contract they are declared in.
_10// Declare a private constant, inaccessible/invisible in outer scope._10//_10access(self) let a = 1_10_10// Declare a public constant, accessible/visible in all scopes._10//_10access(all) let b = 2
_91// Declare a public struct, accessible/visible in all scopes._91//_91access(all) struct SomeStruct {_91_91 // Declare a private constant field which is only readable_91 // in the current and inner scopes._91 //_91 access(self) let a: Int_91_91 // Declare a public constant field which is readable in all scopes._91 //_91 access(all) let b: Int_91_91 // Declare a private variable field which is only readable_91 // and writable in the current and inner scopes._91 //_91 access(self) var c: Int_91_91 // Declare a public variable field which is not settable,_91 // so it is only writable in the current and inner scopes,_91 // and readable in all scopes._91 //_91 access(all) var d: Int_91_91 // Arrays and dictionaries declared without (set) cannot be_91 // mutated in external scopes_91 access(all) let arr: [Int]_91_91 // The initializer is omitted for brevity._91_91 // Declare a private function which is only callable_91 // in the current and inner scopes._91 //_91 access(self) fun privateTest() {_91 // ..._91 }_91_91 // Declare a public function which is callable in all scopes._91 //_91 access(all) fun publicTest() {_91 // ..._91 }_91_91 // The initializer is omitted for brevity._91_91}_91_91let some = SomeStruct()_91_91// Invalid: cannot read private constant field in outer scope._91//_91some.a_91_91// Invalid: cannot set private constant field in outer scope._91//_91some.a = 1_91_91// Valid: can read public constant field in outer scope._91//_91some.b_91_91// Invalid: cannot set public constant field in outer scope._91//_91some.b = 2_91_91// Invalid: cannot read private variable field in outer scope._91//_91some.c_91_91// Invalid: cannot set private variable field in outer scope._91//_91some.c = 3_91_91// Valid: can read public variable field in outer scope._91//_91some.d_91_91// Invalid: cannot set public variable field in outer scope._91//_91some.d = 4_91_91// Invalid: cannot mutate a public field in outer scope._91//_91some.f.append(0)_91_91// Invalid: cannot mutate a public field in outer scope._91//_91some.f[3] = 1_91_91// Valid: can call non-mutating methods on a public field in outer scope_91some.f.contains(0)
Entitlements
Entitlements provide granular access control to each member of a composite.
Entitlements are declared using the syntax entitlement E
,
where E
is the name of the entitlement.
For example, the following code declares two entitlements called E
and F
:
_10entitlement E_10entitlement F
Entitlements can be imported from other contracts and used the same way as other types. When using entitlements defined in another contract, the same qualified name syntax is used as for other types:
_10contract C {_10 entitlement E_10}
Outside of C
, E
is used with C.E
syntax.
Entitlements exist in the same namespace as types, so if a contract declares a resource called R
,
it is impossible to declare an entitlement that is also called R
.
Entitlements can be used in access modifiers composite members (fields and functions) to specify which references to those composites are allowed to access those members.
An access modifier can include more than one entitlement,
joined with either an |
, to indicate disjunction ("or"),
or a ,
, to indicate conjunction ("and").
The two kinds of separators cannot be combined in the same set.
For example:
_18access(all)_18resource SomeResource {_18_18 // requires a reference to have an `E` entitlement to read this field_18 access(E)_18 let a: Int_18_18 // requires a reference to have either an `E` OR an `F` entitlement to read this field._18 access(E | F)_18 let b: Int_18_18 // requires a reference to have both an `E` AND an `F` entitlement to read this field_18 access(E, F)_18 let b: Int_18_18 // intializers omitted for brevity_18 // ..._18}
Assuming the following constants exists, which have owned or reference types:
_10let r: @SomeResource = // ..._10let refE: auth(E) &SomeResource = // ..._10let refF: auth(F) &SomeResource = // ..._10let refEF: auth(E, F) &SomeResource = // ...
The references can be used as follows:
_27// valid, because `r` is owned and thus is "fully entitled"_27r.a_27// valid, because `r` is owned and thus is "fully entitled"_27r.b_27// valid, because `r` is owned and thus is "fully entitled"_27r.c_27_27// valid, because `refE` has an `E` entitlement as required_27refE.a_27// valid, because `refE` has one of the two required entitlements_27refE.b_27// invalid, because `refE` only has one of the two required entitlements_27refE.c_27_27// invalid, because `refF` has an `E` entitlement, not an `F`_27refF.a_27// valid, because `refF` has one of the two required entitlements_27refF.b_27// invalid, because `refF` only has one of the two required entitlements_27refF.c_27_27// valid, because `refEF` has an `E` entitlement_27refEF.a_27// valid, because `refEF` has both of the two required entitlements_27refEF.b_27// valid, because `refEF` has both of the two required entitlements_27refEF.c
Note particularly in this example how the owned value r
can access all entitled members on SomeResource
.
Owned values are not affected by entitled declarations.
Entitlement mappings
Entitlement mappings are a way to statically declare how entitlements are propagated from parents to child objects in a nesting hierarchy.
When objects have fields that are child objects, to grant access to the inner object based on the entitlements of the reference to the parent object.
Consider the following example, which uses entitlements to control access to an inner resource:
_42entitlement OuterEntitlement_42entitlement InnerEntitlement_42_42resource InnerResource {_42 access(all)_42 fun foo() { ... }_42_42 access(InnerEntitlement)_42 fun bar() { ... }_42}_42_42resource OuterResource {_42 access(self)_42 let childResource: @InnerResource_42_42 init(childResource: @InnerResource) {_42 self.childResource <- childResource_42 }_42_42 // The parent resource has to provide two accessor functions_42 // which return a reference to the inner resource._42 //_42 // If the reference to the outer resource is unauthorized_42 // and does not have the OuterEntitlement entitlement,_42 // the outer resource allows getting an unauthorized reference_42 // to the inner resource._42 //_42 // If the reference to the outer resource is authorized_42 // and it has the OuterEntitlement entitlement,_42 // the outer resource allows getting an authorized reference_42 // to the inner resource._42_42 access(all)_42 fun getPubRef(): &InnerResource {_42 return &self.childResource as &InnerResource_42 }_42_42 access(OuterEntitlement)_42 fun getEntitledRef(): auth(InnerEntitlement) &InnerResource {_42 return &self.childResource as auth(InnerEntitlement) &InnerResource_42 }_42}
With this pattern, it is possible to store a InnerResource
in an OuterResource
,
and create different ways to access that nested resource depending on the entitlement one possesses.
An unauthorized reference to OuterResource
can only be used to call the getPubRef
function,
and thus can only obtain an unauthorized reference to InnerResource
.
That reference to the InnerResource
then only allows calling the function foo
, which is publicly accessible,
but not function bar
, as it needs the InnerEntitlement
entitlement, which is not granted.
However a OuterEntitlement
-authorized reference to the OuterResource
can be used to call the getEntitledRef
function,
which returns a InnerEntitlement
-authorized reference to InnerResource
,
which in turn can be used to call function bar
.
This pattern is functional, but it is unfortunate that the accessor functions to InnerResource
have to be "duplicated".
To avoid necessitating this duplication, entitlement mappings can be used.
Entitlement mappings are declared using the syntax:
_10entitlement mapping M {_10 // ..._10}
Where M
is the name of the mapping.
The body of the mapping contains zero or more rules of the form A -> B
,
where A
and B
are entitlements.
Each rule defines that, given a reference with the entitlement on the left,
a reference with the entitlement on the right is returned.
An entitlement mapping thus defines a function from an input set of entitlements (called the domain) to an output set (called the range or the image).
Using entitlement mappings, the above example could be equivalently written as:
_50entitlement OuterEntitlement_50entitlement InnerEntitlement_50_50// Specify a mapping for entitlements called `OuterToInnerMap`,_50// which maps the entitlement `OuterEntitlement` to the entitlement `InnerEntitlement`._50entitlement mapping OuterToInnerMap {_50 OuterEntitlement -> InnerEntitlement_50}_50_50resource InnerResource {_50 access(all)_50 fun foo() { ... }_50_50 access(InnerEntitlement)_50 fun bar() { ... }_50}_50_50resource OuterResource {_50 // Use the entitlement mapping `OuterToInnerMap`._50 //_50 // This declares that when the field `childResource` is accessed_50 // using a reference authorized with the entitlement `OuterEntitlement`,_50 // then a reference with the entitlement `InnerEntitlement` is returned._50 //_50 // This is equivalent to the two accessor functions_50 // that were necessary in the previous example._50 //_50 access(OuterToInnerMap)_50 let childResource: @InnerResource_50_50 init(childResource: @InnerResource) {_50 self.childResource <- childResource_50 }_50_50 // No accessor functions are needed._50}_50_50// given some value `r` of type `@OuterResource`_50_50let pubRef = &r as &OuterResource_50let pubInnerRef = pubRef.childResource // has type `&InnerResource`_50_50let entitledRef = &r as auth(OuterEntitlement) &OuterResource_50let entitledInnerRef = entitledRef.childResource // has type `auth(InnerEntitlement) &InnerResource`,_50 // as `OuterEntitlement` is defined to map to `InnerEntitlement`._50_50// `r` is an owned value, and is thus considered "fully-entitled" to `OuterResource`,_50// so this access yields a value authorized to the entire image of `OuterToInnerMap`,_50// in this case `InnerEntitlement`_50let alsoEntitledInnerRef = r.childResource
Entitlement mappings can be used either in accessor functions (as in the example above), or in fields whose types are either references, or containers (composite types, dictionaries, and arrays).
Entitlement mappings need not be 1:1. It is valid to define a mapping where many inputs map to the same output, or where one input maps to many outputs.
Entitlement mappings preserve the "kind" of the set they are mapping. That is, mapping a conjunction ("and") set produces a conjunction set, and mapping a disjunction ("or") set produces a disjunction set.
Because entitlement separators cannot be combined in the same set, attempting to map disjunction ("or") sets through certain complex mappings can result in a type error.
For example, given the following entitlement mapping:
_10entitlement mapping M {_10 A -> B_10 A -> C_10 D -> E_10}
Attempting to map (A | D)
through M
will fail,
since A
should map to (B, C)
and D
should map to E
,
but these two outputs cannot be combined into a disjunction ("or") set.
A good example for how entitlement mappings can be used is the Account
type.
The Identity
entitlement mapping
Identity
is a built-in entitlement mapping that maps every input to itself as the output.
Any entitlement set passed through the Identity
map will come out unchanged in the output.
For instance:
_24entitlement X_24_24resource InnerResource {_24 // ..._24}_24_24resource OuterResource {_24 access(Identity)_24 let childResource: @InnerResource_24_24 access(Identity)_24 getChildResource(): auth(Identity) &InnerResource {_24 return &self.childResource_24 }_24_24 init(childResource: @InnerResource) {_24 self.childResource <- childResource_24 }_24}_24_24fun example(outerRef: auth(X) &OuterResource) {_24 let innerRef = outerRef.childResource // `innerRef` has type `auth(X) &InnerResource`,_24 // as `outerRef` was authorized with entitlement `X`_24}
One important point to note about the Identity
mapping, however,
is that its full output range is unknown, and theoretically infinite.
Because of that,
accessing an Identity
-mapped field or function with an owned value will yield an empty output set.
For example, calling getChildResource()
on an owned OuterResource
value,
will produce an unauthorized &InnerResource
reference.
Mapping composition
Entitlement mappings can be composed. In the definition of an entitlement mapping, it is possible to include the definition of one or more other mappings, to copy over their mapping relations.
An entitlement mapping is included into another entitlement mapping using the include M
syntax,
where M
is the name of the entitlement mapping to be included.
In general, an include M
statement in the definition of an entitlement mapping N
is equivalent to simply copy-pasting all the relations defined in M
into N
's definition.
Support for include
is provided primarily to reduce code-reuse and promote composition.
For example:
_14entitlement mapping M {_14 X -> Y_14 Y -> Z_14}_14_14entitlement mapping N {_14 E -> F_14}_14_14entitlement mapping P {_14 include M_14 include N_14 F -> G_14}
The entitlement mapping P
includes all of the relations defined in M
and N
,
along with the additional relations defined in its own definition.
It is also possible to include the Identity
mapping.
Any mapping M
that includes the Identity
mapping will map its input set to itself,
along with any additional relations defined in the mapping,
or in other included mappings.
For instance:
_10entitlement mapping M {_10 include Identity_10 X -> Y_10}
The mapping M
maps the entitlement set (X)
to (X, Y)
,
and (Y)
to (Y)
.
Includes that produce a cyclical mapping are rejected by the type-checker.
Built-in mutability entitlements
A prominent use-case of entitlements is to control access to object based on mutability.
For example, in a composite, the author would want to control the access to certain fields to be read-only, and some fields to be mutable, etc.
In order to support this, the following built-in entitlements can be used:
Insert
Remove
Mutate
These are primarily used by the built-in array and dictionary functions, but are available to be used in access modifiers of any declaration.
While Cadence does not support entitlement composition or inheritance,
the Mutate
entitlement is intended to be used as an equivalent form
to the conjunction of the (Insert, Remove)
entitlement set.