Skip to content

Views as security boundaries

A view is a security boundary, like a command. Its declaration lists the middleware that guards its reads and the rejections they may fail with. Its Layer’s validate decides whether the person asking may read the key they asked for, on every read and on every change while they watch.

Write who may read an account’s data as one function, and use it in every view of an account.

packages/domain/src/Account/mustOwn.ts
import * as Effect from "effect/Effect";
import { CurrentActor } from "../CurrentActor.context.ts";
import type { CustomerId } from "../Customer/Customer.id.ts";
import { NotOwner } from "./NotOwner.rejection.ts";
/**
* Who may read an account's data: any agent, or the customer who owns the
* account right now. An account that doesn't exist yet reads as empty, so a
* client can watch for it to appear; someone else's is rejected.
*/
export const mustOwn = Effect.fn(function* (owner: CustomerId | null) {
const actor = yield* CurrentActor;
if (actor._tag === "Agent" || owner === null) return;
if (actor._tag !== "Customer" || owner !== actor.customerId) return yield* new NotOwner();
});

validate lives on the view’s Layer. It gets the request and the key’s state, and succeeds to allow the read or fails with a rejection the view declares. A view that declares middleware or rejects needs its Layer: the model’s Layer is a type error until it’s provided.

packages/domain/src/Account/AccountSummary.view.ts
import * as Domain from "@alchemy.run/domain";
import * as Effect from "effect/Effect";
import * as Schema from "effect/Schema";
import { CustomerId } from "../Customer/Customer.id.ts";
import { Cents } from "../Money.ts";
import { Session } from "../Session.middleware.ts";
import { Account } from "./Account.aggregate.ts";
import { mustOwn } from "./mustOwn.ts";
import { NotOwner } from "./NotOwner.rejection.ts";
const setBalance = <V extends { balance: number }>(
v: V,
e: { readonly balanceAfter: number },
): V => ({
...v,
balance: e.balanceAfter,
});
/**
* The current balance and status of one account. It declares no events, so
* it emits every event it applies, each with the summary after it.
*/
export class AccountSummary extends Domain.View("AccountSummary", {
from: [Account],
key: Account,
middleware: [Session],
rejects: [NotOwner],
state: Schema.Struct({
customerId: Schema.NullOr(CustomerId),
owner: Schema.NullOr(Schema.String),
balance: Cents,
frozen: Schema.Boolean,
closed: Schema.Boolean,
}),
initial: { customerId: null, owner: null, balance: 0, frozen: false, closed: false },
evolve: {
AccountOpened: (v, e) => ({ ...v, customerId: e.customerId, owner: e.owner }),
OwnerChanged: (v, e) => ({ ...v, customerId: e.customerId }),
MoneyDeposited: setBalance,
MoneyWithdrawn: setBalance,
TransferDebited: setBalance,
TransferCredited: setBalance,
TransferRefunded: setBalance,
SettlementReceived: setBalance,
AccountFrozen: (v) => ({ ...v, frozen: true }),
AccountClosed: (v) => ({ ...v, closed: true }),
},
}) {}
/** Re-run on every change while watched: a reassigned account ends the watch with NotOwner. */
export const AccountSummaryLive = AccountSummary.toLayer(
Effect.succeed(
Effect.fn(function* (_request, summary) {
yield* mustOwn(summary?.customerId ?? null);
}),
),
);

The Statement and AccountTransfers views and the AccountCard stream use the same rule, so the account reads the same everywhere. Clients never read the Account aggregate itself: a view projects what they may see of it.

When the key itself says whose data it is, judge the key. A dashboard is keyed by customer, so only that customer, or an agent, may read it.

packages/domain/src/Customer/CustomerDashboard.view.ts
import * as Domain from "@alchemy.run/domain";
import * as Effect from "effect/Effect";
import * as Schema from "effect/Schema";
import { AccountId } from "../Account/Account.id.ts";
import { AccountSummary } from "../Account/AccountSummary.view.ts";
import { CurrentActor } from "../CurrentActor.context.ts";
import { Forbidden } from "../Forbidden.rejection.ts";
import { Cents } from "../Money.ts";
import { Session } from "../Session.middleware.ts";
import { Customer } from "./Customer.aggregate.ts";
type Balances = Readonly<Record<AccountId, number>>;
const without = (balances: Balances, id: AccountId): Balances => {
const { [id]: _removed, ...rest } = balances;
return rest;
};
/**
* A customer's accounts and balances, built from AccountSummary's events and
* the summary after each. An ownership change moves the account between
* dashboards.
*/
export class CustomerDashboard extends Domain.View("CustomerDashboard", {
from: [Customer, AccountSummary],
key: Customer,
middleware: [Session],
rejects: [Forbidden],
keyOf: {
AccountSummary: ({ state }) => (state?.customerId ? Customer.ref(state.customerId) : undefined),
OwnerChanged: {
previous: ({ event }) => Customer.ref(event.previous),
current: ({ event }) => Customer.ref(event.customerId),
},
},
state: Schema.Struct({
name: Schema.NullOr(Schema.String),
balances: Schema.Record(AccountId, Cents),
}),
initial: { name: null, balances: {} },
evolve: {
CustomerRegistered: (d, e) => ({ ...d, name: e.name }),
OwnerChanged: {
previous: (d, _, { source }) => ({ ...d, balances: without(d.balances, source.id) }),
current: (d, _, { source, state }) => ({
...d,
balances: { ...d.balances, [source.id]: state?.balance ?? 0 },
}),
},
AccountSummary: (d, _, { source, state }) => ({
...d,
balances:
state && !state.closed
? { ...d.balances, [source.id]: state.balance }
: without(d.balances, source.id),
}),
},
computed: {
totalBalance: (d) => Object.values<number>(d.balances).reduce((a, b) => a + b, 0),
},
}) {}
/** Keyed by customer, so the key alone decides who may read it. */
export const CustomerDashboardLive = CustomerDashboard.toLayer(
Effect.succeed(
Effect.fn(function* (request) {
const actor = yield* CurrentActor;
if (actor._tag === "Agent") return;
if (actor._tag !== "Customer" || request.key.id !== actor.customerId) {
return yield* new Forbidden({ reason: "that is not your dashboard" });
}
}),
),
);

A view decides who may read it, not which parts they may see. To show an audience different data, make another view. Support agents see an account’s operational events; customers can’t read them at all.

packages/domain/src/Account/AccountActivity.view.ts
import * as Domain from "@alchemy.run/domain";
import * as Effect from "effect/Effect";
import { CurrentActor } from "../CurrentActor.context.ts";
import { Forbidden } from "../Forbidden.rejection.ts";
import { Session } from "../Session.middleware.ts";
import { Account } from "./Account.aggregate.ts";
import { OwnerChanged } from "./ChangeOwner.command.ts";
import { AccountClosed } from "./CloseAccount.command.ts";
import { AccountFrozen } from "./FreezeAccount.command.ts";
import { SettlementOnClosedAccount } from "./RecordSettlement.command.ts";
import { RefundStranded } from "./RefundTransfer.command.ts";
/** Operational events for support tooling, passed through unchanged. Agents only. */
export class AccountActivity extends Domain.View("AccountActivity", {
from: [Account],
key: Account,
middleware: [Session],
rejects: [Forbidden],
events: [AccountFrozen, AccountClosed, OwnerChanged, RefundStranded, SettlementOnClosedAccount],
}) {}
/** Who may read each key of AccountActivity. */
export const AccountActivityLive = AccountActivity.toLayer(
Effect.succeed(
Effect.fn(function* (request) {
const actor = yield* CurrentActor;
if (actor._tag !== "Agent") {
return yield* new Forbidden({ reason: `${request.mode} of account activity is for agents` });
}
}),
),
);

Expose it in the agents’ Api only.

packages/domain/src/SupportApi.api.ts
import * as Domain from "@alchemy.run/domain";
import { Bank } from "./Bank.model.ts";
/** What the support console reaches, over the same Bank. */
export class SupportApi extends Domain.Api("SupportApi", {
model: Bank,
commands: ["FreezeAccount", "ChangeOwner", "ApproveTransfer"],
views: ["AccountSummary", "AccountActivity"],
streams: [],
queries: ["AccountLedger"],
}) {}

Each Layer joins the model’s handlers, next to its policies and projections.

packages/domain/src/Bank.model.ts
/** Who may read each key: the `validate` of every guarded view and stream. */
export const BankReads = Layer.mergeAll(
AccountSummaryLive,
CustomerDashboardLive,
StatementLive,
AccountActivityLive,
TransferStatusLive,
AccountTransfersLive,
AccountCardLive,
);

The Layer’s construction runs once and may resolve services, such as a permissions store. validate itself may only use what the view’s middleware provides.

validate runs again on every change while a client watches. When support moves an account to another customer, the old owner’s watch ends with NotOwner at that change. See the test in Testing an Api.

A read with atLeast is judged on the state it finds, and again on the state after the write it waits for. AccountSummary holds the account’s customerId in its own state, and its validate judges that field. So a customer whom a command just made the account’s owner reads AccountSummary with that command’s receipt at once.

When validate refuses the state the read finds, the read waits only for writes whose receipts the Api signed, and is judged again after them. A receipt the Api didn’t sign could name anyone’s write, and how long its wait took would tell the caller when that write reached a key it may not read. So with no signed receipt, the refusal comes at once.

Give the Apis a ReceiptKey where they’re served, and every command’s receipt carries a sig. Every Api that reads a receipt needs the same key as the one that signed it:

packages/server/src/Receipts.ts
/** Made on the first deploy and kept in the Stack's state. */
export const ReceiptKeyLive = Domain.SecretReceiptKey("ReceiptKey");

Domain.SecretReceiptKey signs with HMAC under an Alchemy.Random of that name, and never leaves the process to sign or check. Pass a Redacted or Config.Redacted("RECEIPT_KEY") instead to supply the secret yourself. Without a ReceiptKey, receipts carry no signature, and a refused caller is refused at once.

To keep the secret out of your Stack’s state, sign with keys AWS KMS or Cloud KMS wraps. Each process has the key service make a signing key, signs with it in memory for keep, then makes another:

export const Receipts = DomainAWS.KmsReceiptKey(ReceiptRootKey, { keep: "15 minutes" }).pipe(
Layer.provide(Layer.mergeAll(AWS.KMS.GenerateDataKeyHttp, AWS.KMS.DecryptHttp)),
);

A signature carries its key as the service wrapped it, so another process checks it with one call the first time it sees that key. Calls grow with processes and keep, not with commands. A longer keep calls the service less; a shorter one cuts a process off sooner once its access is revoked. DomainGCP.KmsReceiptKey does the same with Cloud KMS.

When the key must never leave the key service, DomainAWS.KmsMacReceiptKey and DomainGCP.KmsMacReceiptKey sign and check inside it. That is one request per command, and one per signed receipt a refused read passes back.

A read waits up to 10 seconds before it fails with ConsistencyTimeout. To give up sooner, bound the read yourself:

const support = yield* SupportApi.client;
const bank = yield* CustomerApi.client;
const receipt = yield* support.ChangeOwner({ accountId, customerId });
const summary = yield* bank.AccountSummary.get(accountId, { atLeast: receipt }).pipe(
Effect.timeout("2 seconds"),
);

When the read gives up, the server stops waiting for the write too.

The read waits only for the view it reads. A validate that looks up access in another view, such as a membership view, sees that view as it is now: the caller is refused until that view has the write. Keep the fact that grants access in the state of the view being read, or send the command with consistency: "strong": it answers once every view it reaches has applied it, the membership view included, so the read after it needs no atLeast.

yield* support.ChangeOwner({ accountId, customerId }, { consistency: "strong" });
const summary = yield* bank.AccountSummary.get(accountId, { atLeast: [] });

A client’s where filter narrows a read it’s already allowed to make, and never grants one. validate doesn’t look at filters, so a filter can’t widen what someone sees.

A view’s rule judges one key. A query reads many keys from a store a projection wrote, so its handler judges the caller itself: it fails with a rejection the query declares when the caller may not ask, and scopes the rows it reads to what the caller may see.