mirror of
https://github.com/bitbaum/orangecat.git
synced 2026-12-04 19:07:35 +01:00
fix(entities): Publish Now no longer 404s — server route used browser client
ROOT CAUSE for the Publish Now button being broken across every entity
since the endpoint shipped:
/api/entities/{type}/{id}/status PATCH
→ withAuth (server supabase client, authed) → fetch the row → OK
→ checkOwnership(existing, user.id) — imported from @/services/actors
→ checkOwnership uses `import supabase from '@/lib/supabase/browser'`
→ browser client in serverless context = no cookies, no auth, may not
even resolve a working fetch → returns null actor → false access
→ apiNotFound("X not found") → 404 toast for the user
Confirmed via the diagnostic logging shipped in 410e8439: supabase API
logs show the row fetch returning 200, so the row exists and RLS
allows it. The 404 originates AFTER the fetch — in the ownership
branch. The browser-client-in-server-context mismatch is the
mechanical cause.
Fix: inline the ownership check in the route using the already-authed
server supabase client passed in by withAuth. Replicates the same
logic checkOwnership uses (actor → user_id match, or group_member
check for group-owned entities) but with the right client. Removes
the import of checkOwnership from this route.
User impact: every entity's Publish Now button works again. Affects
projects, products, services, causes, events, loans, investments,
assets, ai_assistants, research, wishlists — the whole entity create
→ publish flow that's been silently 404'ing for ~3 months.
Side effect bonus: the "ownership denied" branch now logs (so we can
spot a real RLS mismatch in future), and the fetch branches keep the
existing structured logging from 410e8439.
`checkOwnership` itself still has the browser-client bug — any other
caller hitting it from a server context will silently fail. That's a
separate fix (audit + migrate to a per-context client). Out of scope
for the immediate Publish Now restore.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
35f3312f0b
commit
136f65acbe
1 file changed
+41
-5
@@ -28,8 +28,8 @@ import {
|
||||
type EntityType,
|
||||
} from '@/config/entity-registry';
|
||||
import { validateUUID, getValidationError } from '@/lib/api/validation';
|
||||
import { checkOwnership } from '@/services/actors';
|
||||
import { ENTITY_STATUS } from '@/config/database-constants';
|
||||
import { DATABASE_TABLES } from '@/config/database-tables';
|
||||
import { z } from 'zod';
|
||||
import {
|
||||
CLIENT_STATUS_INTENTS,
|
||||
@@ -122,11 +122,47 @@ export const PATCH = withAuth(async (request: AuthenticatedRequest, context: Rou
|
||||
return apiNotFound(`${entityType} not found`);
|
||||
}
|
||||
|
||||
const hasAccess =
|
||||
userIdField === 'actor_id'
|
||||
? await checkOwnership(existing as { actor_id: string }, user.id)
|
||||
: existing[userIdField] === user.id;
|
||||
// Ownership check. Previously delegated to `checkOwnership` from
|
||||
// @/services/actors, which imports the BROWSER supabase client (no
|
||||
// cookies, no auth) — when called from a server route, its actor
|
||||
// fetch silently returns null in serverless context, hasAccess is
|
||||
// false, and every Publish Now click 404s. That has been the bug
|
||||
// since the endpoint shipped. Inline the check here using the
|
||||
// already-authed server supabase client so the same code path that
|
||||
// works for the fetch above works for ownership too.
|
||||
let hasAccess = false;
|
||||
if (userIdField === 'actor_id') {
|
||||
const actorId = (existing as { actor_id?: string }).actor_id;
|
||||
if (actorId) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const { data: actor } = await (supabase.from(DATABASE_TABLES.ACTORS) as any)
|
||||
.select('user_id, actor_type, group_id')
|
||||
.eq('id', actorId)
|
||||
.maybeSingle();
|
||||
if (actor) {
|
||||
if (actor.actor_type === 'user') {
|
||||
hasAccess = actor.user_id === user.id;
|
||||
} else if (actor.actor_type === 'group' && actor.group_id) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const { data: membership } = await (supabase.from(DATABASE_TABLES.GROUP_MEMBERS) as any)
|
||||
.select('role')
|
||||
.eq('group_id', actor.group_id)
|
||||
.eq('user_id', user.id)
|
||||
.maybeSingle();
|
||||
hasAccess = !!membership;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
hasAccess = existing[userIdField] === user.id;
|
||||
}
|
||||
if (!hasAccess) {
|
||||
logger.warn('Entity status: ownership denied', {
|
||||
entityType,
|
||||
id,
|
||||
userId: user.id,
|
||||
actorId: (existing as { actor_id?: string }).actor_id,
|
||||
});
|
||||
return apiNotFound(`${entityType} not found`);
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user