Beautiful, AI-native markdown IDE and LLM wiki
TypeScript 2nd-brainagent-skillsclaudecodexcompany-braindocsknowledge-knowledge-baseknowledge-managementllmllm-wikillm-wiki-karpathymarkdownmarkdown-editormdmdxnotespkmsecond-brainskills
Honour GIT_CONFIG_COUNT and use the CLI's own git credential helper (#5026)
Nick Gomez78957494137f
diff --git a/.changeset/clone-honour-git-config-env.md b/.changeset/clone-honour-git-config-env.mdnew file mode 100644index 000000000..adb8816ea--- /dev/null+++ b/.changeset/clone-honour-git-config-env.md@@ -0,0 +1,5 @@+---+"@inkeep/open-knowledge": patch+---++`ok clone` and `ok share publish` no longer fail just because the environment passes Git settings through the `GIT_CONFIG_COUNT` environment variables, as it can when Claude Code runs them; Git now applies those settings.diff --git a/.changeset/share-publish-own-credential-helper.md b/.changeset/share-publish-own-credential-helper.mdnew file mode 100644index 000000000..1c0da6f43--- /dev/null+++ b/.changeset/share-publish-own-credential-helper.md@@ -0,0 +1,5 @@+---+"@inkeep/open-knowledge": patch+---++Publishing to GitHub no longer copies your token into other credential stores, and `ok sync` without the app uses the app's GitHub sign-in. If a private `ok clone`, your own `git` or the app's **Open in worktree** used that copy, set up Git credentials, for example with `gh auth setup-git`.diff --git a/docs/content/reference/cli.mdx b/docs/content/reference/cli.mdxindex 6f78860f6..449265b18 100644--- a/docs/content/reference/cli.mdx+++ b/docs/content/reference/cli.mdx@@ -110,7 +110,7 @@ Sync a project with its remote from the terminal:| `ok pull` | Pull changes from the remote || `ok push` | Push commits to the remote |-Each command goes through the running server when one is up and falls back to plain git otherwise; pass `--json` for JSONL progress events when scripting. See [GitHub sync](/docs/features/github-sync) for how syncing works.+Each command goes through the running server when one is up. Otherwise it runs git directly. When the remote's URL is `https://` and names `github.com` or a declared GitHub Enterprise host exactly, in lowercase and with no port, and OpenKnowledge has a GitHub sign-in for it ([which account is used](/docs/features/github-sync#which-github-account-is-used)), that sign-in replaces your git credential helpers for HTTPS requests to that host during the command, though a `~/.netrc` entry for the host is still used before any helper. The sign-in answers only that exact spelling of the host: for a submodule or extra push URL that writes it with an explicit `:443` or in different letter case, git asks a configured askpass program (`GIT_ASKPASS`, `core.askPass` or `SSH_ASKPASS`; VS Code's integrated terminal sets one), and the request fails when none is configured. Requests to another host, a non-default port or a plain `http://` URL use your own git credentials. Pass `--json` for JSONL progress events when scripting. See [GitHub sync](/docs/features/github-sync) for how syncing works.## Manage local serversdiff --git a/docs/content/reference/configuration.mdx b/docs/content/reference/configuration.mdxindex 099fddc6f..997048159 100644--- a/docs/content/reference/configuration.mdx+++ b/docs/content/reference/configuration.mdx@@ -58,7 +58,7 @@ A key set in a file more specific than its scope (a user-scope key in `.ok/confi| `autoSync.pushIntervalSeconds` | `number` | `60` | project-local | Seconds between scheduled pushes while `autoSync.mode` is `full` (30–3600). Shown in **Settings → Sync** as **Push my edits every**. Ignored in every other mode, which never pushes on a schedule. Each cycle with pending edits authors a commit, so a shorter interval means more, smaller commits in shared history. || `autoSync.enabled` | `boolean \| null` | `null` | project-local | Legacy per-machine auto-sync toggle, superseded by `autoSync.mode`. Read only when `mode` is absent (`true` = full, `false` = off). `null` means "unanswered". || `autoSync.default` | `"off" \| "follow" \| "full" \| boolean \| null` | `null` | project | Committed seed for each machine's first-open sync mode: `off` / `follow` / `full`, or the legacy boolean (`true` = full, `false` = off). `null` = ask. Lets a maintainer pre-answer the onboarding prompt for everyone who clones; a per-machine `autoSync.mode` overrides it. Compatibility note: a committed string value (`off` / `follow` / `full`) is rejected by app versions released before `autoSync.mode` existed, resetting that machine to config defaults until it updates (it never silently syncs). The legacy boolean seed (`true` / `false`) stays readable by older apps — commit `follow` only once collaborators are on a current version. |-| `git.hosts.<host>.provider` | `"github"` | (unset) | user | Declares that `<host>` is a GitHub Enterprise Server instance, so OpenKnowledge treats remotes on it the way it treats `github.com`: OpenKnowledge's own credential handling, the push-permission check, share links, and the default host for `ok auth`. `github` is the only accepted value today. Shown in **Settings → Account** as **GitHub Enterprise Server hosts**, which adds and removes entries for you. A host that is neither `github.com` nor declared here is treated as a generic git remote, so OpenKnowledge leaves the machine's own git credentials alone and reports push permission as unknown instead of pausing sync. Per-machine: one entry covers every project on this machine, and a declaration committed in a project's `.ok/config.yml` is ignored. Read at server start. Matching uses the hostname without its port, across transports. Invalid entries produce config diagnostics without erasing valid siblings. Undeclared origins cause GitHub-specific CLI commands and project sign-in requests to refuse; CLI commands can still target a declared host with `--host`. |+| `git.hosts.<host>.provider` | `"github"` | (unset) | user | Declares that `<host>` is a GitHub Enterprise Server instance, so OpenKnowledge treats remotes on it the way it treats `github.com`: OpenKnowledge's own credential handling, the push-permission check, share links, and the default host for `ok auth`. `github` is the only accepted value today. Shown in **Settings → Account** as **GitHub Enterprise Server hosts**, which adds and removes entries for you. A host that is neither `github.com` nor declared here is treated as a generic git remote, so OpenKnowledge leaves the machine's own git credentials alone and reports push permission as unknown instead of pausing sync. Per-machine: one entry covers every project on this machine, and a declaration committed in a project's `.ok/config.yml` is ignored. Read at server start. Matching uses the hostname without its port, across transports. Without a running server, `ok sync`, `ok pull` and `ok push` use OpenKnowledge's sign-in only for an `https://` remote that names the host in lowercase and with no port; any other form, even an explicit `:443`, keeps your own git credentials. Invalid entries produce config diagnostics without erasing valid siblings. Undeclared origins cause GitHub-specific CLI commands and project sign-in requests to refuse; CLI commands can still target a declared host with `--host`. || `server.bind` | list of addresses | `["127.0.0.1"]` | project-local | Addresses the server binds, e.g. `[127.0.0.1]` or `[0.0.0.0]`. The loopback default keeps everything off-machine out. A non-loopback bind additionally requires the `server.allowExternal` consent interlock. Per-machine — a value committed to `.ok/config.yml` is ignored (the exposing host would otherwise refuse to boot for every teammate who clones and runs locally, since the interlock needs per-machine consent that is never committed). Set it where the server is exposed: `OK_BIND` (space-separated), `ok start --bind <address>`, or `.ok/local/config.yml`. Lists replace, never merge. Read at server start. || `server.port` | number | (unset) | project | TCP port the server listens on. Unset = a local start picks a free port dynamically (deployment platforms inject `PORT` instead). Read at server start. || `server.externalUrl` | string | (unset) | project | Canonical external origin the server is reached at, e.g. `https://kb.example.com` — its host joins the Host/Origin allowlists and issued URLs. Setting it declares external exposure, which additionally requires the `server.allowExternal` consent interlock. Env: `OK_EXTERNAL_URL`; flag: `ok start --external-url <url>`. Read at server start. |diff --git a/packages/cli/src/auth/resolve-auth.ts b/packages/cli/src/auth/resolve-auth.tsindex f869aeaad..b30e71056 100644--- a/packages/cli/src/auth/resolve-auth.ts+++ b/packages/cli/src/auth/resolve-auth.ts@@ -22,9 +22,20 @@ interface ResolveAuthOptions {selfCliArgs?: readonly string[];}+function cliCredentialHelperCommand(selfCliArgs: readonly string[]): string {+ return `!${selfCliArgs.map(shellSingleQuote).join(' ')} auth git-credential`;+}+export function buildCliCredentialHelper(selfCliArgs: readonly string[]): string {- const prefix = selfCliArgs.map(shellSingleQuote).join(' ');- return `credential.helper=!${prefix} auth git-credential`;+ return `credential.helper=${cliCredentialHelperCommand(selfCliArgs)}`;+}++export function buildHttpsHostCliCredentialConfig(+ selfCliArgs: readonly string[],+ host: string,+): string[] {+ const helper = `credential.https://${host}.helper`;+ return [`${helper}=`, `${helper}=${cliCredentialHelperCommand(selfCliArgs)}`];}export async function resolveAuth(diff --git a/packages/cli/src/commands/clone-git-config-env.test.ts b/packages/cli/src/commands/clone-git-config-env.test.tsnew file mode 100644index 000000000..4e09754f6--- /dev/null+++ b/packages/cli/src/commands/clone-git-config-env.test.ts@@ -0,0 +1,350 @@+import { execFileSync } from 'node:child_process';+import {+ existsSync,+ mkdirSync,+ mkdtempSync,+ readFileSync,+ realpathSync,+ rmSync,+ writeFileSync,+} from 'node:fs';+import { createServer } from 'node:http';+import type { AddressInfo } from 'node:net';+import { tmpdir } from 'node:os';+import { dirname, join } from 'node:path';+import { shellSingleQuote } from '@inkeep/open-knowledge-core';+import simpleGit, { GitPluginError, type SimpleGitOptions } from 'simple-git';+import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest';+import type { GhDetectResult } from '../auth/gh-detect.ts';+import { FileBackend } from '../auth/token-store.ts';+import {+ buildCloneArgs,+ buildCloneAuthEnv,+ buildCloneEnv,+ buildCloneGitOptions,+ handleCloneFailure,+ resolveCloneAuth,+ runClone,+} from './clone.ts';++const relayTokenGh = (): GhDetectResult => ({ available: true, token: 'ghs_relay_probe' });++const agentHarnessGitConfig = {+ GIT_CONFIG_COUNT: '2',+ GIT_CONFIG_KEY_0: 'credential.interactive',+ GIT_CONFIG_VALUE_0: 'false',+ GIT_CONFIG_KEY_1: 'credential.guiPrompt',+ GIT_CONFIG_VALUE_1: 'false',+};++function seedBareRepo(bareDir: string, readme: string): void {+ const seedDir = `${bareDir}.seed`;+ mkdirSync(seedDir, { recursive: true });+ const git = (cwd: string, args: string[]) => execFileSync('git', args, { cwd, stdio: 'ignore' });+ git(seedDir, ['init', '--initial-branch=main']);+ writeFileSync(join(seedDir, 'README.md'), readme, 'utf-8');+ git(seedDir, ['add', 'README.md']);+ git(seedDir, [+ '-c',+ 'user.name=Seed',+ '-c',+ '[email protected]',+ '-c',+ 'commit.gpgsign=false',+ 'commit',+ '-m',+ 'seed',+ ]);+ git(dirname(bareDir), ['clone', '--bare', seedDir, bareDir]);+}++function writeCredentialHelper(helperPath: string, reply: string): string {+ writeFileSync(+ helperPath,+ [+ "import { appendFileSync, readFileSync } from 'node:fs';",+ 'readFileSync(0);',+ 'const call = { argv: process.argv.slice(2), relayToken: process.env.OK_GH_TOKEN ?? null };',+ "appendFileSync(process.argv[1] + '.calls', JSON.stringify(call) + '\\n');",+ `if (process.argv.at(-1) === 'get') process.stdout.write(${JSON.stringify(`username=${reply}\npassword=${reply}-secret\n`)});`,+ '',+ ].join('\n'),+ 'utf-8',+ );+ return helperPath;+}++function helperCalls(helperPath: string): unknown[] {+ const callLog = `${helperPath}.calls`;+ if (!existsSync(callLog)) return [];+ return readFileSync(callLog, 'utf-8')+ .split('\n')+ .filter((line) => line !== '')+ .map((line) => JSON.parse(line) as unknown);+}++function helperConfigValue(helperPath: string): string {+ return `!${shellSingleQuote(process.execPath)} ${shellSingleQuote(helperPath)}`;+}++async function listenUnauthorized(): Promise<{ port: number; close: () => Promise<void> }> {+ const server = createServer((req, res) => {+ req.resume();+ res.writeHead(401, { 'WWW-Authenticate': 'Basic realm="ok-test"' });+ res.end();+ });+ await new Promise<void>((resolveListen) => server.listen(0, '127.0.0.1', resolveListen));+ const { port } = server.address() as AddressInfo;+ return {+ port,+ close: () => new Promise<void>((resolveClose) => server.close(() => resolveClose())),+ };+}++describe('clone honours the environment command-scope git config (GIT_CONFIG_COUNT)', () => {+ let workspace: string;++ beforeEach(() => {+ workspace = realpathSync(mkdtempSync(join(tmpdir(), 'ok-clone-envcfg-')));+ vi.stubEnv('HOME', workspace);+ vi.stubEnv('USERPROFILE', workspace);+ vi.stubEnv('XDG_CONFIG_HOME', join(workspace, '.config'));+ vi.stubEnv('GIT_CONFIG_NOSYSTEM', '1');+ vi.spyOn(process.stdout, 'write').mockImplementation(() => true);+ });++ afterEach(() => {+ vi.restoreAllMocks();+ vi.unstubAllEnvs();+ rmSync(workspace, { recursive: true, force: true });+ });++ test('runClone clones through an environment url rewrite and names the remote from the environment', async () => {+ seedBareRepo(join(workspace, 'o', 'r.git'), '# seeded through the environment\n');+ vi.stubEnv('GIT_CONFIG_COUNT', '2');+ vi.stubEnv('GIT_CONFIG_KEY_0', 'url../.insteadOf');+ vi.stubEnv('GIT_CONFIG_VALUE_0', 'https://127.0.0.1:1/');+ vi.stubEnv('GIT_CONFIG_KEY_1', 'clone.defaultRemoteName');+ vi.stubEnv('GIT_CONFIG_VALUE_1', 'ok-env-remote');+ const targetDir = join(workspace, 'target');++ await expect(+ runClone(+ 'https://127.0.0.1:1/o/r.git',+ { json: true, dir: 'target', _detectGhFn: relayTokenGh },+ {} as never,+ workspace,+ ),+ ).resolves.toBe(targetDir);++ expect(readFileSync(join(targetDir, 'README.md'), 'utf-8')).toBe(+ '# seeded through the environment\n',+ );+ expect(execFileSync('git', ['remote'], { cwd: targetDir, encoding: 'utf-8' }).trim()).toBe(+ 'ok-env-remote',+ );+ });++ test("runClone asks the CLI's own credential helper and never an environment-configured one", async () => {+ const okHelper = writeCredentialHelper(join(workspace, 'ok-cli.mjs'), 'ok');+ const envHelper = writeCredentialHelper(join(workspace, 'env-helper.mjs'), 'env');+ vi.stubEnv('GIT_CONFIG_COUNT', '1');+ vi.stubEnv('GIT_CONFIG_KEY_0', 'credential.helper');+ vi.stubEnv('GIT_CONFIG_VALUE_0', helperConfigValue(envHelper));+ const server = await listenUnauthorized();+ const cliEntry = process.argv[1];+ process.argv[1] = okHelper;+ try {+ await expect(+ runClone(+ `http://127.0.0.1:${server.port}/o/r.git`,+ { json: true, dir: 'target', _detectGhFn: relayTokenGh },+ {} as never,+ workspace,+ ),+ ).rejects.toThrow('Authentication failed');+ } finally {+ process.argv[1] = cliEntry;+ await server.close();+ }++ expect(helperCalls(okHelper)).toContainEqual({+ argv: ['auth', 'git-credential', 'get'],+ relayToken: 'ghs_relay_probe',+ });+ expect(helperCalls(envHelper)).toEqual([]);+ });++ test('the stored-token tier asks its own credential helper and never an environment-configured one', async () => {+ const okHelper = writeCredentialHelper(join(workspace, 'ok-cli.mjs'), 'ok');+ const envHelper = writeCredentialHelper(join(workspace, 'env-helper.mjs'), 'env');+ const tokenStore = new FileBackend(join(workspace, 'auth.yml'));+ await tokenStore.set('127.0.0.1', 'alice', 'ghp_stored_probe', { gitProtocol: 'https' });+ const server = await listenUnauthorized();+ try {+ const url = `http://127.0.0.1:${server.port}/o/r.git`;+ const { auth } = await resolveCloneAuth(url, tokenStore, {+ selfCliArgs: [process.execPath, okHelper],+ cwd: workspace,+ _detectGhFn: () => ({ available: false }),+ });+ expect(auth.tier).toBe('B');+ const git = simpleGit(+ buildCloneGitOptions(workspace, auth.gitConfig) as Partial<SimpleGitOptions>,+ ).env(+ buildCloneAuthEnv(auth, {+ PATH: process.env.PATH ?? '',+ HOME: workspace,+ GIT_CONFIG_NOSYSTEM: '1',+ GIT_CONFIG_COUNT: '1',+ GIT_CONFIG_KEY_0: 'credential.helper',+ GIT_CONFIG_VALUE_0: helperConfigValue(envHelper),+ }),+ );++ await expect(git.clone(url, join(workspace, 'target'), buildCloneArgs(null))).rejects.toThrow(+ 'Authentication failed',+ );+ } finally {+ await server.close();+ }++ expect(helperCalls(okHelper)).toContainEqual({+ argv: ['auth', 'git-credential', 'get'],+ relayToken: null,+ });+ expect(helperCalls(envHelper)).toEqual([]);+ });++ test.each([+ {+ pair: 'share publish pair',+ open: async (dir: string, sourceEnv: NodeJS.ProcessEnv) =>+ simpleGit(buildCloneGitOptions(dir, []) as Partial<SimpleGitOptions>).env(+ buildCloneEnv(sourceEnv),+ ),+ },+ {+ pair: 'ok clone pair',+ open: async (dir: string, sourceEnv: NodeJS.ProcessEnv) => {+ const { auth } = await resolveCloneAuth(+ 'https://github.com/o/r',+ new FileBackend(join(dir, 'auth.yml')),+ { selfCliArgs: ['open-knowledge'], cwd: dir, _detectGhFn: relayTokenGh },+ );+ return simpleGit(+ buildCloneGitOptions(dir, auth.gitConfig) as Partial<SimpleGitOptions>,+ ).env(buildCloneAuthEnv(auth, sourceEnv));+ },+ },+ ])(+ '$pair: git reads the environment credential.interactive at command scope',+ async ({ open }) => {+ const git = await open(workspace, {+ PATH: process.env.PATH ?? '',+ HOME: workspace,+ GIT_CONFIG_NOSYSTEM: '1',+ ...agentHarnessGitConfig,+ });++ await expect(+ git+ .raw(['config', '--show-scope', '--get', 'credential.interactive'])+ .then((out) => out.trim()),+ ).resolves.toBe('command\tfalse');+ },+ );++ test.each([+ { key: 'core.hooksPath', value: 'hooks', category: 'allowUnsafeHooksPath' },+ { key: 'core.fsmonitor', value: 'true', category: 'allowUnsafeFsMonitor' },+ { key: 'protocol.file.allow', value: 'always', category: 'allowUnsafeProtocolOverride' },+ ])('an environment $key stays refused under $category', async ({ key, value, category }) => {+ const git = simpleGit(buildCloneGitOptions(workspace, []) as Partial<SimpleGitOptions>).env(+ buildCloneEnv({+ PATH: process.env.PATH ?? '',+ HOME: workspace,+ GIT_CONFIG_COUNT: '1',+ GIT_CONFIG_KEY_0: key,+ GIT_CONFIG_VALUE_0: value,+ }),+ );++ await expect(git.raw(['--version'])).rejects.toThrow(category);+ });++ test('runClone refuses an environment core.hooksPath under allowUnsafeHooksPath', async () => {+ vi.stubEnv('GIT_CONFIG_COUNT', '1');+ vi.stubEnv('GIT_CONFIG_KEY_0', 'core.hooksPath');+ vi.stubEnv('GIT_CONFIG_VALUE_0', join(workspace, 'hooks'));++ await expect(+ runClone(+ 'https://127.0.0.1:1/o/r.git',+ { json: true, dir: 'target', _detectGhFn: relayTokenGh },+ {} as never,+ workspace,+ ),+ ).rejects.toThrow('allowUnsafeHooksPath');+ });++ test("ok clone's failure message names the GIT_CONFIG_* variables that passed a refused setting", async () => {+ vi.stubEnv('GIT_CONFIG_COUNT', '1');+ vi.stubEnv('GIT_CONFIG_KEY_0', 'core.hooksPath');+ vi.stubEnv('GIT_CONFIG_VALUE_0', join(workspace, 'hooks'));+ const url = 'https://127.0.0.1:1/o/r.git';+ const error = await runClone(+ url,+ { json: true, dir: 'target', _detectGhFn: relayTokenGh },+ {} as never,+ workspace,+ ).catch((err: unknown) => err);+ expect(error).toBeInstanceOf(GitPluginError);+ const refusal = (error as GitPluginError).message;+ const emitted: Record<string, unknown>[] = [];+ const stderr: string[] = [];+ const report = (json: boolean) =>+ handleCloneFailure({+ error,+ url,+ branch: null,+ json,+ emit: (event) => emitted.push(event),+ printStderr: (text) => stderr.push(text),+ });++ await report(true);+ await report(false);++ expect(emitted).toEqual([{ type: 'error', message: refusal }]);+ const out = stderr.join('');+ expect(out).toContain('GIT_CONFIG_KEY_<n>');+ expect(out).toContain(refusal);+ });++ test("ok clone's failure message does not blame GIT_CONFIG_KEY_<n> for a variable refused by name", async () => {+ vi.stubEnv('GIT_CONFIG_GLOBAL', join(workspace, 'global.gitconfig'));+ const url = 'https://127.0.0.1:1/o/r.git';+ const error = await runClone(+ url,+ { json: true, dir: 'target', _detectGhFn: relayTokenGh },+ {} as never,+ workspace,+ ).catch((err: unknown) => err);+ expect(error).toBeInstanceOf(GitPluginError);+ const stderr: string[] = [];++ await handleCloneFailure({+ error,+ url,+ branch: null,+ json: false,+ emit: () => undefined,+ printStderr: (text) => stderr.push(text),+ });++ const out = stderr.join('');+ expect(out).toContain('GIT_CONFIG_GLOBAL');+ expect(out).not.toContain('GIT_CONFIG_KEY_<n>');+ });+});diff --git a/packages/cli/src/commands/clone.test.ts b/packages/cli/src/commands/clone.test.tsindex 613e60821..085555c2d 100644--- a/packages/cli/src/commands/clone.test.ts+++ b/packages/cli/src/commands/clone.test.ts@@ -564,7 +564,7 @@ describe('buildCloneAuthEnv', () => {});describe('buildCloneGitOptions', () => {- test('opts into the env-based unsafe flags so the user PAGER/SSH/askpass/editor env is honored', () => {+ test('opts into the env-based unsafe flags so the user PAGER/SSH/askpass/editor/GIT_CONFIG_COUNT 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']);@@ -574,6 +574,7 @@ describe('buildCloneGitOptions', () => {allowUnsafeSshCommand: true,allowUnsafeAskPass: true,allowUnsafeEditor: true,+ allowUnsafeConfigEnvCount: true,});});@@ -602,9 +603,7 @@ describe('buildCloneGitOptions — simple-git EDITOR env guard', () => {});it('buildCloneGitOptions honors EDITOR — git runs instead of tripping the guard', async () => {- const git = simpleGit(buildCloneGitOptions(tmpdir(), []) as Partial<SimpleGitOptions>).env(- editorEnv,- );+ const git = simpleGit(buildCloneGitOptions(tmpdir(), [])).env(editorEnv);const out = await git.raw(['--version']);expect(out).toContain('git version');});@@ -1405,9 +1404,7 @@ describe('clone stores the remote URL verbatim, userinfo included', () => {const targetDir = join(base, 'cloned');const env = buildCloneAuthEnv({}, { PATH: process.env.PATH ?? '' });- const git = simpleGit(- buildCloneGitOptions(base, [redirect]) as Partial<SimpleGitOptions>,- ).env(env);+ const git = simpleGit(buildCloneGitOptions(base, [redirect])).env(env);await git.clone(declaredUrl, targetDir, buildCloneArgs(null));const config = readFileSync(join(targetDir, '.git', 'config'), 'utf-8');diff --git a/packages/cli/src/commands/clone.ts b/packages/cli/src/commands/clone.tsindex b72ccc7a9..f1f8fb8e2 100644--- a/packages/cli/src/commands/clone.ts+++ b/packages/cli/src/commands/clone.ts@@ -20,7 +20,7 @@ import {sameGitHubLogin,} from '@inkeep/open-knowledge-server';import { Command } from 'commander';-import simpleGit, { type SimpleGitOptions } from 'simple-git';+import simpleGit, { GitPluginError, type SimpleGitOptions } from 'simple-git';import type { GhDetectResult } from '../auth/gh-detect.ts';import { type ResolvedAuth, resolveAuth } from '../auth/resolve-auth.ts';import { makeLazyTokenStore, type TokenStore } from '../auth/token-store.ts';@@ -123,20 +123,7 @@ interface CloneOptions {_detectGhFn?: (host?: string, options?: { login?: string }) => GhDetectResult;}-type CredentialHelperUnsafeGitOptions = SimpleGitOptions & {- unsafe?: NonNullable<SimpleGitOptions['unsafe']> & {- allowUnsafeCredentialHelper?: boolean;- allowUnsafePager?: boolean;- allowUnsafeSshCommand?: boolean;- allowUnsafeAskPass?: boolean;- allowUnsafeEditor?: boolean;- };-};--export function buildCloneGitOptions(- cwd: string,- gitConfig: string[],-): Partial<CredentialHelperUnsafeGitOptions> {+export function buildCloneGitOptions(cwd: string, gitConfig: string[]): Partial<SimpleGitOptions> {return {baseDir: cwd,config: gitConfig,@@ -146,6 +133,7 @@ export function buildCloneGitOptions(allowUnsafeSshCommand: true,allowUnsafeAskPass: true,allowUnsafeEditor: true,+ allowUnsafeConfigEnvCount: true,},};}@@ -166,15 +154,16 @@ export interface CloneAuthResolution {};}-export function formatDeclaredMissWarning(+export function formatDeclaredAccountMiss(miss: CloneAuthResolution['declaredMiss'],+ wording: { url: string; operation: string },): string | null {if (miss === undefined) return null;const { declaredLogin, declaredSource } = miss;let mechanism: string;switch (declaredSource) {case 'remote-url':- mechanism = `The clone URL names ${declaredLogin}`;+ mechanism = `${wording.url} names ${declaredLogin}`;break;case 'credential-config':mechanism = `Your Git credential configuration names ${declaredLogin}`;@@ -183,11 +172,17 @@ export function formatDeclaredMissWarning(mechanism = `Your Git configuration names ${declaredLogin}`;}return (- `⚠ ${mechanism}, but the GitHub CLI couldn't confirm that account — the clone used its active account.\n` ++ `⚠ ${mechanism}, but the GitHub CLI couldn't confirm that account — ${wording.operation} used its active account.\n` +` Run \`gh auth status\` to see which account that is. If ${declaredLogin} is listed as active, nothing is wrong; confirming an account by name needs GitHub CLI 2.40 or newer.\n`);}+export function formatDeclaredMissWarning(+ miss: CloneAuthResolution['declaredMiss'],+): string | null {+ return formatDeclaredAccountMiss(miss, { url: 'The clone URL', operation: 'the clone' });+}+export async function resolveCloneAuth(cloneUrl: string,tokenStore: TokenStore,@@ -278,7 +273,7 @@ export async function runClone(const env = buildCloneAuthEnv(resolved);const gitOptions = buildCloneGitOptions(cwd, resolved.gitConfig);- const git = simpleGit(gitOptions as Partial<SimpleGitOptions>).env(env);+ const git = simpleGit(gitOptions).env(env);let lastPct = -1;@@ -428,6 +423,25 @@ export function formatCloneAuthFailure(opts: {].join('\n');}+function formatCloneEnvConfigRefusal(opts: {+ error: unknown;+ message: string;+ url: string;+ branch?: string | null;+}): string | null {+ if (!(opts.error instanceof GitPluginError) || opts.error.plugin !== 'unsafe') return null;+ if (!opts.message.startsWith('Configuring ')) return null;+ const displayUrl = stripUrlPassword(opts.url);+ return [+ `✗ Couldn't clone ${displayUrl} — a Git setting passed in through the GIT_CONFIG_COUNT and GIT_CONFIG_KEY_<n> environment variables was refused:`,+ ` ${opts.message}`,+ '',+ ' To fix:',+ ' 1. Remove that setting from the GIT_CONFIG_KEY_<n> and GIT_CONFIG_VALUE_<n> variables, or unset GIT_CONFIG_COUNT',+ ` 2. Then re-run: ${reconstructCloneCommand(displayUrl, opts.branch)}`,+ ].join('\n');+}+export function emitCloneFailure(opts: {error: unknown;url: string;@@ -445,13 +459,20 @@ export function emitCloneFailure(opts: {opts.emit({ type: 'error', message: rawMessage });return;}- const actionable = formatCloneAuthFailure({- error: opts.error,- url: opts.url,- branch: opts.branch,- principal: opts.principal,- resolvedLogin: opts.resolvedLogin,- });+ const actionable =+ formatCloneAuthFailure({+ error: opts.error,+ url: opts.url,+ branch: opts.branch,+ principal: opts.principal,+ resolvedLogin: opts.resolvedLogin,+ }) ??+ formatCloneEnvConfigRefusal({+ error: opts.error,+ message: rawMessage,+ url: opts.url,+ branch: opts.branch,+ });opts.printStderr(`${actionable ?? `✗ ${rawMessage}`}\n`);}diff --git a/packages/cli/src/commands/git-credential-fixtures.test-helper.ts b/packages/cli/src/commands/git-credential-fixtures.test-helper.tsnew file mode 100644index 000000000..4334efba5--- /dev/null+++ b/packages/cli/src/commands/git-credential-fixtures.test-helper.ts@@ -0,0 +1,127 @@+import { existsSync, readFileSync, writeFileSync } from 'node:fs';+import { createRequire } from 'node:module';+import { join, resolve } from 'node:path';+import { pathToFileURL } from 'node:url';+import { shellSingleQuote } from '@inkeep/open-knowledge-core';++const CLI_PACKAGE_DIR = resolve(import.meta.dirname, '../..');++function recordCliCallLines(callLog: string): string[] {+ return [+ "import { spawnSync } from 'node:child_process';",+ "import { appendFileSync, readFileSync } from 'node:fs';",+ 'const args = process.argv.slice(2);',+ "const input = readFileSync(0, 'utf-8');",+ 'const fields = {};',+ "for (const line of input.split('\\n')) {",+ " const at = line.indexOf('=');",+ ' if (at > 0) fields[line.slice(0, at)] = line.slice(at + 1);',+ '}',+ `appendFileSync(${JSON.stringify(callLog)}, JSON.stringify({ args, protocol: fields.protocol, host: fields.host }) + '\\n');`,+ ];+}++export interface RecordedHelperCall {+ label: string;+ operation: string;+ fields: Record<string, string>;+ relayToken: string | null;+}++export interface CliHelperCall {+ args: string[];+ protocol?: string;+ host?: string;+}++export function readJsonLines<T>(file: string): T[] {+ if (!existsSync(file)) return [];+ return readFileSync(file, 'utf-8')+ .split('\n')+ .filter((line) => line !== '')+ .map((line) => JSON.parse(line) as T);+}++export function linesCarrying(text: string, needle: string): string[] {+ return text.split('\n').filter((line) => line.includes(needle));+}++export function gitConfigParameter(key: string, value: string): string {+ return `'${key}'='${value.replaceAll("'", "'\\''")}'`;+}++export function writeRecordingCredentialHelper(+ dir: string,+ log: string,+): (label: string) => string {+ const script = join(dir, 'recording-credential-helper.mjs');+ writeFileSync(+ script,+ [+ "import { appendFileSync, readFileSync } from 'node:fs';",+ 'const [log, label, operation] = process.argv.slice(2);',+ 'const fields = {};',+ "for (const line of readFileSync(0, 'utf-8').split('\\n')) {",+ " const at = line.indexOf('=');",+ ' if (at > 0) fields[line.slice(0, at)] = line.slice(at + 1);',+ '}',+ 'const relayToken = process.env.OK_GH_TOKEN ?? null;',+ "appendFileSync(log, JSON.stringify({ label, operation, fields, relayToken }) + '\\n');",+ '',+ ].join('\n'),+ 'utf-8',+ );+ return (label) =>+ `!${shellSingleQuote(process.execPath)} ${shellSingleQuote(script)} ${shellSingleQuote(log)} ${label}`;+}++export function writeCliCredentialStandIn(options: {+ dir: string;+ callLog: string;+ authFile: string;+}): string {+ const runner = join(options.dir, 'ok-git-credential-get.mts');+ const moduleUrl = (path: string) =>+ JSON.stringify(pathToFileURL(join(CLI_PACKAGE_DIR, path)).href);+ writeFileSync(+ runner,+ [+ `import { FileBackend } from ${moduleUrl('src/auth/token-store.ts')};`,+ `import { handleCredentialGet } from ${moduleUrl('src/commands/auth/git-credential.ts')};`,+ `const store = new FileBackend(${JSON.stringify(options.authFile)});`,+ 'process.exit(await handleCredentialGet(process.stdin, process.stdout, store));',+ '',+ ].join('\n'),+ 'utf-8',+ );+ const entry = join(options.dir, 'ok-cli.mjs');+ writeFileSync(+ entry,+ [+ ...recordCliCallLines(options.callLog),+ "if (args.join(' ') !== 'auth git-credential get') process.exit(1);",+ `const helper = spawnSync(process.execPath, ['--conditions=development', '--import', 'tsx', ${JSON.stringify(runner)}], { cwd: ${JSON.stringify(CLI_PACKAGE_DIR)}, input, stdio: ['pipe', 'inherit', 'inherit'] });`,+ 'process.exit(helper.status ?? 1);',+ '',+ ].join('\n'),+ 'utf-8',+ );+ return entry;+}++export function writeCliEntryLauncher(options: { dir: string; callLog: string }): string {+ const entry = join(options.dir, 'ok-cli-entry.mjs');+ const tsxLoader = pathToFileURL(createRequire(import.meta.url).resolve('tsx')).href;+ const cli = join(CLI_PACKAGE_DIR, 'src', 'cli.ts');+ writeFileSync(+ entry,+ [+ ...recordCliCallLines(options.callLog),+ `const cli = spawnSync(process.execPath, ['--conditions=development', '--import', ${JSON.stringify(tsxLoader)}, ${JSON.stringify(cli)}, ...args], { input, stdio: ['pipe', 'inherit', 'inherit'] });`,+ 'process.exit(cli.status ?? 1);',+ '',+ ].join('\n'),+ 'utf-8',+ );+ return entry;+}diff --git a/packages/cli/src/commands/github-stand-in.test-helper.ts b/packages/cli/src/commands/github-stand-in.test-helper.tsnew file mode 100644index 000000000..96cdb7f00--- /dev/null+++ b/packages/cli/src/commands/github-stand-in.test-helper.ts@@ -0,0 +1,404 @@+import { execFileSync, spawn } from 'node:child_process';+import { generateKeyPairSync, type KeyObject, sign } from 'node:crypto';+import { existsSync, mkdirSync, writeFileSync } from 'node:fs';+import {+ createServer as createHttpServer,+ type IncomingMessage,+ type ServerResponse,+} from 'node:http';+import { createServer as createHttpsServer } from 'node:https';+import type { AddressInfo } from 'node:net';+import { devNull } from 'node:os';+import { join } from 'node:path';+import type { Duplex } from 'node:stream';++export const GITHUB_HOST = 'github.com';++interface PresentedCredential {+ username: string;+ password: string;+}++interface GitHubStandInRequest {+ method: string;+ path: string;+ credential: PresentedCredential | null;+ status: number;+}++export interface GitHubStandIn {+ proxyUrl: string;+ caFile: string;+ requests: GitHubStandInRequest[];+ requestsTo: (host: string) => GitHubStandInRequest[];+ repositoryDir: (name: string, host?: string) => string;+ close: () => Promise<void>;+}++interface StandInHost {+ acceptedPasswords: string[];+ deniedPasswords: string[];+ repositoriesRoot: string;+ requests: GitHubStandInRequest[];+}++export interface PlainHttpInterceptor {+ proxyUrl: string;+ requests: Array<{ url: string; basicCredential: string | null }>;+ close: () => Promise<void>;+}++export async function startPlainHttpInterceptor(): Promise<PlainHttpInterceptor> {+ const requests: PlainHttpInterceptor['requests'] = [];+ const server = createHttpServer((req, res) => {+ const header = req.headers.authorization;+ requests.push({+ url: req.url ?? '',+ basicCredential:+ typeof header === 'string' && header.startsWith('Basic ')+ ? Buffer.from(header.slice('Basic '.length), 'base64').toString('utf8')+ : null,+ });+ req.resume();+ res.writeHead(401, { 'WWW-Authenticate': 'Basic realm="GitHub"' });+ res.end();+ });+ await new Promise<void>((resolve) => server.listen(0, '127.0.0.1', resolve));+ const { port } = server.address() as AddressInfo;+ return {+ proxyUrl: `http://127.0.0.1:${port}`,+ requests,+ close: () =>+ new Promise<void>((resolve) => {+ server.closeAllConnections();+ server.close(() => resolve());+ }),+ };+}++function derLength(length: number): Buffer {+ if (length < 0x80) return Buffer.from([length]);+ const bytes: number[] = [];+ for (let rest = length; rest > 0; rest = Math.floor(rest / 256)) bytes.unshift(rest % 256);+ return Buffer.from([0x80 | bytes.length, ...bytes]);+}++function der(tag: number, ...content: Buffer[]): Buffer {+ const body = Buffer.concat(content);+ return Buffer.concat([Buffer.from([tag]), derLength(body.length), body]);+}++const sequence = (...items: Buffer[]) => der(0x30, ...items);++function objectIdentifier(dotted: string): Buffer {+ const [first = 0, second = 0, ...arcs] = dotted.split('.').map(Number);+ const bytes = [first * 40 + second];+ for (const arc of arcs) {+ const encoded = [arc % 128];+ for (let rest = Math.floor(arc / 128); rest > 0; rest = Math.floor(rest / 128)) {+ encoded.unshift((rest % 128) | 0x80);+ }+ bytes.push(...encoded);+ }+ return der(0x06, Buffer.from(bytes));+}++const ECDSA_WITH_SHA256 = sequence(objectIdentifier('1.2.840.10045.4.3.2'));+const VALIDITY = sequence(+ der(0x17, Buffer.from('200101000000Z')),+ der(0x17, Buffer.from('491231235959Z')),+);+const CRITICAL = der(0x01, Buffer.from([0xff]));++function distinguishedName(commonName: string): Buffer {+ return sequence(+ der(0x31, sequence(objectIdentifier('2.5.4.3'), der(0x0c, Buffer.from(commonName, 'utf8')))),+ );+}++function certificateExtension(oid: string, critical: boolean, value: Buffer): Buffer {+ return sequence(objectIdentifier(oid), ...(critical ? [CRITICAL] : []), der(0x04, value));+}++function issueCertificate(options: {+ serial: number;+ issuer: string;+ subject: string;+ subjectKey: KeyObject;+ signingKey: KeyObject;+ extensions: Buffer[];+}): Buffer {+ const toBeSigned = sequence(+ der(0xa0, der(0x02, Buffer.from([0x02]))),+ der(0x02, Buffer.from([options.serial])),+ ECDSA_WITH_SHA256,+ distinguishedName(options.issuer),+ VALIDITY,+ distinguishedName(options.subject),+ options.subjectKey.export({ type: 'spki', format: 'der' }),+ der(0xa3, sequence(...options.extensions)),+ );+ const signature = sign('sha256', toBeSigned, options.signingKey);+ return sequence(toBeSigned, ECDSA_WITH_SHA256, der(0x03, Buffer.from([0x00]), signature));+}++function certificatePem(certificate: Buffer): string {+ const lines = certificate.toString('base64').match(/.{1,64}/g) ?? [];+ return `-----BEGIN CERTIFICATE-----\n${lines.join('\n')}\n-----END CERTIFICATE-----\n`;+}++function throwawayGitHubCertificates(hosts: string[]): {+ caPem: string;+ serverKeyPem: string;+ serverCertPem: string;+} {+ const authority = generateKeyPairSync('ec', { namedCurve: 'prime256v1' });+ const server = generateKeyPairSync('ec', { namedCurve: 'prime256v1' });+ const authorityName = 'Open Knowledge publish test authority';+ const authorityCertificate = issueCertificate({+ serial: 1,+ issuer: authorityName,+ subject: authorityName,+ subjectKey: authority.publicKey,+ signingKey: authority.privateKey,+ extensions: [+ certificateExtension('2.5.29.19', true, sequence(CRITICAL)),+ certificateExtension('2.5.29.15', true, der(0x03, Buffer.from([0x01, 0x06]))),+ ],+ });+ const serverCertificate = issueCertificate({+ serial: 2,+ issuer: authorityName,+ subject: GITHUB_HOST,+ subjectKey: server.publicKey,+ signingKey: authority.privateKey,+ extensions: [+ certificateExtension(+ '2.5.29.17',+ false,+ sequence(...hosts.map((host) => der(0x82, Buffer.from(host)))),+ ),+ certificateExtension('2.5.29.15', true, der(0x03, Buffer.from([0x07, 0x80]))),+ certificateExtension('2.5.29.37', false, sequence(objectIdentifier('1.3.6.1.5.5.7.3.1'))),+ ],+ });+ return {+ caPem: certificatePem(authorityCertificate),+ serverKeyPem: String(server.privateKey.export({ type: 'pkcs8', format: 'pem' })),+ serverCertPem: certificatePem(serverCertificate),+ };+}++function presentedCredential(header: string | undefined): PresentedCredential | null {+ if (typeof header !== 'string' || !header.startsWith('Basic ')) return null;+ const decoded = Buffer.from(header.slice('Basic '.length), 'base64').toString('utf8');+ const separator = decoded.indexOf(':');+ if (separator === -1) return { username: decoded, password: '' };+ return { username: decoded.slice(0, separator), password: decoded.slice(separator + 1) };+}++function hermeticGitEnv(home: string): Record<string, string> {+ return {+ PATH: process.env.PATH ?? '',+ HOME: home,+ GIT_CONFIG_NOSYSTEM: '1',+ GIT_CONFIG_GLOBAL: devNull,+ };+}++function serveThroughHttpBackend(+ req: IncomingMessage,+ res: ServerResponse,+ url: URL,+ user: string,+ repositoriesRoot: string,+ settle: (status: number) => void,+): void {+ const env: Record<string, string> = {+ ...hermeticGitEnv(repositoriesRoot),+ GIT_PROJECT_ROOT: repositoriesRoot,+ GIT_HTTP_EXPORT_ALL: '1',+ PATH_INFO: url.pathname,+ QUERY_STRING: url.search.replace(/^\?/, ''),+ REQUEST_METHOD: req.method ?? 'GET',+ CONTENT_TYPE: String(req.headers['content-type'] ?? ''),+ REMOTE_USER: user,+ REMOTE_ADDR: '127.0.0.1',+ };+ for (const [header, variable] of [+ ['content-length', 'CONTENT_LENGTH'],+ ['content-encoding', 'HTTP_CONTENT_ENCODING'],+ ['git-protocol', 'GIT_PROTOCOL'],+ ] as const) {+ const value = req.headers[header];+ if (typeof value === 'string') env[variable] = value;+ }+ const backend = spawn('git', ['http-backend'], { env, stdio: ['pipe', 'pipe', 'ignore'] });+ req.pipe(backend.stdin);+ let pending = Buffer.alloc(0);+ let headersSent = false;+ backend.stdout.on('data', (chunk: Buffer) => {+ if (headersSent) {+ res.write(chunk);+ return;+ }+ pending = Buffer.concat([pending, chunk]);+ const end = pending.indexOf('\r\n\r\n');+ if (end === -1) return;+ let status = 200;+ const headers: Record<string, string> = {};+ for (const line of pending.subarray(0, end).toString('utf8').split('\r\n')) {+ const colon = line.indexOf(':');+ if (colon <= 0) continue;+ const name = line.slice(0, colon).trim();+ const value = line.slice(colon + 1).trim();+ if (name.toLowerCase() === 'status') status = Number.parseInt(value, 10);+ else headers[name] = value;+ }+ res.writeHead(status, headers);+ headersSent = true;+ settle(status);+ const body = pending.subarray(end + 4);+ if (body.length > 0) res.write(body);+ });+ backend.on('close', () => {+ if (!headersSent) {+ settle(500);+ res.writeHead(500);+ }+ res.end();+ });+}++export async function startGitHubStandIn(options: {+ root: string;+ acceptedPassword: string;+ deniedPassword?: string;+ repositories: string[];+ enterpriseHosts?: Record<string, string[]>;+}): Promise<GitHubStandIn> {+ const hosts = new Map<string, StandInHost>();+ for (const [host, acceptedPasswords] of [+ [GITHUB_HOST, [options.acceptedPassword]],+ ...Object.entries(options.enterpriseHosts ?? {}),+ ] as Array<[string, string[]]>) {+ hosts.set(host, {+ acceptedPasswords,+ deniedPasswords:+ host === GITHUB_HOST && options.deniedPassword !== undefined+ ? [options.deniedPassword]+ : [],+ repositoriesRoot: join(options.root, 'repositories', host),+ requests: [],+ });+ }+ const served = (host: string): StandInHost => {+ const standInHost = hosts.get(host);+ if (standInHost === undefined) throw new Error(`the GitHub stand-in does not serve ${host}`);+ return standInHost;+ };+ const gitExecPath = execFileSync('git', ['--exec-path'], {+ encoding: 'utf8',+ env: hermeticGitEnv(options.root),+ }).trim();+ if (+ !['git-http-backend', 'git-http-backend.exe'].some((name) =>+ existsSync(join(gitExecPath, name)),+ )+ ) {+ throw new Error(+ `git http-backend is not installed in ${gitExecPath}; the GitHub stand-in serves pushes through it`,+ );+ }+ for (const { repositoriesRoot } of hosts.values()) {+ for (const name of options.repositories) {+ const repository = join(repositoriesRoot, name);+ mkdirSync(repository, { recursive: true });+ execFileSync('git', ['init', '--quiet', '--bare', '--initial-branch=main', repository], {+ env: hermeticGitEnv(repositoriesRoot),+ });+ }+ }+ const certificates = throwawayGitHubCertificates([...hosts.keys()]);+ const caFile = join(options.root, 'github-stand-in-ca.pem');+ writeFileSync(caFile, certificates.caPem, 'utf8');++ const tlsServer = createHttpsServer(+ { key: certificates.serverKeyPem, cert: certificates.serverCertPem },+ (req, res) => {+ const host = String(req.headers.host ?? '')+ .replace(/:\d+$/, '')+ .toLowerCase();+ const standInHost = hosts.get(host);+ if (standInHost === undefined) {+ req.resume();+ res.writeHead(421);+ res.end();+ return;+ }+ const url = new URL(req.url ?? '/', `https://${host}`);+ const credential = presentedCredential(req.headers.authorization);+ const request: GitHubStandInRequest = {+ method: req.method ?? 'GET',+ path: url.pathname + url.search,+ credential,+ status: 0,+ };+ standInHost.requests.push(request);+ if (credential !== null && standInHost.deniedPasswords.includes(credential.password)) {+ request.status = 403;+ req.resume();+ res.writeHead(403);+ res.end();+ return;+ }+ if (credential === null || !standInHost.acceptedPasswords.includes(credential.password)) {+ request.status = 401;+ req.resume();+ res.writeHead(401, { 'WWW-Authenticate': 'Basic realm="GitHub"' });+ res.end();+ return;+ }+ serveThroughHttpBackend(+ req,+ res,+ url,+ credential.username,+ standInHost.repositoriesRoot,+ (status) => {+ request.status = status;+ },+ );+ },+ );+ const proxy = createHttpServer((req, res) => {+ req.resume();+ res.writeHead(403);+ res.end();+ });+ proxy.on('connect', (req: IncomingMessage, socket: Duplex, head: Buffer) => {+ const target = (req.url ?? '').toLowerCase();+ if (!/:\d+$/.test(target) || !hosts.has(target.replace(/:\d+$/, ''))) {+ socket.end('HTTP/1.1 403 Forbidden\r\n\r\n');+ return;+ }+ socket.write('HTTP/1.1 200 Connection Established\r\n\r\n');+ if (head.length > 0) socket.unshift(head);+ tlsServer.emit('connection', socket);+ });+ await new Promise<void>((resolve) => proxy.listen(0, '127.0.0.1', resolve));+ const { port } = proxy.address() as AddressInfo;+ return {+ proxyUrl: `http://127.0.0.1:${port}`,+ caFile,+ requests: served(GITHUB_HOST).requests,+ requestsTo: (host) => served(host).requests,+ repositoryDir: (name, host = GITHUB_HOST) => join(served(host).repositoriesRoot, name),+ close: () =>+ new Promise<void>((resolve) => {+ tlsServer.closeAllConnections();+ proxy.closeAllConnections();+ proxy.close(() => resolve());+ }),+ };+}diff --git a/packages/cli/src/commands/share/publish-credential-helper.test.ts b/packages/cli/src/commands/share/publish-credential-helper.test.tsnew file mode 100644index 000000000..f9f79ac84--- /dev/null+++ b/packages/cli/src/commands/share/publish-credential-helper.test.ts@@ -0,0 +1,445 @@+import { execFileSync } from 'node:child_process';+import { mkdirSync, mkdtempSync, readFileSync, realpathSync, rmSync, writeFileSync } from 'node:fs';+import { tmpdir } from 'node:os';+import { join } from 'node:path';+import { shellSingleQuote } from '@inkeep/open-knowledge-core';+import type { Octokit } from '@octokit/rest';+import { afterEach, beforeAll, beforeEach, describe, expect, test, vi } from 'vitest';+import {+ type CliHelperCall,+ gitConfigParameter as configParameter,+ type RecordedHelperCall as HelperCall,+ linesCarrying,+ readJsonLines,+ writeCliCredentialStandIn,+ writeCliEntryLauncher,+ writeRecordingCredentialHelper,+} from '../git-credential-fixtures.test-helper.ts';+import {+ GITHUB_HOST,+ type GitHubStandIn,+ type PlainHttpInterceptor,+ startGitHubStandIn,+ startPlainHttpInterceptor,+} from '../github-stand-in.test-helper.ts';+import { type PublishResult, runPublishFlow } from './publish.ts';++const CLONE_URL = `https://${GITHUB_HOST}/alice/demo.git`;+const PUBLISH_TOKEN = 'ok-test-publish-token-the-stand-in-accepts';+const STALE_TOKEN = 'ok-test-publish-token-the-stand-in-rejects';+const AMBIENT_HELPERS = ['env-count', 'env-parameters', 'github-scoped', 'global'];+const ENTERPRISE_HOST = 'ghe.example.com';+const ENTERPRISE_CLONE_URL = `https://${ENTERPRISE_HOST}/alice/demo.git`;+const ENTERPRISE_PUBLISH_TOKEN = 'ok-test-publish-token-for-the-enterprise-host';+const ENTERPRISE_USER_PASSWORD = 'ok-test-enterprise-password-in-the-users-own-helper';+const INHERITED_RELAY_TOKEN = 'ok-test-relay-token-inherited-from-the-environment';++function octokitCreatingRepoAt(cloneUrl: string): Octokit {+ return {+ repos: {+ createForAuthenticatedUser: async () => ({+ data: { clone_url: cloneUrl, default_branch: 'main' },+ }),+ },+ } as unknown as Octokit;+}++describe("share publish authenticates its github.com push only through the CLI's own credential helper", () => {+ let workspace: string;+ let projectDir: string;+ let credentialStore: string;+ let helperLog: string;+ let cliHelperLog: string;+ let traceFile: string;+ let cliStandIn: string;+ let standIn: GitHubStandIn;+ let interceptor: PlainHttpInterceptor | undefined;++ beforeAll(() => {+ expect(+ process.env.GIT_CONFIG_GLOBAL,+ 'precondition: GIT_CONFIG_GLOBAL must be unset, or git reads that file instead of the temporary HOME and the credentials this suite approves reach the helpers it names',+ ).toBeUndefined();+ });++ beforeEach(async () => {+ workspace = realpathSync(mkdtempSync(join(tmpdir(), 'ok-publish-credential-')));+ const home = join(workspace, 'home');+ projectDir = join(workspace, 'project');+ credentialStore = join(workspace, 'git-credentials');+ helperLog = join(workspace, 'ambient-helper-calls.jsonl');+ cliHelperLog = join(workspace, 'cli-helper-calls.jsonl');+ traceFile = join(workspace, 'git-trace.log');+ vi.stubEnv('HOME', home);+ vi.stubEnv('USERPROFILE', home);+ vi.stubEnv('XDG_CONFIG_HOME', join(home, '.config'));+ vi.stubEnv('GIT_CONFIG_NOSYSTEM', '1');+ for (const variable of [+ 'NO_PROXY',+ 'no_proxy',+ 'GIT_SSL_CAINFO',+ 'GIT_SSL_NO_VERIFY',+ 'OK_GH_TOKEN',+ 'OK_GH_TOKEN_HOST',+ 'OK_GH_TOKEN_LOGIN',+ ]) {+ vi.stubEnv(variable, undefined);+ }+ mkdirSync(home, { recursive: true });+ mkdirSync(projectDir, { recursive: true });+ writeFileSync(join(projectDir, 'README.md'), '# Hello\n', 'utf-8');+ standIn = await startGitHubStandIn({+ root: join(workspace, 'github'),+ acceptedPassword: PUBLISH_TOKEN,+ repositories: ['alice/demo.git'],+ enterpriseHosts: { [ENTERPRISE_HOST]: [ENTERPRISE_USER_PASSWORD, ENTERPRISE_PUBLISH_TOKEN] },+ });+ cliStandIn = writeCliCredentialStandIn({+ dir: workspace,+ callLog: cliHelperLog,+ authFile: join(workspace, 'ok-auth.yml'),+ });+ const recordingHelper = writeRecordingCredentialHelper(workspace, helperLog);++ const globalConfig = join(home, '.gitconfig');+ for (const [key, value] of [+ ['user.name', 'Publish Test'],+ ['user.email', '[email protected]'],+ ['init.defaultBranch', 'main'],+ ['commit.gpgsign', 'false'],+ ['http.proxy', standIn.proxyUrl],+ ['http.sslCAInfo', standIn.caFile],+ ['http.curloptResolve', `${GITHUB_HOST}:443:127.0.0.1`],+ ['http.curloptResolve', `${ENTERPRISE_HOST}:443:127.0.0.1`],+ ['credential.helper', recordingHelper('global')],+ ['credential.helper', `store --file ${shellSingleQuote(credentialStore)}`],+ [`credential.https://${GITHUB_HOST}.helper`, recordingHelper('github-scoped')],+ ]) {+ execFileSync('git', ['config', '--file', globalConfig, '--add', key, value]);+ }++ vi.stubEnv('GIT_CONFIG_COUNT', '3');+ vi.stubEnv('GIT_CONFIG_KEY_0', 'credential.interactive');+ vi.stubEnv('GIT_CONFIG_VALUE_0', 'false');+ vi.stubEnv('GIT_CONFIG_KEY_1', 'credential.guiPrompt');+ vi.stubEnv('GIT_CONFIG_VALUE_1', 'false');+ vi.stubEnv('GIT_CONFIG_KEY_2', 'credential.helper');+ vi.stubEnv('GIT_CONFIG_VALUE_2', recordingHelper('env-count'));+ vi.stubEnv(+ 'GIT_CONFIG_PARAMETERS',+ configParameter('credential.helper', recordingHelper('env-parameters')),+ );++ execFileSync('git', ['credential', 'approve'], {+ cwd: projectDir,+ input: `protocol=https\nhost=${GITHUB_HOST}\nusername=ambient-user\npassword=ambient-password\n\n`,+ });+ expect(+ readJsonLines<HelperCall>(helperLog).map((call) => `${call.label} ${call.operation}`),+ ).toEqual(expect.arrayContaining(AMBIENT_HELPERS.map((label) => `${label} store`)));+ expect(readFileSync(credentialStore, 'utf-8')).toContain('ambient-user');+ writeFileSync(helperLog, '', 'utf-8');+ vi.stubEnv('GIT_TRACE', traceFile);+ });++ afterEach(async () => {+ vi.unstubAllEnvs();+ await standIn.close();+ await interceptor?.close();+ interceptor = undefined;+ rmSync(workspace, { recursive: true, force: true });+ });++ async function publishWith(+ token: string,+ cloneUrl = CLONE_URL,+ credentialHelperEntry = cliStandIn,+ ): Promise<PublishResult> {+ const cliEntry = process.argv[1];+ process.argv[1] = credentialHelperEntry;+ try {+ return await runPublishFlow({+ octokit: octokitCreatingRepoAt(cloneUrl),+ token,+ projectDir,+ body: { owner: 'alice', name: 'demo', visibility: 'private' },+ ownerKind: 'user',+ deps: { ensureOkScaffold: () => {} },+ });+ } finally {+ process.argv[1] = cliEntry;+ }+ }++ function expectTokenConfinedToCliHelper(token: string): void {+ const trace = readFileSync(traceFile, 'utf-8');+ expect(trace).toMatch(/built-in: git push /);+ expect(trace).toMatch(/remote-https/);+ const projectGitConfig = readFileSync(join(projectDir, '.git', 'config'), 'utf-8');+ expect(projectGitConfig).toContain('[remote "origin"]');++ expect+ .soft(readJsonLines<HelperCall>(helperLog), 'calls reaching the ambient credential helpers')+ .toEqual([]);+ expect+ .soft(+ linesCarrying(readFileSync(credentialStore, 'utf-8'), token),+ 'the ambient git-credential-store file',+ )+ .toEqual([]);+ expect+ .soft(linesCarrying(trace, token), 'argv of git push and its children (GIT_TRACE)')+ .toEqual([]);+ expect.soft(linesCarrying(projectGitConfig, token), "the project's .git/config").toEqual([]);+ expect+ .soft(readJsonLines<CliHelperCall>(cliHelperLog), "calls reaching the CLI's own helper")+ .toContainEqual({+ args: ['auth', 'git-credential', 'get'],+ protocol: 'https',+ host: GITHUB_HOST,+ });+ }++ test('a publish whose token GitHub accepts pushes with that token and hands it to no ambient helper or command line', async () => {+ const result = await publishWith(PUBLISH_TOKEN);++ expect(result).toEqual({+ kind: 'ok',+ value: { ownerLogin: 'alice', repoName: 'demo', cloneUrl: CLONE_URL, defaultBranch: 'main' },+ });+ expect(standIn.requests).toContainEqual({+ method: 'POST',+ path: '/alice/demo.git/git-receive-pack',+ credential: { username: expect.any(String), password: PUBLISH_TOKEN },+ status: 200,+ });+ const pushedHead = execFileSync(+ 'git',+ ['--git-dir', standIn.repositoryDir('alice/demo.git'), 'rev-parse', 'refs/heads/main'],+ { encoding: 'utf-8' },+ ).trim();+ expect(pushedHead).toBe(+ execFileSync('git', ['rev-parse', 'HEAD'], { cwd: projectDir, encoding: 'utf-8' }).trim(),+ );+ expectTokenConfinedToCliHelper(PUBLISH_TOKEN);+ });++ test("a publish's push authenticates through the real `ok auth git-credential get` command of the CLI entry", async () => {+ const realEntry = writeCliEntryLauncher({ dir: workspace, callLog: cliHelperLog });++ const result = await publishWith(PUBLISH_TOKEN, CLONE_URL, realEntry);++ expect(result).toEqual({+ kind: 'ok',+ value: { ownerLogin: 'alice', repoName: 'demo', cloneUrl: CLONE_URL, defaultBranch: 'main' },+ });+ expect(standIn.requests).toContainEqual({+ method: 'POST',+ path: '/alice/demo.git/git-receive-pack',+ credential: { username: 'x-access-token', password: PUBLISH_TOKEN },+ status: 200,+ });+ expectTokenConfinedToCliHelper(PUBLISH_TOKEN);+ });++ test("a publish whose push recurses into a submodule on a plain http:// GitHub URL never hands publish's token to the submodule's plain-http request", async () => {+ const onPath = await startPlainHttpInterceptor();+ interceptor = onPath;+ const submoduleSource = join(workspace, 'submodule-source');+ const git = (...args: string[]) =>+ execFileSync('git', args, { cwd: projectDir, stdio: 'ignore' });+ execFileSync('git', ['init', '--quiet', '--initial-branch=main', submoduleSource]);+ execFileSync('git', ['-C', submoduleSource, 'commit', '--quiet', '--allow-empty', '-m', 'one']);+ git('init', '--quiet', '--initial-branch=main');+ git('add', 'README.md');+ git('commit', '--quiet', '-m', 'a project');+ git('-c', 'protocol.file.allow=always', 'submodule', 'add', '--quiet', submoduleSource, 'sub');+ git('-C', 'sub', 'remote', 'set-url', 'origin', `http://${GITHUB_HOST}/alice/sub.git`);+ git('-C', 'sub', 'commit', '--quiet', '--allow-empty', '-m', 'not yet pushed');+ git('add', '.');+ git('commit', '--quiet', '-m', 'a project with a submodule');+ git('config', 'push.recurseSubmodules', 'on-demand');+ execFileSync('git', [+ 'config',+ '--file',+ join(workspace, 'home', '.gitconfig'),+ `http.http://${GITHUB_HOST}.proxy`,+ onPath.proxyUrl,+ ]);++ const result = await publishWith(PUBLISH_TOKEN);++ expect(result).toEqual({ kind: 'error', code: 'push-failed' });+ expect(+ onPath.requests.map((request) => request.url),+ "the submodule's plain-http push",+ ).toContain(`http://${GITHUB_HOST}/alice/sub.git/info/refs?service=git-receive-pack`);+ expect+ .soft(+ onPath.requests.filter((request) => request.basicCredential?.includes(PUBLISH_TOKEN)),+ "plain-http requests carrying publish's token",+ )+ .toEqual([]);+ expect+ .soft(+ readJsonLines<CliHelperCall>(cliHelperLog).filter((call) => call.protocol !== 'https'),+ "non-https calls reaching the CLI's own helper",+ )+ .toEqual([]);+ expect+ .soft(+ readJsonLines<HelperCall>(helperLog),+ "the user's own helpers asked for the submodule's http credential",+ )+ .toContainEqual(+ expect.objectContaining({+ label: 'global',+ operation: 'get',+ fields: expect.objectContaining({ protocol: 'http', host: GITHUB_HOST }),+ }),+ );+ });++ test('a publish whose token GitHub rejects presents that token and hands it to no ambient helper or command line', async () => {+ const result = await publishWith(STALE_TOKEN);++ expect(result).toEqual({ kind: 'error', code: 'push-failed' });+ expect(standIn.requests).toContainEqual({+ method: 'GET',+ path: '/alice/demo.git/info/refs?service=git-receive-pack',+ credential: { username: expect.any(String), password: STALE_TOKEN },+ status: 401,+ });+ expectTokenConfinedToCliHelper(STALE_TOKEN);+ });++ test('a publish presents its own token when its environment already carries an OK_GH_TOKEN relay for github.com', async () => {+ vi.stubEnv('OK_GH_TOKEN', INHERITED_RELAY_TOKEN);+ vi.stubEnv('OK_GH_TOKEN_HOST', GITHUB_HOST);++ const result = await publishWith(PUBLISH_TOKEN);++ expect(result).toEqual({+ kind: 'ok',+ value: { ownerLogin: 'alice', repoName: 'demo', cloneUrl: CLONE_URL, defaultBranch: 'main' },+ });+ expect(standIn.requests).toContainEqual({+ method: 'POST',+ path: '/alice/demo.git/git-receive-pack',+ credential: { username: expect.any(String), password: PUBLISH_TOKEN },+ status: 200,+ });+ expect(+ standIn.requests.filter((request) => request.credential?.password === INHERITED_RELAY_TOKEN),+ ).toEqual([]);+ expectTokenConfinedToCliHelper(PUBLISH_TOKEN);+ });++ test("a publish to a GitHub Enterprise host pushes its plain URL through the user's own credential helpers and never hands them publish's token", async () => {+ execFileSync('git', ['credential', 'approve'], {+ cwd: projectDir,+ input: `protocol=https\nhost=${ENTERPRISE_HOST}\nusername=enterprise-user\npassword=${ENTERPRISE_USER_PASSWORD}\n\n`,+ });+ expect(+ readJsonLines<HelperCall>(helperLog).map(+ (call) => `${call.label} ${call.operation} ${call.fields.host}`,+ ),+ ).toEqual(+ expect.arrayContaining(+ ['env-count', 'env-parameters', 'global'].map(+ (label) => `${label} store ${ENTERPRISE_HOST}`,+ ),+ ),+ );+ expect(readFileSync(credentialStore, 'utf-8')).toContain(+ `enterprise-user:${ENTERPRISE_USER_PASSWORD}@${ENTERPRISE_HOST}`,+ );+ writeFileSync(helperLog, '', 'utf-8');+ writeFileSync(traceFile, '', 'utf-8');++ const result = await publishWith(ENTERPRISE_PUBLISH_TOKEN, ENTERPRISE_CLONE_URL);++ const trace = readFileSync(traceFile, 'utf-8');+ expect(trace).toMatch(/built-in: git push /);+ expect(trace).toMatch(/remote-https/);+ const projectGitConfig = readFileSync(join(projectDir, '.git', 'config'), 'utf-8');+ expect(projectGitConfig).toContain('[remote "origin"]');+ const ambientCalls = readJsonLines<HelperCall>(helperLog);+ const enterpriseRequests = standIn.requestsTo(ENTERPRISE_HOST);++ expect.soft(result, 'the publish result').toEqual({+ kind: 'ok',+ value: {+ ownerLogin: 'alice',+ repoName: 'demo',+ cloneUrl: ENTERPRISE_CLONE_URL,+ defaultBranch: 'main',+ },+ });+ expect.soft(enterpriseRequests, 'requests reaching the enterprise host').toContainEqual({+ method: 'POST',+ path: '/alice/demo.git/git-receive-pack',+ credential: { username: 'enterprise-user', password: ENTERPRISE_USER_PASSWORD },+ status: 200,+ });+ expect+ .soft(+ execFileSync(+ 'git',+ [+ '--git-dir',+ standIn.repositoryDir('alice/demo.git', ENTERPRISE_HOST),+ 'for-each-ref',+ '--format=%(objectname)',+ 'refs/heads/main',+ ],+ { encoding: 'utf-8' },+ ).trim(),+ "the enterprise repository's main branch",+ )+ .toBe(+ execFileSync('git', ['rev-parse', 'HEAD'], { cwd: projectDir, encoding: 'utf-8' }).trim(),+ );+ expect.soft(standIn.requests, 'requests reaching github.com').toEqual([]);+ expect+ .soft(ambientCalls, "the user's own helpers asked for the enterprise credential")+ .toContainEqual({+ label: 'global',+ operation: 'get',+ fields: expect.objectContaining({ protocol: 'https', host: ENTERPRISE_HOST }),+ relayToken: null,+ });+ expect+ .soft(+ ambientCalls.filter((call) => call.relayToken !== null),+ 'ambient helper calls that inherited an OK_GH_TOKEN relay',+ )+ .toEqual([]);+ expect+ .soft(readJsonLines<CliHelperCall>(cliHelperLog), "calls reaching the CLI's own helper")+ .toEqual([]);+ expect+ .soft(+ enterpriseRequests.filter(+ (request) => request.credential?.password === ENTERPRISE_PUBLISH_TOKEN,+ ),+ "enterprise requests presenting publish's token",+ )+ .toEqual([]);+ expect+ .soft(+ linesCarrying(readFileSync(helperLog, 'utf-8'), ENTERPRISE_PUBLISH_TOKEN),+ 'calls reaching the ambient credential helpers',+ )+ .toEqual([]);+ expect+ .soft(+ linesCarrying(trace, ENTERPRISE_PUBLISH_TOKEN),+ 'argv of git push and its children (GIT_TRACE)',+ )+ .toEqual([]);+ expect+ .soft(linesCarrying(projectGitConfig, ENTERPRISE_PUBLISH_TOKEN), "the project's .git/config")+ .toEqual([]);+ });+});diff --git a/packages/cli/src/commands/share/publish-git-config-env.test.ts b/packages/cli/src/commands/share/publish-git-config-env.test.tsnew file mode 100644index 000000000..e3f0ac779--- /dev/null+++ b/packages/cli/src/commands/share/publish-git-config-env.test.ts@@ -0,0 +1,77 @@+import { execFileSync } from 'node:child_process';+import { mkdirSync, mkdtempSync, realpathSync, rmSync, writeFileSync } from 'node:fs';+import { tmpdir } from 'node:os';+import { join } from 'node:path';+import { pathToFileURL } from 'node:url';+import type { Octokit } from '@octokit/rest';+import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest';+import { runPublishFlow } from './publish.ts';++function octokitCreatingRepoAt(cloneUrl: string): Octokit {+ return {+ repos: {+ createForAuthenticatedUser: async () => ({+ data: { clone_url: cloneUrl, default_branch: 'main' },+ }),+ },+ } as unknown as Octokit;+}++describe('share publish honours the environment command-scope git config (GIT_CONFIG_COUNT)', () => {+ let workspace: string;+ let projectDir: string;+ let bareRepo: string;++ beforeEach(() => {+ workspace = realpathSync(mkdtempSync(join(tmpdir(), 'ok-publish-envcfg-')));+ projectDir = join(workspace, 'project');+ bareRepo = join(workspace, 'remote.git');+ const home = join(workspace, 'home');+ mkdirSync(projectDir, { recursive: true });+ mkdirSync(home, { recursive: true });+ vi.stubEnv('HOME', home);+ vi.stubEnv('USERPROFILE', home);+ vi.stubEnv('XDG_CONFIG_HOME', join(home, '.config'));+ vi.stubEnv('GIT_CONFIG_NOSYSTEM', '1');+ vi.stubEnv('GIT_AUTHOR_NAME', undefined);+ vi.stubEnv('GIT_AUTHOR_EMAIL', undefined);+ vi.stubEnv('GIT_COMMITTER_NAME', undefined);+ vi.stubEnv('GIT_COMMITTER_EMAIL', undefined);+ execFileSync('git', ['init', '--bare', bareRepo], { stdio: 'ignore' });+ writeFileSync(join(projectDir, 'README.md'), '# Hello\n', 'utf-8');+ });++ afterEach(() => {+ vi.unstubAllEnvs();+ rmSync(workspace, { recursive: true, force: true });+ });++ test('runPublishFlow publishes and the initial commit carries the environment identity', async () => {+ vi.stubEnv('GIT_CONFIG_COUNT', '2');+ vi.stubEnv('GIT_CONFIG_KEY_0', 'user.name');+ vi.stubEnv('GIT_CONFIG_VALUE_0', 'Env Config Author');+ vi.stubEnv('GIT_CONFIG_KEY_1', 'user.email');+ vi.stubEnv('GIT_CONFIG_VALUE_1', '[email protected]');+ const bareUrl = pathToFileURL(bareRepo).href;++ const result = await runPublishFlow({+ octokit: octokitCreatingRepoAt(bareUrl),+ token: 'irrelevant',+ projectDir,+ body: { owner: 'alice', name: 'demo', visibility: 'private' },+ ownerKind: 'user',+ deps: { ensureOkScaffold: () => {} },+ });++ expect(result).toEqual({+ kind: 'ok',+ value: { ownerLogin: 'alice', repoName: 'demo', cloneUrl: bareUrl, defaultBranch: 'main' },+ });+ expect(+ execFileSync('git', ['log', '-1', '--format=%an <%ae>'], {+ cwd: projectDir,+ encoding: 'utf-8',+ }).trim(),+ ).toBe('Env Config Author <[email protected]>');+ });+});diff --git a/packages/cli/src/commands/share/publish.ts b/packages/cli/src/commands/share/publish.tsindex c4cf28ef9..51f82b37c 100644--- a/packages/cli/src/commands/share/publish.ts+++ b/packages/cli/src/commands/share/publish.ts@@ -9,11 +9,17 @@ import {} from '@inkeep/open-knowledge-server';import { Octokit } from '@octokit/rest';import { Command } from 'commander';-import simpleGit, { type SimpleGit, type SimpleGitOptions } from 'simple-git';+import simpleGit, { type SimpleGit } from 'simple-git';+import { buildHttpsHostCliCredentialConfig, type ResolvedAuth } from '../../auth/resolve-auth.ts';import type { TokenStore } from '../../auth/token-store.ts';import { resolveReposToken } from '../auth/repos.ts';import { validateGitHubHost } from '../auth/validate-host.ts';-import { buildCloneEnv, buildCloneGitOptions } from '../clone.ts';+import {+ buildCloneAuthEnv,+ buildCloneEnv,+ buildCloneGitOptions,+ resolveSelfCliArgs,+} from '../clone.ts';interface PublishOptions {host: string;@@ -133,25 +139,32 @@ async function probeOwnerKind(octokit: Octokit, ownerLogin: string): Promise<'usreturn me.data.login.toLowerCase() === ownerLogin.toLowerCase() ? 'user' : 'org';}-function makeGit(projectDir: string): SimpleGit {- return simpleGit(buildCloneGitOptions(projectDir, []) as Partial<SimpleGitOptions>).env(- buildCloneEnv(),- );+type RelayToken = NonNullable<ResolvedAuth['relayToken']>;++function makeGit(projectDir: string, relayToken?: RelayToken): SimpleGit {+ if (relayToken === undefined) {+ return simpleGit(buildCloneGitOptions(projectDir, [])).env(buildCloneEnv());+ }+ return simpleGit(+ buildCloneGitOptions(+ projectDir,+ buildHttpsHostCliCredentialConfig(resolveSelfCliArgs(), relayToken.host),+ ),+ ).env(buildCloneAuthEnv({ relayToken }));}-function injectTokenIntoCloneUrl(cloneUrl: string, token: string): string {- if (!cloneUrl.startsWith('https://')) return cloneUrl;+function isGitHubDotComHttpsUrl(cloneUrl: string): boolean {+ if (!cloneUrl.startsWith('https://')) return false;try {- if (new URL(cloneUrl).hostname !== 'github.com') return cloneUrl;+ return new URL(cloneUrl).hostname === 'github.com';} catch {- return cloneUrl;+ return false;}- return cloneUrl.replace('https://', `https://x-access-token:${token}@`);}interface PublishGitDeps {ensureOkScaffold: (projectDir: string) => void;- gitFactory: (projectDir: string) => SimpleGit;+ gitFactory: (projectDir: string, relayToken?: RelayToken) => SimpleGit;}const DEFAULT_DEPS: PublishGitDeps = {@@ -258,9 +271,11 @@ export async function runPublishFlow(params: PublishParams): Promise<PublishResu}}- const authUrl = injectTokenIntoCloneUrl(created.cloneUrl, params.token);+ const pushGit = isGitHubDotComHttpsUrl(created.cloneUrl)+ ? deps.gitFactory(projectDir, { token: params.token, host: 'github.com' })+ : git;try {- await git.raw(['push', authUrl, `HEAD:refs/heads/${created.defaultBranch}`]);+ await pushGit.raw(['push', created.cloneUrl, `HEAD:refs/heads/${created.defaultBranch}`]);} catch (err) {const message = String((err as { message?: string }).message ?? '').toLowerCase();if (message.includes('saml') || message.includes('sso')) {diff --git a/packages/cli/src/commands/sync-credential-helper.test.ts b/packages/cli/src/commands/sync-credential-helper.test.tsnew file mode 100644index 000000000..6031981fd--- /dev/null+++ b/packages/cli/src/commands/sync-credential-helper.test.ts@@ -0,0 +1,1497 @@+import { execFileSync } from 'node:child_process';+import {+ copyFileSync,+ mkdirSync,+ mkdtempSync,+ readFileSync,+ realpathSync,+ rmSync,+ writeFileSync,+} from 'node:fs';+import { tmpdir } from 'node:os';+import { join } from 'node:path';+import { pathToFileURL } from 'node:url';+import { shellSingleQuote } from '@inkeep/open-knowledge-core';+import { type Config, readServerLock, resolveLockDir } from '@inkeep/open-knowledge-server';+import { afterEach, beforeAll, beforeEach, describe, expect, test, vi } from 'vitest';+import type { GhDetectResult } from '../auth/gh-detect.ts';+import { FileBackend, type TokenStore } from '../auth/token-store.ts';+import {+ type CliHelperCall,+ gitConfigParameter,+ linesCarrying,+ type RecordedHelperCall,+ readJsonLines,+ writeCliCredentialStandIn,+ writeRecordingCredentialHelper,+} from './git-credential-fixtures.test-helper.ts';+import {+ GITHUB_HOST,+ type GitHubStandIn,+ type PlainHttpInterceptor,+ startGitHubStandIn,+ startPlainHttpInterceptor,+} from './github-stand-in.test-helper.ts';+import { runSync } from './sync.ts';++const ORIGIN_URL = `https://${GITHUB_HOST}/alice/demo.git`;+const OK_TOKEN = 'ok-test-sync-token-in-openknowledges-own-store';+const GH_TOKEN = 'ok-test-sync-token-the-gh-cli-supplies';+const STALE_PASSWORD = 'ok-test-sync-stale-password-in-an-ambient-helper';+const REVOKED_OK_TOKEN = 'ok-test-sync-revoked-token-in-openknowledges-own-store';+const REVOKED_GH_TOKEN = 'ok-test-sync-revoked-token-the-gh-cli-supplies';+const DENIED_OK_TOKEN = 'ok-test-sync-token-github-denies-access-to-the-repository';+const USER_PASSWORD = 'ok-test-sync-users-own-password-in-an-ambient-helper';+const INHERITED_RELAY_TOKEN = 'ok-test-sync-relay-token-inherited-from-the-environment';+const AMBIENT_HELPERS = ['env-count', 'env-parameters', 'github-scoped', 'global'];+const ENTERPRISE_HOST = 'ghe.example.com';+const ENTERPRISE_URL = `https://${ENTERPRISE_HOST}/alice/demo.git`;+const ENTERPRISE_USER = {+ username: 'enterprise-user',+ password: 'ok-test-sync-users-own-enterprise-password',+};++type SyncEvent = { type: string } & Record<string, unknown>;++interface SyncCredentialSeam {+ tokenStore: TokenStore;+ _detectGhFn: (host?: string, options?: { login?: string }) => GhDetectResult;+}++interface AmbientCredential {+ username: string;+ password: string;+}++type GitRunner = (...args: string[]) => void;++interface Scenario {+ gitHubAccepts: string;+ gitHubDenies?: string;+ okStoreToken?: string;+ ambientCredential?: AmbientCredential;+ enterpriseAccepts?: string;+ enterpriseAmbientCredential?: AmbientCredential;+ otherHostAccepts?: Record<string, string>;+ repository?: (git: GitRunner, server: GitHubStandIn) => void;+}++const ghUnavailable = (): GhDetectResult => ({ available: false });++function originUpstreamOnGitHub(git: GitRunner, server: GitHubStandIn): void {+ git('remote', 'add', 'origin', ORIGIN_URL);+ git('push', server.repositoryDir('alice/demo.git'), 'main:main');+ git('config', 'branch.main.remote', 'origin');+ git('config', 'branch.main.merge', 'refs/heads/main');+ git('update-ref', 'refs/remotes/origin/main', 'main');+}++function originUpstreamOnEnterprise(git: GitRunner, server: GitHubStandIn): void {+ git('remote', 'add', 'origin', ENTERPRISE_URL);+ git('push', server.repositoryDir('alice/demo.git', ENTERPRISE_HOST), 'main:main');+ git('config', 'branch.main.remote', 'origin');+ git('config', 'branch.main.merge', 'refs/heads/main');+ git('update-ref', 'refs/remotes/origin/main', 'main');+}++function authenticatedRequest(+ kind: 'fetch' | 'push',+ credential: { username: unknown; password: string },+): Record<string, unknown> {+ return kind === 'fetch'+ ? {+ method: 'GET',+ path: '/alice/demo.git/info/refs?service=git-upload-pack',+ credential,+ status: 200,+ }+ : { method: 'POST', path: '/alice/demo.git/git-receive-pack', credential, status: 200 };+}++describe("ok sync without a running server authenticates through OpenKnowledge's own credential helper", () => {+ let workspace: string;+ let home: string;+ let projectDir: string;+ let credentialStore: string;+ let helperLog: string;+ let cliHelperLog: string;+ let traceFile: string;+ let authFile: string;+ let cliStandIn: string;+ let standIn: GitHubStandIn | undefined;+ let interceptor: PlainHttpInterceptor | undefined;++ beforeAll(() => {+ expect(+ process.env.GIT_CONFIG_GLOBAL,+ 'precondition: GIT_CONFIG_GLOBAL must be unset, or git reads that file instead of the temporary HOME and the credentials this suite approves reach the helpers it names',+ ).toBeUndefined();+ });++ beforeEach(() => {+ workspace = realpathSync(mkdtempSync(join(tmpdir(), 'ok-sync-credential-')));+ home = join(workspace, 'home');+ projectDir = join(workspace, 'project');+ credentialStore = join(workspace, 'git-credentials');+ helperLog = join(workspace, 'ambient-helper-calls.jsonl');+ cliHelperLog = join(workspace, 'cli-helper-calls.jsonl');+ traceFile = join(workspace, 'git-trace.log');+ authFile = join(workspace, 'ok-auth.yml');+ vi.stubEnv('HOME', home);+ vi.stubEnv('USERPROFILE', home);+ vi.stubEnv('XDG_CONFIG_HOME', join(home, '.config'));+ vi.stubEnv('GIT_CONFIG_NOSYSTEM', '1');+ for (const variable of [+ 'NO_PROXY',+ 'no_proxy',+ 'GIT_SSL_CAINFO',+ 'GIT_SSL_NO_VERIFY',+ 'OK_GH_TOKEN',+ 'OK_GH_TOKEN_HOST',+ 'OK_GH_TOKEN_LOGIN',+ ]) {+ vi.stubEnv(variable, undefined);+ }+ mkdirSync(home, { recursive: true });+ mkdirSync(projectDir, { recursive: true });+ cliStandIn = writeCliCredentialStandIn({ dir: workspace, callLog: cliHelperLog, authFile });+ });++ afterEach(async () => {+ vi.restoreAllMocks();+ vi.unstubAllEnvs();+ await standIn?.close();+ standIn = undefined;+ await interceptor?.close();+ interceptor = undefined;+ rmSync(workspace, { recursive: true, force: true });+ });++ function gitCredential(+ operation: 'approve' | 'reject',+ credential: AmbientCredential,+ host = GITHUB_HOST,+ ): void {+ execFileSync('git', ['credential', operation], {+ cwd: projectDir,+ input: `protocol=https\nhost=${host}\nusername=${credential.username}\npassword=${credential.password}\n\n`,+ });+ }++ async function arrange(scenario: Scenario): Promise<GitHubStandIn> {+ const started = await startGitHubStandIn({+ root: join(workspace, 'github'),+ acceptedPassword: scenario.gitHubAccepts,+ ...(scenario.gitHubDenies === undefined ? {} : { deniedPassword: scenario.gitHubDenies }),+ repositories: ['alice/demo.git'],+ enterpriseHosts: {+ ...(scenario.enterpriseAccepts === undefined+ ? {}+ : { [ENTERPRISE_HOST]: [scenario.enterpriseAccepts] }),+ ...Object.fromEntries(+ Object.entries(scenario.otherHostAccepts ?? {}).map(([host, password]) => [+ host,+ [password],+ ]),+ ),+ },+ });+ standIn = started;+ const recordingHelper = writeRecordingCredentialHelper(workspace, helperLog);+ const globalConfig = join(home, '.gitconfig');+ for (const [key, value] of [+ ['user.name', 'Sync Test'],+ ['user.email', '[email protected]'],+ ['init.defaultBranch', 'main'],+ ['commit.gpgsign', 'false'],+ ['http.proxy', started.proxyUrl],+ ['http.sslCAInfo', started.caFile],+ ['http.curloptResolve', `${GITHUB_HOST}:443:127.0.0.1`],+ ['http.curloptResolve', `${ENTERPRISE_HOST}:443:127.0.0.1`],+ ['credential.helper', recordingHelper('global')],+ ['credential.helper', `store --file ${shellSingleQuote(credentialStore)}`],+ [`credential.https://${GITHUB_HOST}.helper`, recordingHelper('github-scoped')],+ ]) {+ execFileSync('git', ['config', '--file', globalConfig, '--add', key, value]);+ }+ vi.stubEnv('GIT_CONFIG_COUNT', '3');+ vi.stubEnv('GIT_CONFIG_KEY_0', 'credential.interactive');+ vi.stubEnv('GIT_CONFIG_VALUE_0', 'false');+ vi.stubEnv('GIT_CONFIG_KEY_1', 'credential.guiPrompt');+ vi.stubEnv('GIT_CONFIG_VALUE_1', 'false');+ vi.stubEnv('GIT_CONFIG_KEY_2', 'credential.helper');+ vi.stubEnv('GIT_CONFIG_VALUE_2', recordingHelper('env-count'));+ vi.stubEnv(+ 'GIT_CONFIG_PARAMETERS',+ gitConfigParameter('credential.helper', recordingHelper('env-parameters')),+ );+ if (scenario.okStoreToken !== undefined) {+ await new FileBackend(authFile).set(GITHUB_HOST, 'alice', scenario.okStoreToken, {+ gitProtocol: 'https',+ });+ }++ const git = (...args: string[]) =>+ execFileSync('git', args, { cwd: projectDir, stdio: 'ignore' });+ git('init', '--initial-branch=main');+ git('commit', '--allow-empty', '-m', 'seed');+ (scenario.repository ?? originUpstreamOnGitHub)(git, started);+ git('commit', '--allow-empty', '-m', 'local work to push');++ const liveness = scenario.ambientCredential ?? {+ username: 'liveness-probe',+ password: 'ok-test-sync-liveness-probe-password',+ };+ gitCredential('approve', liveness);+ expect(+ readJsonLines<RecordedHelperCall>(helperLog).map(+ (call) => `${call.label} ${call.operation} ${call.fields.host}`,+ ),+ ).toEqual(+ expect.arrayContaining(AMBIENT_HELPERS.map((label) => `${label} store ${GITHUB_HOST}`)),+ );+ expect(readFileSync(credentialStore, 'utf-8')).toContain(+ `${liveness.username}:${liveness.password}@${GITHUB_HOST}`,+ );+ if (scenario.ambientCredential === undefined) {+ gitCredential('reject', liveness);+ expect(readFileSync(credentialStore, 'utf-8')).not.toContain(liveness.password);+ }+ if (scenario.enterpriseAmbientCredential !== undefined) {+ const enterprise = scenario.enterpriseAmbientCredential;+ gitCredential('approve', enterprise, ENTERPRISE_HOST);+ expect(readFileSync(credentialStore, 'utf-8')).toContain(+ `${enterprise.username}:${enterprise.password}@${ENTERPRISE_HOST}`,+ );+ }+ writeFileSync(helperLog, '', 'utf-8');+ vi.stubEnv('GIT_TRACE', traceFile);+ return started;+ }++ async function syncWith(+ seam: SyncCredentialSeam,+ op: 'sync' | 'pull' | 'push' = 'sync',+ output: 'json' | 'text' = 'json',+ ): Promise<{ events: SyncEvent[]; stderr: string; error: unknown }> {+ expect(readServerLock(resolveLockDir(projectDir))).toBeNull();+ const events: SyncEvent[] = [];+ let stderr = '';+ const writes = vi.spyOn(process.stdout, 'write').mockImplementation((chunk) => {+ for (const line of String(chunk).split('\n')) {+ if (line.trim() !== '') events.push(JSON.parse(line) as SyncEvent);+ }+ return true;+ });+ const errorWrites =+ output === 'text'+ ? vi.spyOn(process.stderr, 'write').mockImplementation((chunk) => {+ stderr += String(chunk);+ return true;+ })+ : undefined;+ const options: Parameters<typeof runSync>[0] & SyncCredentialSeam = {+ json: output === 'json',+ op,+ ...seam,+ };+ const cliEntry = process.argv[1];+ process.argv[1] = cliStandIn;+ let error: unknown = null;+ try {+ await runSync(options, {} as Config, projectDir);+ } catch (caught) {+ error = caught;+ } finally {+ process.argv[1] = cliEntry;+ writes.mockRestore();+ errorWrites?.mockRestore();+ }+ if (output === 'json') {+ expect(events).toContainEqual({ type: 'step', step: op === 'push' ? 'push' : 'pull' });+ expect(events.filter((event) => event.type === 'triggered')).toEqual([]);+ } else {+ expect(stderr).toContain(`Running ${op} directly (no live server)`);+ expect(stderr).not.toContain('via running server');+ }+ return { events, stderr, error };+ }++ function localAndRemoteMain(repositoryDir: string): { local: string; remote: string } {+ return {+ local: execFileSync('git', ['rev-parse', 'main'], {+ cwd: projectDir,+ encoding: 'utf-8',+ }).trim(),+ remote: execFileSync(+ 'git',+ ['--git-dir', repositoryDir, 'for-each-ref', '--format=%(objectname)', 'refs/heads/main'],+ { encoding: 'utf-8' },+ ).trim(),+ };+ }++ function expectSyncCompletedWith(+ outcome: { events: SyncEvent[]; error: unknown },+ server: GitHubStandIn,+ password: string,+ ): void {+ expect.soft(outcome.error, 'the error runSync threw').toBeNull();+ expect.soft(outcome.events, 'the events runSync emitted').toContainEqual({+ type: 'complete',+ op: 'sync',+ });+ expect.soft(server.requests, "the pull's authenticated fetch").toContainEqual({+ method: 'GET',+ path: '/alice/demo.git/info/refs?service=git-upload-pack',+ credential: { username: expect.any(String), password },+ status: 200,+ });+ expect.soft(server.requests, "the push's authenticated receive-pack").toContainEqual({+ method: 'POST',+ path: '/alice/demo.git/git-receive-pack',+ credential: { username: expect.any(String), password },+ status: 200,+ });+ const main = localAndRemoteMain(server.repositoryDir('alice/demo.git'));+ expect.soft(main.remote, "the remote's main after sync").toBe(main.local);+ }++ function expectCredentialConfinedToCliHelper(+ token: string,+ context: { tracedCommand?: 'pull' | 'push'; remoteName?: string } = {},+ ): void {+ const { tracedCommand = 'pull', remoteName = 'origin' } = context;+ const trace = readFileSync(traceFile, 'utf-8');+ expect(trace).toMatch(new RegExp(`built-in: git ${tracedCommand}`));+ expect(trace).toMatch(/remote-https/);+ const projectGitConfig = readFileSync(join(projectDir, '.git', 'config'), 'utf-8');+ expect(projectGitConfig).toContain(`[remote "${remoteName}"]`);++ expect+ .soft(+ readJsonLines<RecordedHelperCall>(helperLog),+ 'calls reaching the ambient credential helpers',+ )+ .toEqual([]);+ expect+ .soft(+ linesCarrying(readFileSync(credentialStore, 'utf-8'), token),+ 'the ambient git-credential-store file',+ )+ .toEqual([]);+ expect+ .soft(+ linesCarrying(trace, token),+ 'argv of git pull, git push and their children (GIT_TRACE)',+ )+ .toEqual([]);+ expect.soft(linesCarrying(projectGitConfig, token), "the project's .git/config").toEqual([]);+ expect+ .soft(readJsonLines<CliHelperCall>(cliHelperLog), "calls reaching the CLI's own helper")+ .toContainEqual({+ args: ['auth', 'git-credential', 'get'],+ protocol: 'https',+ host: GITHUB_HOST,+ });+ }++ test("with OpenKnowledge signed in and no ambient credential, ok sync pulls and pushes with OpenKnowledge's token", async () => {+ const server = await arrange({ gitHubAccepts: OK_TOKEN, okStoreToken: OK_TOKEN });++ const outcome = await syncWith({+ tokenStore: new FileBackend(authFile),+ _detectGhFn: ghUnavailable,+ });++ expectSyncCompletedWith(outcome, server, OK_TOKEN);+ expectCredentialConfinedToCliHelper(OK_TOKEN);+ });++ test("with a stale credential in an ambient helper, OpenKnowledge's helper wins and the ambient helpers are never asked or told", async () => {+ const stale = { username: 'x-access-token', password: STALE_PASSWORD };+ const server = await arrange({+ gitHubAccepts: OK_TOKEN,+ okStoreToken: OK_TOKEN,+ ambientCredential: stale,+ });++ const outcome = await syncWith({+ tokenStore: new FileBackend(authFile),+ _detectGhFn: ghUnavailable,+ });++ expectSyncCompletedWith(outcome, server, OK_TOKEN);+ expect+ .soft(+ server.requests.filter((request) => request.credential?.password === STALE_PASSWORD),+ 'requests presenting the stale ambient credential',+ )+ .toEqual([]);+ expect+ .soft(+ linesCarrying(readFileSync(credentialStore, 'utf-8'), STALE_PASSWORD),+ "the stale entry still in the user's credential store",+ )+ .toHaveLength(1);+ expectCredentialConfinedToCliHelper(OK_TOKEN);+ });++ test("with only the gh CLI's token available, ok sync pulls and pushes with it through OpenKnowledge's helper", async () => {+ const server = await arrange({ gitHubAccepts: GH_TOKEN });++ const outcome = await syncWith({+ tokenStore: new FileBackend(authFile),+ _detectGhFn: () => ({ available: true, token: GH_TOKEN }),+ });++ expectSyncCompletedWith(outcome, server, GH_TOKEN);+ expectCredentialConfinedToCliHelper(GH_TOKEN);+ });++ test("signed out of OpenKnowledge with gh unavailable, ok sync keeps using the user's own ambient credential", async () => {+ const own = { username: 'ambient-user', password: USER_PASSWORD };+ const server = await arrange({ gitHubAccepts: USER_PASSWORD, ambientCredential: own });++ const outcome = await syncWith({+ tokenStore: new FileBackend(authFile),+ _detectGhFn: ghUnavailable,+ });++ expectSyncCompletedWith(outcome, server, USER_PASSWORD);+ const ambientCalls = readJsonLines<RecordedHelperCall>(helperLog);+ expect+ .soft(ambientCalls, "the user's own helpers asked for the github.com credential")+ .toContainEqual({+ label: 'global',+ operation: 'get',+ fields: expect.objectContaining({ protocol: 'https', host: GITHUB_HOST }),+ relayToken: null,+ });+ expect+ .soft(+ ambientCalls.filter((call) => call.relayToken !== null),+ 'ambient helper calls that inherited an OK_GH_TOKEN relay',+ )+ .toEqual([]);+ expect+ .soft(readJsonLines<CliHelperCall>(cliHelperLog), "calls reaching the CLI's own helper")+ .toEqual([]);+ expect+ .soft(+ linesCarrying(readFileSync(credentialStore, 'utf-8'), USER_PASSWORD),+ "the user's entry still in their credential store",+ )+ .toHaveLength(1);+ });++ test.each([+ {+ variable: 'GIT_CONFIG_GLOBAL',+ signIn: "OpenKnowledge's own store",+ token: OK_TOKEN,+ okStoreToken: OK_TOKEN,+ detectGh: ghUnavailable,+ stubEnvironment: () => {+ const homeConfig = join(home, '.gitconfig');+ const configOutsideHome = join(workspace, 'global.gitconfig');+ copyFileSync(homeConfig, configOutsideHome);+ execFileSync('git', ['config', '--file', homeConfig, '--unset', 'http.sslCAInfo']);+ vi.stubEnv('GIT_CONFIG_GLOBAL', configOutsideHome);+ },+ },+ {+ variable: 'GIT_TEMPLATE_DIR',+ signIn: 'the gh CLI',+ token: GH_TOKEN,+ okStoreToken: undefined,+ detectGh: (): GhDetectResult => ({ available: true, token: GH_TOKEN }),+ stubEnvironment: () => {+ const templates = join(workspace, 'empty-templates');+ mkdirSync(templates);+ vi.stubEnv('GIT_TEMPLATE_DIR', templates);+ },+ },+ {+ variable: 'GIT_CONFIG_KEY_3=protocol.file.allow',+ signIn: "OpenKnowledge's own store",+ token: OK_TOKEN,+ okStoreToken: OK_TOKEN,+ detectGh: ghUnavailable,+ stubEnvironment: () => {+ vi.stubEnv('GIT_CONFIG_COUNT', '4');+ vi.stubEnv('GIT_CONFIG_KEY_3', 'protocol.file.allow');+ vi.stubEnv('GIT_CONFIG_VALUE_3', 'always');+ },+ },+ ])(+ "with $variable in the environment, ok sync signed in through $signIn still pulls and pushes through OpenKnowledge's helper, as the signed-out fallback accepts that environment",+ async (row) => {+ const server = await arrange({ gitHubAccepts: row.token, okStoreToken: row.okStoreToken });+ row.stubEnvironment();++ const outcome = await syncWith({+ tokenStore: new FileBackend(authFile),+ _detectGhFn: row.detectGh,+ });++ expectSyncCompletedWith(outcome, server, row.token);+ expectCredentialConfinedToCliHelper(row.token);+ },+ );++ test("with an OK_GH_TOKEN relay for github.com already in its environment, ok sync signed in to OpenKnowledge pulls and pushes with OpenKnowledge's token and never presents the relay's", async () => {+ const server = await arrange({ gitHubAccepts: OK_TOKEN, okStoreToken: OK_TOKEN });+ vi.stubEnv('OK_GH_TOKEN', INHERITED_RELAY_TOKEN);+ vi.stubEnv('OK_GH_TOKEN_HOST', GITHUB_HOST);+ vi.stubEnv('OK_GH_TOKEN_LOGIN', 'inherited-relay-login');++ const outcome = await syncWith({+ tokenStore: new FileBackend(authFile),+ _detectGhFn: ghUnavailable,+ });++ expectSyncCompletedWith(outcome, server, OK_TOKEN);+ expect+ .soft(+ server.requests.filter((request) => request.credential?.password === INHERITED_RELAY_TOKEN),+ "requests presenting the inherited relay's token",+ )+ .toEqual([]);+ expectCredentialConfinedToCliHelper(OK_TOKEN);+ });++ function signedInSeam(): SyncCredentialSeam {+ return { tokenStore: new FileBackend(authFile), _detectGhFn: ghUnavailable };+ }++ function expectPullToGitHubAndPushToEnterprise(+ outcome: { events: SyncEvent[]; error: unknown },+ server: GitHubStandIn,+ ): void {+ const trace = readFileSync(traceFile, 'utf-8');+ expect(trace).toMatch(/built-in: git pull/);+ expect(trace).toMatch(/remote-https/);+ const cliCalls = readJsonLines<CliHelperCall>(cliHelperLog);+ const enterpriseRequests = server.requestsTo(ENTERPRISE_HOST);++ expect.soft(outcome.error, 'the error runSync threw').toBeNull();+ expect.soft(outcome.events, 'the events runSync emitted').toContainEqual({+ type: 'complete',+ op: 'sync',+ });+ expect+ .soft(server.requests, "the pull's fetch from github.com")+ .toContainEqual(+ authenticatedRequest('fetch', { username: expect.any(String), password: OK_TOKEN }),+ );+ expect+ .soft(enterpriseRequests, "the push's receive-pack on the Enterprise host")+ .toContainEqual(authenticatedRequest('push', ENTERPRISE_USER));+ const main = localAndRemoteMain(server.repositoryDir('alice/demo.git', ENTERPRISE_HOST));+ expect.soft(main.remote, "the Enterprise remote's main after sync").toBe(main.local);+ expect.soft(cliCalls, "calls reaching the CLI's own helper").toContainEqual({+ args: ['auth', 'git-credential', 'get'],+ protocol: 'https',+ host: GITHUB_HOST,+ });+ expect+ .soft(+ cliCalls.filter((call) => call.host !== GITHUB_HOST),+ "calls reaching the CLI's own helper for another host",+ )+ .toEqual([]);+ expect+ .soft(+ enterpriseRequests.filter((request) => request.credential?.password === OK_TOKEN),+ "Enterprise requests presenting OpenKnowledge's token",+ )+ .toEqual([]);+ expect+ .soft(+ linesCarrying(readFileSync(helperLog, 'utf-8'), OK_TOKEN),+ "ambient helper calls carrying OpenKnowledge's token",+ )+ .toEqual([]);+ expect+ .soft(+ linesCarrying(readFileSync(credentialStore, 'utf-8'), OK_TOKEN),+ 'the ambient git-credential-store file',+ )+ .toEqual([]);+ expect+ .soft(+ linesCarrying(trace, OK_TOKEN),+ 'argv of git pull, git push and their children (GIT_TRACE)',+ )+ .toEqual([]);+ }++ test("with the branch's push remote on an Enterprise host, the pull presents OpenKnowledge's token to github.com and the push the user's own Enterprise credential", async () => {+ const server = await arrange({+ gitHubAccepts: OK_TOKEN,+ okStoreToken: OK_TOKEN,+ enterpriseAccepts: ENTERPRISE_USER.password,+ enterpriseAmbientCredential: ENTERPRISE_USER,+ repository: (git, github) => {+ originUpstreamOnGitHub(git, github);+ git('remote', 'add', 'enterprise', ENTERPRISE_URL);+ git('push', github.repositoryDir('alice/demo.git', ENTERPRISE_HOST), 'main:main');+ git('config', 'branch.main.pushRemote', 'enterprise');+ },+ });++ const outcome = await syncWith(signedInSeam());++ expectPullToGitHubAndPushToEnterprise(outcome, server);+ });++ test("with origin's pushurl on an Enterprise host, the push's credential follows the push URL", async () => {+ const server = await arrange({+ gitHubAccepts: OK_TOKEN,+ okStoreToken: OK_TOKEN,+ enterpriseAccepts: ENTERPRISE_USER.password,+ enterpriseAmbientCredential: ENTERPRISE_USER,+ repository: (git, github) => {+ originUpstreamOnGitHub(git, github);+ git('config', 'remote.origin.pushurl', ENTERPRISE_URL);+ git('push', github.repositoryDir('alice/demo.git', ENTERPRISE_HOST), 'main:main');+ },+ });++ const outcome = await syncWith(signedInSeam());++ expectPullToGitHubAndPushToEnterprise(outcome, server);+ });++ test("with an insteadOf rewrite of github.com to an Enterprise host, ok sync keeps the user's own Enterprise credential and never asks OpenKnowledge's helper", async () => {+ const server = await arrange({+ gitHubAccepts: OK_TOKEN,+ okStoreToken: OK_TOKEN,+ enterpriseAccepts: ENTERPRISE_USER.password,+ enterpriseAmbientCredential: ENTERPRISE_USER,+ repository: (git, github) => {+ originUpstreamOnGitHub(git, github);+ git('push', github.repositoryDir('alice/demo.git', ENTERPRISE_HOST), 'main:main');+ git('config', `url.https://${ENTERPRISE_HOST}/.insteadOf`, `https://${GITHUB_HOST}/`);+ },+ });++ const outcome = await syncWith(signedInSeam());++ const trace = readFileSync(traceFile, 'utf-8');+ expect(trace).toMatch(/built-in: git pull/);+ expect(trace).toMatch(/remote-https/);+ const enterpriseRequests = server.requestsTo(ENTERPRISE_HOST);+ expect.soft(outcome.error, 'the error runSync threw').toBeNull();+ expect.soft(outcome.events, 'the events runSync emitted').toContainEqual({+ type: 'complete',+ op: 'sync',+ });+ expect+ .soft(enterpriseRequests, "the pull's fetch from the Enterprise host")+ .toContainEqual(authenticatedRequest('fetch', ENTERPRISE_USER));+ expect+ .soft(enterpriseRequests, "the push's receive-pack on the Enterprise host")+ .toContainEqual(authenticatedRequest('push', ENTERPRISE_USER));+ const main = localAndRemoteMain(server.repositoryDir('alice/demo.git', ENTERPRISE_HOST));+ expect.soft(main.remote, "the Enterprise remote's main after sync").toBe(main.local);+ expect.soft(server.requests, 'requests reaching github.com').toEqual([]);+ expect+ .soft(readJsonLines<CliHelperCall>(cliHelperLog), "calls reaching the CLI's own helper")+ .toEqual([]);+ expect+ .soft(+ enterpriseRequests.filter((request) => request.credential?.password === OK_TOKEN),+ "Enterprise requests presenting OpenKnowledge's token",+ )+ .toEqual([]);+ expect+ .soft(+ linesCarrying(readFileSync(helperLog, 'utf-8'), OK_TOKEN),+ "ambient helper calls carrying OpenKnowledge's token",+ )+ .toEqual([]);+ expect+ .soft(+ linesCarrying(trace, OK_TOKEN),+ 'argv of git pull, git push and their children (GIT_TRACE)',+ )+ .toEqual([]);+ });++ test("with a single remote not named origin and no upstream, ok push presents OpenKnowledge's token to that remote", async () => {+ const server = await arrange({+ gitHubAccepts: OK_TOKEN,+ okStoreToken: OK_TOKEN,+ repository: (git, github) => {+ git('remote', 'add', 'upstream', ORIGIN_URL);+ git('push', github.repositoryDir('alice/demo.git'), 'main:main');+ git('config', 'push.default', 'current');+ },+ });++ const outcome = await syncWith(signedInSeam(), 'push');++ expect.soft(outcome.error, 'the error runSync threw').toBeNull();+ expect.soft(outcome.events, 'the events runSync emitted').toContainEqual({+ type: 'complete',+ op: 'push',+ });+ expect+ .soft(server.requests, "the push's authenticated receive-pack")+ .toContainEqual(+ authenticatedRequest('push', { username: expect.any(String), password: OK_TOKEN }),+ );+ const main = localAndRemoteMain(server.repositoryDir('alice/demo.git'));+ expect.soft(main.remote, "the remote's main after the push").toBe(main.local);+ expectCredentialConfinedToCliHelper(OK_TOKEN, {+ tracedCommand: 'push',+ remoteName: 'upstream',+ });+ });++ test("with origin and another remote and no upstream, ok push presents OpenKnowledge's token to origin", async () => {+ const server = await arrange({+ gitHubAccepts: OK_TOKEN,+ okStoreToken: OK_TOKEN,+ enterpriseAccepts: ENTERPRISE_USER.password,+ enterpriseAmbientCredential: ENTERPRISE_USER,+ repository: (git, github) => {+ git('remote', 'add', 'origin', ORIGIN_URL);+ git('push', github.repositoryDir('alice/demo.git'), 'main:main');+ git('remote', 'add', 'enterprise', ENTERPRISE_URL);+ git('push', github.repositoryDir('alice/demo.git', ENTERPRISE_HOST), 'main:main');+ git('config', 'push.default', 'current');+ },+ });++ const outcome = await syncWith(signedInSeam(), 'push');++ expect.soft(outcome.error, 'the error runSync threw').toBeNull();+ expect.soft(outcome.events, 'the events runSync emitted').toContainEqual({+ type: 'complete',+ op: 'push',+ });+ expect+ .soft(server.requests, "the push's authenticated receive-pack on github.com")+ .toContainEqual(+ authenticatedRequest('push', { username: expect.any(String), password: OK_TOKEN }),+ );+ const main = localAndRemoteMain(server.repositoryDir('alice/demo.git'));+ expect.soft(main.remote, "github.com's main after the push").toBe(main.local);+ expect+ .soft(server.requestsTo(ENTERPRISE_HOST), 'requests reaching the Enterprise host')+ .toEqual([]);+ expectCredentialConfinedToCliHelper(OK_TOKEN, { tracedCommand: 'push' });+ });++ test.each([+ { form: 'a local path', urlOf: (repository: string) => repository },+ { form: 'a file:// URL', urlOf: (repository: string) => pathToFileURL(repository).href },+ ])(+ "with origin at $form, ok sync completes as it does today and never asks OpenKnowledge's helper",+ async ({ urlOf }) => {+ const localRemote = join(workspace, 'local-remote.git');+ const server = await arrange({+ gitHubAccepts: OK_TOKEN,+ okStoreToken: OK_TOKEN,+ repository: (git) => {+ execFileSync('git', ['init', '--quiet', '--bare', '--initial-branch=main', localRemote]);+ git('remote', 'add', 'origin', urlOf(localRemote));+ git('push', localRemote, 'main:main');+ git('config', 'branch.main.remote', 'origin');+ git('config', 'branch.main.merge', 'refs/heads/main');+ git('update-ref', 'refs/remotes/origin/main', 'main');+ },+ });++ const outcome = await syncWith(signedInSeam());++ expect.soft(outcome.error, 'the error runSync threw').toBeNull();+ expect.soft(outcome.events, 'the events runSync emitted').toContainEqual({+ type: 'complete',+ op: 'sync',+ });+ const main = localAndRemoteMain(localRemote);+ expect.soft(main.remote, "the local remote's main after sync").toBe(main.local);+ expect+ .soft(readJsonLines<CliHelperCall>(cliHelperLog), "calls reaching the CLI's own helper")+ .toEqual([]);+ expect.soft(server.requests, 'requests reaching github.com').toEqual([]);+ },+ );++ test("with origin at a plain http:// GitHub URL, ok sync never releases OpenKnowledge's token to a request that would carry it over plain http", async () => {+ const onPath = await startPlainHttpInterceptor();+ interceptor = onPath;+ await arrange({+ gitHubAccepts: OK_TOKEN,+ okStoreToken: OK_TOKEN,+ repository: (git, github) => {+ originUpstreamOnGitHub(git, github);+ git('remote', 'set-url', 'origin', `http://${GITHUB_HOST}/alice/demo.git`);+ git('config', 'http.proxy', onPath.proxyUrl);+ },+ });++ const outcome = await syncWith(signedInSeam());++ expect(onPath.requests.map((request) => request.url)).toContain(+ `http://${GITHUB_HOST}/alice/demo.git/info/refs?service=git-upload-pack`,+ );+ expect.soft(outcome.error, 'the error runSync threw').not.toBeNull();+ expect+ .soft(+ onPath.requests.filter((request) => request.basicCredential?.includes(OK_TOKEN)),+ "plain-http requests carrying OpenKnowledge's token",+ )+ .toEqual([]);+ expect+ .soft(readJsonLines<CliHelperCall>(cliHelperLog), "calls reaching the CLI's own helper")+ .toEqual([]);+ expect+ .soft(+ readJsonLines<RecordedHelperCall>(helperLog),+ "the user's own helpers asked for the http credential",+ )+ .toContainEqual({+ label: 'global',+ operation: 'get',+ fields: expect.objectContaining({ protocol: 'http', host: GITHUB_HOST }),+ relayToken: null,+ });+ });++ const ghHoldsAToken = (): SyncCredentialSeam => ({+ tokenStore: new FileBackend(authFile),+ _detectGhFn: () => ({ available: true, token: GH_TOKEN }),+ });++ test("with origin on an Enterprise host that is not declared, ok sync keeps the user's own credential even when the gh CLI holds a token for that host", async () => {+ const server = await arrange({+ gitHubAccepts: OK_TOKEN,+ enterpriseAccepts: ENTERPRISE_USER.password,+ enterpriseAmbientCredential: ENTERPRISE_USER,+ repository: originUpstreamOnEnterprise,+ });++ const outcome = await syncWith(ghHoldsAToken());++ const enterpriseRequests = server.requestsTo(ENTERPRISE_HOST);+ const ambientCalls = readJsonLines<RecordedHelperCall>(helperLog);+ expect.soft(outcome.error, 'the error runSync threw').toBeNull();+ expect.soft(outcome.events, 'the events runSync emitted').toContainEqual({+ type: 'complete',+ op: 'sync',+ });+ expect+ .soft(enterpriseRequests, "the pull's fetch from the Enterprise host")+ .toContainEqual(authenticatedRequest('fetch', ENTERPRISE_USER));+ expect+ .soft(enterpriseRequests, "the push's receive-pack on the Enterprise host")+ .toContainEqual(authenticatedRequest('push', ENTERPRISE_USER));+ expect+ .soft(+ enterpriseRequests.filter((request) => request.credential?.password === GH_TOKEN),+ "Enterprise requests presenting the gh CLI's token",+ )+ .toEqual([]);+ expect+ .soft(readJsonLines<CliHelperCall>(cliHelperLog), "calls reaching the CLI's own helper")+ .toEqual([]);+ expect+ .soft(ambientCalls, "the user's own helpers asked for the Enterprise credential")+ .toContainEqual({+ label: 'global',+ operation: 'get',+ fields: expect.objectContaining({ protocol: 'https', host: ENTERPRISE_HOST }),+ relayToken: null,+ });+ expect+ .soft(+ ambientCalls.filter((call) => call.relayToken !== null),+ 'ambient helper calls that inherited an OK_GH_TOKEN relay',+ )+ .toEqual([]);+ });++ test("with origin on an Enterprise host declared in ~/.ok/global.yml, ok sync presents the gh CLI's token through OpenKnowledge's helper and never asks the user's own helpers", async () => {+ mkdirSync(join(home, '.ok'), { recursive: true });+ writeFileSync(+ join(home, '.ok', 'global.yml'),+ `git:\n hosts:\n ${ENTERPRISE_HOST}:\n provider: github\n`,+ );+ const server = await arrange({+ gitHubAccepts: OK_TOKEN,+ enterpriseAccepts: GH_TOKEN,+ enterpriseAmbientCredential: ENTERPRISE_USER,+ repository: originUpstreamOnEnterprise,+ });++ const outcome = await syncWith(ghHoldsAToken());++ const enterpriseRequests = server.requestsTo(ENTERPRISE_HOST);+ expect.soft(outcome.error, 'the error runSync threw').toBeNull();+ expect+ .soft(enterpriseRequests, "the push's receive-pack on the Enterprise host")+ .toContainEqual(+ authenticatedRequest('push', { username: expect.any(String), password: GH_TOKEN }),+ );+ expect+ .soft(readJsonLines<CliHelperCall>(cliHelperLog), "calls reaching the CLI's own helper")+ .toContainEqual({+ args: ['auth', 'git-credential', 'get'],+ protocol: 'https',+ host: ENTERPRISE_HOST,+ });+ expect+ .soft(readJsonLines<RecordedHelperCall>(helperLog), 'calls reaching the ambient helpers')+ .toEqual([]);+ });++ test("with origin on a declared Enterprise host at a non-default port, ok sync keeps the user's own credential and hands no relay to their helpers", async () => {+ const portedHost = `${ENTERPRISE_HOST}:8443`;+ mkdirSync(join(home, '.ok'), { recursive: true });+ writeFileSync(+ join(home, '.ok', 'global.yml'),+ `git:\n hosts:\n ${ENTERPRISE_HOST}:\n provider: github\n`,+ );+ const server = await arrange({+ gitHubAccepts: OK_TOKEN,+ enterpriseAccepts: ENTERPRISE_USER.password,+ repository: (git, github) => {+ git('remote', 'add', 'origin', `https://${portedHost}/alice/demo.git`);+ git('push', github.repositoryDir('alice/demo.git', ENTERPRISE_HOST), 'main:main');+ git('config', 'branch.main.remote', 'origin');+ git('config', 'branch.main.merge', 'refs/heads/main');+ git('update-ref', 'refs/remotes/origin/main', 'main');+ },+ });+ gitCredential('approve', ENTERPRISE_USER, portedHost);+ writeFileSync(helperLog, '', 'utf-8');++ const outcome = await syncWith(ghHoldsAToken());++ const enterpriseRequests = server.requestsTo(ENTERPRISE_HOST);+ const ambientCalls = readJsonLines<RecordedHelperCall>(helperLog);+ expect.soft(outcome.error, 'the error runSync threw').toBeNull();+ expect+ .soft(enterpriseRequests, "the push's receive-pack on the ported Enterprise host")+ .toContainEqual(authenticatedRequest('push', ENTERPRISE_USER));+ expect+ .soft(readJsonLines<CliHelperCall>(cliHelperLog), "calls reaching the CLI's own helper")+ .toEqual([]);+ expect+ .soft(ambientCalls, "the user's own helpers asked for the ported host's credential")+ .toContainEqual({+ label: 'global',+ operation: 'get',+ fields: expect.objectContaining({ protocol: 'https', host: portedHost }),+ relayToken: null,+ });+ expect+ .soft(+ ambientCalls.filter((call) => call.relayToken !== null),+ 'ambient helper calls that inherited an OK_GH_TOKEN relay',+ )+ .toEqual([]);+ });++ test.each([+ { form: 'an explicit :443', writtenHost: `${GITHUB_HOST}:443`, servedBy: GITHUB_HOST },+ { form: 'a mixed-case host', writtenHost: 'GitHub.com', servedBy: GITHUB_HOST },+ { form: 'the www.github.com alias', writtenHost: 'www.github.com', servedBy: 'www.github.com' },+ ])(+ "with origin's host written as $form, ok sync keeps the user's own credential, never asks OpenKnowledge's helper and hands no relay to the user's helpers",+ async ({ writtenHost, servedBy }) => {+ const own = { username: 'ambient-user', password: USER_PASSWORD };+ const server = await arrange({+ gitHubAccepts: USER_PASSWORD,+ otherHostAccepts: { 'www.github.com': USER_PASSWORD },+ repository: (git, github) => {+ git('remote', 'add', 'origin', `https://${writtenHost}/alice/demo.git`);+ git('push', github.repositoryDir('alice/demo.git', servedBy), 'main:main');+ git('config', 'branch.main.remote', 'origin');+ git('config', 'branch.main.merge', 'refs/heads/main');+ git('update-ref', 'refs/remotes/origin/main', 'main');+ },+ });+ gitCredential('approve', own, writtenHost);+ writeFileSync(helperLog, '', 'utf-8');++ const outcome = await syncWith(ghHoldsAToken());++ const ambientCalls = readJsonLines<RecordedHelperCall>(helperLog);+ expect.soft(outcome.error, 'the error runSync threw').toBeNull();+ expect+ .soft(server.requestsTo(servedBy), "the push's receive-pack with the user's credential")+ .toContainEqual(authenticatedRequest('push', own));+ expect+ .soft(readJsonLines<CliHelperCall>(cliHelperLog), "calls reaching the CLI's own helper")+ .toEqual([]);+ expect+ .soft(ambientCalls, "the user's own helpers asked for the credential as git wrote the host")+ .toContainEqual({+ label: 'global',+ operation: 'get',+ fields: expect.objectContaining({ protocol: 'https', host: writtenHost }),+ relayToken: null,+ });+ expect+ .soft(+ ambientCalls.filter((call) => call.relayToken !== null),+ 'ambient helper calls that inherited an OK_GH_TOKEN relay',+ )+ .toEqual([]);+ },+ );++ test.each(['json', 'text'] as const)(+ "when the remote URL names an account the gh CLI cannot confirm, ok sync's %s output says it used the gh CLI's active account",+ async (output) => {+ const server = await arrange({+ gitHubAccepts: GH_TOKEN,+ repository: (git, github) => {+ originUpstreamOnGitHub(git, github);+ git('remote', 'set-url', 'origin', `https://alice@${GITHUB_HOST}/alice/demo.git`);+ },+ });++ const outcome = await syncWith(+ {+ tokenStore: new FileBackend(authFile),+ _detectGhFn: (_host, options) => ({+ available: true,+ token: GH_TOKEN,+ ...(options?.login !== undefined ? { fallback: true } : {}),+ }),+ },+ 'sync',+ output,+ );++ expect.soft(outcome.error, 'the error runSync threw').toBeNull();+ expect+ .soft(server.requests, "the push's receive-pack with the active account's token")+ .toContainEqual(+ authenticatedRequest('push', { username: expect.any(String), password: GH_TOKEN }),+ );+ const warned =+ output === 'json'+ ? outcome.events+ .filter((event) => event.type === 'warning')+ .map((event) => String(event.message))+ .join('\n')+ : outcome.stderr;+ expect(warned, 'the warning ok sync printed').toContain(+ "The remote URL names alice, but the GitHub CLI couldn't confirm that account — ok sync used its active account.",+ );+ expect(warned, 'the warning ok sync printed').toContain('gh auth status');+ },+ );++ test.each([+ {+ source: "OpenKnowledge's own stored sign-in",+ okStoreToken: REVOKED_OK_TOKEN,+ detectGh: ghUnavailable,+ rejected: REVOKED_OK_TOKEN,+ named: `OpenKnowledge's GitHub sign-in for ${GITHUB_HOST}`,+ remedy: `ok auth login --host ${GITHUB_HOST}`,+ },+ {+ source: "the gh CLI's sign-in",+ okStoreToken: undefined,+ detectGh: (): GhDetectResult => ({ available: true, token: REVOKED_GH_TOKEN }),+ rejected: REVOKED_GH_TOKEN,+ named: `the GitHub CLI's sign-in for ${GITHUB_HOST}`,+ remedy: `gh auth login --hostname ${GITHUB_HOST}`,+ },+ ])(+ "when GitHub rejects $source while the user's own helper holds a working credential, ok sync's error names the credential it used and how to renew it",+ async (row) => {+ const server = await arrange({+ gitHubAccepts: USER_PASSWORD,+ okStoreToken: row.okStoreToken,+ ambientCredential: { username: 'ambient-user', password: USER_PASSWORD },+ });++ const outcome = await syncWith({+ tokenStore: new FileBackend(authFile),+ _detectGhFn: row.detectGh,+ });++ expect(server.requests, 'the fetch presenting the rejected credential').toContainEqual({+ method: 'GET',+ path: '/alice/demo.git/info/refs?service=git-upload-pack',+ credential: { username: expect.any(String), password: row.rejected },+ status: 401,+ });+ const message =+ outcome.error instanceof Error ? outcome.error.message : String(outcome.error);+ expect+ .soft(message, 'the reason git gave, as ok sync reports it')+ .toContain('Authentication failed');+ expect.soft(message, 'the credential ok sync reports it used').toContain(row.named);+ expect.soft(message, 'the renewal command ok sync reports').toContain(`run: ${row.remedy}`);+ },+ );++ test("when github.com denies OpenKnowledge's sign-in access to the repository, ok sync's error names that sign-in and gives no renewal command", async () => {+ const server = await arrange({+ gitHubAccepts: USER_PASSWORD,+ gitHubDenies: DENIED_OK_TOKEN,+ okStoreToken: DENIED_OK_TOKEN,+ });++ const outcome = await syncWith(signedInSeam());++ expect(server.requests, 'the fetch presenting the denied credential').toContainEqual({+ method: 'GET',+ path: '/alice/demo.git/info/refs?service=git-upload-pack',+ credential: { username: expect.any(String), password: DENIED_OK_TOKEN },+ status: 403,+ });+ const message = outcome.error instanceof Error ? outcome.error.message : String(outcome.error);+ expect.soft(message, 'the reason git gave, as ok sync reports it').toContain('403');+ expect+ .soft(message, 'the credential ok sync reports it used')+ .toContain(`OpenKnowledge's GitHub sign-in for ${GITHUB_HOST}`);+ expect+ .soft(message, 'the guidance ok sync gives for a denied account')+ .toContain('check that it has access to the repository');+ expect.soft(message, 'a renewal command in the message').not.toContain('auth login');+ });++ test("when the pull advances a populated submodule on a plain http:// GitHub URL, ok sync never releases OpenKnowledge's credential to the submodule's plain-http request", async () => {+ const onPath = await startPlainHttpInterceptor();+ interceptor = onPath;+ const submoduleSource = join(workspace, 'submodule-source');+ const upstreamWork = join(workspace, 'upstream-work');+ const server = await arrange({+ gitHubAccepts: GH_TOKEN,+ repository: (git, github) => {+ execFileSync('git', ['init', '--quiet', '--initial-branch=main', submoduleSource]);+ execFileSync('git', [+ '-C',+ submoduleSource,+ 'commit',+ '--quiet',+ '--allow-empty',+ '-m',+ 'one',+ ]);+ git(+ '-c',+ 'protocol.file.allow=always',+ 'submodule',+ 'add',+ '--quiet',+ submoduleSource,+ 'sub',+ );+ git('-C', 'sub', 'remote', 'set-url', 'origin', `http://${GITHUB_HOST}/alice/sub.git`);+ git('commit', '--quiet', '-m', 'add the submodule');+ originUpstreamOnGitHub(git, github);+ execFileSync('git', [+ '-C',+ submoduleSource,+ 'commit',+ '--quiet',+ '--allow-empty',+ '-m',+ 'two',+ ]);+ const advanced = execFileSync('git', ['-C', submoduleSource, 'rev-parse', 'HEAD'], {+ encoding: 'utf-8',+ }).trim();+ execFileSync('git', [+ 'clone',+ '--quiet',+ github.repositoryDir('alice/demo.git'),+ upstreamWork,+ ]);+ execFileSync('git', [+ '-C',+ upstreamWork,+ 'update-index',+ '--cacheinfo',+ `160000,${advanced},sub`,+ ]);+ execFileSync('git', [+ '-C',+ upstreamWork,+ 'commit',+ '--quiet',+ '-m',+ 'advance the submodule',+ ]);+ execFileSync('git', ['-C', upstreamWork, 'push', '--quiet', 'origin', 'main']);+ execFileSync('git', [+ 'config',+ '--file',+ join(home, '.gitconfig'),+ `http.http://${GITHUB_HOST}.proxy`,+ onPath.proxyUrl,+ ]);+ },+ });++ await syncWith(ghHoldsAToken());++ expect(server.requests, "the superproject's fetch from github.com").toContainEqual(+ authenticatedRequest('fetch', { username: expect.any(String), password: GH_TOKEN }),+ );+ expect(+ onPath.requests.map((request) => request.url),+ "the submodule's plain-http fetch",+ ).toContain(`http://${GITHUB_HOST}/alice/sub.git/info/refs?service=git-upload-pack`);+ expect+ .soft(+ onPath.requests.filter((request) => request.basicCredential?.includes(GH_TOKEN)),+ "plain-http requests carrying the gh CLI's token",+ )+ .toEqual([]);+ expect+ .soft(+ readJsonLines<CliHelperCall>(cliHelperLog).filter((call) => call.protocol !== 'https'),+ "non-https calls reaching the CLI's own helper",+ )+ .toEqual([]);+ expect+ .soft(+ readJsonLines<RecordedHelperCall>(helperLog),+ "the user's own helpers asked for the submodule's http credential",+ )+ .toContainEqual(+ expect.objectContaining({+ label: 'global',+ operation: 'get',+ fields: expect.objectContaining({ protocol: 'http', host: GITHUB_HOST }),+ }),+ );+ });++ test("with a second plain http:// push URL on origin, ok push presents OpenKnowledge's token to the https push URL and never to the plain-http one", async () => {+ const onPath = await startPlainHttpInterceptor();+ interceptor = onPath;+ const server = await arrange({+ gitHubAccepts: OK_TOKEN,+ okStoreToken: OK_TOKEN,+ repository: (git, github) => {+ originUpstreamOnGitHub(git, github);+ git('config', '--add', 'remote.origin.pushurl', ORIGIN_URL);+ git('config', '--add', 'remote.origin.pushurl', `http://${GITHUB_HOST}/alice/mirror.git`);+ git('config', `http.http://${GITHUB_HOST}.proxy`, onPath.proxyUrl);+ },+ });++ const outcome = await syncWith(signedInSeam(), 'push');++ expect(server.requests, "the https push URL's receive-pack").toContainEqual(+ authenticatedRequest('push', { username: expect.any(String), password: OK_TOKEN }),+ );+ expect(+ onPath.requests.map((request) => request.url),+ "the plain-http push URL's request",+ ).toContain(`http://${GITHUB_HOST}/alice/mirror.git/info/refs?service=git-receive-pack`);+ expect.soft(outcome.error, 'the error runSync threw').not.toBeNull();+ const message = outcome.error instanceof Error ? outcome.error.message : String(outcome.error);+ expect+ .soft(message, 'the failure ok push reports for the plain-http push URL')+ .not.toContain("OpenKnowledge's GitHub sign-in");+ expect.soft(message, 'a renewal command in the message').not.toContain('auth login');+ expect+ .soft(+ onPath.requests.filter((request) => request.basicCredential?.includes(OK_TOKEN)),+ "plain-http requests carrying OpenKnowledge's token",+ )+ .toEqual([]);+ expect+ .soft(+ readJsonLines<CliHelperCall>(cliHelperLog).filter((call) => call.protocol !== 'https'),+ "non-https calls reaching the CLI's own helper",+ )+ .toEqual([]);+ expect+ .soft(+ readJsonLines<RecordedHelperCall>(helperLog),+ "the user's own helpers asked for the plain-http push URL's credential",+ )+ .toContainEqual({+ label: 'global',+ operation: 'get',+ fields: expect.objectContaining({ protocol: 'http', host: GITHUB_HOST }),+ relayToken: null,+ });+ });++ test.each([+ { spelling: 'in different letter case', writtenHost: 'GitHub.com' },+ { spelling: 'with an explicit :443', writtenHost: `${GITHUB_HOST}:443` },+ ])(+ "with a second push URL that writes the signed-in host $spelling, ok push sends that URL's credential request only to OpenKnowledge's helper, which declines it, and never to the user's own helpers",+ async ({ writtenHost }) => {+ const server = await arrange({+ gitHubAccepts: OK_TOKEN,+ okStoreToken: OK_TOKEN,+ repository: (git, github) => {+ originUpstreamOnGitHub(git, github);+ git('config', '--add', 'remote.origin.pushurl', ORIGIN_URL);+ git('config', '--add', 'remote.origin.pushurl', `https://${writtenHost}/alice/demo.git`);+ },+ });++ const outcome = await syncWith(signedInSeam(), 'push');++ expect(server.requests, "the first push URL's receive-pack").toContainEqual(+ authenticatedRequest('push', { username: expect.any(String), password: OK_TOKEN }),+ );+ expect+ .soft(+ readJsonLines<CliHelperCall>(cliHelperLog),+ "OpenKnowledge's helper asked for the second URL",+ )+ .toContainEqual({+ args: ['auth', 'git-credential', 'get'],+ protocol: 'https',+ host: writtenHost,+ });+ expect+ .soft(readJsonLines<RecordedHelperCall>(helperLog), 'calls reaching the ambient helpers')+ .toEqual([]);+ expect.soft(outcome.error, 'the error runSync threw').not.toBeNull();+ const message =+ outcome.error instanceof Error ? outcome.error.message : String(outcome.error);+ expect+ .soft(message, 'the failure ok push reports for the second push URL')+ .not.toContain("OpenKnowledge's GitHub sign-in");+ },+ );++ test("with OpenKnowledge signed in and no ambient credential, ok pull authenticates its fetch with OpenKnowledge's token", async () => {+ const server = await arrange({ gitHubAccepts: OK_TOKEN, okStoreToken: OK_TOKEN });++ const outcome = await syncWith(signedInSeam(), 'pull');++ expect.soft(outcome.error, 'the error runSync threw').toBeNull();+ expect.soft(outcome.events, 'the events runSync emitted').toContainEqual({+ type: 'complete',+ op: 'pull',+ });+ expect+ .soft(server.requests, "the pull's authenticated fetch")+ .toContainEqual(+ authenticatedRequest('fetch', { username: expect.any(String), password: OK_TOKEN }),+ );+ expectCredentialConfinedToCliHelper(OK_TOKEN, { tracedCommand: 'pull' });+ });++ test("with OpenKnowledge signed in and no ambient credential, ok push authenticates with OpenKnowledge's token", async () => {+ const server = await arrange({ gitHubAccepts: OK_TOKEN, okStoreToken: OK_TOKEN });++ const outcome = await syncWith(signedInSeam(), 'push');++ expect.soft(outcome.error, 'the error runSync threw').toBeNull();+ expect.soft(outcome.events, 'the events runSync emitted').toContainEqual({+ type: 'complete',+ op: 'push',+ });+ expect+ .soft(server.requests, "the push's authenticated receive-pack")+ .toContainEqual(+ authenticatedRequest('push', { username: expect.any(String), password: OK_TOKEN }),+ );+ const main = localAndRemoteMain(server.repositoryDir('alice/demo.git'));+ expect.soft(main.remote, "the remote's main after the push").toBe(main.local);+ expectCredentialConfinedToCliHelper(OK_TOKEN, { tracedCommand: 'push' });+ });++ test("with origin at an https URL whose path OpenKnowledge's credential chain cannot read, ok sync keeps the user's own credential and never asks OpenKnowledge's helper", async () => {+ const nestedRepository = 'team/alice/demo.git';+ const server = await arrange({+ gitHubAccepts: OK_TOKEN,+ okStoreToken: OK_TOKEN,+ enterpriseAccepts: ENTERPRISE_USER.password,+ enterpriseAmbientCredential: ENTERPRISE_USER,+ repository: (git, github) => {+ const remote = github.repositoryDir(nestedRepository, ENTERPRISE_HOST);+ execFileSync('git', ['init', '--quiet', '--bare', '--initial-branch=main', remote]);+ git('remote', 'add', 'origin', `https://${ENTERPRISE_HOST}/${nestedRepository}`);+ git('push', remote, 'main:main');+ git('config', 'branch.main.remote', 'origin');+ git('config', 'branch.main.merge', 'refs/heads/main');+ git('update-ref', 'refs/remotes/origin/main', 'main');+ },+ });++ const outcome = await syncWith(signedInSeam());++ const enterpriseRequests = server.requestsTo(ENTERPRISE_HOST);+ expect.soft(outcome.error, 'the error runSync threw').toBeNull();+ expect.soft(outcome.events, 'the events runSync emitted').toContainEqual({+ type: 'complete',+ op: 'sync',+ });+ expect.soft(enterpriseRequests, "the pull's fetch from the Enterprise host").toContainEqual({+ method: 'GET',+ path: `/${nestedRepository}/info/refs?service=git-upload-pack`,+ credential: ENTERPRISE_USER,+ status: 200,+ });+ expect+ .soft(enterpriseRequests, "the push's receive-pack on the Enterprise host")+ .toContainEqual({+ method: 'POST',+ path: `/${nestedRepository}/git-receive-pack`,+ credential: ENTERPRISE_USER,+ status: 200,+ });+ const main = localAndRemoteMain(server.repositoryDir(nestedRepository, ENTERPRISE_HOST));+ expect.soft(main.remote, "the Enterprise remote's main after sync").toBe(main.local);+ expect+ .soft(readJsonLines<CliHelperCall>(cliHelperLog), "calls reaching the CLI's own helper")+ .toEqual([]);+ expect.soft(server.requests, 'requests reaching github.com').toEqual([]);+ });++ test("with no remote configured, ok sync still runs git's own pull and reports its failure", async () => {+ const server = await arrange({+ gitHubAccepts: OK_TOKEN,+ okStoreToken: OK_TOKEN,+ repository: () => {},+ });++ const outcome = await syncWith(signedInSeam());++ expect.soft(outcome.error, 'the error runSync threw').not.toBeNull();+ expect+ .soft(readFileSync(traceFile, 'utf-8'), "git's own pull in the trace")+ .toMatch(/built-in: git pull/);+ expect+ .soft(readJsonLines<CliHelperCall>(cliHelperLog), "calls reaching the CLI's own helper")+ .toEqual([]);+ expect.soft(server.requests, 'requests reaching github.com').toEqual([]);+ });++ test("on a detached HEAD, ok sync's fetch presents OpenKnowledge's token to the only remote", async () => {+ const server = await arrange({+ gitHubAccepts: OK_TOKEN,+ okStoreToken: OK_TOKEN,+ repository: (git, github) => {+ originUpstreamOnGitHub(git, github);+ git('checkout', '--quiet', '--detach');+ },+ });++ await syncWith(signedInSeam());++ expect+ .soft(server.requests, "the pull's authenticated fetch")+ .toContainEqual(+ authenticatedRequest('fetch', { username: expect.any(String), password: OK_TOKEN }),+ );+ expectCredentialConfinedToCliHelper(OK_TOKEN);+ });+});diff --git a/packages/cli/src/commands/sync.ts b/packages/cli/src/commands/sync.tsindex 2e6249f43..15a1fd73c 100644--- a/packages/cli/src/commands/sync.ts+++ b/packages/cli/src/commands/sync.ts@@ -1,12 +1,31 @@-import { clientVersionHeaders } from '@inkeep/open-knowledge-core';+import {+ type ClassifiedGitAuthError,+ classifyGitAuthError,+ clientVersionHeaders,+ isGitHubHost,+ normalizeGitHostname,+} from '@inkeep/open-knowledge-core';import {type Config,RUNTIME_VERSION,+ readDeclaredGitHubHosts,readServerLock,resolveLockDir,} from '@inkeep/open-knowledge-server';import { Command } from 'commander';-import simpleGit from 'simple-git';+import simpleGit, { type SimpleGit } from 'simple-git';+import type { GhDetectResult } from '../auth/gh-detect.ts';+import { buildHttpsHostCliCredentialConfig, type ResolvedAuth } from '../auth/resolve-auth.ts';+import { makeLazyTokenStore, type TokenStore } from '../auth/token-store.ts';+import { parseGitUrl } from '../github/url.ts';+import {+ buildCloneAuthEnv,+ buildCloneGitOptions,+ type CloneAuthResolution,+ formatDeclaredAccountMiss,+ resolveCloneAuth,+ resolveSelfCliArgs,+} from './clone.ts';function emit(json: boolean, obj: Record<string, unknown>): void {if (json) process.stdout.write(`${JSON.stringify(obj)}\n`);@@ -15,6 +34,168 @@ function emit(json: boolean, obj: Record<string, unknown>): void {interface SyncOptions {json: boolean;op?: 'sync' | 'push' | 'pull';+ tokenStore?: TokenStore;+ _detectGhFn?: (host?: string, options?: { login?: string }) => GhDetectResult;+}++type NetworkCommand = 'pull' | 'push';++interface CredentialSources {+ tokenStore: TokenStore;+ detectGh: SyncOptions['_detectGhFn'];+}++function implicitDefaultRemote(remotes: readonly string[]): string {+ const [only, ...others] = remotes;+ return only !== undefined && others.length === 0 ? only : 'origin';+}++async function contactedRemoteUrl(git: SimpleGit, command: NetworkCommand): Promise<string | null> {+ try {+ const branch = (await git.raw(['symbolic-ref', '--quiet', '--short', 'HEAD'])).trim();+ const explicitName =+ branch === ''+ ? ''+ : (+ await git.raw([+ 'for-each-ref',+ `--format=%(${command === 'push' ? 'push' : 'upstream'}:remotename)`,+ `refs/heads/${branch}`,+ ])+ ).trim();+ const name =+ explicitName !== ''+ ? explicitName+ : implicitDefaultRemote(+ (await git.raw(['remote']))+ .split('\n')+ .map((line) => line.trim())+ .filter((line) => line !== ''),+ );+ const url = (+ await git.raw(['remote', 'get-url', ...(command === 'push' ? ['--push'] : []), name])+ ).trim();+ return url === '' ? null : url;+ } catch {+ return null;+ }+}++interface RemoteGit {+ git: SimpleGit;+ signIn?: { host: string; tier: Exclude<ResolvedAuth['tier'], 'none'> };+ declaredMiss?: CloneAuthResolution['declaredMiss'];+}++function httpsCredentialHost(url: string): string | null {+ const host = /^https:\/\/(?:[^/?#@]*@)?([^/?#@]+)/i.exec(url)?.[1];+ return host !== undefined && host === normalizeGitHostname(host) ? host : null;+}++const FAILED_REQUEST_URL_PATTERNS = [+ /Authentication failed for '([^']+)'/g,+ /unable to access '([^']+)'/g,+ /repository '([^']+)' not found/g,+ /could not read (?:Username|Password) for '([^']+)'/g,+];++function failedRequestServedBy(message: string, host: string): boolean {+ return FAILED_REQUEST_URL_PATTERNS.some((pattern) =>+ Array.from(message.matchAll(pattern)).some((match) => httpsCredentialHost(match[1]) === host),+ );+}++function inheritingAmbientEnv(env: Record<string, string>): NodeJS.ProcessEnv {+ const ambient = { ...process.env };+ const inherited: NodeJS.ProcessEnv = Object.create(ambient);+ for (const key of new Set([...Object.keys(ambient), ...Object.keys(env)])) {+ if (env[key] !== ambient[key]) inherited[key] = env[key];+ }+ return inherited;+}++async function gitForRemoteUrl(+ cwd: string,+ url: string | null,+ sources: CredentialSources,+): Promise<RemoteGit> {+ const ambient: RemoteGit = { git: simpleGit({ baseDir: cwd }) };+ const host = url === null ? null : httpsCredentialHost(url);+ if (+ url === null ||+ host === null ||+ parseGitUrl(url) === null ||+ !isGitHubHost(host, readDeclaredGitHubHosts())+ ) {+ return ambient;+ }+ const { auth, declaredMiss } = await resolveCloneAuth(url, sources.tokenStore, {+ cwd,+ ...(sources.detectGh ? { _detectGhFn: sources.detectGh } : {}),+ });+ if (auth.tier === 'none') return ambient;+ return {+ git: simpleGit(+ buildCloneGitOptions(cwd, buildHttpsHostCliCredentialConfig(resolveSelfCliArgs(), host)),+ ).env(inheritingAmbientEnv(buildCloneAuthEnv(auth))),+ signIn: { host, tier: auth.tier },+ ...(declaredMiss !== undefined ? { declaredMiss } : {}),+ };+}++function describeSignInUsed(+ command: NetworkCommand,+ signIn: NonNullable<RemoteGit['signIn']>,+ failure: ClassifiedGitAuthError,+): string | null {+ if (failure.kind !== 'auth') return null;+ const { host } = signIn;+ const viaGh = signIn.tier === 'A';+ const used = viaGh+ ? `The ${command} offered the GitHub CLI's sign-in for ${host}, which OpenKnowledge relays in place of your git credential helpers for that host; a ~/.netrc entry or a password in the remote URL is used before it.`+ : `The ${command} offered OpenKnowledge's GitHub sign-in for ${host} in place of your git credential helpers for that host; a ~/.netrc entry or a password in the remote URL is used before it.`;+ switch (failure.subclass) {+ case 'no-credential':+ case '401':+ case 'unknown-auth': {+ const renew = viaGh ? `gh auth login --hostname ${host}` : `ok auth login --host ${host}`;+ return `${used} If that sign-in has expired or was revoked, run: ${renew}`;+ }+ case '403':+ return `${used} If ${host} denied that account access, check that it has access to the repository.`;+ case 'not-found-as-identity':+ return `${used} If ${host} reported the repository as not found, it may not exist, or that account may not have access.`;+ case 'scope-mismatch': {+ const rescope = viaGh+ ? `run: gh auth refresh --hostname ${host} --scopes repo`+ : `create a token with that scope at https://${host}/settings/tokens and run: ok auth pat --host ${host}`;+ return `${used} If its token is missing a required OAuth scope, likely repo, ${rescope}`;+ }+ case 'ssh-auth':+ return null;+ default: {+ const exhaustive: never = failure.subclass;+ return exhaustive;+ }+ }+}++async function runOnRemote<T>(+ remote: RemoteGit,+ command: NetworkCommand,+ run: (git: SimpleGit) => Promise<T>,+): Promise<T> {+ try {+ return await run(remote.git);+ } catch (err) {+ const reason = err instanceof Error ? err.message : String(err);+ const explanation =+ remote.signIn === undefined || !failedRequestServedBy(reason, remote.signIn.host)+ ? null+ : describeSignInUsed(command, remote.signIn, classifyGitAuthError(err));+ if (explanation === null) throw err;+ throw new Error(`${reason.trimEnd()}\n${explanation}`, { cause: err });+ }}export async function runSync(@@ -67,11 +248,29 @@ export async function runSync(process.stderr.write(`Running ${op} directly (no live server)\n`);}- const git = simpleGit({ baseDir: cwd });+ const local = simpleGit({ baseDir: cwd });+ const sources: CredentialSources = {+ tokenStore: opts.tokenStore ?? makeLazyTokenStore(),+ detectGh: opts._detectGhFn,+ };+ const remoteFor = async (url: string | null): Promise<RemoteGit> => {+ const remote = await gitForRemoteUrl(cwd, url, sources);+ const warning = formatDeclaredAccountMiss(remote.declaredMiss, {+ url: 'The remote URL',+ operation: `ok ${op}`,+ });+ if (warning === null) return remote;+ if (opts.json) emit(true, { type: 'warning', message: warning.trimEnd() });+ else process.stderr.write(warning);+ return remote;+ };+ let pulled: { url: string | null; remote: RemoteGit } | undefined;if (op === 'sync' || op === 'pull') {emit(opts.json, { type: 'step', step: 'pull' });- const result = await git.pull();+ const url = await contactedRemoteUrl(local, 'pull');+ pulled = { url, remote: await remoteFor(url) };+ const result = await runOnRemote(pulled.remote, 'pull', (git) => git.pull());emit(opts.json, { type: 'pull', summary: result.summary });if (!opts.json) {process.stderr.write(` pull: ${result.summary.changes} changes\n`);@@ -80,7 +279,10 @@ export async function runSync(if (op === 'sync' || op === 'push') {emit(opts.json, { type: 'step', step: 'push' });- await git.push();+ const url = await contactedRemoteUrl(local, 'push');+ const remote =+ pulled !== undefined && pulled.url === url ? pulled.remote : await remoteFor(url);+ await runOnRemote(remote, 'push', (git) => git.push());emit(opts.json, { type: 'push', ok: true });if (!opts.json) {process.stderr.write(' push: ok\n');diff --git a/packages/server/src/content/shadow-log.test.ts b/packages/server/src/content/shadow-log.test.tsindex 6599eb9c5..f2c0f3f8e 100644--- a/packages/server/src/content/shadow-log.test.ts+++ b/packages/server/src/content/shadow-log.test.ts@@ -5,7 +5,7 @@ import { resolve } from 'node:path';import { setTimeout as wait } from 'node:timers/promises';import { formatOkActor, type OkActorEntry } from '@inkeep/open-knowledge-core/shadow-repo-layout';import simpleGit from 'simple-git';-import { afterEach, beforeEach, describe, expect, test } from 'vitest';+import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest';import {commitUpstreamImport,commitWip,@@ -349,3 +349,33 @@ describe('readShadowLog — checkpoint-ancestry fallback (PRD-6972 FR7 / D15)',expect(result.commits).toEqual([]);});});++describe('readShadowLog under a process environment carrying GIT_CONFIG_COUNT', () => {+ afterEach(() => {+ vi.unstubAllEnvs();+ });++ test('returns the WIP commit instead of degrading to an empty history', async () => {+ const isolatedHome = resolve(tmpDir, 'home');+ mkdirSync(isolatedHome, { recursive: true });+ vi.stubEnv('HOME', isolatedHome);+ vi.stubEnv('USERPROFILE', isolatedHome);+ vi.stubEnv('XDG_CONFIG_HOME', resolve(isolatedHome, '.config'));+ vi.stubEnv('GIT_CONFIG_COUNT', '1');+ vi.stubEnv('GIT_CONFIG_KEY_0', 'credential.interactive');+ vi.stubEnv('GIT_CONFIG_VALUE_0', 'false');+ const project = await bootstrapProject();+ const shadow = await initShadowRepo(project);+ const contentDir = resolve(project, 'content');+ mkdirSync(contentDir, { recursive: true });+ writeFileSync(resolve(contentDir, 'auth.md'), '# auth v1\n');+ const writer: WriterIdentity = { id: 'agent-x', name: 'Agent X', email: '[email protected]' };+ const branch = (await simpleGit(project).revparse(['--abbrev-ref', 'HEAD'])).trim();+ await commitWip(shadow, writer, contentDir, 'add auth doc', branch);++ const { commits, source } = await readShadowLog(project, 'content/auth.md', 5);++ expect(source).toBe('shadow-repo');+ expect(commits.map((commit) => commit.message)).toEqual(['add auth doc']);+ });+});diff --git a/packages/server/src/git-handle.test.ts b/packages/server/src/git-handle.test.tsindex 1f94c7638..e7c3d0fc4 100644--- a/packages/server/src/git-handle.test.ts+++ b/packages/server/src/git-handle.test.ts@@ -6,7 +6,7 @@ import { tmpdir } from 'node:os';import { join } from 'node:path';import { setTimeout as wait } from 'node:timers/promises';import shellQuote from 'shell-quote';-import { afterEach, beforeEach, describe, expect, test } from 'vitest';+import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest';import {applyGitEnv,buildGitEnv,@@ -188,6 +188,37 @@ describe('createGitInstance (credential.helper config)', () => {});});+describe('createGitInstance under a process environment carrying GIT_CONFIG_COUNT', () => {+ let repoDir: string;+ let isolatedHome: string;++ beforeEach(() => {+ repoDir = mkdtempSync(join(tmpdir(), 'ok-git-handle-envcount-'));+ isolatedHome = mkdtempSync(join(tmpdir(), 'ok-git-handle-envcount-home-'));+ vi.stubEnv('HOME', isolatedHome);+ vi.stubEnv('USERPROFILE', isolatedHome);+ vi.stubEnv('XDG_CONFIG_HOME', join(isolatedHome, '.config'));+ execSync('git init -q', { cwd: repoDir });+ vi.stubEnv('GIT_CONFIG_COUNT', '1');+ vi.stubEnv('GIT_CONFIG_KEY_0', 'credential.interactive');+ vi.stubEnv('GIT_CONFIG_VALUE_0', 'false');+ });++ afterEach(() => {+ vi.unstubAllEnvs();+ rmSync(repoDir, { recursive: true, force: true });+ rmSync(isolatedHome, { recursive: true, force: true });+ });++ test('server git runs instead of meeting simple-git refusing GIT_CONFIG_COUNT', async () => {+ const handle = createGitInstance(repoDir, {+ credentialConfig: buildSyncCredentialConfig(['open-knowledge'], { resetAmbient: true }),+ });++ await expect(handle.git.raw(['rev-parse', '--is-inside-work-tree'])).resolves.toBe('true\n');+ });+});+describe.skipIf(process.platform === 'win32')('createGitInstance × buildSyncCredentialConfig — ambient helper neutralization',() => {