diff --git a/.env.example b/.env.example index 050cc5e..b74e87c 100644 --- a/.env.example +++ b/.env.example @@ -82,7 +82,10 @@ METACULUS_API_KEY= # sign in with read-only access. Register an OAuth client of type "Web # application" in Google Cloud and add the redirect URI # /api/oauth/google/callback -# (http://127.0.0.1:3000/api/oauth/google/callback for `npm run dev`). +# for each origin you open Flyspace at. Google matches it exactly, and +# localhost and 127.0.0.1 are different origins, so for `npm run dev` add both +# http://localhost:3000/api/oauth/google/callback +# http://127.0.0.1:3000/api/oauth/google/callback # Server-side only; the secret never reaches the browser. The access token # goes to the page that asked, for that session. Off in the public preview. # OMNI_OAUTH_ORIGIN pins the redirect origin when a proxy hides it. diff --git a/DEPLOYMENT.md b/DEPLOYMENT.md index d2a316d..e638be4 100644 --- a/DEPLOYMENT.md +++ b/DEPLOYMENT.md @@ -184,8 +184,11 @@ Google" for blocks that read the person's own Google data 2. Configure the OAuth consent screen. While it is in Testing, only the test users you list can sign in, and Google shows them an unverified-app notice. 3. Create an OAuth client of type "Web application" with the authorized - redirect URI `/api/oauth/google/callback`. Set `OMNI_OAUTH_ORIGIN` - when the server cannot see its public origin. + redirect URI `/api/oauth/google/callback` for every origin people + open Flyspace at. Google matches it exactly: for local use add both + `http://localhost:3000/api/oauth/google/callback` and + `http://127.0.0.1:3000/api/oauth/google/callback`. Set + `OMNI_OAUTH_ORIGIN` when the server cannot see its public origin. Flyspace only asks for scopes ending in `.readonly`, checks that the person granted them, requests no refresh token, and stores nothing on the server. The diff --git a/src/core/services/server/oauthConnect.test.ts b/src/core/services/server/oauthConnect.test.ts index 6d14407..3dafa4c 100644 --- a/src/core/services/server/oauthConnect.test.ts +++ b/src/core/services/server/oauthConnect.test.ts @@ -86,6 +86,12 @@ describe('Connect: finishing a sign-in', () => { expect(await finish({ state: `${state.slice(0, -1)}x`, cookie })).toMatchObject({ ok: false, error: 'This sign-in was not started in this browser' }); }); + it('refuses a non-ASCII state of the same length without throwing', async () => { + const { cookie, state } = started(); + const result = await finish({ state: 'é'.repeat(state.length), cookie }); + expect(result).toMatchObject({ ok: false, error: 'This sign-in was not started in this browser' }); + }); + it('refuses a flow older than ten minutes', async () => { const { cookie, state } = started(0); expect(await finish({ state, cookie, now: 10 * 60_000 + 1 })).toMatchObject({ ok: false, nonce: NONCE, error: 'This sign-in took too long; try again' }); diff --git a/src/core/services/server/oauthConnect.ts b/src/core/services/server/oauthConnect.ts index 255d1cb..d1f8ff8 100644 --- a/src/core/services/server/oauthConnect.ts +++ b/src/core/services/server/oauthConnect.ts @@ -165,7 +165,11 @@ export async function finishConnect(args: { if (!flow || flow.provider !== provider.id) return { ok: false, error: 'This sign-in was not started in this browser' }; if ((args.now ?? Date.now()) > flow.exp) return { ok: false, nonce: flow.nonce, error: 'This sign-in took too long; try again' }; const state = args.query.get('state') ?? ''; - const stateMatches = state.length === flow.state.length && timingSafeEqual(Buffer.from(state), Buffer.from(flow.state)); + // Compared as bytes: a non-ASCII state of the same length in characters + // is a different length in bytes, and timingSafeEqual throws on that. + const given = Buffer.from(state); + const expected = Buffer.from(flow.state); + const stateMatches = given.length === expected.length && timingSafeEqual(given, expected); if (!stateMatches) return { ok: false, nonce: flow.nonce, error: 'This sign-in was not started in this browser' }; if (args.query.get('error')) { return { ok: false, nonce: flow.nonce, error: args.query.get('error') === 'access_denied' ? 'Sign-in was cancelled' : 'The provider refused the sign-in' };