embeddedBrowserHelpers.test.ts 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. isAllowedBrowserUrl,
  4. isCertificateBrowserLoadErrorCode,
  5. isDefaultBrowserOpenableUrl,
  6. isRecoverableBrowserGuestExitReason,
  7. isLocalDevelopmentBrowserUrl,
  8. normalizeBrowserUrl,
  9. resolveMessageLinkOpenTarget,
  10. } from "@/embeddedBrowserHelpers.js";
  11. describe("embeddedBrowserHelpers", () => {
  12. it("allows file URLs for local HTML previews", () => {
  13. expect(isAllowedBrowserUrl("file:///workspace/index.html")).toBe(true);
  14. expect(normalizeBrowserUrl("file:///workspace/index.html")).toBe(
  15. "file:///workspace/index.html",
  16. );
  17. });
  18. it("keeps explicit HTTP and HTTPS URLs unchanged", () => {
  19. expect(normalizeBrowserUrl("http://localhost:5173")).toBe(
  20. "http://localhost:5173",
  21. );
  22. expect(normalizeBrowserUrl("https://example.com/docs")).toBe(
  23. "https://example.com/docs",
  24. );
  25. });
  26. it("defaults normal domains to HTTPS", () => {
  27. expect(normalizeBrowserUrl("example.com")).toBe("https://example.com");
  28. expect(normalizeBrowserUrl("example.com/path?q=1#top")).toBe(
  29. "https://example.com/path?q=1#top",
  30. );
  31. expect(normalizeBrowserUrl("//example.com/path")).toBe(
  32. "https://example.com/path",
  33. );
  34. });
  35. it("defaults local development hosts to HTTP", () => {
  36. const cases: Array<[string, string]> = [
  37. ["localhost", "http://localhost"],
  38. ["localhost:5173", "http://localhost:5173"],
  39. ["localhost/path?q=1", "http://localhost/path?q=1"],
  40. ["app.localhost:3000", "http://app.localhost:3000"],
  41. ["127.0.0.1:3000", "http://127.0.0.1:3000"],
  42. ["127.12.0.1", "http://127.12.0.1"],
  43. ["0.0.0.0:8080", "http://0.0.0.0:8080"],
  44. ["10.0.0.2", "http://10.0.0.2"],
  45. ["172.16.0.1", "http://172.16.0.1"],
  46. ["172.31.255.254", "http://172.31.255.254"],
  47. ["192.168.1.5:8080", "http://192.168.1.5:8080"],
  48. ["169.254.1.1", "http://169.254.1.1"],
  49. ["[::1]:5173", "http://[::1]:5173"],
  50. ["::1:5173", "http://[::1]:5173"],
  51. ["app.local:3000", "http://app.local:3000"],
  52. ["project.test", "http://project.test"],
  53. ];
  54. for (const [input, expected] of cases) {
  55. expect(normalizeBrowserUrl(input)).toBe(expected);
  56. }
  57. });
  58. it("classifies only local development web URLs for in-app browser defaults", () => {
  59. const localUrls = [
  60. "http://localhost:5173",
  61. "https://127.0.0.1:3000",
  62. "http://[::1]:5173",
  63. "http://app.localhost:3000",
  64. "http://app.local:8080",
  65. "http://project.test",
  66. "http://192.168.1.5:8080",
  67. ];
  68. const externalUrls = [
  69. "https://example.com",
  70. "http://example.com:8080",
  71. "mailto:hello@example.com",
  72. "javascript:alert(1)",
  73. "localhost:5173",
  74. ];
  75. for (const url of localUrls) {
  76. expect(isLocalDevelopmentBrowserUrl(url)).toBe(true);
  77. }
  78. for (const url of externalUrls) {
  79. expect(isLocalDevelopmentBrowserUrl(url)).toBe(false);
  80. }
  81. });
  82. it("右键菜单显式指定目标时不再受本机/私网白名单影响", () => {
  83. const feishuDocUrl = "https://example.feishu.cn/docx/AbCdEf123456";
  84. const localUrl = "http://localhost:5173";
  85. // 回归:公网链接右键「打开」必须进内置浏览器,之前和「在浏览器中打开」等价。
  86. expect(
  87. resolveMessageLinkOpenTarget({ href: feishuDocUrl, forceInApp: true }),
  88. ).toBe("app-browser");
  89. expect(
  90. resolveMessageLinkOpenTarget({ href: feishuDocUrl, forceExternal: true }),
  91. ).toBe("external-browser");
  92. expect(
  93. resolveMessageLinkOpenTarget({ href: localUrl, forceExternal: true }),
  94. ).toBe("external-browser");
  95. expect(
  96. resolveMessageLinkOpenTarget({ href: localUrl, forceInApp: true }),
  97. ).toBe("app-browser");
  98. // 左键单击(无 flag)仍走白名单启发式。
  99. expect(resolveMessageLinkOpenTarget({ href: feishuDocUrl })).toBe(
  100. "external-browser",
  101. );
  102. expect(resolveMessageLinkOpenTarget({ href: localUrl })).toBe("app-browser");
  103. // 两个 flag 同传时以 forceExternal 为准。
  104. expect(
  105. resolveMessageLinkOpenTarget({
  106. href: localUrl,
  107. forceExternal: true,
  108. forceInApp: true,
  109. }),
  110. ).toBe("external-browser");
  111. });
  112. it("uses HTTP for explicit non-HTTPS ports", () => {
  113. expect(normalizeBrowserUrl("example.com:80")).toBe(
  114. "http://example.com:80",
  115. );
  116. expect(normalizeBrowserUrl("example.com:8080")).toBe(
  117. "http://example.com:8080",
  118. );
  119. expect(normalizeBrowserUrl("example.com:443")).toBe("https://example.com:443");
  120. });
  121. it("rejects unsupported explicit protocols", () => {
  122. expect(normalizeBrowserUrl("ftp://example.com")).toBeNull();
  123. expect(normalizeBrowserUrl("javascript:alert(1)")).toBeNull();
  124. });
  125. it("只允许把 http/https/file 页面交给系统默认浏览器", () => {
  126. expect(isDefaultBrowserOpenableUrl("http://localhost:5173")).toBe(true);
  127. expect(isDefaultBrowserOpenableUrl("https://example.com/docs")).toBe(true);
  128. expect(isDefaultBrowserOpenableUrl("about:blank")).toBe(false);
  129. expect(isDefaultBrowserOpenableUrl("data:text/html,hello")).toBe(false);
  130. expect(isDefaultBrowserOpenableUrl("file:///workspace/index.html")).toBe(true);
  131. expect(isDefaultBrowserOpenableUrl("file:///workspace/data.json")).toBe(true);
  132. expect(isDefaultBrowserOpenableUrl("file:///etc/passwd")).toBe(true);
  133. expect(isDefaultBrowserOpenableUrl("not a url")).toBe(false);
  134. });
  135. it("只恢复运行中 guest 的异常退出,不重试启动或完整性失败", () => {
  136. for (const reason of ["abnormal-exit", "killed", "crashed", "oom", "memory-eviction"]) {
  137. expect(isRecoverableBrowserGuestExitReason(reason)).toBe(true);
  138. }
  139. for (const reason of ["clean-exit", "launch-failed", "integrity-failure", "unknown"]) {
  140. expect(isRecoverableBrowserGuestExitReason(reason)).toBe(false);
  141. }
  142. });
  143. it("只把证书类 net error 认成证书问题,其它加载失败不给证书指引", () => {
  144. // -200..-217 是 Chromium 的证书错误区间;自签名内网站点最常见的是 -202。
  145. for (const code of [-200, -201, -202, -207, -217]) {
  146. expect(isCertificateBrowserLoadErrorCode(code)).toBe(true);
  147. }
  148. // DNS、连接被拒、超时、被打断都不是证书问题,给证书指引会误导。
  149. for (const code of [-105, -102, -7, -3, 0, -199, -218, -324]) {
  150. expect(isCertificateBrowserLoadErrorCode(code)).toBe(false);
  151. }
  152. expect(isCertificateBrowserLoadErrorCode(null)).toBe(false);
  153. });
  154. });