425 lines
15 KiB
Markdown
425 lines
15 KiB
Markdown
# Per-Customer Vault Design
|
|
|
|
Date: 2026-06-19
|
|
Branch: main
|
|
Status: Draft, reviewed by /plan-eng-review
|
|
|
|
## Problem
|
|
|
|
SquareMCP currently treats Obsidian note access as authenticated, but not tenant-scoped.
|
|
The server reads and writes notes through one global filesystem root:
|
|
|
|
```ts
|
|
const VAULT_PATH = process.env['OBSIDIAN_VAULT_PATH'] ?? '/vaults';
|
|
```
|
|
|
|
That means valid customer credentials can reach the same backing vault unless a higher
|
|
layer blocks the request. Authentication answers "who are you"; it does not answer
|
|
"which vault are you allowed to touch."
|
|
|
|
## Current Code Facts
|
|
|
|
- `src/clients/obsidian.ts` reads from one process-wide `VAULT_PATH`.
|
|
- `src/clients/obsidian.ts` allows absolute paths in `getNote`, `appendToNote`,
|
|
and `updateNote`.
|
|
- `src/tools.ts` receives `customer` but the Obsidian handlers do not use it.
|
|
- `src/index.ts` protects `/api/obsidian/*` with `requireAuth`, but the current
|
|
route registrations do not show `requireCapability('obsidian')`.
|
|
- `product/incubation/ARCHITECTURE.md` already names this gap:
|
|
`Obsidian per-customer vault | Currently one global vault path from env`.
|
|
- `TODOS.md` says all `/api/*` routes are guarded by capability middleware, but
|
|
the current route registrations contradict that for `/api/obsidian/*`.
|
|
|
|
## Goal
|
|
|
|
Each customer gets access only to their own vault root. No customer request can read,
|
|
search, append, update, or infer anything from another customer's vault or the
|
|
operator vault.
|
|
|
|
Non-goals:
|
|
|
|
- Do not build a full note-sync product in this change.
|
|
- Do not expose arbitrary filesystem paths to customers.
|
|
- Do not migrate the operator vault into customer-owned storage automatically.
|
|
- Do not make the global operator vault available through customer auth.
|
|
|
|
## Recommended Architecture
|
|
|
|
Use an explicit customer vault binding and pass a vault context into every Obsidian
|
|
operation.
|
|
|
|
```text
|
|
HTTP / MCP request
|
|
|
|
|
v
|
|
requireAuth
|
|
|
|
|
v
|
|
requireCapability('obsidian')
|
|
|
|
|
v
|
|
resolveCustomerVault(customer.id)
|
|
|
|
|
v
|
|
VaultContext { customerId, rootPath, readonly?, syncFolderId? }
|
|
|
|
|
v
|
|
obsidian client with scoped path resolver
|
|
|
|
|
v
|
|
/vaults/customers/<customer-id>/...
|
|
```
|
|
|
|
### Data Model
|
|
|
|
Add a table for vault bindings:
|
|
|
|
```sql
|
|
CREATE TABLE customer_vaults (
|
|
customer_id VARCHAR(64) PRIMARY KEY,
|
|
vault_root VARCHAR(512) NOT NULL,
|
|
sync_folder_id VARCHAR(128) NULL,
|
|
enabled BOOLEAN NOT NULL DEFAULT TRUE,
|
|
readonly BOOLEAN NOT NULL DEFAULT FALSE,
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
|
CONSTRAINT fk_customer_vaults_customer
|
|
FOREIGN KEY (customer_id) REFERENCES customers(id)
|
|
);
|
|
```
|
|
|
|
The application should create roots under a single configured base directory:
|
|
|
|
```text
|
|
OBSIDIAN_CUSTOMER_VAULTS_ROOT=/vaults/customers
|
|
/vaults/customers/cust_123
|
|
/vaults/customers/cust_456
|
|
```
|
|
|
|
`vault_root` should be generated by the server. Never accept a customer-supplied
|
|
absolute root path.
|
|
|
|
### Runtime API
|
|
|
|
Introduce a vault context:
|
|
|
|
```ts
|
|
export interface VaultContext {
|
|
customerId: string;
|
|
rootPath: string;
|
|
syncFolderId?: string;
|
|
readonly?: boolean;
|
|
}
|
|
```
|
|
|
|
Change the Obsidian client signatures:
|
|
|
|
```ts
|
|
searchNotes(ctx, query, tags, limit, pathFilter)
|
|
getNote(ctx, path)
|
|
appendToNote(ctx, path, content, createIfMissing, header)
|
|
updateNote(ctx, path, content)
|
|
getSyncStatus(ctx)
|
|
```
|
|
|
|
### Path Safety
|
|
|
|
Every note path must be resolved through one helper:
|
|
|
|
```text
|
|
input path -> reject absolute -> normalize -> join with root -> resolve real path
|
|
|
|
|
v
|
|
must remain inside ctx.rootPath
|
|
```
|
|
|
|
Rules:
|
|
|
|
- Reject absolute paths.
|
|
- Reject paths that escape with `..`.
|
|
- Resolve symlinks before final access where possible.
|
|
- Require `.md` for read/write targets, adding it only after validation.
|
|
- Ensure the final path is inside the customer's root.
|
|
- Return relative paths only.
|
|
|
|
OWASP's secure coding checklist says authorization should use trusted server-side
|
|
objects, fail securely, and enforce access control on every request:
|
|
https://owasp.org/www-project-secure-coding-practices-quick-reference-guide/stable-en/02-checklist/05-checklist
|
|
|
|
This design uses the authenticated customer id and server-owned vault binding as
|
|
the trusted authorization object.
|
|
|
|
### Auth Rules
|
|
|
|
Customer-facing Obsidian access requires all three:
|
|
|
|
1. Authenticated customer.
|
|
2. `obsidian` capability.
|
|
3. Enabled `customer_vaults` binding.
|
|
|
|
If any are missing, return `403`.
|
|
|
|
Legacy/global access should be separate:
|
|
|
|
- Global operator API key may keep operator-only access for admin maintenance.
|
|
- Legacy bearer tokens with no customer binding should not access customer vault
|
|
routes.
|
|
- Dev mode with no `MCP_API_KEY` should not silently expose vault routes in
|
|
production builds.
|
|
|
|
### Tool Layer
|
|
|
|
`handleToolCall(name, args, customer)` should resolve the vault before dispatching
|
|
any Obsidian tool. The Obsidian cases should receive `VaultContext`, not reach for
|
|
process-global state.
|
|
|
|
```text
|
|
obsidian_search_notes
|
|
-> require customer
|
|
-> require obsidian capability at route/session boundary
|
|
-> resolve customer vault
|
|
-> searchNotes(ctx, ...)
|
|
```
|
|
|
|
For generic MCP routes, the same capability/vault checks need to happen before tool
|
|
execution. Route-level protection is not enough if `/mcp` can call the same tools.
|
|
|
|
## Migration Plan
|
|
|
|
1. Emergency guard
|
|
- Temporarily deny customer-authenticated Obsidian access unless a customer
|
|
vault binding exists.
|
|
- Keep global operator access separate and explicit.
|
|
|
|
2. Capability enforcement
|
|
- Add or restore `requireCapability(capability)`.
|
|
- Apply `requireCapability('obsidian')` to every `/api/obsidian/*` route.
|
|
- Add the same capability check for generic MCP tool execution.
|
|
|
|
3. Vault binding table
|
|
- Add migration for `customer_vaults`.
|
|
- Add DB helpers: `getCustomerVault`, `ensureCustomerVault`,
|
|
`disableCustomerVault`.
|
|
|
|
4. Scoped Obsidian client
|
|
- Replace global `VAULT_PATH` operations with `VaultContext`.
|
|
- Centralize path resolution.
|
|
- Remove support for absolute note paths.
|
|
|
|
5. Route/tool integration
|
|
- Resolve customer vault in REST routes and MCP tool execution.
|
|
- Return `403` for missing binding and `404` for missing notes inside an
|
|
authorized vault.
|
|
|
|
6. Tests
|
|
- Add unit tests for path resolution.
|
|
- Add route tests for customer isolation.
|
|
- Add tool tests proving customer A cannot read customer B.
|
|
- Add regression tests for absolute paths and `../` traversal.
|
|
|
|
7. Rollout
|
|
- Deploy with customer vaults disabled by default.
|
|
- Bind one test customer to a test vault.
|
|
- Verify search, read, append, update, and sync.
|
|
- Only then enable for real customers.
|
|
|
|
## Test Plan
|
|
|
|
```text
|
|
CODE PATHS USER FLOWS
|
|
[+] src/clients/obsidian.ts [+] Customer searches notes
|
|
|-- [GAP] ctx-rooted recursive search |-- [GAP] [->E2E] own vault returns own notes
|
|
|-- [GAP] reject absolute path `-- [GAP] [->E2E] no vault binding returns 403
|
|
|-- [GAP] reject ../ traversal
|
|
|-- [GAP] reject symlink escape [+] Customer reads note
|
|
|-- [GAP] append creates note inside root |-- [GAP] own note returns content
|
|
`-- [GAP] update cannot write outside root `-- [GAP] other-customer path returns 403/404
|
|
|
|
[+] src/index.ts [+] Customer writes note
|
|
|-- [GAP] /api/obsidian/* requires capability |-- [GAP] append succeeds in own vault
|
|
|-- [GAP] /api/obsidian/* requires vault binding `-- [GAP] update outside root rejected
|
|
`-- [GAP] legacy token cannot access customer vault
|
|
|
|
[+] src/tools.ts [+] MCP tool call
|
|
|-- [GAP] Obsidian tools require customer/vault ctx |-- [GAP] valid customer+vault succeeds
|
|
`-- [GAP] generic /mcp cannot bypass route checks `-- [GAP] unbound customer gets 403
|
|
```
|
|
|
|
Required tests:
|
|
|
|
- `src/clients/obsidian.test.ts`
|
|
- Resolves `Projects/A.md` inside the customer root.
|
|
- Rejects `/etc/passwd`.
|
|
- Rejects `../other-customer/secret.md`.
|
|
- Rejects symlink escape if symlinks are present.
|
|
- Search returns only files under the supplied context root.
|
|
|
|
- `src/capability-routes.test.ts`
|
|
- Customer with `obsidian` capability and vault binding can search/read.
|
|
- Customer without `obsidian` capability gets `403`.
|
|
- Customer with `obsidian` capability but no vault binding gets `403`.
|
|
- Legacy OAuth token gets `403` on `/api/obsidian/*`.
|
|
|
|
- `src/tools.test.ts`
|
|
- Obsidian tools pass `VaultContext` to the client.
|
|
- Obsidian tools fail closed when `customer` is missing for customer-facing
|
|
execution.
|
|
- Generic MCP route cannot call Obsidian tools without capability and vault
|
|
binding.
|
|
|
|
## Failure Modes
|
|
|
|
| Failure | Expected behavior | Test |
|
|
| --- | --- | --- |
|
|
| Customer has no vault binding | `403 vault_not_configured` | Route + tool |
|
|
| Customer tries absolute path | `400 invalid_note_path` | Unit |
|
|
| Customer tries `../` traversal | `400 invalid_note_path` | Unit |
|
|
| Symlink points outside vault | `400 invalid_note_path` | Unit |
|
|
| Vault directory missing | `403` until provisioned, or explicit admin create | Route |
|
|
| Sync service unavailable | Search/read/write still work; sync returns degraded status | Unit |
|
|
| Legacy bearer token calls Obsidian | `403` | Route |
|
|
| Global API key calls customer route | Allowed only on explicit admin/operator path | Route |
|
|
|
|
## Engineering Review
|
|
|
|
### Step 0: Scope Challenge
|
|
|
|
What already exists:
|
|
|
|
- Customer identity resolution exists in `requireAuth`.
|
|
- Customer API key resolution exists in `billing/middleware`.
|
|
- OAuth capability data exists in `oauth.ts` and tests.
|
|
- Obsidian read/search/write primitives exist in `src/clients/obsidian.ts`.
|
|
- The test stack is Vitest plus Supertest.
|
|
|
|
Minimum viable change:
|
|
|
|
- Add vault bindings, require them for customer Obsidian access, and scope all note
|
|
operations by `VaultContext`.
|
|
|
|
Scope challenge:
|
|
|
|
- Do not build sync onboarding, UI vault management, or note import in this pass.
|
|
- Do not preserve absolute path support. It is a security bug, not a feature.
|
|
- Do not rely only on route middleware. MCP tool execution must enforce the same
|
|
boundary.
|
|
|
|
### Architecture Review
|
|
|
|
1. [P1] Global vault root is the wrong tenant boundary.
|
|
|
|
The current implementation uses `VAULT_PATH` as process-global state. This is
|
|
simple but fails tenant isolation. The recommended fix is to make the vault root
|
|
a server-resolved property of the authenticated customer.
|
|
|
|
2. [P1] Route-level capability checks are insufficient.
|
|
|
|
REST routes and generic MCP tool calls share `handleToolCall`. If `/mcp` can call
|
|
Obsidian tools without the same capability and vault checks, route middleware does
|
|
not protect the system.
|
|
|
|
3. [P1] Absolute path support must be removed.
|
|
|
|
`getNote`, `appendToNote`, and `updateNote` currently accept paths beginning with
|
|
`/`. Per-customer vaults do not work unless note paths are relative and verified
|
|
against the customer root.
|
|
|
|
### Code Quality Review
|
|
|
|
1. [P2] Path handling should be centralized.
|
|
|
|
The current client repeats path construction in each read/write method. A
|
|
per-customer design should use one resolver and make direct filesystem joins
|
|
outside that helper a code smell.
|
|
|
|
2. [P2] The capability story is split between docs, tests, and runtime code.
|
|
|
|
`TODOS.md` says routes are guarded, tests expect capability behavior, and the
|
|
current route definitions only show `requireAuth`. The implementation should make
|
|
capability enforcement visible in one reusable middleware.
|
|
|
|
### Test Review
|
|
|
|
Coverage today is not enough for this change. The plan needs unit tests for the
|
|
path resolver, route tests for capability/vault binding, and tool tests for MCP
|
|
bypass prevention.
|
|
|
|
Critical gaps:
|
|
|
|
- No test proves Customer A cannot read Customer B's note.
|
|
- No test proves absolute paths are rejected.
|
|
- No test proves `/mcp` cannot bypass REST route guards.
|
|
|
|
### Performance Review
|
|
|
|
The current recursive markdown search is acceptable for small vaults, but
|
|
per-customer roots make indexing/caching a follow-up rather than a blocker.
|
|
|
|
Performance guardrails:
|
|
|
|
- Keep search scoped to one customer root.
|
|
- Enforce a sane result limit.
|
|
- Add a future index only after real vault size requires it.
|
|
|
|
### NOT In Scope
|
|
|
|
- Customer-facing vault management UI: useful, but not needed to close the data
|
|
isolation bug.
|
|
- Automatic migration of existing operator notes: risky and not required for new
|
|
customer isolation.
|
|
- Full-text index service: defer until customer vault size makes recursive search
|
|
too slow.
|
|
- Multi-device sync onboarding: separate product surface.
|
|
|
|
### Parallelization
|
|
|
|
Sequential implementation is safer for the first pass because the route, tool, and
|
|
client changes all touch the same trust boundary.
|
|
|
|
Possible split after the migration contract is agreed:
|
|
|
|
| Step | Modules touched | Depends on |
|
|
| --- | --- | --- |
|
|
| Vault DB helpers | `src/db`, `src/billing` | migration shape |
|
|
| Scoped Obsidian client | `src/clients` | `VaultContext` contract |
|
|
| Route/tool enforcement | `src/index.ts`, `src/tools.ts` | DB helpers + client |
|
|
| Tests | `src/*.test.ts` | implementation |
|
|
|
|
### Implementation Tasks
|
|
|
|
- [ ] **T1 (P1, human: ~2h / CC: ~20min)** - Obsidian auth - Fail closed for
|
|
customer Obsidian access without an enabled customer vault binding.
|
|
- Surfaced by: Architecture Review - global vault root is the wrong tenant boundary.
|
|
- Files: `src/index.ts`, `src/tools.ts`, DB helper/migration files.
|
|
- Verify: `npm test -- src/capability-routes.test.ts src/tools.test.ts`
|
|
|
|
- [ ] **T2 (P1, human: ~3h / CC: ~30min)** - Vault client - Replace global
|
|
`VAULT_PATH` reads/writes with `VaultContext` and centralized safe path resolution.
|
|
- Surfaced by: Architecture Review - absolute path support must be removed.
|
|
- Files: `src/clients/obsidian.ts`, `src/clients/obsidian.test.ts`.
|
|
- Verify: `npm test -- src/clients/obsidian.test.ts`
|
|
|
|
- [ ] **T3 (P1, human: ~2h / CC: ~20min)** - MCP enforcement - Ensure generic
|
|
`/mcp` tool execution cannot bypass Obsidian capability and vault checks.
|
|
- Surfaced by: Architecture Review - route-level capability checks are insufficient.
|
|
- Files: `src/index.ts`, `src/tools.ts`, `src/capability-routes.test.ts`.
|
|
- Verify: `npm test -- src/capability-routes.test.ts src/tools.test.ts`
|
|
|
|
- [ ] **T4 (P2, human: ~1h / CC: ~10min)** - Capability route tests - Align
|
|
runtime capability middleware with tests and update the stale TODO language.
|
|
- Surfaced by: Code Quality Review - docs/tests/runtime disagree.
|
|
- Files: `src/index.ts`, `src/capability-routes.test.ts`, `TODOS.md`.
|
|
- Verify: `npm test -- src/capability-routes.test.ts`
|
|
|
|
## GSTACK REVIEW REPORT
|
|
|
|
| Review | Trigger | Why | Runs | Status | Findings |
|
|
| --- | --- | --- | --- | --- | --- |
|
|
| Eng Review | `/plan-eng-review` | Architecture & tests | 1 | ISSUES OPEN | 6 issues, 3 critical gaps |
|
|
|
|
- **VERDICT:** ENG REVIEW STARTED - per-customer vault is the right architecture, but implementation should not start until the trust-boundary decisions below are accepted.
|
|
|
|
**UNRESOLVED DECISIONS:**
|
|
- Decide whether global operator vault access remains on existing routes with global API key or moves to separate admin-only routes.
|
|
- Decide whether legacy bearer tokens are denied Obsidian immediately or handled with a temporary compatibility window.
|