deploy-runner.php

This commit is contained in:
Dekart Deploy Bot 2026-09-30 17:56:17 +00:00
parent e8b4311e92
commit b74d25e372

View File

@ -1,8 +1,9 @@
<?php
/**
* Deploy Runner — HTTP-triggered migration executor
* Usage: https://site.com/wp-content/themes/dekart/deploy-runner.php?migrate=003
* Deploy Runner — HTTP-triggered migration executor (token-gated)
* Usage: https://site.com/wp-content/themes/dekart/deploy-runner.php?token=SECRET&migrate=003
* (or send the secret in the `X-Deploy-Token` header)
*
* Self-destructs after execution (renames to .done).
* Requires wp-config.php for DB credentials.
@ -11,16 +12,7 @@
// Prevent WordPress from intercepting
define('WP_USE_THEMES', false);
$allowed_migrations = ['003', '004', '005'];
$migrate = isset($_GET['migrate']) ? $_GET['migrate'] : '';
if (!in_array($migrate, $allowed_migrations)) {
header('Content-Type: text/plain');
die("Usage: ?migrate=003 (allowed: " . implode(', ', $allowed_migrations) . ")\n");
}
// Load WordPress config for DB credentials
// Load WordPress config for DB credentials (needed by the token gate below).
$wp_config_path = dirname(__FILE__) . '/../../../wp-config.php';
if (!file_exists($wp_config_path)) {
// Try parent directory
@ -29,9 +21,50 @@ if (!file_exists($wp_config_path)) {
if (!file_exists($wp_config_path)) {
$wp_config_path = dirname(__FILE__) . '/../wp-config.php';
}
require_once $wp_config_path;
// ─── Security: token gate (fail closed) ──────────────────────────────
// The endpoint runs DB/theme migrations, so it must never be reachable
// anonymously. The secret is taken from, in order: DEPLOY_RUNNER_TOKEN
// (env var or wp-config constant), or the wp_options row
// `dekart_deploy_token`. With no secret configured, every request is
// denied with 404 — the endpoint stays closed until a secret is set.
// Set the option once (recommended, no file edits):
// wp option update dekart_deploy_token "$(openssl rand -hex 24)"
$dk_expected = '';
if (getenv('DEPLOY_RUNNER_TOKEN')) {
$dk_expected = (string) getenv('DEPLOY_RUNNER_TOKEN');
} elseif (defined('DEPLOY_RUNNER_TOKEN')) {
$dk_expected = (string) DEPLOY_RUNNER_TOKEN;
} else {
$dk_conn = @mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
if ($dk_conn) {
$dk_sql = "SELECT option_value FROM wp_options WHERE option_name = 'dekart_deploy_token' LIMIT 1";
if ($dk_res = @mysqli_query($dk_conn, $dk_sql)) {
$dk_row = mysqli_fetch_assoc($dk_res);
$dk_expected = isset($dk_row['option_value']) ? (string) $dk_row['option_value'] : '';
}
mysqli_close($dk_conn);
}
}
$dk_given = isset($_GET['token'])
? (string) $_GET['token']
: (isset($_SERVER['HTTP_X_DEPLOY_TOKEN']) ? (string) $_SERVER['HTTP_X_DEPLOY_TOKEN'] : '');
if ($dk_expected === '' || !hash_equals($dk_expected, $dk_given)) {
http_response_code(404);
header('Content-Type: text/plain; charset=utf-8');
exit("Not found\n");
}
$allowed_migrations = ['003', '004', '005'];
$migrate = isset($_GET['migrate']) ? $_GET['migrate'] : '';
if (!in_array($migrate, $allowed_migrations)) {
header('Content-Type: text/plain');
die("Usage: ?token=SECRET&migrate=003 (allowed: " . implode(', ', $allowed_migrations) . ")\n");
}
header('Content-Type: text/plain; charset=utf-8');
echo "=== Deploy Runner: migration $migrate ===\n\n";