oauthStateCodec.test.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. buildOAuthState,
  4. buildReturnToCallbackUrl,
  5. isTrustedDevReturnTo,
  6. parseOAuthState,
  7. resolveSafeAppReturnTo,
  8. } from "../src/auth/oauthStateCodec.js";
  9. describe("oauthStateCodec", () => {
  10. it("round-trips unicode payloads through base64url state", () => {
  11. const state = buildOAuthState({
  12. nonce: "nonce-1",
  13. app_return_to:
  14. "https://zcode.z.ai/web-remote?remoteControlToken=token&relayOrigin=https%3A%2F%2Frelay.example&ignored=1",
  15. return_to: "http://192.168.1.8:5173/web-remote/callback",
  16. });
  17. expect(state).not.toContain("+");
  18. expect(state).not.toContain("/");
  19. expect(state).not.toContain("=");
  20. expect(parseOAuthState(state)).toEqual({
  21. nonce: "nonce-1",
  22. app_return_to:
  23. "https://zcode.z.ai/web-remote?remoteControlToken=token&relayOrigin=https%3A%2F%2Frelay.example&ignored=1",
  24. return_to: "http://192.168.1.8:5173/web-remote/callback",
  25. });
  26. });
  27. it("returns null for malformed state", () => {
  28. expect(parseOAuthState("not-json")).toBeNull();
  29. expect(parseOAuthState(buildOAuthState({ nonce: "" }))).toBeNull();
  30. });
  31. it("only trusts private development callback urls", () => {
  32. expect(isTrustedDevReturnTo(new URL("http://192.168.1.8:5173/web-remote/callback"))).toBe(true);
  33. expect(isTrustedDevReturnTo(new URL("http://172.31.2.3:5173/web-remote/callback"))).toBe(true);
  34. expect(isTrustedDevReturnTo(new URL("http://127.0.0.1:5173/cn/share/callback"))).toBe(true);
  35. expect(isTrustedDevReturnTo(new URL("http://127.0.0.1:5173/share/callback"))).toBe(true);
  36. expect(isTrustedDevReturnTo(new URL("http://172.32.2.3:5173/web-remote/callback"))).toBe(false);
  37. expect(isTrustedDevReturnTo(new URL("https://example.com/web-remote/callback"))).toBe(false);
  38. expect(isTrustedDevReturnTo(new URL("http://127.0.0.1:5173/other"))).toBe(false);
  39. });
  40. it("sanitizes app_return_to to same-origin web-remote paths and allowed query keys", () => {
  41. expect(
  42. resolveSafeAppReturnTo(
  43. "https://zcode.z.ai/web-remote?remoteControlToken=token&relayOrigin=https%3A%2F%2Frelay.example&ignored=1",
  44. { currentOrigin: "https://zcode.z.ai" },
  45. ),
  46. ).toBe("/web-remote?remoteControlToken=token&relayOrigin=https%3A%2F%2Frelay.example");
  47. expect(
  48. resolveSafeAppReturnTo("https://evil.example/web-remote?remoteControlToken=token", {
  49. currentOrigin: "https://zcode.z.ai",
  50. }),
  51. ).toBeNull();
  52. expect(
  53. resolveSafeAppReturnTo("http://192.168.1.8:5173/web-remote?remoteControlToken=token", {
  54. currentOrigin: "http://192.168.1.8:5173",
  55. }),
  56. ).toBe("/web-remote?remoteControlToken=token");
  57. });
  58. it("accepts a same-origin share route as a safe OAuth return target", () => {
  59. expect(
  60. resolveSafeAppReturnTo("https://zcode.z.ai/cn/share/share-1", {
  61. currentOrigin: "https://zcode.z.ai",
  62. }),
  63. ).toBe("/cn/share/share-1");
  64. expect(
  65. resolveSafeAppReturnTo("https://zcode.z.ai/share/share-1", {
  66. currentOrigin: "https://zcode.z.ai",
  67. }),
  68. ).toBe("/share/share-1");
  69. expect(
  70. resolveSafeAppReturnTo("https://evil.example/cn/share/share-1", {
  71. currentOrigin: "https://zcode.z.ai",
  72. }),
  73. ).toBeNull();
  74. });
  75. it("builds a sanitized return_to callback URL for dev redirects", () => {
  76. expect(
  77. buildReturnToCallbackUrl("http://192.168.1.8:5173/web-remote/callback?old=1", {
  78. code: "code-1",
  79. state: "state-1",
  80. }),
  81. ).toBe("http://192.168.1.8:5173/web-remote/callback?code=code-1&state=state-1");
  82. expect(
  83. buildReturnToCallbackUrl("http://127.0.0.1:5173/share/callback", {
  84. code: "code-1",
  85. state: "state-1",
  86. }),
  87. ).toBe("http://127.0.0.1:5173/share/callback?code=code-1&state=state-1");
  88. expect(
  89. buildReturnToCallbackUrl("https://example.com/web-remote/callback", {
  90. error: "access_denied",
  91. state: "state-1",
  92. }),
  93. ).toBeNull();
  94. });
  95. });