Harden admin security and encrypt Gotify tokens
Encrypt per-user Gotify tokens at rest with sodium/openssl, add app_secret config, security headers, Apache .htaccess hardening, and restrictive permissions for stored settings.
This commit is contained in:
parent
77342f4629
commit
5116b13d0c
7 changed files with 126 additions and 7 deletions
12
.htaccess
Normal file
12
.htaccess
Normal file
|
|
@ -0,0 +1,12 @@
|
||||||
|
# Apache hardening for Oscar Admin
|
||||||
|
Options -Indexes
|
||||||
|
|
||||||
|
<FilesMatch "(^\.|config\.php$|config\.example\.php$|lib\.php$|lib_gotify\.php$|user-menu\.php$|gotify-users\.php$|gotify-users\.json$|audit\.log$|README\.md$|SECURITY\.md$|CONTRIBUTING\.md$|AI_DISCLOSURE\.md|LICENSE$)">
|
||||||
|
Require all denied
|
||||||
|
</FilesMatch>
|
||||||
|
|
||||||
|
<IfModule mod_headers.c>
|
||||||
|
Header always set X-Frame-Options "DENY"
|
||||||
|
Header always set X-Content-Type-Options "nosniff"
|
||||||
|
Header always set Referrer-Policy "no-referrer"
|
||||||
|
</IfModule>
|
||||||
|
|
@ -74,6 +74,7 @@ See [`config.example.php`](config.example.php) for all options. Key settings:
|
||||||
| `mgmt_api_host` / `mgmt_api_port` | Private Open OSCAR Management API endpoint |
|
| `mgmt_api_host` / `mgmt_api_port` | Private Open OSCAR Management API endpoint |
|
||||||
| `gotify_enabled` | Enables optional Gotify addon |
|
| `gotify_enabled` | Enables optional Gotify addon |
|
||||||
| `gotify_url` / `gotify_token` | Optional global Gotify fallback (per-user settings preferred) |
|
| `gotify_url` / `gotify_token` | Optional global Gotify fallback (per-user settings preferred) |
|
||||||
|
| `app_secret` | Optional secret for encrypting sensitive stored values (Gotify tokens) |
|
||||||
| `shared_style_path` | Base CSS path (defaults to bundled `assets/style.css`) |
|
| `shared_style_path` | Base CSS path (defaults to bundled `assets/style.css`) |
|
||||||
|
|
||||||
## Gotify (Optional)
|
## Gotify (Optional)
|
||||||
|
|
@ -87,7 +88,7 @@ Gotify support is a 100% optional addon (`lib_gotify.php`). The panel works with
|
||||||
|
|
||||||
If a Gotify URL/token is saved for a user, login codes are sent through Gotify for that user. Clear settings to disable Gotify delivery for that account.
|
If a Gotify URL/token is saved for a user, login codes are sent through Gotify for that user. Clear settings to disable Gotify delivery for that account.
|
||||||
|
|
||||||
Per-user Gotify settings are stored in `gotify-users.php` (gitignored). It is a PHP file to reduce accidental direct exposure.
|
Per-user Gotify settings are stored in `gotify-users.php` (gitignored). Tokens are encrypted at rest; the file is written as PHP with restrictive permissions to reduce accidental exposure. Set `app_secret` in `config.php` for an explicit encryption key.
|
||||||
|
|
||||||
## Deployment / Hardening
|
## Deployment / Hardening
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -20,7 +20,8 @@ If you need a private contact channel, use the repository owner's preferred secu
|
||||||
## Security notes for deployers
|
## Security notes for deployers
|
||||||
|
|
||||||
- Keep `config.php`, `audit.log`, and `gotify-users.php` outside public access and out of git.
|
- Keep `config.php`, `audit.log`, and `gotify-users.php` outside public access and out of git.
|
||||||
|
- Set a strong `app_secret` in `config.php`; Gotify tokens are encrypted at rest.
|
||||||
- **Never expose the OSCAR Management API publicly.** Bind it to localhost or a trusted private network.
|
- **Never expose the OSCAR Management API publicly.** Bind it to localhost or a trusted private network.
|
||||||
- Serve the admin panel behind HTTPS.
|
- Serve the admin panel behind HTTPS.
|
||||||
- Restrict access at the web server where appropriate (VPN/IP allowlist/auth proxy).
|
- Restrict access at the web server where appropriate (VPN/IP allowlist/auth proxy).
|
||||||
- Review web server rules to deny direct access to sensitive files.
|
- Review web server rules to deny direct access to sensitive files (`.htaccess` is included for Apache; nginx example in README).
|
||||||
|
|
|
||||||
|
|
@ -40,6 +40,10 @@ return [
|
||||||
'max_login_attempts' => 8,
|
'max_login_attempts' => 8,
|
||||||
'login_attempt_window_seconds' => 900,
|
'login_attempt_window_seconds' => 900,
|
||||||
|
|
||||||
|
// Optional secret used to encrypt sensitive stored values (e.g. Gotify tokens).
|
||||||
|
// Set a long random string. If empty, a local fallback key is derived.
|
||||||
|
'app_secret' => '',
|
||||||
|
|
||||||
// --- Optional Gotify addon (lib_gotify.php) ---
|
// --- Optional Gotify addon (lib_gotify.php) ---
|
||||||
// 100% optional. The admin panel works without this addon.
|
// 100% optional. The admin panel works without this addon.
|
||||||
// Enable the feature here; each admin user sets their own Gotify URL/token
|
// Enable the feature here; each admin user sets their own Gotify URL/token
|
||||||
|
|
|
||||||
9
lib.php
9
lib.php
|
|
@ -333,6 +333,15 @@ function admin_page(string $title, string $active, callable $body): void
|
||||||
$site = (string) admin_cfg('site_name', 'Oscar Admin');
|
$site = (string) admin_cfg('site_name', 'Oscar Admin');
|
||||||
$shared = (string) admin_cfg('shared_style_path', 'assets/style.css');
|
$shared = (string) admin_cfg('shared_style_path', 'assets/style.css');
|
||||||
$sharedHref = file_exists(__DIR__ . '/' . ltrim($shared, '/')) ? $shared : $shared;
|
$sharedHref = file_exists(__DIR__ . '/' . ltrim($shared, '/')) ? $shared : $shared;
|
||||||
|
|
||||||
|
if (!headers_sent()) {
|
||||||
|
header('X-Frame-Options: DENY');
|
||||||
|
header('X-Content-Type-Options: nosniff');
|
||||||
|
header('Referrer-Policy: no-referrer');
|
||||||
|
header('Permissions-Policy: camera=(), microphone=(), geolocation=()');
|
||||||
|
header('X-Robots-Tag: noindex, nofollow');
|
||||||
|
header("Content-Security-Policy: default-src 'self'; base-uri 'self'; object-src 'none'; frame-ancestors 'none'; img-src 'self' data: blob:; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline'; connect-src 'self'; form-action 'self'");
|
||||||
|
}
|
||||||
?>
|
?>
|
||||||
<!doctype html>
|
<!doctype html>
|
||||||
<html lang="en">
|
<html lang="en">
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@ declare(strict_types=1);
|
||||||
* 100% optional - the admin panel works without this file.
|
* 100% optional - the admin panel works without this file.
|
||||||
*
|
*
|
||||||
* Per-user settings are stored in gotify-users.php (gitignored).
|
* Per-user settings are stored in gotify-users.php (gitignored).
|
||||||
|
* Tokens are encrypted at rest (sodium secretbox / openssl AES-256-GCM).
|
||||||
* Global gotify_url/gotify_token in config are optional fallbacks.
|
* Global gotify_url/gotify_token in config are optional fallbacks.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
@ -25,6 +26,68 @@ function admin_gotify_users_legacy_json_path(): string
|
||||||
return __DIR__ . '/gotify-users.json';
|
return __DIR__ . '/gotify-users.json';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Key for encrypting Gotify tokens at rest.
|
||||||
|
* Prefers app_secret config; falls back to deterministic derivation from local config.
|
||||||
|
*/
|
||||||
|
function admin_gotify_key(): string
|
||||||
|
{
|
||||||
|
$secret = trim((string) (function_exists('admin_cfg') ? admin_cfg('app_secret', '') : ''));
|
||||||
|
if ($secret === '') {
|
||||||
|
$secret = (string) (function_exists('admin_cfg') ? admin_cfg('system_password', '') : '')
|
||||||
|
. '|' . (string) (function_exists('admin_cfg') ? admin_cfg('session_name', 'oscar_admin') : 'oscar_admin')
|
||||||
|
. '|oscar-admin-gotify';
|
||||||
|
}
|
||||||
|
return hash('sha256', $secret, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
function admin_gotify_encrypt(string $plaintext): string
|
||||||
|
{
|
||||||
|
if ($plaintext === '') return '';
|
||||||
|
$key = admin_gotify_key();
|
||||||
|
if (function_exists('sodium_crypto_secretbox')) {
|
||||||
|
$nonce = random_bytes(SODIUM_CRYPTO_SECRETBOX_NONCEBYTES);
|
||||||
|
$cipher = sodium_crypto_secretbox($plaintext, $nonce, $key);
|
||||||
|
return 's1:' . base64_encode($nonce . $cipher);
|
||||||
|
}
|
||||||
|
$iv = random_bytes(12);
|
||||||
|
$tag = '';
|
||||||
|
$cipher = openssl_encrypt($plaintext, 'aes-256-gcm', $key, OPENSSL_RAW_DATA, $iv, $tag);
|
||||||
|
if ($cipher === false) return '';
|
||||||
|
return 'o1:' . base64_encode($iv . $tag . $cipher);
|
||||||
|
}
|
||||||
|
|
||||||
|
function admin_gotify_decrypt(string $value): string
|
||||||
|
{
|
||||||
|
$value = trim($value);
|
||||||
|
if ($value === '') return '';
|
||||||
|
if (str_starts_with($value, 's1:')) {
|
||||||
|
if (!function_exists('sodium_crypto_secretbox_open')) return '';
|
||||||
|
$raw = base64_decode(substr($value, 3), true);
|
||||||
|
if ($raw === false || strlen($raw) < SODIUM_CRYPTO_SECRETBOX_NONCEBYTES + SODIUM_CRYPTO_SECRETBOX_MACBYTES) return '';
|
||||||
|
$nonce = substr($raw, 0, SODIUM_CRYPTO_SECRETBOX_NONCEBYTES);
|
||||||
|
$cipher = substr($raw, SODIUM_CRYPTO_SECRETBOX_NONCEBYTES);
|
||||||
|
$plain = sodium_crypto_secretbox_open($cipher, $nonce, admin_gotify_key());
|
||||||
|
return is_string($plain) ? $plain : '';
|
||||||
|
}
|
||||||
|
if (str_starts_with($value, 'o1:')) {
|
||||||
|
$raw = base64_decode(substr($value, 3), true);
|
||||||
|
if ($raw === false || strlen($raw) < 28) return '';
|
||||||
|
$iv = substr($raw, 0, 12);
|
||||||
|
$tag = substr($raw, 12, 16);
|
||||||
|
$cipher = substr($raw, 28);
|
||||||
|
$plain = openssl_decrypt($cipher, 'aes-256-gcm', admin_gotify_key(), OPENSSL_RAW_DATA, $iv, $tag);
|
||||||
|
return is_string($plain) ? $plain : '';
|
||||||
|
}
|
||||||
|
// Legacy plaintext token - returned as-is; will be migrated on next save.
|
||||||
|
return $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
function admin_gotify_is_encrypted(string $value): bool
|
||||||
|
{
|
||||||
|
return str_starts_with($value, 's1:') || str_starts_with($value, 'o1:');
|
||||||
|
}
|
||||||
|
|
||||||
function admin_gotify_users_load(): array
|
function admin_gotify_users_load(): array
|
||||||
{
|
{
|
||||||
$path = admin_gotify_users_path();
|
$path = admin_gotify_users_path();
|
||||||
|
|
@ -50,8 +113,12 @@ function admin_gotify_users_save_all(array $data): bool
|
||||||
{
|
{
|
||||||
$path = admin_gotify_users_path();
|
$path = admin_gotify_users_path();
|
||||||
$export = var_export($data, true);
|
$export = var_export($data, true);
|
||||||
$php = "<?php\ndeclare(strict_types=1);\n\n// Gotify per-user settings. Kept as PHP to avoid direct browser access.\nreturn " . $export . ";\n";
|
$php = "<?php\ndeclare(strict_types=1);\n\n// Gotify per-user settings. Kept as PHP to avoid direct browser access.\n// Tokens are encrypted at rest; do not treat this file as public.\nreturn " . $export . ";\n";
|
||||||
return @file_put_contents($path, $php, LOCK_EX) !== false;
|
$ok = @file_put_contents($path, $php, LOCK_EX) !== false;
|
||||||
|
if ($ok) {
|
||||||
|
@chmod($path, 0600);
|
||||||
|
}
|
||||||
|
return $ok;
|
||||||
}
|
}
|
||||||
|
|
||||||
function admin_gotify_user_get(string $screenName): ?array
|
function admin_gotify_user_get(string $screenName): ?array
|
||||||
|
|
@ -63,8 +130,19 @@ function admin_gotify_user_get(string $screenName): ?array
|
||||||
$row = $all[$key] ?? null;
|
$row = $all[$key] ?? null;
|
||||||
if (!is_array($row)) return null;
|
if (!is_array($row)) return null;
|
||||||
$url = trim((string) ($row['url'] ?? ''));
|
$url = trim((string) ($row['url'] ?? ''));
|
||||||
$token = trim((string) ($row['token'] ?? ''));
|
$storedToken = trim((string) ($row['token'] ?? ''));
|
||||||
if ($url === '' || $token === '') return null;
|
if ($url === '' || $storedToken === '') return null;
|
||||||
|
$token = admin_gotify_decrypt($storedToken);
|
||||||
|
if ($token === '') return null;
|
||||||
|
|
||||||
|
// Transparently migrate legacy plaintext tokens to encrypted storage.
|
||||||
|
if (!admin_gotify_is_encrypted($storedToken)) {
|
||||||
|
$row['token'] = admin_gotify_encrypt($token);
|
||||||
|
$row['updated_at'] = (string) ($row['updated_at'] ?? date('c'));
|
||||||
|
$all[$key] = $row;
|
||||||
|
admin_gotify_users_save_all($all);
|
||||||
|
}
|
||||||
|
|
||||||
return [
|
return [
|
||||||
'url' => $url,
|
'url' => $url,
|
||||||
'token' => $token,
|
'token' => $token,
|
||||||
|
|
@ -97,10 +175,15 @@ function admin_gotify_user_save(string $screenName, string $url, string $token,
|
||||||
return ['ok' => false, 'error' => 'Gotify URL must start with http:// or https://'];
|
return ['ok' => false, 'error' => 'Gotify URL must start with http:// or https://'];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$encToken = admin_gotify_encrypt($token);
|
||||||
|
if ($encToken === '') {
|
||||||
|
return ['ok' => false, 'error' => 'Failed to secure Gotify token.'];
|
||||||
|
}
|
||||||
|
|
||||||
$all[$key] = [
|
$all[$key] = [
|
||||||
'screen_name' => $screenName,
|
'screen_name' => $screenName,
|
||||||
'url' => $url,
|
'url' => $url,
|
||||||
'token' => $token,
|
'token' => $encToken,
|
||||||
'priority' => $priority,
|
'priority' => $priority,
|
||||||
'updated_at' => date('c'),
|
'updated_at' => date('c'),
|
||||||
];
|
];
|
||||||
|
|
|
||||||
|
|
@ -27,6 +27,15 @@ if (admin_current_user() !== null) {
|
||||||
admin_redirect('index.php');
|
admin_redirect('index.php');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!headers_sent()) {
|
||||||
|
header('X-Frame-Options: DENY');
|
||||||
|
header('X-Content-Type-Options: nosniff');
|
||||||
|
header('Referrer-Policy: no-referrer');
|
||||||
|
header('Permissions-Policy: camera=(), microphone=(), geolocation=()');
|
||||||
|
header('X-Robots-Tag: noindex, nofollow');
|
||||||
|
header("Content-Security-Policy: default-src 'self'; base-uri 'self'; object-src 'none'; frame-ancestors 'none'; img-src 'self' data:; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline'; connect-src 'self'; form-action 'self'");
|
||||||
|
}
|
||||||
|
|
||||||
function admin_login_math_new(): array
|
function admin_login_math_new(): array
|
||||||
{
|
{
|
||||||
$a = random_int(2, 15);
|
$a = random_int(2, 15);
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue