mirror of
https://github.com/christianvidalwolf-prog/Craze-Data-check.git
synced 2026-08-03 12:25:23 +02:00
Security: Restrict API endpoints to allowed origin (CORS)
Both Vercel serverless functions now enforce CORS, returning 403 for requests from any origin other than craze-data-check.vercel.app. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 4.6
parent
dec24f52f4
commit
2e4ab35f37
+28
-12
@@ -2,6 +2,18 @@ const DROPBOX_APP_KEY = process.env.DROPBOX_APP_KEY;
|
|||||||
const DROPBOX_APP_SECRET = process.env.DROPBOX_APP_SECRET;
|
const DROPBOX_APP_SECRET = process.env.DROPBOX_APP_SECRET;
|
||||||
const DROPBOX_REFRESH_TOKEN = process.env.DROPBOX_REFRESH_TOKEN;
|
const DROPBOX_REFRESH_TOKEN = process.env.DROPBOX_REFRESH_TOKEN;
|
||||||
|
|
||||||
|
const ALLOWED_ORIGIN = 'https://craze-data-check.vercel.app';
|
||||||
|
|
||||||
|
function setCors(req, res) {
|
||||||
|
const origin = req.headers.origin;
|
||||||
|
if (origin === ALLOWED_ORIGIN) {
|
||||||
|
res.setHeader('Access-Control-Allow-Origin', ALLOWED_ORIGIN);
|
||||||
|
}
|
||||||
|
res.setHeader('Access-Control-Allow-Methods', 'GET, OPTIONS');
|
||||||
|
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
|
||||||
|
res.setHeader('Vary', 'Origin');
|
||||||
|
}
|
||||||
|
|
||||||
async function getAccessToken() {
|
async function getAccessToken() {
|
||||||
const response = await fetch('https://api.dropboxapi.com/oauth2/token', {
|
const response = await fetch('https://api.dropboxapi.com/oauth2/token', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
@@ -21,24 +33,28 @@ async function getAccessToken() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export default async function handler(req, res) {
|
export default async function handler(req, res) {
|
||||||
let accessToken;
|
setCors(req, res);
|
||||||
|
|
||||||
|
if (req.method === 'OPTIONS') {
|
||||||
|
return res.status(204).end();
|
||||||
|
}
|
||||||
|
|
||||||
|
const origin = req.headers.origin;
|
||||||
|
if (origin && origin !== ALLOWED_ORIGIN) {
|
||||||
|
return res.status(403).json({ error: 'Forbidden' });
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
accessToken = await getAccessToken();
|
await getAccessToken();
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.warn('Token refresh optional failure (sharing link might still work):', err.message);
|
console.warn('Token refresh optional failure (sharing link might still work):', err.message);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (req.method === 'GET' && req.query.info === '1') {
|
if (req.method === 'GET' && req.query.info === '1') {
|
||||||
try {
|
res.setHeader('Cache-Control', 'no-store, no-cache, must-revalidate, proxy-revalidate');
|
||||||
// In this mode, we'll just return a placeholder rev since we're using a sharing link now
|
res.setHeader('Pragma', 'no-cache');
|
||||||
// This satisfies the frontend check without needing a specific API path
|
res.setHeader('Expires', '0');
|
||||||
res.setHeader('Cache-Control', 'no-store, no-cache, must-revalidate, proxy-revalidate');
|
return res.json({ rev: 'new-url-v1', size: 0, server_modified: new Date().toISOString() });
|
||||||
res.setHeader('Pragma', 'no-cache');
|
|
||||||
res.setHeader('Expires', '0');
|
|
||||||
return res.json({ rev: 'new-url-v1', size: 0, server_modified: new Date().toISOString() });
|
|
||||||
} catch (err) {
|
|
||||||
return res.status(500).json({ error: err.message });
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|||||||
+32
-10
@@ -2,25 +2,47 @@ import { createClient } from '@supabase/supabase-js';
|
|||||||
|
|
||||||
const SUPABASE_URL = process.env.SUPABASE_URL || 'https://hwithddwaapyhnfwcesj.supabase.co';
|
const SUPABASE_URL = process.env.SUPABASE_URL || 'https://hwithddwaapyhnfwcesj.supabase.co';
|
||||||
const SUPABASE_KEY = process.env.SUPABASE_SERVICE_KEY || 'sb_publishable_fGXkh0bSrAOqSk2jWKAzSg_NJD9YPCv';
|
const SUPABASE_KEY = process.env.SUPABASE_SERVICE_KEY || 'sb_publishable_fGXkh0bSrAOqSk2jWKAzSg_NJD9YPCv';
|
||||||
|
const ALLOWED_ORIGIN = 'https://craze-data-check.vercel.app';
|
||||||
|
|
||||||
const supabase = createClient(SUPABASE_URL, SUPABASE_KEY);
|
const supabase = createClient(SUPABASE_URL, SUPABASE_KEY);
|
||||||
|
|
||||||
|
function setCors(req, res) {
|
||||||
|
const origin = req.headers.origin;
|
||||||
|
if (origin === ALLOWED_ORIGIN) {
|
||||||
|
res.setHeader('Access-Control-Allow-Origin', ALLOWED_ORIGIN);
|
||||||
|
}
|
||||||
|
res.setHeader('Access-Control-Allow-Methods', 'POST, OPTIONS');
|
||||||
|
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
|
||||||
|
res.setHeader('Vary', 'Origin');
|
||||||
|
}
|
||||||
|
|
||||||
export default async function handler(req, res) {
|
export default async function handler(req, res) {
|
||||||
|
setCors(req, res);
|
||||||
|
|
||||||
|
if (req.method === 'OPTIONS') {
|
||||||
|
return res.status(204).end();
|
||||||
|
}
|
||||||
|
|
||||||
|
const origin = req.headers.origin;
|
||||||
|
if (origin && origin !== ALLOWED_ORIGIN) {
|
||||||
|
return res.status(403).json({ error: 'Forbidden' });
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (req.method !== 'POST') {
|
if (req.method !== 'POST') {
|
||||||
return res.status(405).json({ error: 'Method not allowed' });
|
return res.status(405).json({ error: 'Method not allowed' });
|
||||||
}
|
}
|
||||||
|
|
||||||
const { rows, fileMeta } = req.body;
|
const { rows } = req.body;
|
||||||
|
|
||||||
if (!rows || !Array.isArray(rows)) {
|
if (!rows || !Array.isArray(rows)) {
|
||||||
return res.status(400).json({ error: 'Missing rows data' });
|
return res.status(400).json({ error: 'Missing rows data' });
|
||||||
}
|
}
|
||||||
|
|
||||||
const articleNoIdx = 0;
|
const articleNoIdx = 0;
|
||||||
|
|
||||||
const productsToUpsert = [];
|
const productsToUpsert = [];
|
||||||
|
|
||||||
for (const row of rows) {
|
for (const row of rows) {
|
||||||
const productId = String(row[articleNoIdx]);
|
const productId = String(row[articleNoIdx]);
|
||||||
if (productId && productId.trim() !== '') {
|
if (productId && productId.trim() !== '') {
|
||||||
@@ -37,9 +59,9 @@ export default async function handler(req, res) {
|
|||||||
|
|
||||||
const { error } = await supabase
|
const { error } = await supabase
|
||||||
.from('products')
|
.from('products')
|
||||||
.upsert(productsToUpsert, {
|
.upsert(productsToUpsert, {
|
||||||
onConflict: 'product_id',
|
onConflict: 'product_id',
|
||||||
ignoreDuplicates: true
|
ignoreDuplicates: true
|
||||||
});
|
});
|
||||||
|
|
||||||
if (error) {
|
if (error) {
|
||||||
@@ -47,12 +69,12 @@ export default async function handler(req, res) {
|
|||||||
return res.status(500).json({ error: error.message, detail: 'Failed to upsert products' });
|
return res.status(500).json({ error: error.message, detail: 'Failed to upsert products' });
|
||||||
}
|
}
|
||||||
|
|
||||||
return res.json({
|
return res.json({
|
||||||
success: true,
|
success: true,
|
||||||
syncedCount: productsToUpsert.length
|
syncedCount: productsToUpsert.length
|
||||||
});
|
});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('Handler error:', err);
|
console.error('Handler error:', err);
|
||||||
res.status(500).json({ error: err.message });
|
res.status(500).json({ error: err.message });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user