Security hardening from 2026-07-01 audit
- Exclude supabase/ from Docker build context (leaked service_role key file) - /api/files: exact per-user namespace match + reject path traversal; storage resolveKey rejects ".."/"." segments (fixes cross-user file read) - Add ownsProperty/Unit/Tenant checks to tenants, maintenance (landlord path), and documents (JSON branch, now field-whitelisted) create handlers - Escape user data in follow-up + payment-link emails (reuse escapeHtml) - Neutralize CSV formula injection in toCsv + export routes - Tighter sign-in rate limit (10/min); env-gated email verification + sender - Per-request nonce CSP; drop script-src 'unsafe-inline' (styles unchanged) - Add input length bounds; validate follow-ups POST body Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.8
parent
857b9a7811
commit
969d5d4c8a
@@ -4,6 +4,7 @@ import { db } from "@/lib/db"
|
||||
import { documents, properties } from "@/lib/db/schema"
|
||||
import { getSessionUser } from "@/lib/session"
|
||||
import { saveFile } from "@/lib/storage"
|
||||
import { ownsProperty, ownsTenant } from "@/lib/db/ownership"
|
||||
|
||||
export async function GET(request: Request) {
|
||||
const user = await getSessionUser()
|
||||
@@ -76,9 +77,31 @@ export async function POST(request: Request) {
|
||||
|
||||
// JSON fallback (metadata only)
|
||||
const body = (await request.json()) as Record<string, unknown>
|
||||
const propertyId = body.property_id as string | undefined
|
||||
const tenantId = body.tenant_id as string | undefined
|
||||
|
||||
// Verify the property/tenant belong to the user before attaching a document.
|
||||
if (!(await ownsProperty(user.id, propertyId))) {
|
||||
return NextResponse.json({ error: "Property not found" }, { status: 404 })
|
||||
}
|
||||
if (!(await ownsTenant(user.id, tenantId))) {
|
||||
return NextResponse.json({ error: "Tenant not found" }, { status: 404 })
|
||||
}
|
||||
|
||||
// Whitelist insertable columns — never trust client-supplied user_id/id/created_at.
|
||||
const [data] = await db
|
||||
.insert(documents)
|
||||
.values({ ...(body as typeof documents.$inferInsert), user_id: user.id })
|
||||
.values({
|
||||
user_id: user.id,
|
||||
property_id: propertyId as string,
|
||||
tenant_id: tenantId,
|
||||
name: body.name as string,
|
||||
category: (body.category as typeof documents.$inferInsert.category) ?? "other",
|
||||
file_url: body.file_url as string,
|
||||
storage_path: body.storage_path as string | undefined,
|
||||
file_type: body.file_type as string | undefined,
|
||||
file_size: body.file_size as number | undefined,
|
||||
})
|
||||
.returning()
|
||||
|
||||
return NextResponse.json(data, { status: 201 })
|
||||
|
||||
Reference in New Issue
Block a user