remoteSshHostKey.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import type { RemoteTargetSnapshot } from "./protocol.js";
  2. import type { SSHConnectOptions } from "./remoteTarget.js";
  3. type SshRemoteHostKeyTarget = SSHConnectOptions | Extract<RemoteTargetSnapshot, { kind: "ssh" }>;
  4. interface PrivateKeyPathRoot {
  5. prefix: string;
  6. body: string;
  7. blocksParentTraversal: boolean;
  8. }
  9. function splitPrivateKeyPathRoot(value: string): PrivateKeyPathRoot {
  10. const windowsDriveRoot = value.match(/^([A-Z]:)\/(.*)$/);
  11. if (windowsDriveRoot) {
  12. return {
  13. prefix: `${windowsDriveRoot[1]}/`,
  14. body: windowsDriveRoot[2] ?? "",
  15. blocksParentTraversal: true,
  16. };
  17. }
  18. if (value.startsWith("//")) {
  19. const segments = value.slice(2).split("/").filter(Boolean);
  20. if (segments.length >= 2) {
  21. const [server, share, ...bodySegments] = segments;
  22. return {
  23. prefix: `//${server}/${share}/`,
  24. body: bodySegments.join("/"),
  25. blocksParentTraversal: true,
  26. };
  27. }
  28. return {
  29. prefix: "//",
  30. body: segments.join("/"),
  31. blocksParentTraversal: true,
  32. };
  33. }
  34. if (value.startsWith("/")) {
  35. return {
  36. prefix: "/",
  37. body: value.replace(/^\/+/, ""),
  38. blocksParentTraversal: true,
  39. };
  40. }
  41. if (value.startsWith("~/")) {
  42. return {
  43. prefix: "~/",
  44. body: value.slice(2),
  45. blocksParentTraversal: false,
  46. };
  47. }
  48. const windowsDriveRelative = value.match(/^([A-Z]:)(.*)$/);
  49. if (windowsDriveRelative) {
  50. return {
  51. prefix: windowsDriveRelative[1] ?? "",
  52. body: windowsDriveRelative[2] ?? "",
  53. blocksParentTraversal: false,
  54. };
  55. }
  56. return {
  57. prefix: "",
  58. body: value,
  59. blocksParentTraversal: false,
  60. };
  61. }
  62. function normalizePrivateKeyPath(value: string | undefined): string {
  63. const trimmed = value?.trim();
  64. if (!trimmed) {
  65. return "";
  66. }
  67. const normalizedSeparators = trimmed.replace(/\\/g, "/");
  68. const driveNormalized = normalizedSeparators.replace(
  69. /^([a-z]):/i,
  70. (_, drive: string) => `${drive.toUpperCase()}:`,
  71. );
  72. const root = splitPrivateKeyPathRoot(driveNormalized);
  73. const segments: string[] = [];
  74. for (const segment of root.body.split("/")) {
  75. if (!segment || segment === ".") {
  76. continue;
  77. }
  78. if (segment === "..") {
  79. if (segments.length > 0 && segments.at(-1) !== "..") {
  80. segments.pop();
  81. continue;
  82. }
  83. // 盘符、UNC share 和 POSIX root 不是普通路径段,`..` 不能把根弹掉;
  84. // 相对路径与 `~` 无法在浏览器侧安全求值,保留未消解的 parent segment 以避免错误复用。
  85. if (root.blocksParentTraversal) {
  86. continue;
  87. }
  88. segments.push(segment);
  89. continue;
  90. }
  91. segments.push(segment);
  92. }
  93. return `${root.prefix}${segments.join("/")}` || root.prefix;
  94. }
  95. function resolveSshAuthKind(target: SshRemoteHostKeyTarget): "agent" | "password" | "private-key" {
  96. if (target.privateKeyPath?.trim()) {
  97. return "private-key";
  98. }
  99. if (
  100. ("password" in target && typeof target.password === "string") ||
  101. ("passwordCredentialKey" in target && Boolean(target.passwordCredentialKey?.trim()))
  102. ) {
  103. return "password";
  104. }
  105. return "agent";
  106. }
  107. /**
  108. * 构造窗口内 SSH Remote Host 的共享身份。
  109. * 密码和私钥口令只用于建连,禁止进入共享键或日志。
  110. */
  111. export function buildSshRemoteHostKey(target: SshRemoteHostKeyTarget): string {
  112. return JSON.stringify([
  113. "ssh:v1",
  114. target.host.trim().toLowerCase(),
  115. target.port ?? 22,
  116. target.username.trim(),
  117. resolveSshAuthKind(target),
  118. normalizePrivateKeyPath(target.privateKeyPath),
  119. ]);
  120. }