diff --git a/.htaccess b/.htaccess
new file mode 100644
index 0000000..fa49837
--- /dev/null
+++ b/.htaccess
@@ -0,0 +1,12 @@
+# Apache hardening for Oscar Admin
+Options -Indexes
+
+
+ Require all denied
+
+
+
+ Header always set X-Frame-Options "DENY"
+ Header always set X-Content-Type-Options "nosniff"
+ Header always set Referrer-Policy "no-referrer"
+
diff --git a/README.md b/README.md
index b4812a2..d786147 100755
--- a/README.md
+++ b/README.md
@@ -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 |
| `gotify_enabled` | Enables optional Gotify addon |
| `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`) |
## 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.
-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
diff --git a/SECURITY.md b/SECURITY.md
index b8a60e1..16e3cd4 100644
--- a/SECURITY.md
+++ b/SECURITY.md
@@ -20,7 +20,8 @@ If you need a private contact channel, use the repository owner's preferred secu
## Security notes for deployers
- 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.
- Serve the admin panel behind HTTPS.
- 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).
diff --git a/config.example.php b/config.example.php
index 4da4e19..3bff411 100644
--- a/config.example.php
+++ b/config.example.php
@@ -40,6 +40,10 @@ return [
'max_login_attempts' => 8,
'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) ---
// 100% optional. The admin panel works without this addon.
// Enable the feature here; each admin user sets their own Gotify URL/token
diff --git a/lib.php b/lib.php
index 39b0b12..eefea1a 100644
--- a/lib.php
+++ b/lib.php
@@ -333,6 +333,15 @@ function admin_page(string $title, string $active, callable $body): void
$site = (string) admin_cfg('site_name', 'Oscar Admin');
$shared = (string) admin_cfg('shared_style_path', 'assets/style.css');
$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'");
+ }
?>
diff --git a/lib_gotify.php b/lib_gotify.php
index 07097cf..911471c 100644
--- a/lib_gotify.php
+++ b/lib_gotify.php
@@ -6,6 +6,7 @@ declare(strict_types=1);
* 100% optional - the admin panel works without this file.
*
* 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.
*/
@@ -25,6 +26,68 @@ function admin_gotify_users_legacy_json_path(): string
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
{
$path = admin_gotify_users_path();
@@ -50,8 +113,12 @@ function admin_gotify_users_save_all(array $data): bool
{
$path = admin_gotify_users_path();
$export = var_export($data, true);
- $php = " $url,
'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://'];
}
+ $encToken = admin_gotify_encrypt($token);
+ if ($encToken === '') {
+ return ['ok' => false, 'error' => 'Failed to secure Gotify token.'];
+ }
+
$all[$key] = [
'screen_name' => $screenName,
'url' => $url,
- 'token' => $token,
+ 'token' => $encToken,
'priority' => $priority,
'updated_at' => date('c'),
];
diff --git a/login.php b/login.php
index 6c52967..615daa6 100644
--- a/login.php
+++ b/login.php
@@ -27,6 +27,15 @@ if (admin_current_user() !== null) {
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
{
$a = random_int(2, 15);