Skip to content

Commit 2ae773c

Browse files
committed
chore: Apply technical audit fixes (middleware, inventory locking, security headers)
1 parent 14eb0e4 commit 2ae773c

File tree

5 files changed

+47
-11
lines changed

5 files changed

+47
-11
lines changed

MASTER.sql

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2429,17 +2429,19 @@ VALUES (
24292429
(item_record->>'quantity')::integer,
24302430
(item_record->>'price')::numeric
24312431
);
2432-
-- b. Deduct product stock
2433-
UPDATE public.products
2434-
SET stock = GREATEST(0, stock - (item_record->>'quantity')::integer)
2435-
WHERE id = (item_record->>'product_id')::uuid;
2436-
-- c. Deduct product_variant stock
2437-
IF (item_record->>'variant_id') IS NOT NULL
2438-
AND (item_record->>'variant_id') != '' THEN
2439-
UPDATE public.product_variants
2440-
SET stock = GREATEST(0, stock - (item_record->>'quantity')::integer)
2441-
WHERE id = (item_record->>'variant_id')::uuid;
2442-
END IF;
2432+
-- b. Deduct product stock (Atomic lock)
2433+
PERFORM id FROM public.products WHERE id = (item_record->>'product_id')::uuid FOR UPDATE;
2434+
UPDATE public.products
2435+
SET stock = GREATEST(0, stock - (item_record->>'quantity')::integer)
2436+
WHERE id = (item_record->>'product_id')::uuid;
2437+
-- c. Deduct product_variant stock (Atomic lock)
2438+
IF (item_record->>'variant_id') IS NOT NULL
2439+
AND (item_record->>'variant_id') != '' THEN
2440+
PERFORM id FROM public.product_variants WHERE id = (item_record->>'variant_id')::uuid FOR UPDATE;
2441+
UPDATE public.product_variants
2442+
SET stock = GREATEST(0, stock - (item_record->>'quantity')::integer)
2443+
WHERE id = (item_record->>'variant_id')::uuid;
2444+
END IF;
24432445
END LOOP;
24442446
END IF;
24452447
RETURN v_order_id;

lib/supabase/admin.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import 'server-only';
12
import { createClient as createSupabaseClient } from "@supabase/supabase-js";
23

34
const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL;

next.config.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,31 @@ const nextConfig: NextConfig = {
2121
compiler: {
2222
removeConsole: process.env.NODE_ENV === 'production' ? { exclude: ['error', 'warn'] } : false,
2323
},
24+
async headers() {
25+
return [
26+
{
27+
source: '/(.*)',
28+
headers: [
29+
{
30+
key: 'X-Frame-Options',
31+
value: 'DENY',
32+
},
33+
{
34+
key: 'X-Content-Type-Options',
35+
value: 'nosniff',
36+
},
37+
{
38+
key: 'Referrer-Policy',
39+
value: 'strict-origin-when-cross-origin',
40+
},
41+
{
42+
key: 'Permissions-Policy',
43+
value: 'camera=(), microphone=(), geolocation=(), interest-cohort=()',
44+
},
45+
],
46+
},
47+
]
48+
},
2449
}
2550

2651
export default nextConfig

package-lock.json

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
"react-dropzone": "^14.3.5",
4444
"react-icons": "^5.5.0",
4545
"resend": "^6.9.3",
46+
"server-only": "^0.0.1",
4647
"shippo": "^2.18.0",
4748
"sonner": "^2.0.7",
4849
"stripe": "^20.4.1",

0 commit comments

Comments
 (0)