From cfcf6ced33c77e1e41f05b1f31cefc3435df0fd2 Mon Sep 17 00:00:00 2001 From: Daniel Naab Date: Thu, 3 Dec 2026 17:50:32 -0600 Subject: [PATCH] fix(auth): link access-request notifications to the right deployment The Slack notification for access requests hardcoded https://forms.labs.flexion.us/admin/users, which 404s: the app is served under a per-branch prefix (/main/...) and access lists are per-deployment. Build the link from the request origin plus the base path, and name the deployment in the title and details. Pot-Run: 9e13 --- src/entrypoints/app/routes/auth/index.tsx | 12 +++--- test/auth-routes.test.ts | 45 ++++++++++++++++++++++- 2 files changed, 51 insertions(+), 6 deletions(-) diff --git a/src/entrypoints/app/routes/auth/index.tsx b/src/entrypoints/app/routes/auth/index.tsx index 883b0998..01fc00fd 100644 --- a/src/entrypoints/app/routes/auth/index.tsx +++ b/src/entrypoints/app/routes/auth/index.tsx @@ -12,7 +12,7 @@ import { fetchUserProfile, hasAllowedEmailDomain, } from '../../../../services/auth' -import { resolveUrl } from '../../../../shared/base-path' +import { getBasePath, resolveUrl } from '../../../../shared/base-path' import { AccessDeniedPage, AccessPendingPage, @@ -254,15 +254,17 @@ export function createAuthRoutes( } accessStore.requestAccess(user.login) - // Fire notification (best-effort) + // Fire notification (best-effort). Access lists are per-deployment, so + // name the branch and link to that deployment's admin page. try { const { notifyEvent } = await import('../../../../services/notifications') + const deployment = getBasePath().replace(/^\/|\/$/g, '') || 'root' await notifyEvent({ type: 'access.requested', - title: `Access requested by @${user.login}`, + title: `Access requested by @${user.login} on ${deployment}`, status: 'info', - details: `${user.name} (${user.login}) requested access to Forms Lab`, - url: 'https://forms.labs.flexion.us/admin/users', + details: `${user.name} (${user.login}) requested access to Forms Lab (deployment: ${deployment})`, + url: `${getExternalOrigin(c)}${resolveUrl('/admin/users')}`, }) } catch { // Notification failure should not block the request diff --git a/test/auth-routes.test.ts b/test/auth-routes.test.ts index 54cbba21..2b3ae6ca 100644 --- a/test/auth-routes.test.ts +++ b/test/auth-routes.test.ts @@ -3,7 +3,7 @@ import { Hono } from 'hono' import { sessionReader } from '../src/entrypoints/app/middleware/auth' import { createAuthRoutes } from '../src/entrypoints/app/routes/auth' import type { AccessStore, GitHubUser, UserStore } from '../src/services/auth' -import { COOKIE_NAME } from '../src/services/auth' +import { COOKIE_NAME, encryptSession } from '../src/services/auth' describe('Auth Routes', () => { let app: Hono @@ -247,4 +247,47 @@ describe('Auth Routes', () => { expect(cookie).toContain('Max-Age=0') }) }) + + describe('POST /auth/request-access', () => { + async function postRequestAccess(): Promise> { + let notified: Record | undefined + global.fetch = mock(async (_url: string | URL, init?: RequestInit) => { + notified = JSON.parse(String(init?.body)) + return new Response('{}', { status: 200 }) + }) as unknown as typeof fetch + + const cookie = await encryptSession( + { login: 'outsider', name: 'Out Sider', avatarUrl: '' }, + process.env.SESSION_SECRET as string, + ) + const res = await app.request('/auth/request-access', { + method: 'POST', + headers: { + Cookie: `${COOKIE_NAME}=${cookie}`, + 'x-forwarded-proto': 'https', + 'x-forwarded-host': 'forms.example.test', + }, + }) + expect(res.status).toBe(302) + expect(mockAccessStore.requestAccess).toHaveBeenCalledWith('outsider') + if (!notified) throw new Error('no notification sent') + return notified + } + + it('links to the admin page of the branch the request came from', async () => { + process.env.BASE_PATH = '/story-7-thing' + const event = await postRequestAccess() + expect(event.url).toBe( + 'https://forms.example.test/story-7-thing/admin/users', + ) + expect(String(event.title)).toContain('story-7-thing') + expect(String(event.details)).toContain('story-7-thing') + }) + + it('links to the root admin page when there is no base path', async () => { + delete process.env.BASE_PATH + const event = await postRequestAccess() + expect(event.url).toBe('https://forms.example.test/admin/users') + }) + }) })