oauthStateCodec.test.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. buildOAuthState,
  4. buildReturnToCallbackUrl,
  5. buildWebRemoteOAuthRedirectUri,
  6. isTrustedDevReturnTo,
  7. parseOAuthState,
  8. resolveSafeAppReturnTo,
  9. } from "../src/auth/oauthStateCodec.js";
  10. describe("oauthStateCodec", () => {
  11. it("round-trips unicode payloads through base64url state", () => {
  12. const state = buildOAuthState({
  13. nonce: "nonce-1",
  14. app_return_to:
  15. "https://zcode.z.ai/remote/v4?app_version=3.13.0&sid=device-1&mid=mid-1&name=Desktop&ignored=1",
  16. return_to: "http://192.168.1.8:5173/remote/v4",
  17. });
  18. expect(state).not.toContain("+");
  19. expect(state).not.toContain("/");
  20. expect(state).not.toContain("=");
  21. expect(parseOAuthState(state)).toEqual({
  22. nonce: "nonce-1",
  23. app_return_to:
  24. "https://zcode.z.ai/remote/v4?app_version=3.13.0&sid=device-1&mid=mid-1&name=Desktop&ignored=1",
  25. return_to: "http://192.168.1.8:5173/remote/v4",
  26. });
  27. });
  28. it("returns null for malformed state", () => {
  29. expect(parseOAuthState("not-json")).toBeNull();
  30. expect(parseOAuthState(buildOAuthState({ nonce: "" }))).toBeNull();
  31. });
  32. it("only trusts private development callback urls", () => {
  33. expect(isTrustedDevReturnTo(new URL("http://192.168.1.8:5173/remote/v4"))).toBe(true);
  34. expect(isTrustedDevReturnTo(new URL("http://172.31.2.3:5173/remote/v4"))).toBe(true);
  35. expect(isTrustedDevReturnTo(new URL("http://127.0.0.1:5173/cn/share/callback"))).toBe(true);
  36. expect(isTrustedDevReturnTo(new URL("http://127.0.0.1:5173/share/callback"))).toBe(true);
  37. expect(isTrustedDevReturnTo(new URL("http://172.32.2.3:5173/remote/v4"))).toBe(false);
  38. expect(isTrustedDevReturnTo(new URL("https://example.com/remote/v4"))).toBe(false);
  39. expect(isTrustedDevReturnTo(new URL("http://127.0.0.1:5173/other"))).toBe(false);
  40. });
  41. it("sanitizes app_return_to to same-origin web-remote paths and allowed query keys", () => {
  42. expect(
  43. resolveSafeAppReturnTo(
  44. "https://zcode.z.ai/remote/v4?app_version=3.13.0&sid=device-1&mid=mid-1&name=Desktop&ignored=1",
  45. { currentOrigin: "https://zcode.z.ai" },
  46. ),
  47. ).toBe("/remote/v4?app_version=3.13.0&sid=device-1&mid=mid-1&name=Desktop");
  48. expect(
  49. resolveSafeAppReturnTo("https://evil.example/remote/v4?sid=device-1", {
  50. currentOrigin: "https://zcode.z.ai",
  51. }),
  52. ).toBeNull();
  53. expect(
  54. resolveSafeAppReturnTo("http://192.168.1.8:5173/remote/v4?sid=device-1", {
  55. currentOrigin: "http://192.168.1.8:5173",
  56. }),
  57. ).toBe("/remote/v4?sid=device-1");
  58. });
  59. it("rejects legacy routes and strips credentials from V2 return targets", () => {
  60. expect(resolveSafeAppReturnTo("https://zcode.z.ai/web-remote?sid=device-1")).toBeNull();
  61. expect(
  62. resolveSafeAppReturnTo(
  63. "https://zcode.z.ai/remote/v4/?sid=device-1&hash=secret&t=1&token=secret&code=code#secret",
  64. ),
  65. ).toBe("/remote/v4/?sid=device-1");
  66. });
  67. it.each(["/remote/__test__", "/remote/__test__/"])(
  68. "测试入口 %s 保留安全返回参数,但不成为 OAuth callback",
  69. (path) => {
  70. const origin = "https://zcode.z.ai";
  71. const query = "app_version=3.14.0&sid=device-1&mid=mid-1&name=Desk";
  72. const href = `${origin}${path}?${query}&token=secret&hash=old&t=1&code=old&state=old#fragment`;
  73. expect(resolveSafeAppReturnTo(href, { currentOrigin: origin })).toBe(`${path}?${query}`);
  74. expect(buildWebRemoteOAuthRedirectUri(`${origin}/remote/v4`, href)).toBe(
  75. `${origin}/remote/v4?${query}`,
  76. );
  77. const testCallback = `http://127.0.0.1:5173${path}`;
  78. expect(isTrustedDevReturnTo(new URL(testCallback))).toBe(false);
  79. expect(
  80. buildReturnToCallbackUrl(testCallback, { code: "code-1", state: "state-1" }),
  81. ).toBeNull();
  82. expect(buildWebRemoteOAuthRedirectUri(testCallback, href)).toBe(testCallback);
  83. },
  84. );
  85. it.each([
  86. "https://evil.example/remote/__test__?sid=device-1",
  87. "https://zcode.z.ai/unknown?sid=device-1",
  88. "https://zcode.z.ai/remote/__test__/unknown?sid=device-1",
  89. ])("拒绝不受信任的测试来源 %s", (href) => {
  90. const origin = "https://zcode.z.ai";
  91. expect(resolveSafeAppReturnTo(href, { currentOrigin: origin })).toBeNull();
  92. expect(buildWebRemoteOAuthRedirectUri(`${origin}/remote/v4`, href)).toBe(`${origin}/remote/v4`);
  93. });
  94. it("accepts a same-origin share route as a safe OAuth return target", () => {
  95. expect(
  96. resolveSafeAppReturnTo("https://zcode.z.ai/cn/share/share-1", {
  97. currentOrigin: "https://zcode.z.ai",
  98. }),
  99. ).toBe("/cn/share/share-1");
  100. expect(
  101. resolveSafeAppReturnTo("https://zcode.z.ai/share/share-1", {
  102. currentOrigin: "https://zcode.z.ai",
  103. }),
  104. ).toBe("/share/share-1");
  105. expect(
  106. resolveSafeAppReturnTo("https://evil.example/cn/share/share-1", {
  107. currentOrigin: "https://zcode.z.ai",
  108. }),
  109. ).toBeNull();
  110. });
  111. it("builds a sanitized return_to callback URL for dev redirects", () => {
  112. expect(
  113. buildReturnToCallbackUrl(
  114. "http://192.168.1.8:5173/remote/v4?app_version=3.14.0&sid=device-1&mid=&name=My+Mac&authCode=old&state=old&token=secret#fragment",
  115. {
  116. error: "access_denied",
  117. state: "new-state",
  118. },
  119. ),
  120. ).toBe(
  121. "http://192.168.1.8:5173/remote/v4?app_version=3.14.0&sid=device-1&mid=&name=My+Mac&error=access_denied&state=new-state",
  122. );
  123. expect(
  124. buildReturnToCallbackUrl("http://192.168.1.8:5173/remote/v4?old=1", {
  125. code: "code-1",
  126. state: "state-1",
  127. }),
  128. ).toBe("http://192.168.1.8:5173/remote/v4?code=code-1&state=state-1");
  129. expect(
  130. buildReturnToCallbackUrl("http://127.0.0.1:5173/share/callback", {
  131. code: "code-1",
  132. state: "state-1",
  133. }),
  134. ).toBe("http://127.0.0.1:5173/share/callback?code=code-1&state=state-1");
  135. expect(
  136. buildReturnToCallbackUrl("https://example.com/remote/v4", {
  137. error: "access_denied",
  138. state: "state-1",
  139. }),
  140. ).toBeNull();
  141. });
  142. });