conversationShareIntegrity.test.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. buildConversationShareConfirmRequest,
  4. canonicalizeConversationShareJson,
  5. sha256ConversationShareJson,
  6. verifyConversationShareIntegrity,
  7. } from "../src/conversation-share/conversationShareIntegrity.js";
  8. describe("conversation share RFC 8785 integrity", () => {
  9. it("canonicalizes the RFC 8785 primitive sample", () => {
  10. const value = {
  11. numbers: [333_333_333.3333333, 1e30, 4.5, 2e-3, 1e-27],
  12. string: "€$\u000f\nA'B\"\\\"/",
  13. literals: [null, true, false],
  14. };
  15. expect(canonicalizeConversationShareJson(value)).toBe(
  16. "{\"literals\":[null,true,false],\"numbers\":[333333333.3333333,1e+30,4.5,0.002,1e-27],\"string\":\"€$\\u000f\\nA'B\\\"\\\\\\\"/\"}",
  17. );
  18. });
  19. it("sorts object keys recursively without reordering arrays", () => {
  20. expect(
  21. canonicalizeConversationShareJson({ z: { b: 1, a: 2 }, a: [3, { d: 4, c: 5 }] }),
  22. ).toBe('{"a":[3,{"c":5,"d":4}],"z":{"a":2,"b":1}}');
  23. });
  24. it("uses ECMAScript JSON number serialization", () => {
  25. expect(canonicalizeConversationShareJson([-0, 1e30, 1e-7, 0.000001])).toBe(
  26. "[0,1e+30,1e-7,0.000001]",
  27. );
  28. });
  29. it.each([Number.NaN, Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY])(
  30. "rejects non-finite number %s",
  31. (value) => {
  32. expect(() => canonicalizeConversationShareJson(value)).toThrow("finite");
  33. },
  34. );
  35. it("rejects lone Unicode surrogates in keys and values", () => {
  36. expect(() => canonicalizeConversationShareJson("\ud800")).toThrow("Unicode");
  37. expect(() => canonicalizeConversationShareJson({ "\udfff": true })).toThrow("Unicode");
  38. });
  39. it("produces stable lower-case SHA-256", () => {
  40. expect(sha256ConversationShareJson([])).toBe(
  41. "4f53cda18c2baa0c0354bb5f9a3ecbe5ed12ab4d8e11ba873c2f11161202b945",
  42. );
  43. expect(sha256ConversationShareJson({ b: 1, a: 2 })).toBe(
  44. "d3626ac30a87e6f7a6428233b3c68299976865fa5508e4267c5415c76af7a772",
  45. );
  46. });
  47. it("builds the revision 23 slim confirm request and its two integrity hashes", () => {
  48. const rows = [
  49. {
  50. rowId: 1,
  51. turnId: "turn-1",
  52. productTurnId: "product-turn-1",
  53. createdAt: 1,
  54. createdAtSeq: 1,
  55. kind: "turnHeader" as const,
  56. origin: "userInput" as const,
  57. state: "completedSuccess" as const,
  58. startedAt: 1,
  59. endedAt: 2,
  60. },
  61. ];
  62. const disclosureConfirmation = {
  63. version: 1 as const,
  64. accepted_at: 3,
  65. acknowledged_no_secret_detection: true as const,
  66. };
  67. const request = buildConversationShareConfirmRequest({
  68. selected_product_turn_ids: ["product-turn-1"],
  69. projection: { rows },
  70. artifacts: [],
  71. disclosure_confirmation: disclosureConfirmation,
  72. });
  73. expect(request).toEqual({
  74. selected_product_turn_ids: ["product-turn-1"],
  75. projection: { rows },
  76. integrity: {
  77. projection_sha256: "7a748f1dac380029522841af7a40f7d33004368435c7e08a09c4a09c7592b7ba",
  78. artifact_set_sha256: "4f53cda18c2baa0c0354bb5f9a3ecbe5ed12ab4d8e11ba873c2f11161202b945",
  79. },
  80. disclosure_confirmation: disclosureConfirmation,
  81. });
  82. expect(sha256ConversationShareJson(request)).toBe(
  83. "fab3b7aa64630a19ea2c92bbc47363184300e9cbc03e90cb453a01077c777fc2",
  84. );
  85. });
  86. });
  87. /**
  88. * 跨版本完整性:摘要必须对服务端原样发来的值算,否则「给 row 加一个 optional 字段」这种
  89. * 纯 additive 演进会被老客户端误报成「分享文件校验失败」。
  90. */
  91. describe("verifyConversationShareIntegrity", () => {
  92. const descriptor = {
  93. artifact_id: "artifact-1",
  94. logical_artifact_key: "report",
  95. producer_product_turn_id: "product-turn-1",
  96. artifact_version: 1,
  97. state: "current",
  98. ref: "zcode-artifact://share/artifact-1",
  99. artifact_type: "md",
  100. display_name: "report.md",
  101. extension: "md",
  102. mime_type: "text/markdown",
  103. size_bytes: 3,
  104. sha256: "b".repeat(64),
  105. };
  106. const rows = [
  107. {
  108. rowId: 1,
  109. turnId: "turn-1",
  110. createdAt: 1,
  111. createdAtSeq: 1,
  112. kind: "assistantText",
  113. text: "hi",
  114. state: "complete",
  115. },
  116. ];
  117. it("接受未知字段:更新版发布端加的 row 字段与 descriptor 字段都算进摘要", () => {
  118. const futureRows = [{ ...rows[0], futureRowField: "x" }];
  119. const futureDescriptor = { ...descriptor, futureDescriptorField: "y" };
  120. expect(
  121. verifyConversationShareIntegrity({
  122. rawRows: futureRows,
  123. // 服务端读取时附加的 signed URL 字段不参与摘要。
  124. rawArtifacts: [
  125. { ...futureDescriptor, download_url: "https://x/y", download_url_expires_at: 9 },
  126. ],
  127. integrity: {
  128. projection_sha256: sha256ConversationShareJson(futureRows),
  129. artifact_set_sha256: sha256ConversationShareJson([futureDescriptor]),
  130. },
  131. }),
  132. ).toBe(true);
  133. });
  134. it("按 artifact_id 排序后再算 manifest 摘要,响应顺序不影响判定", () => {
  135. const second = { ...descriptor, artifact_id: "artifact-0" };
  136. expect(
  137. verifyConversationShareIntegrity({
  138. rawRows: rows,
  139. rawArtifacts: [descriptor, second],
  140. integrity: {
  141. projection_sha256: sha256ConversationShareJson(rows),
  142. artifact_set_sha256: sha256ConversationShareJson([second, descriptor]),
  143. },
  144. }),
  145. ).toBe(true);
  146. });
  147. it("内容真被改过时仍然拒绝", () => {
  148. expect(
  149. verifyConversationShareIntegrity({
  150. rawRows: [{ ...rows[0], text: "tampered" }],
  151. rawArtifacts: [descriptor],
  152. integrity: {
  153. projection_sha256: sha256ConversationShareJson(rows),
  154. artifact_set_sha256: sha256ConversationShareJson([descriptor]),
  155. },
  156. }),
  157. ).toBe(false);
  158. });
  159. });