Skip to content

Commit e468d25

Browse files
committed
🧪 Tell a helper that declined from one that never ran
Continues 08aed11. The leak, then items 1 and 2. **The injected removal fault leaked.** It threw instead of removing, so every iteration left an `xmd-workflow-credential-*` directory behind. It now performs the real removal and then raises: what is under test is the reporting of a teardown failure, not the failure to clean up. The stale directories that fault created are gone, and a run of the suite now leaves none. **A transport failure has two explanations and they were the same answer.** A helper that declined and a helper that could not be started both leave Git unable to authenticate, but only the first is a credential this host lacks. The helper now records that it ran, before it decides anything, and a transport that failed while an identity was held and the helper never ran raises `CredentialInfrastructureError` instead of refusing. The evidence drives it: a launcher naming a runtime that is not on this machine, through the protected loopback remote, proving infrastructure rather than authentication-unavailable or invalid-locator, no retained Repository, no accepted request, and no credential in the cause or its stack. The launcher inspection it replaces proved none of that. **The hostile ambient helper now records every operation it is asked**, so "nothing reached it" covers the `store` and `erase` Git sends after a success or a rejection rather than only the `get` a sentinel would have caught. Remaining: ambient redirect refusal, rejection classification and real cancellation; A14; A10. Refs #522
1 parent 08aed11 commit e468d25

5 files changed

Lines changed: 144 additions & 29 deletions

File tree

‎packages/workflow/src/deno/composition/authentication.ts‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ export interface GitAuthenticationSession {
115115
* none, and the refusal vocabulary says so.
116116
*/
117117
rejected(): Operation<boolean>;
118+
/** Whether the provider's own helper ran, when one was installed. */
119+
ran(): Operation<boolean>;
118120
}
119121

120122
/**
@@ -151,6 +153,10 @@ export const UNAUTHENTICATED: GitAuthenticationSession = Object.freeze({
151153
*rejected(): Operation<boolean> {
152154
return false;
153155
},
156+
// deno-lint-ignore require-yield
157+
*ran(): Operation<boolean> {
158+
return true;
159+
},
154160
});
155161

156162
/**
@@ -255,6 +261,14 @@ export interface CredentialRequest {
255261
*/
256262
export interface CredentialLease {
257263
readonly acquired: boolean;
264+
/**
265+
* Whether the provider's own helper ran at all.
266+
*
267+
* A transport that failed while holding an identity has two very different
268+
* explanations: a helper that declined, and a helper that could not be
269+
* started. Only the first is a credential this host does not have.
270+
*/
271+
ran(): Operation<boolean>;
258272
rejected(): Operation<boolean>;
259273
attachment(): GitAttachment;
260274
}
@@ -459,11 +473,15 @@ export function denoCredentialBroker(
459473
assembly.platform !== "windows",
460474
);
461475
const marker = join(directory, "rejected");
476+
const invoked = join(directory, "invoked");
462477

463478
yield* provide({
464479
get acquired() {
465480
return held !== undefined && !closed;
466481
},
482+
*ran(): Operation<boolean> {
483+
return closed ? true : yield* operations.markerPresent(invoked);
484+
},
467485
*rejected(): Operation<boolean> {
468486
// A fixed, nonsecret file the helper writes when the transport
469487
// refused what it was given. Read when a failed command is being
@@ -484,6 +502,7 @@ export function denoCredentialBroker(
484502
[HELPER_VARIABLES.host]: request.host,
485503
[HELPER_VARIABLES.path]: request.path ?? "",
486504
[HELPER_VARIABLES.marker]: marker,
505+
[HELPER_VARIABLES.invoked]: invoked,
487506
}),
488507
configuration: Object.freeze([
489508
// Forced on the transport too: without it Git asks the
@@ -506,6 +525,10 @@ function unacquired(): CredentialLease {
506525
return {
507526
acquired: false,
508527
// deno-lint-ignore require-yield
528+
*ran(): Operation<boolean> {
529+
return true;
530+
},
531+
// deno-lint-ignore require-yield
509532
*rejected(): Operation<boolean> {
510533
return false;
511534
},
@@ -584,6 +607,10 @@ export function denoGitAuthentication(options: GitAuthenticationOptions = {}): G
584607
*rejected(): Operation<boolean> {
585608
return false;
586609
},
610+
// deno-lint-ignore require-yield
611+
*ran(): Operation<boolean> {
612+
return true;
613+
},
587614
});
588615
return;
589616
}
@@ -601,6 +628,9 @@ export function denoGitAuthentication(options: GitAuthenticationOptions = {}): G
601628
*rejected(): Operation<boolean> {
602629
return lease === undefined ? false : yield* lease.rejected();
603630
},
631+
*ran(): Operation<boolean> {
632+
return lease === undefined ? true : yield* lease.ran();
633+
},
604634
// Acquisition, not configuration. A helper that answered nothing
605635
// leaves this `none`, and a transport that then fails failed for want
606636
// of authentication rather than because the locator was wrong.
@@ -631,6 +661,32 @@ export function noGitAuthentication(): GitAuthentication {
631661
* authenticated — which is a different thing from a locator that names nothing
632662
* and from a remote that holds nothing.
633663
*/
664+
/**
665+
* A host that could not run its own credential helper.
666+
*
667+
* Infrastructure, not authentication. The provider installed a launcher, Git was
668+
* told to use it, and the program never ran — so this run learned nothing about
669+
* whether an identity was available, and reporting one absent would be inventing
670+
* an answer. It carries no credential and nothing a helper printed.
671+
*/
672+
export class CredentialInfrastructureError extends Error {
673+
override name = "CredentialInfrastructureError";
674+
675+
constructor() {
676+
super("the credential helper this host installed could not be run");
677+
}
678+
}
679+
680+
/**
681+
* Whether a failed transport failed because this host could not run its helper.
682+
*
683+
* Asked before the refusal is classified, and only when an identity was proved:
684+
* a helper that was never installed is not a helper that failed to start.
685+
*/
686+
export function* uninvokable(session: GitAuthenticationSession): Operation<boolean> {
687+
return session.mechanism === "credential-helper" && !(yield* session.ran());
688+
}
689+
634690
export function* unauthenticable(
635691
locator: string,
636692
session: GitAuthenticationSession,

‎packages/workflow/src/deno/composition/credential-helper.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,15 @@ export const HELPER_VARIABLES = Object.freeze({
4646
host: "XMD_HTTP_CREDENTIAL_HOST",
4747
path: "XMD_HTTP_CREDENTIAL_PATH",
4848
marker: "XMD_HTTP_CREDENTIAL_MARKER",
49+
/**
50+
* Where the helper records that it ran at all.
51+
*
52+
* The one way an invocation can tell a helper that declined from a helper
53+
* that never started. Both leave Git unable to authenticate, but the first is
54+
* a host that holds nothing and the second is a host that could not run its
55+
* own program — and those are not the same answer.
56+
*/
57+
invoked: "XMD_HTTP_CREDENTIAL_INVOKED",
4958
});
5059

5160
/** The argument that selects the internal helper mode. */
@@ -217,6 +226,12 @@ export function isCredentialHelperMode(argv: readonly string[]): boolean {
217226
export function* runCredentialHelper(argv: readonly string[]): Operation<void> {
218227
const operation = argv[1] ?? "";
219228
const input = yield* until(readAll());
229+
// Recorded before anything is decided, so it says this program ran rather
230+
// than that it agreed to anything.
231+
const ran = process.env[HELPER_VARIABLES.invoked];
232+
if (ran !== undefined && ran !== "") {
233+
yield* until(writeFile(ran, "invoked\n", { mode: 0o600 }));
234+
}
220235
let marked: string | undefined;
221236
const answer = answerCredentialRequest(operation, input, process.env, (path) => {
222237
marked = path;

‎packages/workflow/src/deno/composition/git.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import {
3333
type GitFailureReason,
3434
} from "../../composition/errors.ts";
3535
import type { GitOutcome, RepositoryHost } from "./host.ts";
36-
import { unauthenticable } from "./authentication.ts";
36+
import { CredentialInfrastructureError, unauthenticable, uninvokable } from "./authentication.ts";
3737
import type { GitAttachment, GitAuthenticationSession } from "./authentication.ts";
3838

3939
/** The refusal a locator needing a credential this host has none for reports. */
@@ -153,6 +153,11 @@ export function* clone(
153153
{ attachment: session.attachment },
154154
);
155155
if (outcome.code !== 0) {
156+
if (yield* uninvokable(session)) {
157+
// Not a refusal at all. This host installed a helper and could not run
158+
// it, so nothing was learned about whether an identity was available.
159+
throw new CredentialInfrastructureError();
160+
}
156161
// Which refusal this is comes from what the host had, never from what Git
157162
// printed: a remote writes into that stream, and a classification read out
158163
// of a sentence would let it decide what this run reports. A transport that

‎packages/workflow/tests/ambient-authentication.test.ts‎

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import type { ComponentRegistration } from "@executablemd/core";
3232
import { RepositoryCompositionError } from "../src/composition/errors.ts";
3333
import { denoRepositoryHost } from "../src/deno/composition/host.ts";
3434
import {
35+
CredentialInfrastructureError,
3536
denoGitAuthentication,
3637
gitTransport,
3738
sshCommand,
@@ -154,6 +155,10 @@ function isRepositoryRefusal(value: unknown): value is RepositoryCompositionErro
154155
return value instanceof RepositoryCompositionError;
155156
}
156157

158+
function isInfrastructure(value: unknown): value is CredentialInfrastructureError {
159+
return value instanceof CredentialInfrastructureError;
160+
}
161+
157162
function* present(path: string): Operation<boolean> {
158163
try {
159164
yield* lstat(path);
@@ -499,6 +504,41 @@ describe("workflow ambient Git authentication", () => {
499504
});
500505
});
501506

507+
describe("workflow ambient authentication infrastructure", () => {
508+
it("fails stop when the helper it installed cannot be run", function* () {
509+
const root = yield* useStorageRoot();
510+
const served = yield* protectedRemote(FIRST, "first");
511+
const home = yield* useInvokingHome([{ host: served.host, path: REPOSITORY, ...FIRST }]);
512+
// A launcher naming a runtime that is not on this machine. The identity was
513+
// proved; what fails is this host running its own program.
514+
const broken = { ...TEST_HELPER, execPath: "/xmd-no-such-runtime" };
515+
516+
yield* withStorage(root, function* () {
517+
const database = yield* createRun();
518+
const counting = countingHost(
519+
denoRepositoryHost({
520+
authentication: denoGitAuthentication({ ambient: home.ambient, assembly: broken }),
521+
}),
522+
);
523+
const failure = yield* raised(
524+
runDocument(database, document(served.locator, "unreachable"), countingOptions(counting)),
525+
);
526+
527+
// Infrastructure, not a refusal. A run that reported "no credential"
528+
// here would be turning a broken host into a clean answer, and one that
529+
// reported an invalid locator would be blaming the document.
530+
expect(causedBy(failure, isInfrastructure)).toBeDefined();
531+
expect(causedBy(failure, isRepositoryRefusal)).toBeUndefined();
532+
// Nothing was completed and nothing retained.
533+
expect(yield* retainedRepositories(database)).toHaveLength(0);
534+
expect(served.requests.every((request) => !request.accepted)).toBe(true);
535+
// And no credential travels with the cause.
536+
expect(carriesCredential(String(failure))).toBe(false);
537+
expect(carriesCredential((failure as Error)?.stack ?? "")).toBe(false);
538+
});
539+
});
540+
});
541+
502542
describe("workflow ambient authentication containment", () => {
503543
/**
504544
* A checkout's own configuration, naming the two settings this change adds to
@@ -516,10 +556,22 @@ describe("workflow ambient authentication containment", () => {
516556
const witness = yield* useTempDirectory("xmd-ambient-helper-");
517557
const sentinel = `${witness}/helper-ran`;
518558
const planted = `${witness}/helper.sh`;
559+
const operations = `${witness}/operations`;
519560
yield* until(
520-
writeFile(planted, ["#!/bin/sh", `echo ran > ${sentinel}`, "exit 1", ""].join("\n"), {
521-
mode: 0o700,
522-
}),
561+
writeFile(
562+
planted,
563+
[
564+
"#!/bin/sh",
565+
// Every operation it is ever asked, recorded. `store` and `erase`
566+
// reaching an ambient helper would be this run deciding what the
567+
// machine should remember, which it never does.
568+
`echo "$1" >> ${operations}`,
569+
`echo ran > ${sentinel}`,
570+
"exit 1",
571+
"",
572+
].join("\n"),
573+
{ mode: 0o700 },
574+
),
523575
);
524576
yield* until(chmod(planted, 0o700));
525577

@@ -552,6 +604,9 @@ describe("workflow ambient authentication containment", () => {
552604
// document named never ran.
553605
expect(remoteBranch(bare, "publish/1.4")).toBeDefined();
554606
expect(yield* present(sentinel)).toBe(false);
607+
// Not one operation reached it — no `get`, and no `store` or `erase`
608+
// afterwards either.
609+
expect(yield* present(operations)).toBe(false);
555610
expect((yield* gitHostOutcomes(database))[0]?.status).toBe("ok");
556611

557612
// The trap really was armed. The same checkout, pushed the ordinary way —

‎packages/workflow/tests/credential-helper.test.ts‎

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -352,30 +352,6 @@ describe("workflow credential infrastructure failures", () => {
352352
expect(String(raised).includes(HELD.username)).toBe(false);
353353
});
354354

355-
it("cannot install a launcher into a place it may not write", function* () {
356-
const home = yield* useInvokingHome([{ host: HOST, path: PATH, ...HELD }]);
357-
// A launcher naming a runtime that is not there is written, but what runs it
358-
// answers nothing — the acquisition succeeded and the helper is the part
359-
// that fails, which is infrastructure rather than an absent credential.
360-
const broken: HelperAssembly = { ...TEST_HELPER, execPath: "/xmd-no-such-runtime" };
361-
362-
yield* scoped(function* () {
363-
const lease = yield* denoCredentialBroker(home.ambient, {}, broken).lease(EXACT);
364-
expect(lease.acquired).toBe(true);
365-
const named = lease
366-
.attachment()
367-
.configuration.find((entry: string) => entry.startsWith("credential.helper="));
368-
expect(String(named)).toContain("credential-helper");
369-
// The launcher names the runtime it was told to name, and nothing about
370-
// the credential is in it.
371-
const written = yield* until(
372-
readFile(String(named).replace("credential.helper=", ""), "utf8"),
373-
);
374-
expect(written).toContain("/xmd-no-such-runtime");
375-
expect(written.includes(HELD.password)).toBe(false);
376-
});
377-
});
378-
379355
it("treats a marker it cannot read as a failure, never as `not rejected`", function* () {
380356
const home = yield* useInvokingHome([{ host: HOST, path: PATH, ...HELD }]);
381357

@@ -412,7 +388,15 @@ describe("workflow credential injected infrastructure faults", () => {
412388
writeLauncher: broken === "install" ? fault("install") : real.writeLauncher,
413389
markerPresent: broken === "marker-read" ? fault("marker-read") : real.markerPresent,
414390
removeWorkingDirectory:
415-
broken === "marker-remove" ? fault("marker-remove") : real.removeWorkingDirectory,
391+
broken === "marker-remove"
392+
? // Removes, then raises. A fault that skipped the removal would be
393+
// leaving this suite's own directories behind on every iteration —
394+
// the failure under test is the reporting of it, not the leak.
395+
function* (path: string): Operation<never> {
396+
yield* real.removeWorkingDirectory(path);
397+
throw new Error("injected marker-remove failure");
398+
}
399+
: real.removeWorkingDirectory,
416400
};
417401
}
418402

0 commit comments

Comments
 (0)