|
| 1 | +/** |
| 2 | + * Tier WA — what a loaded package can reach through the published entrypoint. |
| 3 | + * |
| 4 | + * The adapter holds a credential in memory for one invocation and hands it to |
| 5 | + * its own Git children. That containment is only worth as much as the surface |
| 6 | + * around it: a `RepositoryHost` sees every `GitInvocation`, and an authenticated |
| 7 | + * one carries the attachment the credential travels in. If a package that a |
| 8 | + * document loaded could install one — or reach the options that accept one — |
| 9 | + * then everything else in this contract is arithmetic on a value it has already |
| 10 | + * read. |
| 11 | + * |
| 12 | + * So this suite asks the question from outside. It imports the entrypoint the |
| 13 | + * way anybody else would, and looks for the seams rather than trusting that they |
| 14 | + * are absent. |
| 15 | + */ |
| 16 | + |
| 17 | +import { describe, it } from "@executablemd/test-support/bdd"; |
| 18 | +import { expect } from "@executablemd/test-support/expect"; |
| 19 | +import * as published from "../deno.ts"; |
| 20 | +import type { WorkflowHostOptions } from "../deno.ts"; |
| 21 | +// Source-relative, and only resolvable for code that already lives here. |
| 22 | +import { withWorkflowWorkspace } from "../src/deno/workspace/host.ts"; |
| 23 | + |
| 24 | +/** The seams a host may configure, and the whole of them. */ |
| 25 | +const HOST_OWNED = ["gitHubIssues", "helper"] as const; |
| 26 | + |
| 27 | +/** |
| 28 | + * Every name a package that imported the entrypoint can see. |
| 29 | + * |
| 30 | + * Read from the module rather than from the source, so a re-export added |
| 31 | + * anywhere in the graph is caught here rather than by reading the file it was |
| 32 | + * added to. |
| 33 | + */ |
| 34 | +const REACHABLE = Object.keys(published); |
| 35 | + |
| 36 | +describe("workflow published Deno entrypoint", () => { |
| 37 | + it("offers nothing that can observe a Git invocation", function* () { |
| 38 | + // Each of these is a way to see, or to become, the thing an authenticated |
| 39 | + // command runs through. None of them is a host's business, and none is |
| 40 | + // published. |
| 41 | + for (const seam of [ |
| 42 | + "denoRepositoryHost", |
| 43 | + "withWorkflowWorkspace", |
| 44 | + "useRepositoryComposition", |
| 45 | + "useGitComposition", |
| 46 | + "denoGitAuthentication", |
| 47 | + "denoCredentialBroker", |
| 48 | + ]) { |
| 49 | + expect(REACHABLE).not.toContain(seam); |
| 50 | + } |
| 51 | + }); |
| 52 | + |
| 53 | + it("publishes one Workspace attachment, and it is the narrow one", function* () { |
| 54 | + expect(REACHABLE).toContain("withWorkflowHostWorkspace"); |
| 55 | + expect(REACHABLE).not.toContain("withWorkflowWorkspace"); |
| 56 | + }); |
| 57 | + |
| 58 | + it("accepts only what a host owns", function* () { |
| 59 | + // A value, so the shape is checked by the compiler rather than described in |
| 60 | + // a comment. Adding `composition` here stops compiling, which is the point: |
| 61 | + // the option that carries a substituted host is not on this type. |
| 62 | + const options: WorkflowHostOptions = {}; |
| 63 | + const named: readonly string[] = HOST_OWNED; |
| 64 | + expect(named).toEqual(["gitHubIssues", "helper"]); |
| 65 | + expect(Object.keys(options)).toEqual([]); |
| 66 | + }); |
| 67 | + |
| 68 | + it("hands a substituted host no route in, however it is spelled", function* () { |
| 69 | + // The exploit, attempted rather than reasoned about: a package that loaded |
| 70 | + // this entrypoint builds the recording host it would want and looks for |
| 71 | + // somewhere to put it. |
| 72 | + const recorded: unknown[] = []; |
| 73 | + const hostile = { |
| 74 | + git(invocation: unknown) { |
| 75 | + recorded.push(invocation); |
| 76 | + return { code: 0, stdout: "", stderr: "" }; |
| 77 | + }, |
| 78 | + useDirectory: () => "/tmp", |
| 79 | + }; |
| 80 | + |
| 81 | + const attempted = published as unknown as Record<string, unknown>; |
| 82 | + // Nothing published takes one. `withWorkflowHostWorkspace` is the only |
| 83 | + // attachment, and its third argument names two members, neither of which is |
| 84 | + // a host. |
| 85 | + const attach = attempted["withWorkflowHostWorkspace"]; |
| 86 | + expect(typeof attach).toBe("function"); |
| 87 | + // Its own arity: a database, an operation, and options. A fourth positional |
| 88 | + // seam would be another way in. |
| 89 | + expect((attach as (...args: unknown[]) => unknown).length).toBeLessThanOrEqual(3); |
| 90 | + |
| 91 | + // And no published name is a host factory that would produce one either. |
| 92 | + for (const name of REACHABLE) { |
| 93 | + expect(name).not.toContain("RepositoryHost"); |
| 94 | + expect(name).not.toContain("Composition"); |
| 95 | + } |
| 96 | + expect(recorded).toEqual([]); |
| 97 | + expect(hostile.git({})).toEqual({ code: 0, stdout: "", stderr: "" }); |
| 98 | + }); |
| 99 | + |
| 100 | + it("keeps the broad options reachable from inside the package", function* () { |
| 101 | + // Not a loophole: this import is source-relative and only resolves for code |
| 102 | + // that already lives here. The suites that substitute a Git subprocess need |
| 103 | + // it, and a loaded package cannot write it. |
| 104 | + expect(typeof withWorkflowWorkspace).toBe("function"); |
| 105 | + }); |
| 106 | +}); |
0 commit comments