mirror of
https://github.com/pezkuwichain/pwap.git
synced 2026-08-12 20:51:37 +00:00
0fe8c85dfe
two-factor-auth and notifications-manager run with the service role, so they
bypass RLS and whatever user id they act on is the entire authorisation
decision. Both read that id from the request body, and neither checked who was
calling. Access-Control-Allow-Origin was '*' on both.
Confirmed against the live endpoint: with only the public anon key and no user
session, from an arbitrary origin,
POST /functions/v1/two-factor-auth {"action":"check","userId":"<any-uuid>"}
-> 200 {"success":true,"enabled":false}
VERIFY_JWT is on and does not stop this, because the anon key is itself a valid
JWT and every browser has it. The same path reaches "disable", so anyone could
turn off another account's 2FA, and read, delete or forge their notifications.
The client already sends the signed-in user's token — functions.invoke puts the
session access token in the Authorization header — so the caller can simply be
read from it. Both functions now do that and ignore the body's userId. An
anon-key call carries no user, so it is rejected with 401.
create is the one action that may target someone else, because AdminPanel
notifies other users; that path now requires admin or super_admin in
admin_roles. CORS drops to the same allowlist the other functions here use.
No frontend change: the body may keep sending userId, it is simply not read.
Note: 2FA is not yet enforced at login — TwoFactorVerify is not referenced
anywhere and Login.tsx never checks it, so enrolling does not currently protect
sign-in. Tracked separately; this change is about the endpoints themselves.
147 lines
4.2 KiB
TypeScript
147 lines
4.2 KiB
TypeScript
import {
|
|
forbidden,
|
|
getCaller,
|
|
getCorsHeaders,
|
|
isAdmin,
|
|
unauthorized,
|
|
} from "../_shared/caller-auth.ts";
|
|
|
|
Deno.serve(async (req) => {
|
|
const corsHeaders = getCorsHeaders(req.headers.get("origin"));
|
|
|
|
if (req.method === "OPTIONS") {
|
|
return new Response("ok", { headers: corsHeaders });
|
|
}
|
|
|
|
try {
|
|
const {
|
|
action,
|
|
userId: requestedUserId,
|
|
notificationId,
|
|
title,
|
|
message,
|
|
type,
|
|
actionUrl,
|
|
notificationIds,
|
|
} = await req.json();
|
|
|
|
// Import Supabase client
|
|
const { createClient } =
|
|
await import("https://esm.sh/@supabase/supabase-js@2");
|
|
const supabaseUrl = Deno.env.get("SUPABASE_URL")!;
|
|
const supabaseServiceKey = Deno.env.get("SUPABASE_SERVICE_ROLE_KEY")!;
|
|
const supabase = createClient(supabaseUrl, supabaseServiceKey);
|
|
|
|
// Read the account from the caller's token, not the body. Reading it from
|
|
// the body let an anon-key request list, delete or forge anyone's
|
|
// notifications.
|
|
const caller = await getCaller(req, supabase);
|
|
if (!caller) return unauthorized(corsHeaders);
|
|
|
|
// AdminPanel legitimately notifies other users, so create is the one action
|
|
// allowed to target someone else - and only for an admin.
|
|
let userId = caller.id;
|
|
if (action === "create" && requestedUserId && requestedUserId !== caller.id) {
|
|
if (!(await isAdmin(supabase, caller.id))) {
|
|
return forbidden(corsHeaders, "Only admins can notify other users");
|
|
}
|
|
userId = requestedUserId;
|
|
}
|
|
|
|
let result;
|
|
|
|
switch (action) {
|
|
case "create":
|
|
// Create notification
|
|
const { data: notification, error: createError } = await supabase
|
|
.from("notifications")
|
|
.insert({
|
|
user_id: userId,
|
|
title,
|
|
message,
|
|
type: type || "info",
|
|
action_url: actionUrl,
|
|
})
|
|
.select()
|
|
.single();
|
|
|
|
if (createError) throw createError;
|
|
result = { success: true, notification };
|
|
break;
|
|
|
|
case "markRead":
|
|
// Mark notification as read
|
|
const { error: readError } = await supabase
|
|
.from("notifications")
|
|
.update({ read: true, read_at: new Date().toISOString() })
|
|
.eq("id", notificationId)
|
|
.eq("user_id", userId);
|
|
|
|
if (readError) throw readError;
|
|
result = { success: true };
|
|
break;
|
|
|
|
case "markAllRead":
|
|
// Mark all notifications as read
|
|
const { error: allReadError } = await supabase
|
|
.from("notifications")
|
|
.update({ read: true, read_at: new Date().toISOString() })
|
|
.eq("user_id", userId)
|
|
.eq("read", false);
|
|
|
|
if (allReadError) throw allReadError;
|
|
result = { success: true };
|
|
break;
|
|
|
|
case "delete":
|
|
// Delete notification
|
|
const { error: deleteError } = await supabase
|
|
.from("notifications")
|
|
.delete()
|
|
.eq("id", notificationId)
|
|
.eq("user_id", userId);
|
|
|
|
if (deleteError) throw deleteError;
|
|
result = { success: true };
|
|
break;
|
|
|
|
case "deleteMultiple":
|
|
// Delete multiple notifications
|
|
const { error: deleteMultipleError } = await supabase
|
|
.from("notifications")
|
|
.delete()
|
|
.in("id", notificationIds)
|
|
.eq("user_id", userId);
|
|
|
|
if (deleteMultipleError) throw deleteMultipleError;
|
|
result = { success: true };
|
|
break;
|
|
|
|
case "getUnreadCount":
|
|
// Get unread notification count
|
|
const { count, error: countError } = await supabase
|
|
.from("notifications")
|
|
.select("*", { count: "exact", head: true })
|
|
.eq("user_id", userId)
|
|
.eq("read", false);
|
|
|
|
if (countError) throw countError;
|
|
result = { success: true, count };
|
|
break;
|
|
|
|
default:
|
|
throw new Error("Invalid action");
|
|
}
|
|
|
|
return new Response(JSON.stringify(result), {
|
|
headers: { ...corsHeaders, "Content-Type": "application/json" },
|
|
status: 200,
|
|
});
|
|
} catch (error) {
|
|
return new Response(JSON.stringify({ error: error.message }), {
|
|
headers: { ...corsHeaders, "Content-Type": "application/json" },
|
|
status: 400,
|
|
});
|
|
}
|
|
});
|