Repository navigation
test(herdr): refuse the user's own herdr and herdr-core to a process that names a test session #3155
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
test(herdr): refuse the user's own herdr and herdr-core to a process that names a test session #3155
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| import { afterEach, expect, it } from "bun:test"; | ||
| import { mkdtempSync, rmSync } from "node:fs"; | ||
| import { tmpdir } from "node:os"; | ||
| import { join } from "node:path"; | ||
| import { herdrRpc, herdrSocketPath } from "./client.ts"; | ||
| import { spawnHcConnect } from "../herdr-core/connection.ts"; | ||
| import { coreSocket } from "../herdr-core/discovery.ts"; | ||
|
|
||
| // A script that names a test session but never ran scripts/test-herdr.ts (an import Bun drops | ||
| // because its binding is unused) is left on herdr's default socket: the user's own session. | ||
| // HOME is a temp folder here, so the default socket is nobody's and nothing live is reached. | ||
| const NAMES = ["HOME", "HERDR_SOCKET", "HERDR_TEST_SESSION", "HERDR_TEST_LIVE", "OVIS_ALLOW_LIVE_HERDR", "HC_SOCKET", "HC_SESSION"] as const; | ||
| const saved = Object.fromEntries(NAMES.map((name) => [name, process.env[name]])); | ||
| const home = mkdtempSync(join(tmpdir(), "ovis-live-guard-")); | ||
| afterEach(() => { | ||
| for (const [name, value] of Object.entries(saved)) if (value === undefined) delete process.env[name]; else process.env[name] = value; | ||
| }); | ||
| const codeOf = (error: unknown): string | undefined => (error as { code?: string; kind?: string } | null)?.code ?? (error as { kind?: string } | null)?.kind; | ||
| const isolatedIntent = () => { | ||
| process.env["HOME"] = home; | ||
| for (const name of ["HERDR_SOCKET", "HERDR_TEST_LIVE", "OVIS_ALLOW_LIVE_HERDR", "HC_SOCKET", "HC_SESSION"]) delete process.env[name]; | ||
| process.env["HERDR_TEST_SESSION"] = "ovis-test-guard"; | ||
| }; | ||
|
|
||
| it("refuses herdr's default socket to a process that names a test session", async () => { | ||
| try { | ||
| isolatedIntent(); | ||
| const refused = await herdrRpc("ping", {}).then(() => null, (error: unknown) => error); | ||
| expect(codeOf(refused)).toBe("live_herdr_refused"); | ||
| // another spelling of the same socket | ||
| const spelled = await herdrRpc("ping", {}, join(home, ".config", "herdr", "..", "herdr", "herdr.sock")).then(() => null, (error: unknown) => error); | ||
| expect(codeOf(spelled)).toBe(process.platform === "win32" ? codeOf(spelled) : "live_herdr_refused"); | ||
| // said out loud, the live session may be used | ||
| process.env["OVIS_ALLOW_LIVE_HERDR"] = "1"; | ||
| const reached = await herdrRpc("ping", {}, herdrSocketPath()).then(() => null, (error: unknown) => error); | ||
| expect(codeOf(reached)).not.toBe("live_herdr_refused"); | ||
| } finally { | ||
| rmSync(home, { recursive: true, force: true }); | ||
| } | ||
| }); | ||
|
|
||
| it("refuses hc's default core to a process that names a test session, not a core it names", () => { | ||
| isolatedIntent(); | ||
| const refused = (() => { try { spawnHcConnect(join(home, "no-such-hc")); return null; } catch (error) { return error; } })(); | ||
| expect(codeOf(refused)).toBe("live_core_refused"); | ||
| const named = (() => { try { spawnHcConnect(join(home, "no-such-hc"), { HC_SOCKET: join(home, "hc.sock") }); return null; } catch (error) { return error; } })(); | ||
| expect(codeOf(named)).not.toBe("live_core_refused"); | ||
| // an inherited HC_SOCKET that names the default core is the user's core all the same | ||
| const inherited = (() => { try { spawnHcConnect(join(home, "no-such-hc"), { HC_SOCKET: coreSocket({ env: { ...process.env, HC_SOCKET: undefined, HC_SESSION: undefined } }).path }); return null; } catch (error) { return error; } })(); | ||
| expect(codeOf(inherited)).toBe("live_core_refused"); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.