Access control
A component app runs on its group’s leader, and its writes are the log’s: no other node replays them. So a rule about who may write is a check in the method, before anything is written. Two layers enforce it:
- The node, from the method’s attributes in the ABI, before the app runs.
- The app, with
guardsandenv::caller(), inside the method.
What the node enforces
Section titled “What the node enforces”| Attribute | The node refuses |
|---|---|
| none | Callers who are not signed in (401). |
#[app::public] |
Nothing: no access grant admits a caller without an account, but a visitor through one of the instance’s public links may call it on an instance of any access, as Caller::Visitor { link, key }, optionally rate-limited (rate = "10/min"). |
#[app::confirm] |
A call without a fresh passkey assertion. |
#[app::allow(system)] |
Only the node itself calls it (timers imply it). |
#[app::allow(app = "<id>")] |
Cross-app calls from any other app. |
#[app::allow(action)] |
Anyone but the app’s own actions. |
A method with no attribute still admits any signed-in account the node lets reach the app. Membership of the app’s org is the app’s to check.
Guards: the org role
Section titled “Guards: the org role”pub fn set_user_simple(&mut self, value: String) -> app::Result<()> { guards::require_member()?; self.user_simple.set(&value)?; Ok(())}guards::require_member() and guards::require_admin() return the caller’s
account, or fail with LoginRequired (not an account: public, a visitor, system
or another app) or Forbidden (below the role). For anything else, match on the
caller yourself, as apps/forum does:
use calimero_sdk::env::Caller;
fn caller() -> app::Result<String> { match env::caller() { Caller::Account(a) | Caller::ApiKey { account: a, .. } => Ok(a), _ => Err(AppError::login_required()), }}env::org_role(&account) gives Guest, Member, Admin or Owner (ordered);
env::in_team(&account, team) checks a team.
Visitors through a public link
Section titled “Visitors through a public link”An instance’s managers make a public link (POST /apps/{app}/public-links);
anyone holding its token calls the instance’s #[app::public] methods without
an account, whatever the instance’s access, and nothing else. The app sees
Caller::Visitor { link, key }: the link’s key and the visitor’s own Ed25519
public key (hex), which the visitor proves it holds on every request. Two
visitors have different keys and the same browser keeps its key, so a form can
count one answer per visitor or let a visitor edit what it wrote:
#[app::public]pub fn submit(&mut self, answer: String) -> app::Result<()> { let Caller::Visitor { link, key } = env::caller() else { return Err(AppError::forbidden()); }; self.answers.insert(&format!("{link}/{key}"), &answer)?; Ok(())}The node also limits each visitor’s calls through a link (its ratePerMinute),
besides a method’s own rate. A component built before WIT 0.5.6 sees a
visitor as Caller::Public.
Scoped guests: the platform admits, the app scopes
Section titled “Scoped guests: the platform admits, the app scopes”The node decides who reaches an instance at all (its grants).
Anything finer is the app’s own, but the node carries it for the app: an
instance invite, and the grant it makes, may carry a small JSON scope the app
defines, and env::grant_scope() answers the caller’s as JSON text. A chat whose
managers invite a partner with {"channel":"partners"} keeps them to that
channel, while members (no scope) reach all of it:
#[derive(serde::Deserialize)]struct Scope { channel: String,}
pub fn post(&mut self, channel: String, text: String) -> app::Result<()> { if let Some(scope) = env::grant_scope() { let scope: Scope = serde_json::from_str(&scope).map_err(|_| AppError::forbidden())?; if scope.channel != channel { return Err(AppError::forbidden()); } } // ... Ok(())}None means the caller reaches the instance as a member, a team, anyone, or is
named without a scope. The scope is the managers’ setting, not the caller’s
claim: it comes from the instance’s grants, never from an argument. A component
built before WIT 0.5.6 cannot read it.
Write rules as data shapes
Section titled “Write rules as data shapes”Each rule below is a key layout plus a check, all from the component apps in
apps/ of core.
| Rule | Shape |
|---|---|
| Only the author edits it | Key the row by (author, id) with the author taken from the caller, never from an argument. Nobody can name another’s row. |
| Write once | Key by (…, author) and refuse when the row exists (AlreadyWritten); offer no edit or remove. |
| Moderated | Never edited; removal checks the caller against a moderators table the owner seeded in init. |
| Each account’s own value | PerUser<T>: set/get/remove act on the caller’s account; get_for(account) reads another’s. |
| Content-addressed, never changed | Frozen<T>: insert returns the hash and whether it was new; equal content stores nothing. |
| Fixed at creation | Write it in #[app::init] and expose no method that writes it. |
/// Sets `key` in the caller's own nested map.pub fn set_user_nested(&mut self, key: String, value: String) -> app::Result<()> { let account = guards::require_member()?; self.user_nested.insert(&(account.clone(), key.clone()), &value)?; Ok(())}Test it
Section titled “Test it”The SDK’s harness runs the real component with an org of your choosing, so a refusal is a test away (Testing):
let mut t = TestApp::new(component, Org::new().admins(["ada"]).members(["bob"]).guests(["eve"]), T0);assert!(t.as_("eve").call("setUserSimple", json!({ "value": "x" })).is_err());