File size: 9,207 Bytes
a080db2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import {afterEach, beforeEach, describe, expect, test} from "bun:test"

import {ensureOidcClient} from "./api"
import {loopbackLogin, LoginError} from "./loopback-login"
import {startFakeOidcIssuer, type FakeOidcIssuer} from "./testing/fake-oidc-issuer"
import {expectLoopbackPortsFree, freeLoopbackPorts, occupy, rawGet} from "./testing/loopback"

const SIGNED_IN = "Signed in to Mentra. You can close this tab."

let idp: FakeOidcIssuer
const releases: Array<() => Promise<void>> = []
/** The CLI's ports free when the test starts: the ones it will use, in order. */
let free: number[] = []

beforeEach(async () => {
  free = await freeLoopbackPorts()
  idp = await startFakeOidcIssuer({publicClientId: "mentra-cli"})
})

afterEach(async () => {
  for (const release of releases.splice(0)) await release()
  idp.stop()
  // No test may leave a listener on the CLI's ports.
  await expectLoopbackPortsFree(free)
})

async function oidc() {
  return ensureOidcClient({oidcIssuer: idp.issuer, cliClientId: "mentra-cli"})
}

function portOf(authorizationUrl: string): number {
  return Number(new URL(new URL(authorizationUrl).searchParams.get("redirect_uri")!).port)
}

describe("loopbackLogin", () => {
  test("loopback login redeems the code once and stores the refresh token (L09)", async () => {
    let opened = ""
    let browser: Promise<{status: number; text: string}> | undefined
    const result = await loopbackLogin(await oidc(), {
      openBrowser: async url => {
        opened = url
        browser = idp.approveInBrowser(url, {sub: "mu_1", email: "ada@example.com"})
        return true
      },
      print: () => {},
    })
    const request = new URL(opened)
    expect(request.searchParams.get("redirect_uri")).toBe(`http://127.0.0.1:${free[0]}/callback`)
    expect(request.searchParams.get("code_challenge_method")).toBe("S256")
    expect(request.searchParams.get("scope")).toBe("openid email profile offline_access")
    expect(request.searchParams.get("state")).toBeTruthy()
    expect(request.searchParams.get("nonce")).toBeTruthy()
    expect(result.refreshToken).toBeTruthy()
    expect(result.accessToken).toBeTruthy()
    expect(result.subject).toBe("mu_1")
    expect(result.email).toBe("ada@example.com")
    expect(result.expiresAt).toBeGreaterThan(Date.now() + 3_000_000)
    expect(idp.codeRedemptions).toBe(1)
    expect(await browser).toEqual({status: 200, text: SIGNED_IN})
  })

  test("each login uses fresh state, nonce and PKCE", async () => {
    const urls: string[] = []
    for (let i = 0; i < 2; i++) {
      let browser: Promise<unknown> | undefined
      await loopbackLogin(await oidc(), {
        openBrowser: async url => {
          urls.push(url)
          browser = idp.approveInBrowser(url, {sub: "mu_1"})
          return true
        },
        print: () => {},
      })
      await browser
    }
    const [first, second] = urls.map(url => new URL(url).searchParams)
    for (const name of ["state", "nonce", "code_challenge"]) expect(first!.get(name)).not.toBe(second!.get(name))
  })

  test("with its first port taken, login uses the next one", async () => {
    releases.push(await occupy(free[0]!))
    let browser: Promise<unknown> | undefined
    let opened = ""
    await loopbackLogin(await oidc(), {
      openBrowser: async url => {
        opened = url
        browser = idp.approveInBrowser(url, {sub: "mu_1"})
        return true
      },
      print: () => {},
    })
    await browser
    expect(new URL(opened).searchParams.get("redirect_uri")).toBe(`http://127.0.0.1:${free[1]}/callback`)
  })

  test("with every loopback port taken, login fails naming the ports", async () => {
    for (const port of free) releases.push(await occupy(port))
    const error = await loopbackLogin(await oidc(), {openBrowser: async () => true, print: () => {}}).catch(caught => caught)
    expect(error).toBeInstanceOf(LoginError)
    expect((error as Error).message).toContain("53682, 53683 and 53684")
    expect(idp.authorizationRequests).toHaveLength(0)
  })

  test("a callback with the wrong state is refused and the login keeps waiting for the right one", async () => {
    let browser: Promise<unknown> | undefined
    let refused: {status: number; text: string} | undefined
    const result = await loopbackLogin(await oidc(), {
      openBrowser: async url => {
        const port = portOf(url)
        refused = await rawGet(port, "/callback?code=stolen&state=not-the-state", `127.0.0.1:${port}`)
        browser = idp.approveInBrowser(url, {sub: "mu_1"})
        return true
      },
      print: () => {},
    })
    await browser
    expect(refused!.status).toBe(400)
    expect(refused!.text).not.toContain("stolen")
    expect(result.subject).toBe("mu_1")
    expect(idp.codeRedemptions).toBe(1)
  })

  test("a callback with another Host is refused and the login keeps waiting for the right one", async () => {
    let browser: Promise<unknown> | undefined
    let refused: {status: number; text: string} | undefined
    const result = await loopbackLogin(await oidc(), {
      openBrowser: async url => {
        const port = portOf(url)
        const state = new URL(url).searchParams.get("state")!
        // A page on another site (DNS rebinding) reaches the port with its own Host.
        refused = await rawGet(port, `/callback?code=forged&state=${state}`, `evil.example:${port}`)
        browser = idp.approveInBrowser(url, {sub: "mu_1"})
        return true
      },
      print: () => {},
    })
    await browser
    expect(refused!.status).toBe(400)
    expect(result.subject).toBe("mu_1")
    expect(idp.codeRedemptions).toBe(1)
  })

  test("only refused callbacks: the login times out", async () => {
    const error = await loopbackLogin(await oidc(), {
      openBrowser: async url => {
        const port = portOf(url)
        await rawGet(port, "/callback?code=x&state=wrong", `127.0.0.1:${port}`)
        await rawGet(port, "/callback?code=x&state=wrong", `localhost:${port}`)
        return true
      },
      print: () => {},
      timeoutMs: 400,
    }).catch(caught => caught)
    expect(error).toBeInstanceOf(LoginError)
    expect((error as Error).message).toContain("timed out")
    expect(idp.codeRedemptions).toBe(0)
  })

  test("an error callback with the right state ends the login with a clear message", async () => {
    let browser: Promise<{status: number; text: string}> | undefined
    const error = await loopbackLogin(await oidc(), {
      openBrowser: async url => {
        browser = idp.denyInBrowser(url, "access_denied")
        return true
      },
      print: () => {},
    }).catch(caught => caught)
    expect(error).toBeInstanceOf(LoginError)
    expect((error as Error).message).toBe("Sign-in was cancelled in the browser. Run `mentra login` to try again.")
    const page = await browser!
    expect(page.status).toBe(400)
    expect(page.text).not.toContain("access_denied")
    expect(idp.codeRedemptions).toBe(0)
  })

  test("the browser gets fixed text and never the request's data", async () => {
    const error = await loopbackLogin(await oidc(), {
      openBrowser: async url => {
        const port = portOf(url)
        const state = new URL(url).searchParams.get("state")!
        const probe = "%3Cscript%3Ealert(1)%3C%2Fscript%3E"
        for (const path of [`/callback?state=${probe}`, `/other?${probe}`, `/callback?state=${state}&error=${probe}`]) {
          const page = await rawGet(port, path, `127.0.0.1:${port}`)
          expect(page.text).not.toContain("script")
          expect(page.text).not.toContain("alert")
        }
        return true
      },
      print: () => {},
    }).catch(caught => caught)
    expect((error as Error).message).toBe("Sign-in did not complete (unknown_error). Run `mentra login` to try again.")
  })

  test("a code the issuer refuses ends the login", async () => {
    const error = await loopbackLogin(await oidc(), {
      openBrowser: async url => {
        const port = portOf(url)
        const state = new URL(url).searchParams.get("state")!
        const page = await rawGet(port, `/callback?code=unknown&state=${state}&iss=${encodeURIComponent(idp.issuer)}`, `127.0.0.1:${port}`)
        expect(page.status).toBe(400)
        return true
      },
      print: () => {},
    }).catch(caught => caught)
    expect(error).toBeInstanceOf(LoginError)
    expect((error as Error).message).toContain("invalid_grant")
  })

  test("the URL is always printed, and how to reach the port when no browser opens", async () => {
    const lines: string[] = []
    let opened = ""
    let browser: Promise<unknown> | undefined
    await loopbackLogin(await oidc(), {
      openBrowser: async url => {
        opened = url
        browser = idp.approveInBrowser(url, {sub: "mu_1"})
        return false
      },
      print: line => lines.push(line),
    })
    await browser
    expect(lines).toContain(opened)
    expect(lines.join("\n")).toContain(`ssh -L ${free[0]}:127.0.0.1:${free[0]}`)
  })
})