From 3d8783b19956a39ca4d6989383f8855c7756d8ca Mon Sep 17 00:00:00 2001 From: gbarnai Date: Sun, 29 Nov 2026 14:54:03 -0500 Subject: [PATCH 1/3] fix: keep extension, transfer and trash deletion commands with the signed-in person Window commands in the extensions. and transfer. families, and every trash. command except opening the trash and restoring from it, are now refused for API tokens, OAuth grants and form-md cli scripts, and are no longer listed or described to them. The refusal says the command works only for the person signed in to the window. --- .../references/connecting.md | 2 +- .docs/wiki/API-and-agents.md | 2 +- .docs/wiki/Extensions.md | 2 +- .docs/wiki/Terminal-and-Claude-Code.md | 2 + README.md | 2 +- docs/EXTENSIONS.md | 2 + internal/HASHOUT_LOG.md | 15 +++++ internal/claudecode/bridge_test.go | 56 +++++++++++++++++++ internal/claudecode/module.go | 9 ++- 9 files changed, 86 insertions(+), 6 deletions(-) diff --git a/.claude/skills/contextual-compression/references/connecting.md b/.claude/skills/contextual-compression/references/connecting.md index 3e63f3c..0b4db11 100644 --- a/.claude/skills/contextual-compression/references/connecting.md +++ b/.claude/skills/contextual-compression/references/connecting.md @@ -77,7 +77,7 @@ Add a project rule in `.cursor/rules/` that tells the agent to take a `context_p ## Extensions in the window -Form extensions installed with FormPortal or run from form.ext live in the person's open window. `extensions_list` shows them (id, version, source, state, commands) and `extension_command` runs one of their commands with `id`, `command` and `args`. Both go through the window like `window_command_run`, so they fail when no window is open, and the window asks the person to allow an extension version the first time; a refusal is the person's answer, not a fault to retry. From a terminal: `form-md cli extensions` and `form-md cli extension ID COMMAND`. +Form extensions installed with FormPortal or run from form.ext live in the person's open window, and only the person runs them. `extensions_list` and `extension_command` are refused for an agent credential, as are window commands for import and export and for deleting from the trash for good; say so and ask the person to do it in the window rather than retrying. Opening the trash and restoring from it still work. ## When a call fails diff --git a/.docs/wiki/API-and-agents.md b/.docs/wiki/API-and-agents.md index 2f260b9..80080e9 100644 --- a/.docs/wiki/API-and-agents.md +++ b/.docs/wiki/API-and-agents.md @@ -93,7 +93,7 @@ The credential decides what an agent may do; there is no separate question in th - **Read-only.** A read-only token or OAuth grant can read and search pages, databases, comments and agent runs, build a context pack and search or review the knowledge bank (it cannot record or confirm knowledge), and every request that changes something is refused with `403 read_only`. - **What a read-only credential is shown.** `tools/list` shows it only the tools that announce themselves as read-only, plus `window_command_run`, which stays listed because the window also offers commands that change nothing, such as opening a page or switching a view. That tool is announced as able to change the workspace, and a read-only credential is refused any window command that the window marks as changing it. -- **Extensions.** `extension_command` is a write tool, so a read-only credential does not see it and cannot call it. +- **Extensions.** `extension_command` is a write tool, so a read-only credential does not see it and cannot call it. `extensions_list` and `extension_command` are refused for every agent credential, read-only or not: extensions are installed, allowed and run only by the person in the window. - **Read and write.** A read and write credential sees every tool. `agents/manifest.json` in the repository lists each tool and which of them are read-only; `./form-md agents manifest` writes it from the server's own tool list and `./form-md agents manifest --check` fails when it is out of date. diff --git a/.docs/wiki/Extensions.md b/.docs/wiki/Extensions.md index 8b096b9..b7bf916 100644 --- a/.docs/wiki/Extensions.md +++ b/.docs/wiki/Extensions.md @@ -45,4 +45,4 @@ The list needs a signed-in session or token. The file route needs none, because | `./form-md cli extensions` | the same list from a terminal | | `./form-md cli extension ID COMMAND key=value` | runs a contributed command | -They go through the window like `window_command_run` (see [[Terminal-and-Claude-Code]]), so a window must be open for the same account, and consent is asked there. +These commands are for the person signed in to the window. An agent, a token or a `form-md cli` script calling them is refused with a message saying so, because extension commands install, allow or run code; choose the command in the window yourself instead (see [[Terminal-and-Claude-Code]]). diff --git a/.docs/wiki/Terminal-and-Claude-Code.md b/.docs/wiki/Terminal-and-Claude-Code.md index e0f3064..7125784 100644 --- a/.docs/wiki/Terminal-and-Claude-Code.md +++ b/.docs/wiki/Terminal-and-Claude-Code.md @@ -59,3 +59,5 @@ Every command in the window has an id, a label, a group and, for newer commands, | `./form-md cli list`, `describe ID`, `run ID key=value` | the same from a terminal, with `FORMMD_MCP_TOKEN` | A call needs a form.md window open for the same account; otherwise it fails with a message saying so. A read-only token can run only commands that change nothing. + +Some commands stay with the person signed in to the window, and an agent, a token or a `form-md cli` script is refused them and does not see them listed: your account, admin, preferences, the terminal panel and its agents, making a workspace, extensions (installing, allowing, reloading or running them), import and export, and deleting pages from the trash for good. Opening the trash and restoring a page from it still work for agents. diff --git a/README.md b/README.md index 32f9f90..3151a31 100644 --- a/README.md +++ b/README.md @@ -103,7 +103,7 @@ form.md is a server you sign in to, so what an agent may do is set by its creden ## Extensions -form.md loads Form extensions (Form Extension Contract, API 1): install them with FormPortal, or run a folder from form.ext. They appear under Extensions in the sidebar (and the desktop menu bar), in the Extensions panel on the right and in Find a Command; each runs in a sandboxed frame and changes the page only through form.md's own `doc.*` commands, as one undo step, after you allow it. Agents reach them with the MCP tools `extensions_list` and `extension_command`, or `./form-md cli extensions`. The API for extension authors is [`docs/EXTENSIONS.md`](docs/EXTENSIONS.md); the wiki page is Extensions. +form.md loads Form extensions (Form Extension Contract, API 1): install them with FormPortal, or run a folder from form.ext. They appear under Extensions in the sidebar (and the desktop menu bar), in the Extensions panel on the right and in Find a Command; each runs in a sandboxed frame and changes the page only through form.md's own `doc.*` commands, as one undo step, after you allow it. Extensions are installed, allowed and run only by the person in the window; agents and scripts are refused the extension commands. The API for extension authors is [`docs/EXTENSIONS.md`](docs/EXTENSIONS.md); the wiki page is Extensions. ## Under the hood diff --git a/docs/EXTENSIONS.md b/docs/EXTENSIONS.md index 4315d71..49c374d 100644 --- a/docs/EXTENSIONS.md +++ b/docs/EXTENSIONS.md @@ -113,6 +113,8 @@ Commands marked risky in the catalogue, and the `extensions.*` commands themselv - MCP `extension_command` with `id`, `command`, `args`: runs a contributed command in the open window. The window asks for consent first if the person has not allowed that version yet; the call waits up to 60 s. - `form-md cli extensions` and `form-md cli extension ID COMMAND [key=value...]` do the same from a terminal. +These are refused for an agent, an API token or a script: extension commands stay with the person signed in to the window, who runs them from the Extensions menu, the Extensions panel or Find a Command. + ## Running an extension - **From a folder (form.ext Run in App)**: form.ext writes `extensions/formmd/dev.json` under the FormPortal root with the folder's path. form.md checks the file every 2 s while its window has focus, and on Reload Extensions, and shows the extension with the source Development and its folder. diff --git a/internal/HASHOUT_LOG.md b/internal/HASHOUT_LOG.md index e8774db..4c1b39a 100644 --- a/internal/HASHOUT_LOG.md +++ b/internal/HASHOUT_LOG.md @@ -2656,3 +2656,18 @@ Owner request of 2026-11-22: a guide for taking one's own copy of form.md to one The Auth0 page is derived from `internal/auth/oidc.go` and Auth0's documentation, with no Auth0 tenant involved. What the code settles: the issuer check uses the discovery document's own `issuer`, so Auth0's trailing slash is fine; only RS256 and ES256 verify; the client secret goes in the token request body; an existing account links only when both sides hold the email verified; a new account needs `SignupAllowed`; a provider sign-in starts a session without the form.md two-factor step. What it does not settle: `prompt=select_account` is sent to every provider, and Auth0 documents only `none` and `login` for it. Ran: `bash -n` on the three scripts; the `example.com` guard and the host-name line of `install` on a copy of the env file; `go run ./tools/wikicheck` (22 pages agree). Not run: `provision`, `ship` and `install` against Google Cloud or any host. + +## Window commands kept for the person {#window-person-only} + +### internal/claudecode · extensions, transfer and deleting from the trash stay with the signed-in person +- refers-to: internal/claudecode/module.go :: `sessionOnlyPrefixes`, `agentTrashCommands`, `sessionOnly`, `RunCommand`; internal/claudecode/bridge_test.go :: `TestExtensionTransferAndEmptyTrashCommandsStayWithTheSignedInPerson`; internal/mcp/tools_window.go :: `extensions_list`, `extension_command`; cmd/form-md/cli.go :: `extensions`, `extension` +- kind: fix +- logged: v0.61.1 · 2026-11-29 + +Owner decision: three more families join `account.`, `admin.`, `prefs.`, `dock.`, `agent.` and `workspace.new` as window commands only the signed-in person runs. `extensions.` installs, allows, reloads or runs extension code; `transfer.` moves workspaces in or out (today `transfer.import`); `trash.` covers deleting pages for good. A token, an OAuth grant or a `form-md cli` script is refused with "ID works only for the person signed in to the window; an agent or script cannot run it", is not shown the command in `CatalogueFor` and gets no description from `DescribeFor`. + +The trash rule is closed by default: every `trash.` id is person-only except those in `agentTrashCommands` (`trash.open` and `trash.restore`), so a future command that empties the trash or deletes a page for good is covered without being named, and a new harmless trash command needs adding there to reach agents. `page.trash` moves a page to the trash, where it can be restored, and stays open. The window has no command that empties the trash today; the Empty trash and Delete forever buttons in the trash view call `DELETE /api/workspaces/{id}/trash` and `DELETE /api/pages/{id}/forever` directly. + +Consequence: MCP never runs as a session, so `extensions_list` and `extension_command`, and `form-md cli extensions` / `form-md cli extension`, now always end in that refusal. They are left in place, with the docs saying so, until the owner decides whether to remove them. Extension-contributed commands are registered as `ext..`, outside `extensions.`, and are still reachable through `window_command_run`; whether they join the person-only set is the owner's call. + +Ran: the new test failed on the code before the change (`extensions.list` ran for a token credential) and passes after it; `go test ./internal/claudecode` passes. diff --git a/internal/claudecode/bridge_test.go b/internal/claudecode/bridge_test.go index 90b663c..15c54dc 100644 --- a/internal/claudecode/bridge_test.go +++ b/internal/claudecode/bridge_test.go @@ -5,6 +5,7 @@ import ( "encoding/json" "errors" "reflect" + "strings" "sync" "testing" @@ -119,3 +120,58 @@ func TestAccountAndAdminCommandsStayWithTheSignedInPerson(t *testing.T) { t.Fatal(err) } } + +func TestExtensionTransferAndEmptyTrashCommandsStayWithTheSignedInPerson(t *testing.T) { + hub := &windowHub{workspace: "w1"} + s := &Service{Bridge: NewBridge(hub)} + hub.bridge = s.Bridge + s.SetReach(reachW1) + personOnly := []string{"extensions.list", "extensions.run", "extensions.reload", "transfer.import", "transfer.export", "trash.empty", "trash.deleteForever"} + open := []string{"trash.open", "trash.restore", "page.trash"} + var specs []CommandSpec + for _, id := range append(append([]string{}, personOnly...), open...) { + specs = append(specs, CommandSpec{ID: id, Mutates: true}) + } + s.Bridge.SetCatalogue("u1", specs) + for _, kind := range []policy.Kind{policy.KindToken, policy.KindOAuth} { + agent := &policy.Principal{UserID: "u1", Kind: kind, Scope: "write", AllWorkspaces: true} + for _, id := range personOnly { + _, err := s.RunCommand(context.Background(), agent, id, nil) + if err == nil || !strings.Contains(err.Error(), "signed in to the window") { + t.Fatalf("%s for a %s credential: err = %v", id, kind, err) + } + if _, ok := s.DescribeFor(agent, id); ok { + t.Fatalf("%s described to a %s credential", id, kind) + } + } + listed := map[string]bool{} + for _, c := range s.CatalogueFor(agent, "").([]CommandSpec) { + listed[c.ID] = true + } + for _, id := range personOnly { + if listed[id] { + t.Fatalf("%s listed to a %s credential", id, kind) + } + } + for _, id := range open { + if !listed[id] { + t.Fatalf("%s hidden from a %s credential", id, kind) + } + } + } + if len(hub.sent) != 0 { + t.Fatal("a person-only command reached the window") + } + agent := &policy.Principal{UserID: "u1", Kind: policy.KindToken, Scope: "write", AllWorkspaces: true} + for _, id := range open { + if _, err := s.RunCommand(context.Background(), agent, id, nil); err != nil { + t.Fatalf("%s refused for an agent credential: %v", id, err) + } + } + session := &policy.Principal{UserID: "u1", Kind: policy.KindSession, Scope: "write"} + for _, id := range personOnly { + if _, err := s.RunCommand(context.Background(), session, id, nil); err != nil { + t.Fatalf("%s refused for the signed-in person: %v", id, err) + } + } +} diff --git a/internal/claudecode/module.go b/internal/claudecode/module.go index 95822b1..927ecb2 100644 --- a/internal/claudecode/module.go +++ b/internal/claudecode/module.go @@ -708,14 +708,19 @@ func (s *Service) SetReach(fn func(ctx context.Context, p *policy.Principal) ([] var ErrNoReach = errors.New("this credential cannot reach a workspace the window can show") -var sessionOnlyPrefixes = []string{"account.", "admin.", "prefs.", "dock.", "agent."} +var sessionOnlyPrefixes = []string{"account.", "admin.", "prefs.", "dock.", "agent.", "extensions.", "transfer.", "trash."} var sessionOnlyCommands = map[string]bool{"workspace.new": true} +var agentTrashCommands = map[string]bool{"trash.open": true, "trash.restore": true} + func sessionOnly(id string) bool { if sessionOnlyCommands[id] { return true } + if agentTrashCommands[id] { + return false + } for _, pre := range sessionOnlyPrefixes { if strings.HasPrefix(id, pre) { return true @@ -726,7 +731,7 @@ func sessionOnly(id string) bool { func (s *Service) RunCommand(ctx context.Context, p *policy.Principal, id string, params map[string]any) (json.RawMessage, error) { if !p.IsSession() && sessionOnly(id) { - return nil, fmt.Errorf("%s works only for the person signed in to the window", id) + return nil, fmt.Errorf("%s works only for the person signed in to the window; an agent or script cannot run it", id) } spec, ok := s.Bridge.Describe(p.UserID, id) if ok && spec.Mutates && !p.CanWrite() { From b00e93710ca0789878fa8ab138151adad9ff4f6b Mon Sep 17 00:00:00 2001 From: gbarnai Date: Sun, 29 Nov 2026 15:02:34 -0500 Subject: [PATCH 2/3] fix: keep extension-contributed window commands with the signed-in person Commands an extension contributes (ext..) run the same code as extensions.run, so they are now person-only as well and are no longer listed or described to API tokens, OAuth grants or form-md cli scripts. --- internal/claudecode/bridge_test.go | 2 +- internal/claudecode/module.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/claudecode/bridge_test.go b/internal/claudecode/bridge_test.go index 15c54dc..511e746 100644 --- a/internal/claudecode/bridge_test.go +++ b/internal/claudecode/bridge_test.go @@ -126,7 +126,7 @@ func TestExtensionTransferAndEmptyTrashCommandsStayWithTheSignedInPerson(t *test s := &Service{Bridge: NewBridge(hub)} hub.bridge = s.Bridge s.SetReach(reachW1) - personOnly := []string{"extensions.list", "extensions.run", "extensions.reload", "transfer.import", "transfer.export", "trash.empty", "trash.deleteForever"} + personOnly := []string{"extensions.list", "extensions.run", "extensions.reload", "ext.sample.render", "transfer.import", "transfer.export", "trash.empty", "trash.deleteForever"} open := []string{"trash.open", "trash.restore", "page.trash"} var specs []CommandSpec for _, id := range append(append([]string{}, personOnly...), open...) { diff --git a/internal/claudecode/module.go b/internal/claudecode/module.go index 927ecb2..353c523 100644 --- a/internal/claudecode/module.go +++ b/internal/claudecode/module.go @@ -708,7 +708,7 @@ func (s *Service) SetReach(fn func(ctx context.Context, p *policy.Principal) ([] var ErrNoReach = errors.New("this credential cannot reach a workspace the window can show") -var sessionOnlyPrefixes = []string{"account.", "admin.", "prefs.", "dock.", "agent.", "extensions.", "transfer.", "trash."} +var sessionOnlyPrefixes = []string{"account.", "admin.", "prefs.", "dock.", "agent.", "extensions.", "ext.", "transfer.", "trash."} var sessionOnlyCommands = map[string]bool{"workspace.new": true} From e69b6e57381fe49c933eb54c27258c6ee41609b7 Mon Sep 17 00:00:00 2001 From: gbarnai Date: Sun, 29 Nov 2026 15:02:34 -0500 Subject: [PATCH 3/3] fix: keep deleting from the trash for good with the signed-in person DELETE /api/pages/{id}/forever and DELETE /api/workspaces/{id}/trash now need a signed-in browser or the desktop app; an API token or OAuth grant gets session_only. Moving a page to the trash and restoring it still work with a token. --- .docs/wiki/API-and-agents.md | 2 +- internal/HASHOUT_LOG.md | 10 ++++---- internal/apidoc/apidoc.go | 4 ++-- internal/page/module.go | 5 ++-- internal/server/trash_person_test.go | 36 ++++++++++++++++++++++++++++ 5 files changed, 48 insertions(+), 9 deletions(-) create mode 100644 internal/server/trash_person_test.go diff --git a/.docs/wiki/API-and-agents.md b/.docs/wiki/API-and-agents.md index 80080e9..f31bf6f 100644 --- a/.docs/wiki/API-and-agents.md +++ b/.docs/wiki/API-and-agents.md @@ -35,7 +35,7 @@ Create one in **Account settings, API tokens**: give it a name, choose **Read an curl -fsS -H "Authorization: Bearer fmd_..." https://notes.example.com/api/me ``` -A token can only narrow what its owner can do. A read-only token gets an error on any request that changes something. Revoking one credential ends everything it holds open: deleting a token, signing out or removing a session, and revoking an OAuth grant or token close that credential's event stream, live editing sockets and terminal attachments straight away, and leave the person's other credentials connected. Expiry ends them the same way within about a minute: a session that runs past `auth.session_days`, and an OAuth grant whose last access token (one hour) runs out without a refreshed one; a client that refreshed in time keeps its streams. Some routes (instance administration, token management) accept only a signed-in browser session. +A token can only narrow what its owner can do. A read-only token gets an error on any request that changes something. Deleting a page from the trash for good (`DELETE /api/pages/{id}/forever`) and emptying a trash (`DELETE /api/workspaces/{id}/trash`) work only in a signed-in browser or the desktop app: a token or an OAuth grant gets `session_only`. Moving a page to the trash and restoring it still work with a token. Revoking one credential ends everything it holds open: deleting a token, signing out or removing a session, and revoking an OAuth grant or token close that credential's event stream, live editing sockets and terminal attachments straight away, and leave the person's other credentials connected. Expiry ends them the same way within about a minute: a session that runs past `auth.session_days`, and an OAuth grant whose last access token (one hour) runs out without a refreshed one; a client that refreshed in time keeps its streams. Some routes (instance administration, token management) accept only a signed-in browser session. ## OAuth 2.1 diff --git a/internal/HASHOUT_LOG.md b/internal/HASHOUT_LOG.md index 4c1b39a..71bd735 100644 --- a/internal/HASHOUT_LOG.md +++ b/internal/HASHOUT_LOG.md @@ -2660,14 +2660,16 @@ Ran: `bash -n` on the three scripts; the `example.com` guard and the host-name l ## Window commands kept for the person {#window-person-only} ### internal/claudecode · extensions, transfer and deleting from the trash stay with the signed-in person -- refers-to: internal/claudecode/module.go :: `sessionOnlyPrefixes`, `agentTrashCommands`, `sessionOnly`, `RunCommand`; internal/claudecode/bridge_test.go :: `TestExtensionTransferAndEmptyTrashCommandsStayWithTheSignedInPerson`; internal/mcp/tools_window.go :: `extensions_list`, `extension_command`; cmd/form-md/cli.go :: `extensions`, `extension` +- refers-to: internal/claudecode/module.go :: `sessionOnlyPrefixes`, `agentTrashCommands`, `sessionOnly`, `RunCommand`; internal/claudecode/bridge_test.go :: `TestExtensionTransferAndEmptyTrashCommandsStayWithTheSignedInPerson`; internal/page/module.go :: `personRoute`; internal/server/trash_person_test.go :: `TestDeletingFromTheTrashForGoodStaysWithTheSignedInPerson`; internal/mcp/tools_window.go :: `extensions_list`, `extension_command`; cmd/form-md/cli.go :: `extensions`, `extension` - kind: fix - logged: v0.61.1 · 2026-11-29 Owner decision: three more families join `account.`, `admin.`, `prefs.`, `dock.`, `agent.` and `workspace.new` as window commands only the signed-in person runs. `extensions.` installs, allows, reloads or runs extension code; `transfer.` moves workspaces in or out (today `transfer.import`); `trash.` covers deleting pages for good. A token, an OAuth grant or a `form-md cli` script is refused with "ID works only for the person signed in to the window; an agent or script cannot run it", is not shown the command in `CatalogueFor` and gets no description from `DescribeFor`. -The trash rule is closed by default: every `trash.` id is person-only except those in `agentTrashCommands` (`trash.open` and `trash.restore`), so a future command that empties the trash or deletes a page for good is covered without being named, and a new harmless trash command needs adding there to reach agents. `page.trash` moves a page to the trash, where it can be restored, and stays open. The window has no command that empties the trash today; the Empty trash and Delete forever buttons in the trash view call `DELETE /api/workspaces/{id}/trash` and `DELETE /api/pages/{id}/forever` directly. +The trash rule is closed by default: every `trash.` id is person-only except those in `agentTrashCommands` (`trash.open` and `trash.restore`), so a future command that empties the trash or deletes a page for good is covered without being named, and a new harmless trash command needs adding there to reach agents. `page.trash` moves a page to the trash, where it can be restored, and stays open. The window has no command that empties the trash today; the Empty trash and Delete forever buttons in the trash view call `DELETE /api/workspaces/{id}/trash` and `DELETE /api/pages/{id}/forever` directly, so those two routes are mounted with `RequireSession`: a token or an OAuth grant gets `session_only`. `POST /api/pages/{id}/restore` and `DELETE /api/pages/{id}` (move to the trash) keep `Require`. -Consequence: MCP never runs as a session, so `extensions_list` and `extension_command`, and `form-md cli extensions` / `form-md cli extension`, now always end in that refusal. They are left in place, with the docs saying so, until the owner decides whether to remove them. Extension-contributed commands are registered as `ext..`, outside `extensions.`, and are still reachable through `window_command_run`; whether they join the person-only set is the owner's call. +Consequence: MCP never runs as a session, so `extensions_list` and `extension_command`, and `form-md cli extensions` / `form-md cli extension`, now always end in that refusal. They are left in place, with the docs saying so, until the owner decides whether to remove them. Extension-contributed commands are registered as `ext..` and run the same code as `extensions.run`, so `ext.` is person-only too; otherwise `window_command_run` with an `ext.` id would do what `extension_command` is refused. -Ran: the new test failed on the code before the change (`extensions.list` ran for a token credential) and passes after it; `go test ./internal/claudecode` passes. +Ran: the new test failed on the code before the change (`extensions.list` ran for a token credential) and passes after it; `go test ./internal/claudecode` passes. The `ext.` case (`ext.sample.render` ran for a token credential) and the trash routes (a token deleted a page for good, 204) each failed before their change and pass after it. + +Open: an extension in the window can still run window commands that are not `extensions.`, `ext.` or marked risky, which includes the other person-only families; the window does not check the person-only list. Import and export over REST and MCP are unchanged; the decision covered the window's `transfer.` commands. diff --git a/internal/apidoc/apidoc.go b/internal/apidoc/apidoc.go index d66a701..f951758 100644 --- a/internal/apidoc/apidoc.go +++ b/internal/apidoc/apidoc.go @@ -260,7 +260,7 @@ func init() { for k, v := range map[string]string{ "GET /.well-known/oauth-authorization-server": "OAuth 2.1 authorization server metadata", "GET /.well-known/oauth-protected-resource/api": "Protected resource metadata for the REST API", - "DELETE /api/pages/{id}/forever": "Delete a trashed page permanently", + "DELETE /api/pages/{id}/forever": "Delete a trashed page permanently (signed-in browser only)", "GET /api/views/{id}/export.csv": "Export a view as CSV", "POST /api/public/forms/{token}": "Submit a public form", "PATCH /api/me": "Update the signed-in account", @@ -270,7 +270,7 @@ func init() { "GET /api/events": "Live event stream (server-sent events)", "POST /api/rows/{id}/buttons/{prop}": "Press a row's button property", "POST /api/buttons/run": "Run a button block's server actions", - "DELETE /api/workspaces/{id}/trash": "Empty a workspace's trash", + "DELETE /api/workspaces/{id}/trash": "Empty a workspace's trash (signed-in browser only)", "GET /api/workspaces/{id}/graph": "Get the link graph of a workspace", "POST /api/views/{id}/calendar-feed": "Get or renew a private calendar subscription address", "GET /api/public/calendar/{file}": "Calendar subscription feed (iCalendar)", diff --git a/internal/page/module.go b/internal/page/module.go index fb3511b..fbb92ba 100644 --- a/internal/page/module.go +++ b/internal/page/module.go @@ -34,6 +34,7 @@ func done(w http.ResponseWriter) error { func (m module) Mount(r *httpx.Router, _ *app.Deps) error { s, a := m.s, m.auth route := func(pattern string, h handler) { r.HandleFunc(pattern, a.Require(h)) } + personRoute := func(pattern string, h handler) { r.HandleFunc(pattern, a.RequireSession(h)) } route("GET /api/workspaces/{id}/pages", func(w http.ResponseWriter, r *http.Request, p *policy.Principal) error { pages, err := s.Tree(r.Context(), p, r.PathValue("id")) @@ -57,7 +58,7 @@ func (m module) Mount(r *httpx.Router, _ *app.Deps) error { } return ok(w, map[string]any{"items": items}) }) - route("DELETE /api/workspaces/{id}/trash", func(w http.ResponseWriter, r *http.Request, p *policy.Principal) error { + personRoute("DELETE /api/workspaces/{id}/trash", func(w http.ResponseWriter, r *http.Request, p *policy.Principal) error { n, err := s.EmptyTrash(r.Context(), p, r.PathValue("id")) if err != nil { return err @@ -153,7 +154,7 @@ func (m module) Mount(r *httpx.Router, _ *app.Deps) error { } return ok(w, map[string]any{"page": m}) }) - route("DELETE /api/pages/{id}/forever", func(w http.ResponseWriter, r *http.Request, p *policy.Principal) error { + personRoute("DELETE /api/pages/{id}/forever", func(w http.ResponseWriter, r *http.Request, p *policy.Principal) error { if err := s.DeleteForever(r.Context(), p, r.PathValue("id")); err != nil { return err } diff --git a/internal/server/trash_person_test.go b/internal/server/trash_person_test.go new file mode 100644 index 0000000..35a362b --- /dev/null +++ b/internal/server/trash_person_test.go @@ -0,0 +1,36 @@ +package server_test + +import ( + "net/http" + "testing" +) + +func TestDeletingFromTheTrashForGoodStaysWithTheSignedInPerson(t *testing.T) { + e := newEnv(t, "") + owner := e.setupOwner() + ws := owner.workspaceID() + kept := owner.createPage(ws, "Kept") + gone := owner.createPage(ws, "Gone") + for _, id := range []string{kept, gone} { + if r := owner.do("DELETE", "/api/pages/"+id, nil); r.Status >= 300 { + t.Fatalf("move to the trash: %d %s", r.Status, r.Body) + } + } + tok := owner.apiToken("write") + anon := e.client() + for _, path := range []string{"/api/pages/" + kept + "/forever", "/api/workspaces/" + ws + "/trash"} { + r := anon.do("DELETE", path, nil, withBearer(tok)) + if r.Status != http.StatusForbidden || r.Code(t) != "session_only" { + t.Fatalf("DELETE %s with a token: %d %s, want 403 session_only", path, r.Status, r.Body) + } + } + if r := anon.do("POST", "/api/pages/"+kept+"/restore", nil, withBearer(tok)); r.Status != http.StatusOK { + t.Fatalf("restore with a token: %d %s", r.Status, r.Body) + } + if r := owner.do("DELETE", "/api/pages/"+gone+"/forever", nil); r.Status != http.StatusNoContent { + t.Fatalf("delete for good in the window: %d %s", r.Status, r.Body) + } + if r := owner.do("DELETE", "/api/workspaces/"+ws+"/trash", nil); r.Status != http.StatusOK { + t.Fatalf("empty the trash in the window: %d %s", r.Status, r.Body) + } +}