oauth-callback.test.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import { test, expect, describe, afterEach } from "bun:test"
  2. import { createConnection, createServer as createNetServer } from "net"
  3. import { McpOAuthCallback } from "../../src/mcp/oauth-callback"
  4. import { parseRedirectUri } from "../../src/mcp/oauth-provider"
  5. async function getFreeLoopbackPort(): Promise<number> {
  6. return new Promise((resolve, reject) => {
  7. const probe = createNetServer()
  8. probe.once("error", reject)
  9. probe.listen(0, "127.0.0.1", () => {
  10. const address = probe.address()
  11. probe.close(() => {
  12. if (typeof address === "object" && address) {
  13. resolve(address.port)
  14. return
  15. }
  16. reject(new Error("Could not allocate a loopback port"))
  17. })
  18. })
  19. })
  20. }
  21. async function canConnect(host: string, port: number): Promise<boolean> {
  22. return new Promise((resolve) => {
  23. const socket = createConnection({ host, port })
  24. const done = (ok: boolean) => {
  25. socket.removeAllListeners()
  26. socket.destroy()
  27. resolve(ok)
  28. }
  29. socket.setTimeout(500)
  30. socket.once("connect", () => done(true))
  31. socket.once("error", () => done(false))
  32. socket.once("timeout", () => done(false))
  33. })
  34. }
  35. describe("parseRedirectUri", () => {
  36. test("returns defaults when no URI provided", () => {
  37. const result = parseRedirectUri()
  38. expect(result.port).toBe(19876)
  39. expect(result.path).toBe("/mcp/oauth/callback")
  40. })
  41. test("parses port and path from URI", () => {
  42. const result = parseRedirectUri("http://127.0.0.1:8080/oauth/callback")
  43. expect(result.port).toBe(8080)
  44. expect(result.path).toBe("/oauth/callback")
  45. })
  46. test("returns defaults for invalid URI", () => {
  47. const result = parseRedirectUri("not-a-valid-url")
  48. expect(result.port).toBe(19876)
  49. expect(result.path).toBe("/mcp/oauth/callback")
  50. })
  51. })
  52. describe("McpOAuthCallback.ensureRunning", () => {
  53. afterEach(async () => {
  54. await McpOAuthCallback.stop()
  55. })
  56. test("starts server with custom redirectUri port and path", async () => {
  57. await McpOAuthCallback.ensureRunning("http://127.0.0.1:18000/custom/callback")
  58. expect(McpOAuthCallback.isRunning()).toBe(true)
  59. })
  60. test("stops after the callback completes", async () => {
  61. const redirectUri = "http://127.0.0.1:18003/custom/callback"
  62. await McpOAuthCallback.ensureRunning(redirectUri)
  63. const callback = McpOAuthCallback.waitForCallback("success")
  64. const response = await fetch(`${redirectUri}?code=code&state=success`)
  65. expect(response.status).toBe(200)
  66. expect(await callback).toBe("code")
  67. expect(McpOAuthCallback.isRunning()).toBe(false)
  68. })
  69. test("escapes provider error markup in callback HTML", async () => {
  70. const redirectUri = "http://127.0.0.1:18001/custom/callback"
  71. await McpOAuthCallback.ensureRunning(redirectUri)
  72. const error = `<script>alert("xss" & 'more')</script>`
  73. const response = await fetch(
  74. `${redirectUri}?state=test&error=access_denied&error_description=${encodeURIComponent(error)}`,
  75. )
  76. const body = await response.text()
  77. expect(response.headers.get("content-type")).toBe("text/html; charset=utf-8")
  78. expect(body).toContain("&lt;script&gt;alert(&quot;xss&quot; &amp; &#39;more&#39;)&lt;/script&gt;")
  79. expect(body).not.toContain(error)
  80. })
  81. test("keeps normal provider errors readable", async () => {
  82. const redirectUri = "http://127.0.0.1:18002/custom/callback"
  83. await McpOAuthCallback.ensureRunning(redirectUri)
  84. const response = await fetch(
  85. `${redirectUri}?state=test&error=access_denied&error_description=${encodeURIComponent("The user denied access")}`,
  86. )
  87. expect(await response.text()).toContain('<pre class="detail" id="oc-detail">The user denied access</pre>')
  88. })
  89. test("binds the callback server to IPv4 loopback", async () => {
  90. const port = await getFreeLoopbackPort()
  91. await McpOAuthCallback.ensureRunning(`http://127.0.0.1:${port}/custom/callback`)
  92. expect(await canConnect("127.0.0.1", port)).toBe(true)
  93. expect(await canConnect("::1", port)).toBe(false)
  94. })
  95. })