Copy safety

community mirrored

Every page is checked for what isn’t safe before it is written into the vault, by mirror, fork or update, and the user is asked first. The preview defuses a page for rendering (preview.md); this is for what is written, which nobody may have seen.

Why ask first

  • A single fork opens its note as soon as it is written, so a dataviewjs block runs at once.
  • Templater, set to process new files, runs <% %> commands as a note is created, front matter included. A notice afterwards is too late.
  • An update overwrites a page already trusted: a later upstream version can add code to it, unseen.

What is asked about

What runs once it is a note (runs(), src/copy/risk.ts): code blocks a plugin runs, inline JavaScript, Templater commands, HTML that runs or embeds, event handlers. That is the preview’s Found less web. A page longer than SCAN_LIMIT asks too: the rest wasn’t looked through. Web images and embeds alone ask nothing; the notice afterwards names the pages that have them.

Mirrors are written as is, never converted: a mirror is upstream’s copy, and update and compare tell edits by matching the version it came from; a converted one would read as edited. Forks can be wrapped for review (below). defused() is for rendering only: its zero-width spaces are invisible, and in a note they would break queries and copied code unseen.

Where a copy may go

Never outside the vault, nor in its config folder (Vault#configDir) or any hidden file or folder, which Obsidian neither shows nor indexes, so a file already there would go unseen (vaultPathProblem(), src/util/validate.ts). Every note and attachment written by mirror, fork or update, and a mirror moved to become a fork, is checked first (needVaultPath()); one that fails isn’t written, and the notice says why.

Nor in the plugin’s own folders (placeProblem(), src/copy/place.ts; needCopyPath(), src/copy/place-run.ts): nothing in follows/ or the clones folder, and no fork in a followed source’s mirror folder (mirrorFolderOf()), where it would pass for upstream’s. The community folder around them takes forks. The fork modal checks the plan first (VaultLook.refused), so it says why before writing. Upstream’s paths are made safe before this (git.md), so this catches a folder from settings or a follow note, and a repo’s own hidden folders (.github/).

When

  • Mirror (check view, command): every page to mirror is read first (host.blobs(), one fetch), checked, and asked about once, then written. Already mirrored or taken: not read.
  • Fork: after the fork modal’s choice, before writing; not before the modal, which would open slower for every fork. The question opens over the modal. A mirror forked isn’t checked: it is in the vault already, and moving a file isn’t creating one.
  • Updates: Update and Update mirrors, Update selected forks. The new version is checked like a new copy, whatever the old one held: simple, at the cost of a question at every update of a source of Dataview pages. A file left out isn’t updated and stays listed. No Preview link: the page is copied already.
  • Not asked: Mirror and Fork in the Preview view, Overwrite and Write alongside in the Upstream view. Its alert was on screen; asking again would loop preview → question → preview.

The question

One per batch (askToCopy(), src/ui/copy-question.ts), naming each page and what it holds, a sentence each (runsText()).

  • One page: Preview, Cancel, Fork for review (forks only), Mirror anyway / Fork anyway / Update anyway.
  • Several: each page with a Preview link, then Cancel, Mirror the others (and so on: when some pages ask nothing), Fork for review (the pages asked about wrapped, the others as they are), Mirror all anyway.

Preview writes nothing: the batch is cancelled, so nothing is half done, and the page opens in the Preview view, where it can be copied. Cancelling the fork question leaves the fork modal open, saying nothing was forked. Pages left out are named in the notice.

Fork for review

The page written whole inside a fence, exactly as upstream wrote it (wrap(), src/copy/review.ts), and fork_review: true added:

---
…upstream's front matter, the fork_* keys, fork_review: true…
---
<!-- Forked as plain text to review: remove these fence lines to make it a note. -->
`````made-safe-txt
…the page, exactly as upstream wrote it…

- **Front matter stays outside**: the `fork_*` keys that make it a fork are written into it.
- **The fence**: five backticks, or one more than the longest run of backticks at a line start
  (up to three spaces in), so nothing in the page closes it. Always closed, last.
- **What runs**: inside, code blocks, inline queries, HTML, embeds and links are all text: no
  plugin runs them, and nothing is linked. Not Templater: it runs `<% %>` anywhere, fences and
  front matter included. The question says so; reading Templater's settings to tell whether it
  would comes later.
- **Why wrap, not convert each form**: the text stays upstream's, so `fork_blob` stays true, and
  forms nobody listed are covered too.

Told by its text, not the key (`isWrapped()`): the comment line, the opening fence, and the same
fence closing it, last. A key left after unwrapping by hand does no harm.

- **Edited?** (`isEdited()`): a wrapped body is compared unwrapped, and `fork_review` is left out
  like the `fork_*` keys. So compare, update and merge base work on upstream's text.
- **Overwrite** and **Update selected forks** write a wrapped fork's new version wrapped; **Write
  alongside** beside one writes wrapped.
- **Make this fork for review a note** (command `unwrap-fork`, active note): unwraps it and
  removes `fork_review`. When what it holds runs, it asks first, saying what. A change, not a
  creation, so Templater's trigger on new files doesn't fire. Not wrapped: removes the key.
- Links in a wrapped fork aren't converted: Obsidian sees none in a code block.

## Cost

About 1 ms a page (`defuse()`: 0.3 ms for 5,000 characters, 8 ms for 500,000). No extra network:
the pages were read anyway, now all before the first is written.

## Later

Worth it if the questions nag:

- Read Templater's settings: ask about `<% %>` only when it would run (trigger on new files on,
  the note not in a folder it skips).
- Ask about an update only when it adds something the old version didn't have (`defuse()` both,
  compare `Found`). Update and compare read the old version already. Likewise, a file whose
  copies are all edited is asked about though none would be overwritten.
- Remember a page accepted, per upstream path and blob.
- Attachments (`.svg`, `.html`): not checked. Obsidian shows an SVG as an image, where its
  scripts don't run.
- Dataview's inline prefixes can be changed in its settings; only the defaults (`=`, `$=`) are
  known. Other plugins' code block languages: `RUNS_CODE` lists those known.

## Code

Pure: `src/copy/risk.ts` on `src/copy/safe.ts`. Vault: `src/copy/risk-run.ts`
(`checkBeforeCopy()`), called from `mirrorFiles()` (`src/mirror/run.ts`) and `startFork()`
(`src/fork/run.ts`), each with a `checked` option the Preview view sets; `updateMirror()`,
`updateMirrors()` (`src/mirror/update-run.ts`); `updateForks()` (`src/fork/update-run.ts`).
Fork for review: pure `src/copy/review.ts`; the command `src/fork/review-run.ts`. UI:
`src/ui/copy-question.ts`.