Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions SECURITY.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,16 @@ What FormPDF promises:
can change the choice.
- The key Claude Code uses inside the agent dock belongs to that one tab, is never
on a command line or in a plain terminal, and stops working when the tab closes.
- The choice is sealed with a key kept in the system keychain. If settings.json is
changed by hand to loosen it, FormPDF acts as Ask until the person sets it again
in the window. Without a usable keychain the seal is skipped and the stored
choice stands; FormPDF still starts.
- The choice is sealed with a key kept in the system keychain, together with the
places of the agent programs and the Claude Code folder and first message. If
settings.json is changed by hand to loosen it or to change any of those,
FormPDF acts as Ask and empties them until the person sets them again in the
window. Without a usable keychain the seal is skipped and the stored choice
stands; FormPDF still starts.
- The Claude Code folder and first message were not sealed in earlier versions.
When a person had set them and had agents allowed, the first start of a version
that seals them keeps both, puts Agent access back to Ask once and says why in the
window.

What it does not promise:

Expand Down
12 changes: 12 additions & 0 deletions backend/HASHOUT_LOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -919,3 +919,15 @@ kind: change · logged: v0.66.0 · 2026-11-20
Entry 134's wave made every sentence in the window that lists the agent programs name Claude Code, Codex, Cursor and Antigravity. Three lines printed in a terminal still named three: the `agents install|sync` line of `./formpdf help` and of its twin `formpdf.bat help`, and the second of the next steps `./formpdf agents install` prints ("Open Claude Code, Codex or Cursor in ..."). They now name the four, in that order. The install already sets up Antigravity for the user (`install_antigravity_user`), and Antigravity reads that user file wherever it is opened, so the next step is true as written. The `agents` usage text already named Antigravity ("and Antigravity for this user") and was left.

Ran: `bash -n formpdf`; `./formpdf help` shows the new line; `formpdf.bat` by reading (the `^|` escape kept); `python -m py_compile setup/agents.py`; `setup/agents.py install` in a scratch copy of the tree with a scratch home and caller folder printed the new step 2.

## 137. The Claude Code folder and first message are sealed, and a file saved before that asks once · _v0.66.0, logged 2026-11-29 (core/seal.py WORDS, LATER, SEALED, loosened, effective, upgraded, mark_upgraded, upgrade; core/config.py read_settings, write_settings; routers/system.py set_settings sealNote; tests/test_seal.py)

kind: change · logged: v0.66.0 · 2026-11-29

The owner's decision: `claudeFolder` and `claudeFirstMessage` join the seal. The folder is where the dock's Claude Code starts and the first message is what it is given when it starts, so they belong with the agent choice the window alone sets. Both are now in `SEALED` and in `loosened`; a hand edit of either reads as broken, like a program place: both are emptied with the places, and access goes back to Ask (or stays Blocked). A first message added by hand while access is Ask is caught too, because a filled folder or first message now counts as loosened.

The update path. Like entry 134's three places, the two keys enter the HMAC body only when they hold a value (`LATER`), so a file that never set them hashes as before and nothing changes for it. A file that did set them was sealed without them, so the owner chose that such a person gets agent access reset to Ask once, with a note. `config._upgraded_settings` runs before every read and write until the keychain item `settings-seal-shape` says `2`. While it does not: a file with no folder or first message is left as it is; one whose seal matches the body without the two keys (the earlier shape) is resealed in the new shape, the folder, first message and program places kept, `outsideAgents` set to `ask` and `agentsAllowed` to false when they allowed agents (Blocked stays Blocked, with no note), and `sealNote: "words"` stored when access changed; one with no seal at all whose access was never loosened (a person who set only the folder or first message) is resealed as it is. Anything else is left for `effective` to read as broken. Then the item is written, and from then on only the new shape counts. Keeping the flag in the keychain rather than in the file is what makes the reset happen once: a flag in `settings.json` could be edited along with the rest of the file, and without a flag a later hand edit to a file sealed in the earlier shape would be taken as part of the update. If the item cannot be read the check counts as done, so the strict reading applies. The item is written, and read back, before the updated file is stored: if the keychain will not keep it, the file is not updated and the strict reading applies, because otherwise the update would run again at every start and accept a folder or first message added by hand to a file whose seal never covered one. Going back to an earlier version after this reads a file with a folder or first message as broken there.

`sealNote` is not sealed: written by hand it only shows the note. `POST /api/settings` takes `sealNote` only to clear it, whatever value is sent.

Ran: `tests/test_seal.py` 21 passed (7 of the new tests fail on the code before the change; the Blocked one passes on both and guards the reset); the whole suite 246 passed. With a stand-in keyring kept in a scratch file, a settings file of the earlier shape (agents allowed, a folder and a first message) and the built window: the window showed the note once, the engine then read `seal: ok`, access `ask`, the folder and first message kept and `sealNote` empty, and a reload showed no note.
30 changes: 25 additions & 5 deletions backend/app/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,23 +112,43 @@ def stored_settings() -> dict:
return found if isinstance(found, dict) else {}


def _store(values: dict) -> None:
tmp = settings_path().with_suffix(".tmp")
tmp.write_text(json.dumps(values, indent=2), encoding="utf-8")
tmp.replace(settings_path())


def _upgraded_settings() -> dict:
from app.core import seal

stored = stored_settings()
if seal.upgraded():
return stored
fresh = seal.upgrade(stored)
if not seal.mark_upgraded() or fresh is None:
return stored
try:
_store(fresh)
except OSError:
return stored
return fresh


def read_settings() -> dict:
from app.core import seal

return seal.effective(stored_settings())
return seal.effective(_upgraded_settings())


def write_settings(patch: dict) -> dict:
from app.core import seal

stored = stored_settings()
stored = _upgraded_settings()
patch = {k: v for k, v in patch.items() if k not in ("seal", "sealBroken")}
if any(k in patch for k in seal.SEALED):
current = seal.effective(stored)
merged = seal.sign({**stored, **{k: current.get(k) for k in seal.SEALED if k in stored or k in current}, **patch})
else:
merged = {**stored, **patch}
tmp = settings_path().with_suffix(".tmp")
tmp.write_text(json.dumps(merged, indent=2), encoding="utf-8")
tmp.replace(settings_path())
_store(merged)
return read_settings()
82 changes: 73 additions & 9 deletions backend/app/core/seal.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@
SERVICE = "FormPDF"
ACCOUNT = "settings-seal"
FIELD = "seal"
SHAPE_ACCOUNT = "settings-seal-shape"
SHAPE = "2"
NOTE = "sealNote"
PATHS = ("claudeBin", "codexBin", "cursorBin", "antigravityBin")
LATER = ("codexBin", "cursorBin", "antigravityBin")
SEALED = ("outsideAgents", "agentsAllowed") + PATHS
_CACHE: dict = {"tried": False, "available": False, "key": None}
WORDS = ("claudeFolder", "claudeFirstMessage")
LATER = ("codexBin", "cursorBin", "antigravityBin") + WORDS
SEALED = ("outsideAgents", "agentsAllowed") + PATHS + WORDS
_CACHE: dict = {"tried": False, "available": False, "key": None, "shape": None}


def reset() -> None:
_CACHE.update(tried=False, available=False, key=None)
_CACHE.update(tried=False, available=False, key=None, shape=None)


def _load(create: bool) -> bytes | None:
Expand All @@ -40,13 +44,29 @@ def available() -> bool:
return _CACHE["available"]


def digest(values: dict, key: bytes) -> str:
body = json.dumps({k: values.get(k) for k in SEALED if k not in LATER or values.get(k)}, sort_keys=True, separators=(",", ":"), ensure_ascii=False)
def _digest(values: dict, key: bytes, keys: tuple) -> str:
body = json.dumps({k: values.get(k) for k in keys if k not in LATER or values.get(k)}, sort_keys=True, separators=(",", ":"), ensure_ascii=False)
return hmac.new(key, body.encode("utf-8"), hashlib.sha256).hexdigest()


def digest(values: dict, key: bytes) -> str:
return _digest(values, key, SEALED)


def _matches(mark, expected: str) -> bool:
return isinstance(mark, str) and hmac.compare_digest(mark, expected)


def _filled(values: dict, keys: tuple) -> bool:
return any(str(values.get(k) or "").strip() for k in keys)


def _access_loosened(values: dict) -> bool:
return values.get("outsideAgents") == "allowed" or bool(values.get("agentsAllowed")) or _filled(values, PATHS)


def loosened(values: dict) -> bool:
return values.get("outsideAgents") == "allowed" or bool(values.get("agentsAllowed")) or any(str(values.get(k) or "").strip() for k in PATHS)
return _access_loosened(values) or _filled(values, WORDS)


def effective(stored: dict) -> dict:
Expand All @@ -55,14 +75,58 @@ def effective(stored: dict) -> dict:
if not _CACHE["available"]:
return {**out, "seal": "unavailable", "sealBroken": False}
mark = stored.get(FIELD)
if key is not None and isinstance(mark, str) and hmac.compare_digest(mark, digest(stored, key)):
if key is not None and _matches(mark, digest(stored, key)):
return {**out, "seal": "ok", "sealBroken": False}
if not loosened(stored):
return {**out, "seal": "unsealed", "sealBroken": False}
out.update(outsideAgents="blocked" if stored.get("outsideAgents") == "blocked" else "ask", agentsAllowed=False, **{k: "" for k in PATHS})
out.update(outsideAgents="blocked" if stored.get("outsideAgents") == "blocked" else "ask", agentsAllowed=False, **{k: "" for k in PATHS + WORDS})
return {**out, "seal": "broken", "sealBroken": True}


def upgraded() -> bool:
if _CACHE["shape"] is None:
try:
import keyring

_CACHE["shape"] = keyring.get_password(SERVICE, SHAPE_ACCOUNT) == SHAPE
except Exception:
_CACHE["shape"] = True
return _CACHE["shape"]


def mark_upgraded() -> bool:
_CACHE["shape"] = True
try:
import keyring

keyring.set_password(SERVICE, SHAPE_ACCOUNT, SHAPE)
return keyring.get_password(SERVICE, SHAPE_ACCOUNT) == SHAPE
except Exception:
return False


def _ask_once(stored: dict) -> dict:
if stored.get("outsideAgents") == "blocked":
return {**stored, "agentsAllowed": False}
if stored.get("outsideAgents") == "allowed" or stored.get("agentsAllowed"):
return {**stored, "outsideAgents": "ask", "agentsAllowed": False, NOTE: "words"}
return stored


def upgrade(stored: dict) -> dict | None:
if not available() or upgraded() or not _filled(stored, WORDS):
return None
key = _load(False)
mark = stored.get(FIELD)
if key is not None and _matches(mark, digest(stored, key)):
return None
if key is not None and _matches(mark, _digest(stored, key, tuple(k for k in SEALED if k not in WORDS))):
return sign(_ask_once(stored))
if mark is None and not _access_loosened(stored):
return sign(stored)
return None


def vouch(text: str) -> str | None:
key = _load(True)
return hmac.new(key, text.encode("utf-8"), hashlib.sha256).hexdigest() if key is not None else None
Expand Down
4 changes: 3 additions & 1 deletion backend/app/routers/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ def get_settings() -> dict:
@router.post("/settings")
def set_settings(patch: dict, request: Request) -> dict:
window_key.require(request)
allowed = {k: v for k, v in patch.items() if k in {"agentsAllowed", "outsideAgents", "claudeBin", "codexBin", "cursorBin", "antigravityBin", "claudeFolder", "claudeFirstMessage", "author", "units", "spellLang", "timestampUrl", "formScripts", "language"}}
allowed = {k: v for k, v in patch.items() if k in {"agentsAllowed", "outsideAgents", "claudeBin", "codexBin", "cursorBin", "antigravityBin", "claudeFolder", "claudeFirstMessage", "author", "units", "spellLang", "timestampUrl", "formScripts", "language", "sealNote"}}
if "sealNote" in allowed:
allowed["sealNote"] = ""
if "outsideAgents" in allowed:
if allowed["outsideAgents"] not in ("ask", "allowed", "blocked"):
raise HTTPException(400, "Agent access is ask, allowed or blocked")
Expand Down
101 changes: 101 additions & 0 deletions backend/tests/test_seal.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,107 @@ def test_the_other_programs_places_are_sealed_and_given_to_the_finder_first(clie
assert s[key] == "" and s["claudeBin"] == "" and s["sealBroken"] is True and s["outsideAgents"] == "ask"


@pytest.mark.parametrize("key", ["claudeFolder", "claudeFirstMessage"])
def test_a_hand_edit_of_the_claude_code_folder_or_first_message_is_caught(client, keychain, key):
r = client.post("/api/settings", json={"outsideAgents": "allowed", key: " chosen in the window "}, headers=window_headers()).json()
assert r["seal"] == "ok" and r[key] == "chosen in the window" and r["outsideAgents"] == "allowed"
_hand_edit(**{key: "written by another program"})
s = client.get("/api/settings").json()
assert s[key] == "" and s["sealBroken"] is True and s["outsideAgents"] == "ask"
from app.routers import mcp
from app.services import claude_code

assert mcp.access() == "ask"
assert claude_code.pref(key) == ""


def test_a_first_message_added_by_hand_with_access_at_ask_is_not_used(client, keychain):
client.post("/api/settings", json={"outsideAgents": "ask"}, headers=window_headers())
client.get("/api/settings")
_hand_edit(claudeFirstMessage="written by another program")
s = client.get("/api/settings").json()
assert s["claudeFirstMessage"] == "" and s["sealBroken"] is True


def _previous_shape(values):
import hashlib
import hmac

from app.core import config, seal

key = seal._load(True)
sealed = ("outsideAgents", "agentsAllowed", "claudeBin")
body = json.dumps({k: values.get(k) for k in sealed}, sort_keys=True, separators=(",", ":"), ensure_ascii=False)
config.settings_path().write_text(json.dumps({**values, "seal": hmac.new(key, body.encode("utf-8"), hashlib.sha256).hexdigest()}, indent=2), encoding="utf-8")


def test_a_folder_and_first_message_saved_before_they_were_sealed_ask_once_and_say_why(client, keychain):
from app.routers import mcp

_previous_shape({"outsideAgents": "allowed", "agentsAllowed": True, "claudeBin": "/opt/tools/claude", "claudeFolder": "/work/pdfs", "claudeFirstMessage": "Hello"})
s = client.get("/api/settings").json()
assert s["seal"] == "ok" and s["sealBroken"] is False
assert s["outsideAgents"] == "ask" and s["agentsAllowed"] is False and mcp.access() == "ask"
assert s["claudeBin"] == "/opt/tools/claude" and s["claudeFolder"] == "/work/pdfs" and s["claudeFirstMessage"] == "Hello"
assert s["sealNote"] == "words"
assert client.get("/api/settings").json()["sealNote"] == "words"
r = client.post("/api/settings", json={"sealNote": "anything"}, headers=window_headers()).json()
assert r["sealNote"] == "" and r["seal"] == "ok" and r["claudeFolder"] == "/work/pdfs"
r = client.post("/api/settings", json={"outsideAgents": "allowed"}, headers=window_headers()).json()
assert r["outsideAgents"] == "allowed" and r["sealNote"] == "" and r["claudeFirstMessage"] == "Hello"
assert client.get("/api/settings").json()["outsideAgents"] == "allowed"


def test_the_one_time_reset_does_not_happen_again(client, keychain):
_previous_shape({"outsideAgents": "allowed", "agentsAllowed": True, "claudeBin": "", "claudeFolder": "/work/pdfs"})
client.get("/api/settings")
client.post("/api/settings", json={"outsideAgents": "allowed", "sealNote": ""}, headers=window_headers())
_previous_shape({"outsideAgents": "allowed", "agentsAllowed": True, "claudeBin": "", "claudeFirstMessage": "written by another program"})
s = client.get("/api/settings").json()
assert s["sealBroken"] is True and s["claudeFirstMessage"] == "" and s["outsideAgents"] == "ask" and not s.get("sealNote")


def test_blocked_stays_blocked_through_the_one_time_reset_without_a_note(client, keychain):
_previous_shape({"outsideAgents": "blocked", "agentsAllowed": False, "claudeBin": "", "claudeFolder": "/work/pdfs"})
s = client.get("/api/settings").json()
assert s["seal"] == "ok" and s["outsideAgents"] == "blocked" and s["claudeFolder"] == "/work/pdfs" and not s.get("sealNote")


def test_the_one_time_update_waits_until_the_keychain_can_record_it(client, keychain, monkeypatch):
from app.core import config, seal

config.settings_path().write_text("{}", encoding="utf-8")
client.post("/api/settings", json={"outsideAgents": "ask"}, headers=window_headers())
keychain.items.pop((seal.SERVICE, seal.SHAPE_ACCOUNT), None)
seal.reset()
plain = keychain.set_password

def refuse_shape(service, username, password):
if username == seal.SHAPE_ACCOUNT:
raise RuntimeError("keychain item could not be written")
plain(service, username, password)

monkeypatch.setattr(keychain, "set_password", refuse_shape)
_hand_edit(claudeFirstMessage="written by another program")
s = client.get("/api/settings").json()
assert s["sealBroken"] is True and s["claudeFirstMessage"] == "" and not s.get("sealNote")
seal.reset()
s = client.get("/api/settings").json()
assert s["sealBroken"] is True and s["claudeFirstMessage"] == ""


def test_a_folder_saved_with_access_never_set_is_kept_and_sealed(client, keychain):
from app.core import config

config.settings_path().write_text(json.dumps({"claudeFolder": "/work/pdfs", "author": "Someone"}), encoding="utf-8")
s = client.get("/api/settings").json()
assert s["seal"] == "ok" and s["sealBroken"] is False and s["claudeFolder"] == "/work/pdfs" and not s.get("sealNote")
assert _stored()["seal"]
_hand_edit(claudeFolder="/elsewhere")
s = client.get("/api/settings").json()
assert s["sealBroken"] is True and s["claudeFolder"] == ""


def _plugin(name):
from app.services import plugins

Expand Down
2 changes: 1 addition & 1 deletion docs/MANUAL.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ The choice is called **Agent access**. You can set it in Preferences > Plugins >
| Allowed | Agents change PDFs and write files without asking. |
| Blocked | Every agent call is refused, reads included. |

Only the formpdf window can change Agent access: an agent cannot, and neither can a command run from the command line. If the saved choice is changed outside the app, formpdf treats it as Ask until you set it again in the window, and Preferences says so. The app's own command line (`./formpdf run`, `./formpdf cli`) counts as an outside agent and follows the same choice. The server listens on 127.0.0.1 only.
Only the formpdf window can change Agent access: an agent cannot, and neither can a command run from the command line. If the saved choice is changed outside the app, formpdf treats it as Ask until you set it again in the window, and Preferences says so. The same goes for the Claude Code program, folder and first message and the other agent programs' places: one changed outside the app is emptied and Agent access goes back to Ask. Earlier versions did not protect the Claude Code folder and first message, so if you had set either of them and had agents allowed, the first start after the update keeps them, puts Agent access back to Ask once and shows a note saying so. Check them in Preferences > Plugins > Outside Agents and set Agent access again. The app's own command line (`./formpdf run`, `./formpdf cli`) counts as an outside agent and follows the same choice. The server listens on 127.0.0.1 only.

### Undoing an agent

Expand Down
Loading
Loading