WeaveDocs
Strand

Policy

Policy hooks for Strand append, read, and replication — pluggable validators, capability checks, and signed authorisation.

Purpose

Append-only log with Merkle proofs, bitfields, storage backends, policies, and replication streams.

This page follows the real source shape for Strand and explains the workflow a developer is likely to use first.

Trust contract

Document which keys authorize the operation, what is signed, what is encrypted, what is deliberately public, and which policy hook can deny the call. Security examples should use deterministic test vectors where possible.

Note

AllowAllPolicy is intended for tests and prototyping. Production strands should bind a policy that checks the writer DID against an authorisation set and rate-limits append.

Primary types to know

  • AllowAllPolicy — models/strand/src/policy.rs
  • Bitfield — models/strand/src/bitfield.rs
  • BitfieldIterator — models/strand/src/bitfield.rs
  • FileStorage — models/strand/src/storage.rs
  • Header — models/strand/src/lib.rs
  • Info — models/strand/src/storage.rs
  • MemoryStorage — models/strand/src/storage.rs
  • MerkleTree — models/strand/src/merkle_tree.rs
  • Node — models/strand/src/merkle_tree.rs
  • Proof — models/strand/src/merkle_tree.rs
  • RangeHandle — models/strand/src/replication_api.rs
  • RangeSpec — models/strand/src/replication_api.rs

Example shape

use strand::{set_strand_policy, StrandPolicy};

/// Reject blocks larger than 4 KiB. Policies are evaluated before append commits.
struct MaxSizePolicy {
    max_bytes: usize,
}

impl StrandPolicy for MaxSizePolicy {
    fn can_append(&self, _writer_public_key: &[u8; 32], data: &[u8]) -> bool {
        data.len() <= self.max_bytes
    }
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Install a process-global policy. The backing OnceCell guarantees the policy
    // is observed by every Strand instance for the rest of the process; setting
    // it twice returns Err with the rejected policy box.
    let policy: Box<dyn StrandPolicy> = Box::new(MaxSizePolicy { max_bytes: 4 * 1024 });
    set_strand_policy(policy).map_err(|_| "policy already set")?;

    println!("strand policy installed: MaxSizePolicy(max_bytes=4096)");
    Ok(())
}