| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- import { describe, expect, it } from "vitest";
- import { buildSshRemoteHostKey } from "../src/remoteSshHostKey.js";
- import { stripRemoteTargetSecrets } from "../src/remoteTarget.js";
- describe("buildSshRemoteHostKey", () => {
- it("normalizes host, default port and private key path without resolving the filesystem", () => {
- expect(
- buildSshRemoteHostKey({
- kind: "ssh",
- host: " Mac-Studio ",
- username: " root ",
- privateKeyPath: " c:\\Users\\me\\.ssh\\..\\.ssh\\id_ed25519\\ ",
- }),
- ).toBe(
- buildSshRemoteHostKey({
- kind: "ssh",
- host: "mac-studio",
- port: 22,
- username: "root",
- privateKeyPath: "C:/Users/me/.ssh/id_ed25519",
- }),
- );
- });
- it("keeps Windows, UNC and POSIX roots from being removed by parent segments", () => {
- const target = {
- kind: "ssh" as const,
- host: "example.com",
- username: "root",
- };
- expect(buildSshRemoteHostKey({ ...target, privateKeyPath: String.raw`C:\..\keys` })).toBe(
- buildSshRemoteHostKey({ ...target, privateKeyPath: String.raw`C:\keys` }),
- );
- expect(buildSshRemoteHostKey({ ...target, privateKeyPath: String.raw`C:\..\keys` })).not.toBe(
- buildSshRemoteHostKey({ ...target, privateKeyPath: "keys" }),
- );
- expect(
- buildSshRemoteHostKey({
- ...target,
- privateKeyPath: String.raw`\\server\share\..\keys`,
- }),
- ).toBe(
- buildSshRemoteHostKey({
- ...target,
- privateKeyPath: String.raw`\\server\share\keys`,
- }),
- );
- expect(buildSshRemoteHostKey({ ...target, privateKeyPath: "/../keys" })).toBe(
- buildSshRemoteHostKey({ ...target, privateKeyPath: "/keys" }),
- );
- });
- it("preserves parent segments that cannot be resolved safely", () => {
- const target = {
- kind: "ssh" as const,
- host: "example.com",
- username: "root",
- };
- expect(buildSshRemoteHostKey({ ...target, privateKeyPath: "~/../keys" })).not.toBe(
- buildSshRemoteHostKey({ ...target, privateKeyPath: "keys" }),
- );
- expect(buildSshRemoteHostKey({ ...target, privateKeyPath: "a/../../keys" })).toBe(
- buildSshRemoteHostKey({ ...target, privateKeyPath: "../keys" }),
- );
- });
- it("does not include password or private key passphrase contents", () => {
- expect(
- buildSshRemoteHostKey({
- kind: "ssh",
- host: "example.com",
- username: "Alice",
- password: "first-secret",
- }),
- ).toBe(
- buildSshRemoteHostKey({
- kind: "ssh",
- host: "example.com",
- username: "Alice",
- password: "second-secret",
- }),
- );
- expect(
- buildSshRemoteHostKey({
- kind: "ssh",
- host: "example.com",
- username: "Alice",
- privateKeyPath: "~/.ssh/id_ed25519",
- privateKeyPassphrase: "first-secret",
- }),
- ).toBe(
- buildSshRemoteHostKey({
- kind: "ssh",
- host: "example.com",
- username: "Alice",
- privateKeyPath: "~/.ssh/id_ed25519",
- privateKeyPassphrase: "second-secret",
- }),
- );
- });
- it("keeps auth kind, username case and private key identity distinct", () => {
- const base = { kind: "ssh" as const, host: "example.com", username: "Alice" };
- expect(buildSshRemoteHostKey({ ...base, password: "secret" })).not.toBe(
- buildSshRemoteHostKey(base),
- );
- expect(buildSshRemoteHostKey({ ...base, username: "alice" })).not.toBe(
- buildSshRemoteHostKey(base),
- );
- expect(buildSshRemoteHostKey({ ...base, privateKeyPath: "/keys/a" })).not.toBe(
- buildSshRemoteHostKey({ ...base, privateKeyPath: "/keys/b" }),
- );
- });
- it("infers password auth from a persisted credential reference", () => {
- expect(
- buildSshRemoteHostKey({
- kind: "ssh",
- host: "example.com",
- username: "root",
- passwordCredentialKey: "remote-workspace:one:password",
- }),
- ).toBe(
- buildSshRemoteHostKey({
- kind: "ssh",
- host: "example.com",
- username: "root",
- password: "secret",
- }),
- );
- });
- });
- describe("stripRemoteTargetSecrets", () => {
- it("removes SSH password material without changing connection identity fields", () => {
- expect(
- stripRemoteTargetSecrets({
- kind: "ssh",
- host: "example.com",
- port: 2222,
- username: "root",
- password: "password-secret",
- privateKeyPath: "/keys/id_ed25519",
- privateKeyPassphrase: "key-secret",
- }),
- ).toEqual({
- kind: "ssh",
- host: "example.com",
- port: 2222,
- username: "root",
- privateKeyPath: "/keys/id_ed25519",
- });
- });
- it("removes server token material without changing server identity fields", () => {
- expect(
- stripRemoteTargetSecrets({
- kind: "server",
- url: "https://studio.example.com",
- name: "Studio",
- serverId: "studio",
- token: "server-secret",
- }),
- ).toEqual({
- kind: "server",
- url: "https://studio.example.com",
- name: "Studio",
- serverId: "studio",
- });
- });
- });
|