remoteSshHostKey.test.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import { describe, expect, it } from "vitest";
  2. import { buildSshRemoteHostKey } from "../src/remoteSshHostKey.js";
  3. import { stripRemoteTargetSecrets } from "../src/remoteTarget.js";
  4. describe("buildSshRemoteHostKey", () => {
  5. it("normalizes host, default port and private key path without resolving the filesystem", () => {
  6. expect(
  7. buildSshRemoteHostKey({
  8. kind: "ssh",
  9. host: " Mac-Studio ",
  10. username: " root ",
  11. privateKeyPath: " c:\\Users\\me\\.ssh\\..\\.ssh\\id_ed25519\\ ",
  12. }),
  13. ).toBe(
  14. buildSshRemoteHostKey({
  15. kind: "ssh",
  16. host: "mac-studio",
  17. port: 22,
  18. username: "root",
  19. privateKeyPath: "C:/Users/me/.ssh/id_ed25519",
  20. }),
  21. );
  22. });
  23. it("keeps Windows, UNC and POSIX roots from being removed by parent segments", () => {
  24. const target = {
  25. kind: "ssh" as const,
  26. host: "example.com",
  27. username: "root",
  28. };
  29. expect(buildSshRemoteHostKey({ ...target, privateKeyPath: String.raw`C:\..\keys` })).toBe(
  30. buildSshRemoteHostKey({ ...target, privateKeyPath: String.raw`C:\keys` }),
  31. );
  32. expect(buildSshRemoteHostKey({ ...target, privateKeyPath: String.raw`C:\..\keys` })).not.toBe(
  33. buildSshRemoteHostKey({ ...target, privateKeyPath: "keys" }),
  34. );
  35. expect(
  36. buildSshRemoteHostKey({
  37. ...target,
  38. privateKeyPath: String.raw`\\server\share\..\keys`,
  39. }),
  40. ).toBe(
  41. buildSshRemoteHostKey({
  42. ...target,
  43. privateKeyPath: String.raw`\\server\share\keys`,
  44. }),
  45. );
  46. expect(buildSshRemoteHostKey({ ...target, privateKeyPath: "/../keys" })).toBe(
  47. buildSshRemoteHostKey({ ...target, privateKeyPath: "/keys" }),
  48. );
  49. });
  50. it("preserves parent segments that cannot be resolved safely", () => {
  51. const target = {
  52. kind: "ssh" as const,
  53. host: "example.com",
  54. username: "root",
  55. };
  56. expect(buildSshRemoteHostKey({ ...target, privateKeyPath: "~/../keys" })).not.toBe(
  57. buildSshRemoteHostKey({ ...target, privateKeyPath: "keys" }),
  58. );
  59. expect(buildSshRemoteHostKey({ ...target, privateKeyPath: "a/../../keys" })).toBe(
  60. buildSshRemoteHostKey({ ...target, privateKeyPath: "../keys" }),
  61. );
  62. });
  63. it("does not include password or private key passphrase contents", () => {
  64. expect(
  65. buildSshRemoteHostKey({
  66. kind: "ssh",
  67. host: "example.com",
  68. username: "Alice",
  69. password: "first-secret",
  70. }),
  71. ).toBe(
  72. buildSshRemoteHostKey({
  73. kind: "ssh",
  74. host: "example.com",
  75. username: "Alice",
  76. password: "second-secret",
  77. }),
  78. );
  79. expect(
  80. buildSshRemoteHostKey({
  81. kind: "ssh",
  82. host: "example.com",
  83. username: "Alice",
  84. privateKeyPath: "~/.ssh/id_ed25519",
  85. privateKeyPassphrase: "first-secret",
  86. }),
  87. ).toBe(
  88. buildSshRemoteHostKey({
  89. kind: "ssh",
  90. host: "example.com",
  91. username: "Alice",
  92. privateKeyPath: "~/.ssh/id_ed25519",
  93. privateKeyPassphrase: "second-secret",
  94. }),
  95. );
  96. });
  97. it("keeps auth kind, username case and private key identity distinct", () => {
  98. const base = { kind: "ssh" as const, host: "example.com", username: "Alice" };
  99. expect(buildSshRemoteHostKey({ ...base, password: "secret" })).not.toBe(
  100. buildSshRemoteHostKey(base),
  101. );
  102. expect(buildSshRemoteHostKey({ ...base, username: "alice" })).not.toBe(
  103. buildSshRemoteHostKey(base),
  104. );
  105. expect(buildSshRemoteHostKey({ ...base, privateKeyPath: "/keys/a" })).not.toBe(
  106. buildSshRemoteHostKey({ ...base, privateKeyPath: "/keys/b" }),
  107. );
  108. });
  109. it("infers password auth from a persisted credential reference", () => {
  110. expect(
  111. buildSshRemoteHostKey({
  112. kind: "ssh",
  113. host: "example.com",
  114. username: "root",
  115. passwordCredentialKey: "remote-workspace:one:password",
  116. }),
  117. ).toBe(
  118. buildSshRemoteHostKey({
  119. kind: "ssh",
  120. host: "example.com",
  121. username: "root",
  122. password: "secret",
  123. }),
  124. );
  125. });
  126. });
  127. describe("stripRemoteTargetSecrets", () => {
  128. it("removes SSH password material without changing connection identity fields", () => {
  129. expect(
  130. stripRemoteTargetSecrets({
  131. kind: "ssh",
  132. host: "example.com",
  133. port: 2222,
  134. username: "root",
  135. password: "password-secret",
  136. privateKeyPath: "/keys/id_ed25519",
  137. privateKeyPassphrase: "key-secret",
  138. }),
  139. ).toEqual({
  140. kind: "ssh",
  141. host: "example.com",
  142. port: 2222,
  143. username: "root",
  144. privateKeyPath: "/keys/id_ed25519",
  145. });
  146. });
  147. it("removes server token material without changing server identity fields", () => {
  148. expect(
  149. stripRemoteTargetSecrets({
  150. kind: "server",
  151. url: "https://studio.example.com",
  152. name: "Studio",
  153. serverId: "studio",
  154. token: "server-secret",
  155. }),
  156. ).toEqual({
  157. kind: "server",
  158. url: "https://studio.example.com",
  159. name: "Studio",
  160. serverId: "studio",
  161. });
  162. });
  163. });