Beautiful, AI-native markdown IDE and LLM wiki
TypeScript 2nd-brainagent-skillsclaudecodexcompany-braindocsknowledge-knowledge-baseknowledge-managementllmllm-wikillm-wiki-karpathymarkdownmarkdown-editormdmdxnotespkmsecond-brainskills
fix(open-knowledge): allow EDITOR env in clone (allowUnsafeEditor) (#2560)
mike-inkeep84a37940c489
diff --git a/.changeset/clone-allow-unsafe-editor.md b/.changeset/clone-allow-unsafe-editor.mdnew file mode 100644index 000000000..eb9672b2d--- /dev/null+++ b/.changeset/clone-allow-unsafe-editor.md@@ -0,0 +1,7 @@+---+"@inkeep/open-knowledge": patch+---++Fix "clone to a new folder" (Open shared document) failing with `Use of "EDITOR" is not permitted without enabling allowUnsafeEditor` for anyone who has `EDITOR` or `GIT_EDITOR` set in their environment — i.e. most developers. `ok clone` runs git as the user with the user's own environment, and simple-git 3.36 refuses to run when it sees an editor env var present (it checks presence, not value) unless explicitly told the env is trusted. The clone path already opted into the sibling PAGER / SSH / askpass flags for the same reason; this adds the missing `allowUnsafeEditor` opt-in. A clone never launches an editor, so honoring the env is safe.++Also hardens the background sync path: the server's `git merge` now runs with `GIT_MERGE_AUTOEDIT=no`, so a merge commit can never launch an editor and hang the TTY-less sync process.diff --git a/packages/cli/src/commands/clone.test.ts b/packages/cli/src/commands/clone.test.tsindex 9378c4b60..81f390d70 100644--- a/packages/cli/src/commands/clone.test.ts+++ b/packages/cli/src/commands/clone.test.ts@@ -4,6 +4,7 @@ import { mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs';import { tmpdir } from 'node:os';import { join, resolve } from 'node:path';import type { Config } from '@inkeep/open-knowledge-server';+import simpleGit, { type SimpleGitOptions } from 'simple-git';import type { TokenStore } from '../auth/token-store.ts';import { OK_DIR } from '../constants.ts';import {@@ -174,10 +175,12 @@ describe('buildCloneEnv', () => {describe('buildCloneGitOptions', () => {// `ok clone` runs git as the user with the user's env; simple-git refuses to- // run with PAGER / GIT_SSH_COMMAND / GIT_ASKPASS present unless these flags- // opt in. Reverting any of them silently re-breaks clone for users who set- // those env vars (the reported `PAGER is not permitted` failure).- test('opts into the env-based unsafe flags so the user PAGER/SSH/askpass env is honored', () => {+ // run with PAGER / GIT_SSH_COMMAND / GIT_ASKPASS / EDITOR / GIT_EDITOR present+ // unless these flags opt in. Reverting any of them silently re-breaks clone+ // for users who set those env vars — allowUnsafeEditor in particular, since+ // nearly every developer exports EDITOR (the reported `Use of "EDITOR" is not+ // permitted` failure; PAGER had the same failure mode).+ test('opts into the env-based unsafe flags so the user PAGER/SSH/askpass/editor env is honored', () => {const o = buildCloneGitOptions('/work/dir', ['credential.helper=!gh auth git-credential']);expect(o.baseDir).toBe('/work/dir');expect(o.config).toEqual(['credential.helper=!gh auth git-credential']);@@ -186,6 +189,7 @@ describe('buildCloneGitOptions', () => {allowUnsafePager: true,allowUnsafeSshCommand: true,allowUnsafeAskPass: true,+ allowUnsafeEditor: true,});});@@ -196,6 +200,43 @@ describe('buildCloneGitOptions', () => {});});+// Integration guard against the exact reported failure: drives a real+// `git --version` through simple-git with EDITOR present in the spawn env.+// This would have thrown before the fix and passes after — a stronger+// regression guard than the static shape pin above (it also catches+// simple-git renaming the flag on a version bump). EDITOR is injected via+// `.env()` (never `process.env`), so it's deterministic on CI runners that+// don't export EDITOR and safe under Bun's concurrent runner. The env is+// minimal (PATH + EDITOR only) so the guard fires specifically on EDITOR,+// not on an ambient PAGER.+describe('buildCloneGitOptions — simple-git EDITOR env guard', () => {+ const editorEnv: Record<string, string> = {+ PATH: process.env.PATH ?? '',+ EDITOR: 'vim',+ };++ // simple-git's `.raw()` returns a chainable thenable, not a plain Promise, so+ // the `.resolves`/`.rejects` matchers reject it — await directly instead.+ it('negative control: without allowUnsafeEditor the guard rejects when EDITOR is set', async () => {+ const git = simpleGit({ baseDir: tmpdir() } as Partial<SimpleGitOptions>).env(editorEnv);+ let err: unknown;+ try {+ await git.raw(['--version']);+ } catch (e) {+ err = e;+ }+ expect(String(err instanceof Error ? err.message : err)).toContain('allowUnsafeEditor');+ });++ it('buildCloneGitOptions honors EDITOR — git runs instead of tripping the guard', async () => {+ const git = simpleGit(buildCloneGitOptions(tmpdir(), []) as Partial<SimpleGitOptions>).env(+ editorEnv,+ );+ const out = await git.raw(['--version']);+ expect(out).toContain('git version');+ });+});+describe('shouldSkipAuthForPublicRepo', () => {test('https + github.com + isPublic=true → true (anonymous clone path)', () => {expect(shouldSkipAuthForPublicRepo('https', 'github.com', true)).toBe(true);diff --git a/packages/cli/src/commands/clone.ts b/packages/cli/src/commands/clone.tsindex f7094aa79..730434671 100644--- a/packages/cli/src/commands/clone.ts+++ b/packages/cli/src/commands/clone.ts@@ -154,6 +154,7 @@ type CredentialHelperUnsafeGitOptions = SimpleGitOptions & {allowUnsafePager?: boolean;allowUnsafeSshCommand?: boolean;allowUnsafeAskPass?: boolean;+ allowUnsafeEditor?: boolean;};};@@ -162,12 +163,18 @@ type CredentialHelperUnsafeGitOptions = SimpleGitOptions & {* running git as the user with the user's own environment (`buildCloneEnv`* spreads `process.env`), so we opt into the env-based `unsafe` flags simple-git* gates by default: it refuses to run when PAGER / GIT_SSH_COMMAND / GIT_ASKPASS- * are present in the env unless told they're trusted. That guard targets- * untrusted config/args in server-side usage; here the env IS the user's own- * interactive shell, so honoring it is correct — and lets their pager, SSH- * config, and credential-prompt helper actually work (an attacker who can set- * these env vars already owns the shell). `allowUnsafeCredentialHelper` is the- * same posture for the `-c credential.helper` we inject. simple-git 3.36's+ * / EDITOR / GIT_EDITOR are present in the env unless told they're trusted. That+ * guard targets untrusted config/args in server-side usage; here the env IS the+ * user's own interactive shell, so honoring it is correct — and lets their+ * pager, SSH config, and credential-prompt helper actually work (an attacker who+ * can set these env vars already owns the shell). `allowUnsafeCredentialHelper`+ * is the same posture for the `-c credential.helper` we inject.+ *+ * `allowUnsafeEditor` is load-bearing for the common case: nearly every+ * developer has EDITOR (and often GIT_EDITOR) exported, and the guard fires on+ * the var's mere presence regardless of value — so without this flag, clone+ * fails with `Use of "EDITOR" is not permitted…` for most users. A clone never+ * launches an editor anyway, so honoring the env is safe. simple-git 3.36's* published typings don't expose these runtime flags, hence the local type.*/export function buildCloneGitOptions(@@ -182,6 +189,7 @@ export function buildCloneGitOptions(allowUnsafePager: true,allowUnsafeSshCommand: true,allowUnsafeAskPass: true,+ allowUnsafeEditor: true,},};}diff --git a/packages/server/src/git-handle.test.ts b/packages/server/src/git-handle.test.tsindex beab2ec26..35ae6ede5 100644--- a/packages/server/src/git-handle.test.ts+++ b/packages/server/src/git-handle.test.ts@@ -42,6 +42,13 @@ describe('buildGitEnv', () => {expect(buildGitEnv().GIT_TERMINAL_PROMPT).toBe('0');});+ test('disables merge auto-edit so a sync merge commit never launches an editor', () => {+ // sync-engine's `git merge origin/<branch>` can produce a merge commit; a+ // launched editor with no TTY would hang the background sync. Pinning this+ // off makes git use the default merge message unconditionally.+ expect(buildGitEnv().GIT_MERGE_AUTOEDIT).toBe('no');+ });+test('preserves PATH so a bare-command credential helper resolves', () => {withEnvEntries({ PATH: '/custom/bin:/usr/bin' }, () => {expect(buildGitEnv().PATH).toBe('/custom/bin:/usr/bin');diff --git a/packages/server/src/git-handle.ts b/packages/server/src/git-handle.tsindex b3ab8f9b7..6a9a5dac9 100644--- a/packages/server/src/git-handle.ts+++ b/packages/server/src/git-handle.ts@@ -121,13 +121,28 @@ const GIT_AUTH_ENV_KEYS = [* classifier maps to the reconnect-required auth state. Both strings classify* as no-credential, but this avoids a misleading errno in logs and the UI.*+ * `GIT_MERGE_AUTOEDIT=no` is set unconditionally for the same non-interactive+ * reason: `sync-engine`'s `git merge origin/<branch>` is the one sync op that+ * can produce a merge commit, and git may open an editor for its message. In a+ * TTY-less spawn git usually skips that, but the outcome is version- and+ * global-config-dependent (a user's `[merge] edit = true` can force it), and a+ * launched editor with no TTY hangs the background sync. Pinning auto-edit off+ * makes git use the default merge message and never launch an editor. Unlike+ * setting GIT_EDITOR, this doesn't trip simple-git's env guard (the var isn't in+ * its unsafe map), so it needs no `allowUnsafe*` opt-in.+ ** `OK_GH_TOKEN`/`OK_GH_TOKEN_HOST` are added only when a {@link RelayGhToken} is* supplied. This is the deliberate, named channel that carries a server-resolved* gh token to the credential helper across the env replacement — see* {@link RelayGhToken}.*/export function buildGitEnv(ghToken?: RelayGhToken): Record<string, string> {- const env: Record<string, string> = { LANG: 'C', LC_ALL: 'C', GIT_TERMINAL_PROMPT: '0' };+ const env: Record<string, string> = {+ LANG: 'C',+ LC_ALL: 'C',+ GIT_TERMINAL_PROMPT: '0',+ GIT_MERGE_AUTOEDIT: 'no',+ };const path = process.env.PATH ?? process.env.Path;if (path !== undefined) {env.PATH = path;