Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion server/middleware/01.auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,25 @@ const PUBLIC_PATHS = [
'/api/media/', // Media management API — Bearer CDN key (media:* scope)
]

// Migrate's server-to-server calls carry no session: each one is a request
// signed with Migrate's key, verified inside the route. Exact paths, never a
// prefix: `/api/migrate/claim` and `/api/migrate/grants/:id/*` are user
// routes and stay behind the session.
const MIGRATE_S2S_PATHS = [
'/api/migrate/account-state',
'/api/migrate/provision',
'/api/migrate/grants/status',
'/api/migrate/grants/install-url',
]

// Refresh tokens 5 minutes before expiry to avoid edge-case failures
const REFRESH_BUFFER_SECONDS = 5 * 60

export default defineEventHandler(async (event) => {
const path = getRequestPath(event)

// Skip non-API routes and public paths
if (!path.startsWith('/api') || PUBLIC_PATHS.some(p => path.startsWith(p)))
if (!path.startsWith('/api') || PUBLIC_PATHS.some(p => path.startsWith(p)) || MIGRATE_S2S_PATHS.includes(path))
return

let sessionData
Expand Down
23 changes: 23 additions & 0 deletions tests/unit/auth-middleware-public-paths.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,29 @@ describe('auth middleware public paths', () => {
expect(getServerSession).not.toHaveBeenCalled()
})

// Migrate's signed server-to-server calls: no session, the route verifies the signature.
it.each([
'/api/migrate/account-state',
'/api/migrate/provision',
'/api/migrate/grants/status',
'/api/migrate/grants/install-url',
])('lets Migrate\'s signed server-to-server call %s through without a session lookup', async (path) => {
await expect(run(path)).resolves.toBeUndefined()
expect(getServerSession).not.toHaveBeenCalled()
})

// The allowlist is exact paths: the user-facing Migrate routes keep their session.
it.each([
'/api/migrate/claim',
'/api/migrate/grants/grant-1',
'/api/migrate/grants/grant-1/checkout',
'/api/migrate/grants/status/extra',
'/api/migrate/account-state/extra',
])('still 401s the user-facing Migrate route %s without a session', async (path) => {
getServerSession.mockResolvedValue(null)
await expect(run(path)).rejects.toMatchObject({ statusCode: 401 })
})

it('still 401s a protected API path when there is no session', async () => {
getServerSession.mockResolvedValue(null)
await expect(run('/api/workspaces/w1/projects')).rejects.toMatchObject({ statusCode: 401 })
Expand Down
Loading