railyard/apps/ritgram/web/middleware.ts
caelum0x e5dafe826b fix(security): stop leaking email/password-hash/2FA-secret/github-token from Ritgram APIs
Three unauthenticated/cross-user PII leaks found and fixed before PH launch:
- GET /api/v1/accounts/:username (fully public, no auth middleware) returned
  the full User row including bcrypt password hash, twoFactorSecret and
  githubAccessToken via an unscoped `user: true` Prisma include.
- GET /api/v1/users/:username leaked any user's real email to unauthenticated
  callers; email is now only attached when viewing your own profile.
- GET /api/v1/auth/me returned the caller's own password hash/2FA
  secret/github token in the response body.
- POST /api/v1/stories/:id/reply (storyReactionsController) returned the
  story owner's full User row to any other authenticated replier.
- POST /api/switch/account could return a shared account's full User row
  (incl. secrets) to any repo manager/org member switching into it.

Also:
- backend/src/index.ts: app.set('trust proxy', 1) — every rate limiter keys
  on req.ip, which without this always resolved to Caddy's internal IP, so
  limits were shared across all visitors combined instead of per-visitor.
- web/middleware.ts: /explore added to publicPaths for the PH launch (its
  page component has no client-side auth redirect, unlike /feed and
  /trending which hardcode router.push('/login') and were left alone).
- web/components/StoriesRing.tsx: fixed a double-prefixed, nonexistent
  /api/v1/stories/feed call to the real /stories endpoint.
2026-11-17 17:27:11 +03:00

119 lines
3.3 KiB
TypeScript

import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
import {
buildRepoPath,
buildRepoTextRawPath,
buildRepoTextPath,
isReservedRepoAliasSegment,
} from './utils/repositoryRouting'
// Public routes that don't require authentication
const publicPaths = [
'/',
'/login',
'/register',
'/forgot-password',
'/reset-password',
'/pricing',
'/terms',
'/privacy-policy',
'/auth/github/callback',
'/auth/google/callback',
'/auth/rithub/callback',
'/embed',
'/docs',
'/import-from-github',
// PH launch (2026-11-17): public read-only. `/explore`'s own page
// component (app/explore/page.tsx) has no client-side auth redirect — it
// already renders trending repos for anonymous visitors — so this was
// purely a middleware gate with no matching page-level reason to keep it.
// `/feed` and `/trending` were NOT added here: both hardcode
// `router.push('/login')` when `!isAuthenticated`, so unlike `/explore`
// that's a real feature change, not a small/safe one — left as-is.
'/explore',
]
export function middleware(request: NextRequest) {
const { pathname } = request.nextUrl
const segments = pathname.split('/').filter(Boolean)
// Allow public paths
if (publicPaths.some(path => pathname === path || pathname.startsWith(path + '/'))) {
return NextResponse.next()
}
// Allow static files and API routes
if (
pathname.startsWith('/_next') ||
pathname.startsWith('/api') ||
pathname.includes('.')
) {
return NextResponse.next()
}
if (
segments.length === 5 &&
segments[3] === 'text' &&
segments[4] === 'raw' &&
(segments[0] === 'repo' || segments[0] === 'project' || segments[0] === 'github')
) {
const url = request.nextUrl.clone()
url.pathname = buildRepoTextRawPath(segments[1], segments[2])
return NextResponse.redirect(url)
}
if (
segments.length === 4 &&
segments[3] === 'text' &&
(segments[0] === 'repo' || segments[0] === 'project' || segments[0] === 'github')
) {
const url = request.nextUrl.clone()
url.pathname = buildRepoTextPath(segments[1], segments[2])
return NextResponse.redirect(url)
}
if (
segments.length === 3 &&
(segments[0] === 'repo' || segments[0] === 'project' || segments[0] === 'github')
) {
const destination = buildRepoPath(segments[1], segments[2])
const url = request.nextUrl.clone()
url.pathname = destination
return NextResponse.redirect(url)
}
if (
segments.length === 4 &&
segments[2] === 'text' &&
segments[3] === 'raw' &&
!isReservedRepoAliasSegment(segments[0])
) {
return NextResponse.next()
}
if (
segments.length === 3 &&
segments[2] === 'text' &&
!isReservedRepoAliasSegment(segments[0])
) {
return NextResponse.next()
}
if (segments.length === 2 && !isReservedRepoAliasSegment(segments[0])) {
return NextResponse.next()
}
// For protected routes, check if user has a token cookie
// Note: Gitgram uses localStorage for auth, but we set a lightweight
// cookie indicator on login to enable server-side redirect.
// If no cookie, the client-side auth check in each page will handle redirect.
return NextResponse.next()
}
export const config = {
matcher: [
'/((?!_next/static|_next/image|favicon.ico).*)',
],
}