Spaces:
Sleeping

corpusdb / proxy /api.php
mrsavage1's picture
Upload 5 files
015c315 verified
Raw History Blame Contribute Delete
3.46 kB
<?php
/**
* CorpusDB API Proxy
*
* Place this file on YOUR hosting (e.g., api.php)
* Users connect to YOUR domain β€” backend URL is hidden.
*/
// CORS headers β€” restrict to your domain in production
$allowed_origins = ['https://corpusdb.66ghz.com', 'http://localhost:8000', 'http://localhost:3000'];
$origin = $_SERVER['HTTP_ORIGIN'] ?? '';
if (in_array($origin, $allowed_origins)) {
header('Access-Control-Allow-Origin: ' . $origin);
}
header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type, X-Api-Key, X-Secret-Key');
header('Content-Type: application/json');
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
http_response_code(200);
exit;
}
// ── CONFIG ────────────────────────────────────────────────────────────────────
define('BACKEND_URL', 'https://mrsavage1-corpusdb.hf.space');
// Allowlist of permitted endpoint prefixes (prevents SSRF)
$ALLOWED_PREFIXES = [
'api/cdb/',
'api/databases',
'api/tables',
'api/schema/',
'api/rows/',
'api/sql',
'api/health',
'api/ai/',
'api/query-builder/',
'api/visualize/',
'api/performance/',
'api/projects',
'api/templates',
'api/settings/',
];
// ─────────────────────────────────────────────────────────────────────────────
$method = $_SERVER['REQUEST_METHOD'];
$endpoint = isset($_GET['endpoint']) ? trim($_GET['endpoint'], '/') : '';
$headers = getallheaders();
// Validate API keys
$apiKey = $headers['X-Api-Key'] ?? ($headers['x-api-key'] ?? '');
$secretKey = $headers['X-Secret-Key'] ?? ($headers['x-secret-key'] ?? '');
if (empty($apiKey) || empty($secretKey)) {
http_response_code(401);
echo json_encode(['ok' => false, 'error' => 'API keys required']);
exit;
}
// Validate endpoint against allowlist (prevents SSRF)
$allowed = false;
foreach ($ALLOWED_PREFIXES as $prefix) {
if (strpos($endpoint, $prefix) === 0) {
$allowed = true;
break;
}
}
if (!$allowed || strpos($endpoint, '..') !== false) {
http_response_code(403);
echo json_encode(['ok' => false, 'error' => 'Endpoint not allowed']);
exit;
}
// Forward request to backend
$body = file_get_contents('php://input');
$url = BACKEND_URL . '/' . $endpoint;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'X-Api-Key: ' . $apiKey,
'X-Secret-Key: ' . $secretKey,
]);
if ($body && in_array($method, ['POST', 'PUT', 'PATCH'])) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
}
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$curlError = curl_error($ch);
curl_close($ch);
if ($response === false) {
http_response_code(502);
echo json_encode(['ok' => false, 'error' => 'Backend unreachable: ' . $curlError]);
exit;
}
http_response_code($httpCode);
echo $response;