Deploy on DigitalOcean App Platform (GitHub-source build) + consolidate audit-fixes
Deploy config: - .do/app.yaml: build the Dockerfile directly from GitHub (deploy_on_push) instead of a pre-built DOCR image; NEXT_PUBLIC_* set RUN_AND_BUILD_TIME with the propertymanagement.network domain so they bake into the client bundle; add custom domains block (apex + www); wire Sentry DSN (server + browser). Included pending work from the audit-fixes branch: - AI provider abstraction (OpenAI/Anthropic, admin-selectable; Anthropic default) - Per-landlord e-signature (DocuSign OAuth + Dropbox Sign) + migration 0010 - Outbound webhooks / Zapier integration - PayPal removal (Stripe-only billing) - Storage hardening (fail-loud when Spaces unconfigured), security fixes Verified: full production Docker build (same build-args as DO) passes clean. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.8
parent
917a06ee85
commit
5495b94924
@@ -1,37 +1,51 @@
|
||||
import { NextResponse } from "next/server"
|
||||
import { cookies } from "next/headers"
|
||||
import { getSessionUser } from "@/lib/session"
|
||||
import { getAccountContext } from "@/lib/account"
|
||||
import { getProvider, saveConnection, type Provider } from "@/lib/accounting"
|
||||
import { verifyState } from "@/lib/accounting/state"
|
||||
import { verifyState, OAUTH_NONCE_COOKIE } from "@/lib/accounting/state"
|
||||
|
||||
// OAuth callback — exchanges the code for tokens and stores the connection.
|
||||
export async function GET(request: Request, { params }: { params: Promise<{ provider: string }> }) {
|
||||
const { provider: pid } = await params
|
||||
const prov = getProvider(pid)
|
||||
const url = new URL(request.url)
|
||||
const settings = new URL("/settings/integrations", request.url)
|
||||
|
||||
// Always clear the one-shot nonce cookie on the way out.
|
||||
const cookieStore = await cookies()
|
||||
const nonceCookie = cookieStore.get(OAUTH_NONCE_COOKIE)?.value
|
||||
const done = (params: Record<string, string>) => {
|
||||
for (const [k, v] of Object.entries(params)) settings.searchParams.set(k, v)
|
||||
const res = NextResponse.redirect(settings)
|
||||
res.cookies.set(OAUTH_NONCE_COOKIE, "", { path: "/", maxAge: 0 })
|
||||
return res
|
||||
}
|
||||
|
||||
const url = new URL(request.url)
|
||||
const code = url.searchParams.get("code")
|
||||
const state = url.searchParams.get("state")
|
||||
const realmId = url.searchParams.get("realmId") // QuickBooks includes this
|
||||
const oauthError = url.searchParams.get("error")
|
||||
|
||||
if (oauthError || !prov) {
|
||||
settings.searchParams.set("error", "connect_failed")
|
||||
return NextResponse.redirect(settings)
|
||||
}
|
||||
if (oauthError || !prov) return done({ error: "connect_failed" })
|
||||
|
||||
const st = state ? verifyState(state) : null
|
||||
if (!code || !st || st.provider !== pid) {
|
||||
settings.searchParams.set("error", "invalid_state")
|
||||
return NextResponse.redirect(settings)
|
||||
// CSRF: the state's nonce must match the cookie set at connect time, and the
|
||||
// current session must be the same owner that initiated the connect.
|
||||
if (!code || !st || st.provider !== pid || !nonceCookie || nonceCookie !== st.nonce) {
|
||||
return done({ error: "invalid_state" })
|
||||
}
|
||||
const user = await getSessionUser()
|
||||
if (!user) return done({ error: "invalid_state" })
|
||||
const ctx = await getAccountContext(user.id)
|
||||
if (ctx.ownerId !== st.ownerId) return done({ error: "invalid_state" })
|
||||
|
||||
try {
|
||||
const tokens = await prov.exchangeCode(code, realmId)
|
||||
if (!tokens.realmId) throw new Error("No organisation returned from provider")
|
||||
await saveConnection(st.ownerId, pid as Provider, tokens)
|
||||
settings.searchParams.set("connected", pid)
|
||||
return done({ connected: pid })
|
||||
} catch {
|
||||
settings.searchParams.set("error", "connect_failed")
|
||||
return done({ error: "connect_failed" })
|
||||
}
|
||||
return NextResponse.redirect(settings)
|
||||
}
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
import crypto from "crypto"
|
||||
import { NextResponse } from "next/server"
|
||||
import { getSessionUser } from "@/lib/session"
|
||||
import { getAccountContext } from "@/lib/account"
|
||||
import { getProvider } from "@/lib/accounting"
|
||||
import { signState } from "@/lib/accounting/state"
|
||||
import { signState, OAUTH_NONCE_COOKIE } from "@/lib/accounting/state"
|
||||
|
||||
// Starts the OAuth connect flow for an accounting provider (owner-only).
|
||||
export async function GET(request: Request, { params }: { params: Promise<{ provider: string }> }) {
|
||||
@@ -28,6 +29,17 @@ export async function GET(request: Request, { params }: { params: Promise<{ prov
|
||||
return NextResponse.redirect(settings)
|
||||
}
|
||||
|
||||
const state = signState({ ownerId: ctx.ownerId, provider: pid })
|
||||
return NextResponse.redirect(prov.getAuthUrl(state))
|
||||
// Bind the OAuth round-trip to this browser: a random nonce goes into the
|
||||
// signed state AND an httpOnly cookie; the callback requires them to match.
|
||||
const nonce = crypto.randomUUID()
|
||||
const state = signState({ ownerId: ctx.ownerId, provider: pid, nonce })
|
||||
const res = NextResponse.redirect(prov.getAuthUrl(state))
|
||||
res.cookies.set(OAUTH_NONCE_COOKIE, nonce, {
|
||||
httpOnly: true,
|
||||
secure: process.env.NODE_ENV === "production",
|
||||
sameSite: "lax",
|
||||
path: "/",
|
||||
maxAge: 600,
|
||||
})
|
||||
return res
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user