dsh-git-branch-manage 1.0.9 → 1.0.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/credentials.d.ts +7 -3
- package/lib/credentials.js +7 -3
- package/lib/helpers/git-credential-bridge.d.ts +32 -0
- package/lib/helpers/git-credential-bridge.js +114 -0
- package/lib/index.js +17 -6
- package/lib/network-tool.d.ts +8 -8
- package/lib/network-tool.js +14 -10
- package/lib/routes.d.ts +3 -0
- package/lib/routes.js +20 -0
- package/lib/shell-bridge.d.ts +93 -0
- package/lib/shell-bridge.js +121 -0
- package/package.json +1 -1
package/lib/credentials.d.ts
CHANGED
|
@@ -7,9 +7,13 @@
|
|
|
7
7
|
* and the store is read/written by the host alone.
|
|
8
8
|
*
|
|
9
9
|
* Authentication rides `git -c http.https://<host>/.extraHeader=…` on the
|
|
10
|
-
* panel's own network commands: the user's
|
|
11
|
-
*
|
|
12
|
-
*
|
|
10
|
+
* panel's own network commands and on the `git_network` tool: the user's git
|
|
11
|
+
* configuration FILES are never written. Plain `git` commands in a spawned
|
|
12
|
+
* shell are covered separately by the environment bridge in `shell-bridge.ts`,
|
|
13
|
+
* which installs this store's per-host tokens as URL-scoped credential helpers
|
|
14
|
+
* through `GIT_CONFIG_*` (process environment only, never a config file).
|
|
15
|
+
* Only `https://` remotes receive the header; ssh and plain-http remotes are
|
|
16
|
+
* never sent a secret.
|
|
13
17
|
*/
|
|
14
18
|
import type { CredentialMethod } from './wire.js';
|
|
15
19
|
/**
|
package/lib/credentials.js
CHANGED
|
@@ -7,9 +7,13 @@
|
|
|
7
7
|
* and the store is read/written by the host alone.
|
|
8
8
|
*
|
|
9
9
|
* Authentication rides `git -c http.https://<host>/.extraHeader=…` on the
|
|
10
|
-
* panel's own network commands: the user's
|
|
11
|
-
*
|
|
12
|
-
*
|
|
10
|
+
* panel's own network commands and on the `git_network` tool: the user's git
|
|
11
|
+
* configuration FILES are never written. Plain `git` commands in a spawned
|
|
12
|
+
* shell are covered separately by the environment bridge in `shell-bridge.ts`,
|
|
13
|
+
* which installs this store's per-host tokens as URL-scoped credential helpers
|
|
14
|
+
* through `GIT_CONFIG_*` (process environment only, never a config file).
|
|
15
|
+
* Only `https://` remotes receive the header; ssh and plain-http remotes are
|
|
16
|
+
* never sent a secret.
|
|
13
17
|
*/
|
|
14
18
|
import { mkdir, readFile, rename, rm, writeFile } from 'node:fs/promises';
|
|
15
19
|
import { dirname, join } from 'node:path';
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Standalone git credential helper shipped with the dsh-git-branch plugin.
|
|
3
|
+
*
|
|
4
|
+
* The panel's credential file (`~/.dsh/git-credentials.json`) normally rides
|
|
5
|
+
* `-c http.<url>.extraHeader` config on the routes and the `git_network` tool.
|
|
6
|
+
* This helper extends that coverage to a plain shell `git push`: the plugin's
|
|
7
|
+
* host process exports `GIT_CONFIG_COUNT/KEY/VALUE` entries that install this
|
|
8
|
+
* script as a URL-scoped credential helper for every stored host, so commands
|
|
9
|
+
* the model runs in the shell authenticate as the configured account too.
|
|
10
|
+
*
|
|
11
|
+
* Design constraints (see docs/decisions/0029 and 0032):
|
|
12
|
+
* - The user's global git config and credential helpers are never written;
|
|
13
|
+
* the injection lives in the process environment only.
|
|
14
|
+
* - The token never enters the conversation: this script reads the
|
|
15
|
+
* credential file directly on the host and answers git on stdout.
|
|
16
|
+
* - URL scope (`credential.https://<host>/.helper`) confines the helper to
|
|
17
|
+
* hosts with a stored credential. The host half also clears the machine
|
|
18
|
+
* helper list, so a host this script cannot answer fails fast instead of
|
|
19
|
+
* opening an interactive credential dialog.
|
|
20
|
+
* - `get` is silent on a miss (git falls through to the next helper);
|
|
21
|
+
* `store`/`erase` are pass-through by decision — writing back would race
|
|
22
|
+
* the panel's atomic save and leak the file shape.
|
|
23
|
+
*
|
|
24
|
+
* Environment contract (set by src/shell-bridge.ts):
|
|
25
|
+
* - `DSH_GIT_BRIDGE_HELPER` — credential file path (required).
|
|
26
|
+
* - `DSH_GIT_BRIDGE_HOME` — user home directory (fallback resolution).
|
|
27
|
+
* - `DSH_GIT_BRIDGE_DSH_HOME` — `$DSH_HOME` override (optional).
|
|
28
|
+
* - `DSH_GIT_BRIDGE_DEBUG` — when non-blank, a debug log path (optional).
|
|
29
|
+
*
|
|
30
|
+
* @module dsh-git-branch/git-credential-bridge
|
|
31
|
+
*/
|
|
32
|
+
export {};
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Standalone git credential helper shipped with the dsh-git-branch plugin.
|
|
3
|
+
*
|
|
4
|
+
* The panel's credential file (`~/.dsh/git-credentials.json`) normally rides
|
|
5
|
+
* `-c http.<url>.extraHeader` config on the routes and the `git_network` tool.
|
|
6
|
+
* This helper extends that coverage to a plain shell `git push`: the plugin's
|
|
7
|
+
* host process exports `GIT_CONFIG_COUNT/KEY/VALUE` entries that install this
|
|
8
|
+
* script as a URL-scoped credential helper for every stored host, so commands
|
|
9
|
+
* the model runs in the shell authenticate as the configured account too.
|
|
10
|
+
*
|
|
11
|
+
* Design constraints (see docs/decisions/0029 and 0032):
|
|
12
|
+
* - The user's global git config and credential helpers are never written;
|
|
13
|
+
* the injection lives in the process environment only.
|
|
14
|
+
* - The token never enters the conversation: this script reads the
|
|
15
|
+
* credential file directly on the host and answers git on stdout.
|
|
16
|
+
* - URL scope (`credential.https://<host>/.helper`) confines the helper to
|
|
17
|
+
* hosts with a stored credential. The host half also clears the machine
|
|
18
|
+
* helper list, so a host this script cannot answer fails fast instead of
|
|
19
|
+
* opening an interactive credential dialog.
|
|
20
|
+
* - `get` is silent on a miss (git falls through to the next helper);
|
|
21
|
+
* `store`/`erase` are pass-through by decision — writing back would race
|
|
22
|
+
* the panel's atomic save and leak the file shape.
|
|
23
|
+
*
|
|
24
|
+
* Environment contract (set by src/shell-bridge.ts):
|
|
25
|
+
* - `DSH_GIT_BRIDGE_HELPER` — credential file path (required).
|
|
26
|
+
* - `DSH_GIT_BRIDGE_HOME` — user home directory (fallback resolution).
|
|
27
|
+
* - `DSH_GIT_BRIDGE_DSH_HOME` — `$DSH_HOME` override (optional).
|
|
28
|
+
* - `DSH_GIT_BRIDGE_DEBUG` — when non-blank, a debug log path (optional).
|
|
29
|
+
*
|
|
30
|
+
* @module dsh-git-branch/git-credential-bridge
|
|
31
|
+
*/
|
|
32
|
+
import { appendFileSync, readFileSync } from 'node:fs';
|
|
33
|
+
import { homedir } from 'node:os';
|
|
34
|
+
import { join } from 'node:path';
|
|
35
|
+
/** git helpers receive exactly one argument: the operation. */
|
|
36
|
+
const OPERATION = process.argv[2] ?? 'get';
|
|
37
|
+
/** The credential file this helper reads (`DSH_GIT_BRIDGE_HELPER` wins). */
|
|
38
|
+
const DEFAULT_CREDENTIAL_FILE = 'git-credentials.json';
|
|
39
|
+
/** The dsh home directory below the user profile when `$DSH_HOME` is unset. */
|
|
40
|
+
const PACKAGE_HOME = '.dsh';
|
|
41
|
+
/** The Basic-auth name a token without a username is sent under. */
|
|
42
|
+
const TOKEN_USERNAME = 'oauth2';
|
|
43
|
+
/**
|
|
44
|
+
* Append one debug line behind the user's back; a broken log path must never
|
|
45
|
+
* break the credential lookup.
|
|
46
|
+
*/
|
|
47
|
+
function debug(line) {
|
|
48
|
+
const path = process.env.DSH_GIT_BRIDGE_DEBUG;
|
|
49
|
+
if (path === undefined || path.trim() === '')
|
|
50
|
+
return;
|
|
51
|
+
try {
|
|
52
|
+
appendFileSync(path, `${line}\n`, 'utf8');
|
|
53
|
+
}
|
|
54
|
+
catch {
|
|
55
|
+
// Deliberately swallowed: debugging is best-effort.
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
/** The credential file this helper reads (`DSH_GIT_BRIDGE_HELPER` wins). */
|
|
59
|
+
function credentialFile() {
|
|
60
|
+
const helper = process.env.DSH_GIT_BRIDGE_HELPER;
|
|
61
|
+
if (helper !== undefined && helper.trim() !== '')
|
|
62
|
+
return helper;
|
|
63
|
+
const dshHome = process.env.DSH_GIT_BRIDGE_DSH_HOME;
|
|
64
|
+
const home = dshHome !== undefined && dshHome.trim() !== ''
|
|
65
|
+
? dshHome
|
|
66
|
+
: process.env.DSH_GIT_BRIDGE_HOME ?? homedir();
|
|
67
|
+
return join(home, DEFAULT_CREDENTIAL_FILE);
|
|
68
|
+
}
|
|
69
|
+
/** One `key=value` request from git on stdin. */
|
|
70
|
+
function readRequest() {
|
|
71
|
+
const text = readFileSync(0, 'utf8');
|
|
72
|
+
const request = new Map();
|
|
73
|
+
for (const line of text.split('\n')) {
|
|
74
|
+
const at = line.indexOf('=');
|
|
75
|
+
if (at > 0)
|
|
76
|
+
request.set(line.slice(0, at), line.slice(at + 1));
|
|
77
|
+
}
|
|
78
|
+
return request;
|
|
79
|
+
}
|
|
80
|
+
/** The stored credential for one host, or undefined when there is none. */
|
|
81
|
+
function readCredential(host) {
|
|
82
|
+
let parsed;
|
|
83
|
+
try {
|
|
84
|
+
parsed = JSON.parse(readFileSync(credentialFile(), 'utf8'));
|
|
85
|
+
}
|
|
86
|
+
catch (error) {
|
|
87
|
+
debug(`read failed: ${String(error)}`);
|
|
88
|
+
return undefined;
|
|
89
|
+
}
|
|
90
|
+
const credentials = parsed.credentials;
|
|
91
|
+
if (!Array.isArray(credentials))
|
|
92
|
+
return undefined;
|
|
93
|
+
const entry = credentials.find((item) => typeof item === 'object' && item !== null && item.host === host);
|
|
94
|
+
if (entry === undefined)
|
|
95
|
+
return undefined;
|
|
96
|
+
const secret = typeof entry.secret === 'string' ? entry.secret : '';
|
|
97
|
+
if (secret === '')
|
|
98
|
+
return undefined;
|
|
99
|
+
const username = typeof entry.username === 'string' && entry.username.trim() !== ''
|
|
100
|
+
? entry.username
|
|
101
|
+
: TOKEN_USERNAME;
|
|
102
|
+
return { username, secret };
|
|
103
|
+
}
|
|
104
|
+
if (OPERATION === 'get') {
|
|
105
|
+
const request = readRequest();
|
|
106
|
+
const host = request.get('host') ?? '';
|
|
107
|
+
debug(`get host=${host} protocol=${request.get('protocol') ?? ''}`);
|
|
108
|
+
const credential = readCredential(host);
|
|
109
|
+
if (credential !== undefined) {
|
|
110
|
+
process.stdout.write(`username=${credential.username}\npassword=${credential.secret}\n`);
|
|
111
|
+
}
|
|
112
|
+
// A miss stays silent: git tries the next helper in the chain.
|
|
113
|
+
}
|
|
114
|
+
// `store` / `erase` are deliberate no-ops (see the header comment).
|
package/lib/index.js
CHANGED
|
@@ -13,6 +13,7 @@ import { handleCredentials, handleCredentialsDelete, handleCredentialsOAuthPoll,
|
|
|
13
13
|
import { validateRootDir } from './settings.js';
|
|
14
14
|
import { gitNetworkTool } from './network-tool.js';
|
|
15
15
|
import { gitCredentialsTool } from './tool.js';
|
|
16
|
+
import { createShellBridge } from './shell-bridge.js';
|
|
16
17
|
import { ROUTE_AI_COMMIT_MESSAGE, ROUTE_BRANCH, ROUTE_BRANCH_DELETE, ROUTE_BRANCH_RENAME, ROUTE_CHANGES, ROUTE_COMMIT, ROUTE_COMMIT_FILES, ROUTE_CREDENTIALS, ROUTE_CREDENTIALS_DELETE, ROUTE_CREDENTIALS_OAUTH_POLL, ROUTE_CREDENTIALS_OAUTH_START, ROUTE_CREDENTIALS_SAVE, ROUTE_ENSURE_DIRECTORY, ROUTE_EXISTS, ROUTE_FETCH, ROUTE_GROUP, ROUTE_INSPECT, ROUTE_LOG, ROUTE_PUSH, ROUTE_PURGE, ROUTE_REMOTE, ROUTE_REMOTES, ROUTE_REMOVE, ROUTE_STAGE, ROUTE_STATUS, ROUTE_SWITCH, ROUTE_TAG, ROUTE_TAG_PUSH, ROUTE_UPDATE, ROUTE_WORKTREE, ROUTE_WORKTREES_ALL } from './wire.js';
|
|
17
18
|
export const name = 'dsh-git-branch';
|
|
18
19
|
export const inject = [];
|
|
@@ -141,6 +142,14 @@ export function sectionOf(config) {
|
|
|
141
142
|
export function apply(ctx, config = {}) {
|
|
142
143
|
validateConfig(config);
|
|
143
144
|
const currentConfig = () => ctx.fiber?.config ?? config;
|
|
145
|
+
// Host half of the shell credential bridge: derive the environment that
|
|
146
|
+
// lets a plain `git push` in a spawned shell authenticate with the panel's
|
|
147
|
+
// stored token instead of the machine helper's account. It also clears the
|
|
148
|
+
// machine helper chain, so a host without a stored token fails fast rather
|
|
149
|
+
// than opening a credential dialog no agent shell can answer. Applied at
|
|
150
|
+
// activate and re-derived after every credential save / delete / sign-in.
|
|
151
|
+
const shellBridge = createShellBridge({ home: () => homedir(), envHome: () => process.env.DSH_HOME });
|
|
152
|
+
void shellBridge.apply();
|
|
144
153
|
const deps = () => ({
|
|
145
154
|
exec: childProcessExec,
|
|
146
155
|
sectionRootDir: () => readVolatile(currentConfig().rootDir),
|
|
@@ -150,6 +159,7 @@ export function apply(ctx, config = {}) {
|
|
|
150
159
|
sectionCnAccelerateUrl: () => readVolatile(currentConfig().cnAccelerateUrl),
|
|
151
160
|
home: () => homedir(),
|
|
152
161
|
envHome: () => process.env.DSH_HOME,
|
|
162
|
+
refreshShellBridge: () => shellBridge.apply(),
|
|
153
163
|
});
|
|
154
164
|
// In dsh 0.1.7+, the settings service projects volatile Config fields into forms.
|
|
155
165
|
// We configure auto: false to inform the host that this plugin provides its own
|
|
@@ -164,12 +174,13 @@ export function apply(ctx, config = {}) {
|
|
|
164
174
|
ctx.inject(['tools'], (toolsCtx) => {
|
|
165
175
|
const tools = toolsCtx.tools;
|
|
166
176
|
toolsCtx.effect(() => tools.register(gitCredentialsTool({ home: () => homedir(), envHome: () => process.env.DSH_HOME })), 'git-worktree: git_credentials tool');
|
|
167
|
-
// The panel routes
|
|
168
|
-
//
|
|
169
|
-
//
|
|
170
|
-
//
|
|
171
|
-
//
|
|
172
|
-
//
|
|
177
|
+
// The panel routes inject the stored credentials into the requests the
|
|
178
|
+
// browser half makes; a shell command is served by the environment bridge
|
|
179
|
+
// (see `shell-bridge.ts`) instead. This tool is the deterministic path for
|
|
180
|
+
// a session: it runs one network subcommand with the `-c
|
|
181
|
+
// http.<url>.extraHeader` prefix, so the command authenticates as the
|
|
182
|
+
// configured account and the secret never enters the conversation — even
|
|
183
|
+
// when the bridge is missing (an older host, a shell outside the plugin).
|
|
173
184
|
toolsCtx.effect(() => tools.register(gitNetworkTool({ home: () => homedir(), envHome: () => process.env.DSH_HOME })), 'git-worktree: git_network tool');
|
|
174
185
|
});
|
|
175
186
|
ctx.inject(['webServer'], (webCtx) => {
|
package/lib/network-tool.d.ts
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* The model-facing `git_network` tool (host half).
|
|
3
3
|
*
|
|
4
|
-
* The credentials panel stores one token per remote host
|
|
5
|
-
* fetch / pull / push routes
|
|
6
|
-
* http.<url>.extraHeader
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
4
|
+
* The credentials panel stores one token per remote host and makes it work on
|
|
5
|
+
* two channels: the panel's own fetch / pull / push routes (`-c
|
|
6
|
+
* http.<url>.extraHeader=…`, see `authConfigArgs`) and the environment bridge
|
|
7
|
+
* the host installs for the shells it spawns (`shell-bridge.ts`). This tool is
|
|
8
|
+
* the third, explicit one: it runs ONE git network command with the same
|
|
9
|
+
* `authConfigArgs` the routes use, so a session reliably pushes as the account
|
|
10
|
+
* the user configured and the secret never enters the conversation — including
|
|
11
|
+
* on a host that predates the bridge.
|
|
12
12
|
*
|
|
13
13
|
* It is deliberately narrower than a shell wrapper: a fixed subcommand
|
|
14
14
|
* allow-list (fetch / pull / push / ls-remote), no force/delete refspecs, no
|
package/lib/network-tool.js
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* The model-facing `git_network` tool (host half).
|
|
3
3
|
*
|
|
4
|
-
* The credentials panel stores one token per remote host
|
|
5
|
-
* fetch / pull / push routes
|
|
6
|
-
* http.<url>.extraHeader
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
4
|
+
* The credentials panel stores one token per remote host and makes it work on
|
|
5
|
+
* two channels: the panel's own fetch / pull / push routes (`-c
|
|
6
|
+
* http.<url>.extraHeader=…`, see `authConfigArgs`) and the environment bridge
|
|
7
|
+
* the host installs for the shells it spawns (`shell-bridge.ts`). This tool is
|
|
8
|
+
* the third, explicit one: it runs ONE git network command with the same
|
|
9
|
+
* `authConfigArgs` the routes use, so a session reliably pushes as the account
|
|
10
|
+
* the user configured and the secret never enters the conversation — including
|
|
11
|
+
* on a host that predates the bridge.
|
|
12
12
|
*
|
|
13
13
|
* It is deliberately narrower than a shell wrapper: a fixed subcommand
|
|
14
14
|
* allow-list (fetch / pull / push / ls-remote), no force/delete refspecs, no
|
|
@@ -168,8 +168,12 @@ export function gitNetworkTool(deps) {
|
|
|
168
168
|
name: GIT_NETWORK_TOOL_NAME,
|
|
169
169
|
description: 'Run ONE git network command (fetch, pull, push or ls-remote) inside a repository with the '
|
|
170
170
|
+ 'plugin\'s stored Git credentials injected, so the command authenticates as the account configured in the '
|
|
171
|
-
+ 'Git credential panel instead of whatever the machine\'s git credential helper happens to hold.
|
|
172
|
-
+ '
|
|
171
|
+
+ 'Git credential panel instead of whatever the machine\'s git credential helper happens to hold. Prefer it for '
|
|
172
|
+
+ 'every push/fetch/pull: the credential is injected explicitly and redacted from the output, while a plain '
|
|
173
|
+
+ 'shell `git push` only reaches these credentials through the environment bridge the plugin host installs '
|
|
174
|
+
+ 'for the shells it spawns. That bridge is absent in an older host or in a terminal started outside it; there '
|
|
175
|
+
+ 'git clears the machine helper chain too, so an unconfigured host fails fast with a "terminal prompts '
|
|
176
|
+
+ 'disabled" error instead of opening a credential dialog nobody can answer. Pass the git '
|
|
173
177
|
+ 'arguments subcommand-first, for example ["push"], ["fetch", "--all", "--prune"] or '
|
|
174
178
|
+ '["push", "origin", "main"]. Force/delete refspecs, --force/--delete/--mirror/--prune and '
|
|
175
179
|
+ '--upload-pack/--receive-pack style options are rejected by design. The secret itself never appears in the '
|
package/lib/routes.d.ts
CHANGED
|
@@ -44,6 +44,9 @@ export interface RouteDeps {
|
|
|
44
44
|
/** Global per-host credential store. Absent = the fs-backed store at the
|
|
45
45
|
* resolved dsh home; tests substitute an in-memory one. */
|
|
46
46
|
credentialsStore?: CredentialStore;
|
|
47
|
+
/** Re-derive the shell credential bridge after the store changed (absent =
|
|
48
|
+
* a host that does not provide the bridge; tests omit it). */
|
|
49
|
+
refreshShellBridge?: () => Promise<void>;
|
|
47
50
|
}
|
|
48
51
|
/** Real fs-backed one-level directory listing (names only). Plain FILES are
|
|
49
52
|
* filtered out — the storage root can carry strays (a legacy settings.json
|
package/lib/routes.js
CHANGED
|
@@ -73,6 +73,23 @@ async function authArgsFor(deps) {
|
|
|
73
73
|
return [];
|
|
74
74
|
}
|
|
75
75
|
}
|
|
76
|
+
/**
|
|
77
|
+
* Re-derive the shell credential bridge after a credential changed, so a
|
|
78
|
+
* message sent in the same session can `git push` with the token the user just
|
|
79
|
+
* saved. A bridge failure must never fail the save itself: the store write has
|
|
80
|
+
* already landed, and the worst case is a stale environment until the next
|
|
81
|
+
* save (an added host is not reachable, a deleted one may still be).
|
|
82
|
+
*/
|
|
83
|
+
async function refreshBridge(deps) {
|
|
84
|
+
if (deps.refreshShellBridge === undefined)
|
|
85
|
+
return;
|
|
86
|
+
try {
|
|
87
|
+
await deps.refreshShellBridge();
|
|
88
|
+
}
|
|
89
|
+
catch {
|
|
90
|
+
// Deliberately swallowed; see the contract above.
|
|
91
|
+
}
|
|
92
|
+
}
|
|
76
93
|
/** Upper bound on distinct paths one /group request may probe. */
|
|
77
94
|
const GROUP_PATHS_LIMIT = 256;
|
|
78
95
|
/** Probes per in-flight batch — polite on Windows, where each git call is a
|
|
@@ -1362,6 +1379,7 @@ export async function handleCredentialsSave(deps, body) {
|
|
|
1362
1379
|
updatedAt: Date.now(),
|
|
1363
1380
|
});
|
|
1364
1381
|
await store.save(next);
|
|
1382
|
+
await refreshBridge(deps);
|
|
1365
1383
|
const result = { credentials: sortCredentials(next).map(credentialView) };
|
|
1366
1384
|
return { status: 200, body: result };
|
|
1367
1385
|
}
|
|
@@ -1389,6 +1407,7 @@ export async function handleCredentialsDelete(deps, body) {
|
|
|
1389
1407
|
if (next.length === entries.length)
|
|
1390
1408
|
return fail(400, `no credential is stored for "${host}"`);
|
|
1391
1409
|
await store.save(next);
|
|
1410
|
+
await refreshBridge(deps);
|
|
1392
1411
|
const result = { credentials: sortCredentials(next).map(credentialView) };
|
|
1393
1412
|
return { status: 200, body: result };
|
|
1394
1413
|
}
|
|
@@ -1495,6 +1514,7 @@ export async function handleCredentialsOAuthPoll(deps, body) {
|
|
|
1495
1514
|
updatedAt: Date.now(),
|
|
1496
1515
|
});
|
|
1497
1516
|
await store.save(next);
|
|
1517
|
+
await refreshBridge(deps);
|
|
1498
1518
|
const done = { status: 'done', credentials: sortCredentials(next).map(credentialView) };
|
|
1499
1519
|
return { status: 200, body: done };
|
|
1500
1520
|
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shell credential bridge (host half). The credential panel stores one token
|
|
3
|
+
* per remote host, but 0029 could only inject it into the panel's own routes
|
|
4
|
+
* and the `git_network` tool; a plain `git push` run in a shell authenticated
|
|
5
|
+
* as whatever the machine's credential helper held — often a different
|
|
6
|
+
* account — or hung on an interactive prompt nobody could answer.
|
|
7
|
+
*
|
|
8
|
+
* This module closes that gap without touching the user's git configuration
|
|
9
|
+
* FILES (0021's constraint): it exports `GIT_CONFIG_COUNT`/`GIT_CONFIG_KEY_n`/
|
|
10
|
+
* `GIT_CONFIG_VALUE_n` entries that install the bundled credential helper
|
|
11
|
+
* (`helpers/git-credential-bridge.js`) as a URL-scoped helper for every stored
|
|
12
|
+
* host. git reads that environment as configuration, every child process the
|
|
13
|
+
* host spawns inherits it, and a non-empty value list is always preceded by the
|
|
14
|
+
* same key with an empty value — git resets the accumulated helper list
|
|
15
|
+
* there, so the machine chain (`helper-selector` → GCM) can neither answer
|
|
16
|
+
* with a different account nor open a blocking dialog for those hosts.
|
|
17
|
+
* The list is reset globally as well: a host with no stored credential then
|
|
18
|
+
* fails at once ("terminal prompts disabled") instead of reaching the machine
|
|
19
|
+
* chain, whose interactive GUI dialog nobody in an agent shell can answer.
|
|
20
|
+
*
|
|
21
|
+
* `GIT_TERMINAL_PROMPT=0` and `GCM_INTERACTIVE=never` ride along so a host
|
|
22
|
+
* with no usable credential fails fast instead of parking a shell on a prompt
|
|
23
|
+
* (the failure mode that started this: a 3m38s silent hang).
|
|
24
|
+
*
|
|
25
|
+
* The assignments are re-derived whenever the credential store changes
|
|
26
|
+
* (`src/routes.ts` calls the bridge after every save / delete / sign-in).
|
|
27
|
+
*/
|
|
28
|
+
import { type CredentialStore } from './credentials.js';
|
|
29
|
+
/** Env var carrying the credential file path to the helper script. */
|
|
30
|
+
export declare const BRIDGE_HELPER_ENV = "DSH_GIT_BRIDGE_HELPER";
|
|
31
|
+
/** Env var carrying a debug log path to the helper script (unset by default). */
|
|
32
|
+
export declare const BRIDGE_DEBUG_ENV = "DSH_GIT_BRIDGE_DEBUG";
|
|
33
|
+
/** Host-side seams the bridge reads through (tests substitute all of them). */
|
|
34
|
+
export interface ShellBridgeDeps {
|
|
35
|
+
/** User home directory seam. */
|
|
36
|
+
readonly home: () => string;
|
|
37
|
+
/** `$DSH_HOME` seam (blank/undefined = unset). */
|
|
38
|
+
readonly envHome: () => string | undefined;
|
|
39
|
+
/** Store seam; the fs-backed store at the resolved path when absent. */
|
|
40
|
+
readonly store?: CredentialStore;
|
|
41
|
+
/** The environment object to mutate; `process.env` when absent. */
|
|
42
|
+
readonly env?: Record<string, string | undefined>;
|
|
43
|
+
/** Absolute helper script path; the compiled sibling under `helpers/` when
|
|
44
|
+
* absent. */
|
|
45
|
+
readonly helperPath?: string;
|
|
46
|
+
/** Node executable that runs the helper; `process.execPath` when absent. */
|
|
47
|
+
readonly nodePath?: string;
|
|
48
|
+
}
|
|
49
|
+
/** The bridge's one operation: re-derive the environment from the store. */
|
|
50
|
+
export interface ShellBridge {
|
|
51
|
+
/** Read the credential store and rewrite the injected environment.
|
|
52
|
+
* A store that cannot be read drops every URL-scoped helper (no host can be
|
|
53
|
+
* bridged), but keeps the global reset, so shell git still fails fast
|
|
54
|
+
* instead of opening an interactive credential dialog. */
|
|
55
|
+
readonly apply: () => Promise<void>;
|
|
56
|
+
}
|
|
57
|
+
/** One ordered environment assignment. */
|
|
58
|
+
export type BridgeAssignment = readonly [key: string, value: string];
|
|
59
|
+
/**
|
|
60
|
+
* Quote one executable or path for git's `!` shell command. Backslashes are
|
|
61
|
+
* normalized to forward slashes first: git runs `!` helpers through `sh`,
|
|
62
|
+
* where `C:\…` would be read as escape sequences.
|
|
63
|
+
* @param value - an absolute path.
|
|
64
|
+
*/
|
|
65
|
+
export declare function shellQuote(value: string): string;
|
|
66
|
+
/**
|
|
67
|
+
* The helper command git runs for one configured host: a `!`-prefixed shell
|
|
68
|
+
* command, because git does not word-split a helper value by itself (a bare
|
|
69
|
+
* `node "C:/…/bridge.js"` is looked up as the program `git-credential-node`).
|
|
70
|
+
* @param nodePath - the node executable.
|
|
71
|
+
* @param helperPath - the compiled helper script.
|
|
72
|
+
*/
|
|
73
|
+
export declare function bridgeCommand(nodePath: string, helperPath: string): string;
|
|
74
|
+
/**
|
|
75
|
+
* The ordered environment assignments for the given hosts: per host an empty
|
|
76
|
+
* helper value (git resets the accumulated list for that URL scope) and then
|
|
77
|
+
* the bridge command, plus the credential-file variable the helper reads and
|
|
78
|
+
* the two fail-fast switches.
|
|
79
|
+
* @param hosts - normalized hosts with a stored credential.
|
|
80
|
+
* @param options - process paths and the credential file.
|
|
81
|
+
*/
|
|
82
|
+
export declare function shellBridgeEnv(hosts: readonly string[], options: {
|
|
83
|
+
readonly nodePath: string;
|
|
84
|
+
readonly helperPath: string;
|
|
85
|
+
readonly credentialFile: string;
|
|
86
|
+
}): readonly BridgeAssignment[];
|
|
87
|
+
/**
|
|
88
|
+
* Build the bridge over one host process. Every {@link ShellBridge.apply}
|
|
89
|
+
* first removes the assignments it made last time, so a deleted host loses
|
|
90
|
+
* its helper scope just like a freshly added one gains it.
|
|
91
|
+
* @param deps - host seams.
|
|
92
|
+
*/
|
|
93
|
+
export declare function createShellBridge(deps: ShellBridgeDeps): ShellBridge;
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shell credential bridge (host half). The credential panel stores one token
|
|
3
|
+
* per remote host, but 0029 could only inject it into the panel's own routes
|
|
4
|
+
* and the `git_network` tool; a plain `git push` run in a shell authenticated
|
|
5
|
+
* as whatever the machine's credential helper held — often a different
|
|
6
|
+
* account — or hung on an interactive prompt nobody could answer.
|
|
7
|
+
*
|
|
8
|
+
* This module closes that gap without touching the user's git configuration
|
|
9
|
+
* FILES (0021's constraint): it exports `GIT_CONFIG_COUNT`/`GIT_CONFIG_KEY_n`/
|
|
10
|
+
* `GIT_CONFIG_VALUE_n` entries that install the bundled credential helper
|
|
11
|
+
* (`helpers/git-credential-bridge.js`) as a URL-scoped helper for every stored
|
|
12
|
+
* host. git reads that environment as configuration, every child process the
|
|
13
|
+
* host spawns inherits it, and a non-empty value list is always preceded by the
|
|
14
|
+
* same key with an empty value — git resets the accumulated helper list
|
|
15
|
+
* there, so the machine chain (`helper-selector` → GCM) can neither answer
|
|
16
|
+
* with a different account nor open a blocking dialog for those hosts.
|
|
17
|
+
* The list is reset globally as well: a host with no stored credential then
|
|
18
|
+
* fails at once ("terminal prompts disabled") instead of reaching the machine
|
|
19
|
+
* chain, whose interactive GUI dialog nobody in an agent shell can answer.
|
|
20
|
+
*
|
|
21
|
+
* `GIT_TERMINAL_PROMPT=0` and `GCM_INTERACTIVE=never` ride along so a host
|
|
22
|
+
* with no usable credential fails fast instead of parking a shell on a prompt
|
|
23
|
+
* (the failure mode that started this: a 3m38s silent hang).
|
|
24
|
+
*
|
|
25
|
+
* The assignments are re-derived whenever the credential store changes
|
|
26
|
+
* (`src/routes.ts` calls the bridge after every save / delete / sign-in).
|
|
27
|
+
*/
|
|
28
|
+
import { fileURLToPath } from 'node:url';
|
|
29
|
+
import { credentialFilePath, createCredentialStore, } from './credentials.js';
|
|
30
|
+
/** Env var carrying the credential file path to the helper script. */
|
|
31
|
+
export const BRIDGE_HELPER_ENV = 'DSH_GIT_BRIDGE_HELPER';
|
|
32
|
+
/** Env var carrying a debug log path to the helper script (unset by default). */
|
|
33
|
+
export const BRIDGE_DEBUG_ENV = 'DSH_GIT_BRIDGE_DEBUG';
|
|
34
|
+
/**
|
|
35
|
+
* Quote one executable or path for git's `!` shell command. Backslashes are
|
|
36
|
+
* normalized to forward slashes first: git runs `!` helpers through `sh`,
|
|
37
|
+
* where `C:\…` would be read as escape sequences.
|
|
38
|
+
* @param value - an absolute path.
|
|
39
|
+
*/
|
|
40
|
+
export function shellQuote(value) {
|
|
41
|
+
return `"${value.replace(/\\/g, '/')}"`;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* The helper command git runs for one configured host: a `!`-prefixed shell
|
|
45
|
+
* command, because git does not word-split a helper value by itself (a bare
|
|
46
|
+
* `node "C:/…/bridge.js"` is looked up as the program `git-credential-node`).
|
|
47
|
+
* @param nodePath - the node executable.
|
|
48
|
+
* @param helperPath - the compiled helper script.
|
|
49
|
+
*/
|
|
50
|
+
export function bridgeCommand(nodePath, helperPath) {
|
|
51
|
+
return `!${shellQuote(nodePath)} ${shellQuote(helperPath)}`;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* The ordered environment assignments for the given hosts: per host an empty
|
|
55
|
+
* helper value (git resets the accumulated list for that URL scope) and then
|
|
56
|
+
* the bridge command, plus the credential-file variable the helper reads and
|
|
57
|
+
* the two fail-fast switches.
|
|
58
|
+
* @param hosts - normalized hosts with a stored credential.
|
|
59
|
+
* @param options - process paths and the credential file.
|
|
60
|
+
*/
|
|
61
|
+
export function shellBridgeEnv(hosts, options) {
|
|
62
|
+
const unique = [...new Set(hosts)];
|
|
63
|
+
const assignments = [];
|
|
64
|
+
// Reset the WHOLE helper list first, even when no host is stored. Without it,
|
|
65
|
+
// any host without a stored credential still reaches the machine chain
|
|
66
|
+
// (`helper-selector` → GCM), which raises a GUI prompt no agent shell can
|
|
67
|
+
// answer — the dialog that kept popping up. With it, such a host fails at
|
|
68
|
+
// once with "terminal prompts disabled", and the URL-scoped pairs below are
|
|
69
|
+
// the only way credentials get back into a shell.
|
|
70
|
+
assignments.push(['GIT_CONFIG_COUNT', String(unique.length * 2 + 1)]);
|
|
71
|
+
assignments.push(['GIT_CONFIG_KEY_0', 'credential.helper'], ['GIT_CONFIG_VALUE_0', '']);
|
|
72
|
+
unique.forEach((host, index) => {
|
|
73
|
+
const key = `credential.https://${host}/.helper`;
|
|
74
|
+
const empty = String(index * 2 + 1);
|
|
75
|
+
const command = String(index * 2 + 2);
|
|
76
|
+
assignments.push([`GIT_CONFIG_KEY_${empty}`, key], [`GIT_CONFIG_VALUE_${empty}`, '']);
|
|
77
|
+
assignments.push([`GIT_CONFIG_KEY_${command}`, key], [`GIT_CONFIG_VALUE_${command}`, bridgeCommand(options.nodePath, options.helperPath)]);
|
|
78
|
+
});
|
|
79
|
+
assignments.push([BRIDGE_HELPER_ENV, options.credentialFile]);
|
|
80
|
+
// Fail fast instead of parking a shell on a prompt or a GUI dialog.
|
|
81
|
+
assignments.push(['GIT_TERMINAL_PROMPT', '0'], ['GCM_INTERACTIVE', 'never']);
|
|
82
|
+
return assignments;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Build the bridge over one host process. Every {@link ShellBridge.apply}
|
|
86
|
+
* first removes the assignments it made last time, so a deleted host loses
|
|
87
|
+
* its helper scope just like a freshly added one gains it.
|
|
88
|
+
* @param deps - host seams.
|
|
89
|
+
*/
|
|
90
|
+
export function createShellBridge(deps) {
|
|
91
|
+
const env = deps.env ?? process.env;
|
|
92
|
+
let applied = [];
|
|
93
|
+
return {
|
|
94
|
+
async apply() {
|
|
95
|
+
let hosts = [];
|
|
96
|
+
try {
|
|
97
|
+
hosts = (await (deps.store ?? createCredentialStore(credentialFilePath(deps.home(), deps.envHome()))).load()).map(entry => entry.host);
|
|
98
|
+
}
|
|
99
|
+
catch {
|
|
100
|
+
// An unreadable store means no host can be bridged. The global helper
|
|
101
|
+
// reset still goes out, so shell git fails fast rather than popping a
|
|
102
|
+
// credential dialog nobody can answer.
|
|
103
|
+
hosts = [];
|
|
104
|
+
}
|
|
105
|
+
const helperPath = deps.helperPath
|
|
106
|
+
?? fileURLToPath(new URL('./helpers/git-credential-bridge.js', import.meta.url));
|
|
107
|
+
const assignments = shellBridgeEnv(hosts, {
|
|
108
|
+
nodePath: deps.nodePath ?? process.execPath,
|
|
109
|
+
helperPath,
|
|
110
|
+
credentialFile: credentialFilePath(deps.home(), deps.envHome()),
|
|
111
|
+
});
|
|
112
|
+
for (const key of applied)
|
|
113
|
+
delete env[key];
|
|
114
|
+
applied = [];
|
|
115
|
+
for (const [key, value] of assignments) {
|
|
116
|
+
env[key] = value;
|
|
117
|
+
applied.push(key);
|
|
118
|
+
}
|
|
119
|
+
},
|
|
120
|
+
};
|
|
121
|
+
}
|
package/package.json
CHANGED