clientConfig.test.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import { expect, it } from "vitest";
  2. import { parseClientConfigSnapshot } from "../src/clientConfig.js";
  3. it("有效的空响应撤销公开配置,未知字段不会透传", () => {
  4. expect(parseClientConfigSnapshot({ code: 0, data: null })).toEqual({
  5. pluginStoreOrder: null,
  6. zsrcUrl: null,
  7. uiPluginRecommendations: [],
  8. });
  9. expect(
  10. parseClientConfigSnapshot({ code: 0, data: { configs: { privateField: "not public" } } }),
  11. ).toEqual({ pluginStoreOrder: null, zsrcUrl: null, uiPluginRecommendations: [] });
  12. });
  13. it("严格校验 envelope 并隔离坏的模式字段", () => {
  14. expect(() => parseClientConfigSnapshot({ code: 1001 })).toThrow(
  15. "Invalid public client config response",
  16. );
  17. expect(() => parseClientConfigSnapshot({ data: { configs: {} } })).toThrow();
  18. expect(
  19. parseClientConfigSnapshot({
  20. code: 0,
  21. data: {
  22. configs: {
  23. pluginStoreOrder: {
  24. code: { categoryOrder: 3 },
  25. work: { categoryOrder: ["finance"] },
  26. },
  27. },
  28. },
  29. }),
  30. ).toEqual({
  31. pluginStoreOrder: { work: { categoryOrder: ["finance"] } },
  32. zsrcUrl: null,
  33. uiPluginRecommendations: [],
  34. });
  35. });
  36. it("读取公开漏洞入口,裁剪空白且不透传其它 zsrc 字段", () => {
  37. expect(
  38. parseClientConfigSnapshot({
  39. code: 0,
  40. data: {
  41. configs: {
  42. zsrc: { url: " https://security.example.test/report ", privateField: "secret" },
  43. },
  44. },
  45. }),
  46. ).toEqual({
  47. pluginStoreOrder: null,
  48. zsrcUrl: "https://security.example.test/report",
  49. uiPluginRecommendations: [],
  50. });
  51. });
  52. it.each([
  53. undefined,
  54. null,
  55. {},
  56. { url: "" },
  57. { url: " " },
  58. { url: 42 },
  59. { url: "/relative" },
  60. { url: "javascript:alert(1)" },
  61. { url: "file:///tmp/test" },
  62. { url: "https://" },
  63. "https://security.example.test",
  64. ])("坏的 zsrc 配置隐藏入口且不影响排序:%j", (zsrc) => {
  65. expect(
  66. parseClientConfigSnapshot({
  67. code: 0,
  68. data: {
  69. configs: {
  70. zsrc,
  71. pluginStoreOrder: { work: { categoryOrder: ["finance"] } },
  72. },
  73. },
  74. }),
  75. ).toEqual({
  76. pluginStoreOrder: { work: { categoryOrder: ["finance"] } },
  77. zsrcUrl: null,
  78. uiPluginRecommendations: [],
  79. });
  80. });
  81. it("漏洞地址支持带空白的大小写混合 HTTP(S) scheme", () => {
  82. expect(
  83. parseClientConfigSnapshot({
  84. code: 0,
  85. data: {
  86. configs: {
  87. zsrc: { url: " HtTpS://security.example.test/report " },
  88. },
  89. },
  90. }).zsrcUrl,
  91. ).toBe("HtTpS://security.example.test/report");
  92. });
  93. it("读取 UI 推荐且只保留插件和面板身份", () => {
  94. expect(
  95. parseClientConfigSnapshot({
  96. code: 0,
  97. data: {
  98. configs: {
  99. uiPluginRecommendations: [
  100. { pluginId: " panel@example ", surfaceId: " main ", resourceUri: "not trusted" },
  101. ],
  102. },
  103. },
  104. }).uiPluginRecommendations,
  105. ).toEqual([{ pluginId: "panel@example", surfaceId: "main" }]);
  106. });
  107. it.each([
  108. undefined,
  109. null,
  110. {},
  111. [],
  112. [{ pluginId: "p" }],
  113. [{ pluginId: "", surfaceId: "s" }],
  114. [{ pluginId: "p", surfaceId: 2 }],
  115. ])("无效或空推荐返回空列表:%j", (uiPluginRecommendations) => {
  116. const snapshot = parseClientConfigSnapshot({
  117. code: 0,
  118. data: { configs: { uiPluginRecommendations, zsrc: { url: "https://example.com/report" } } },
  119. });
  120. expect(snapshot.uiPluginRecommendations).toEqual([]);
  121. expect(snapshot.zsrcUrl).toBe("https://example.com/report");
  122. });