Support Gotify key migration when app_secret is added

Try legacy fallback key for decryption and re-encrypt with primary app_secret key on read.
This commit is contained in:
Oscar Admin Contributors 2026-12-03 18:36:52 -05:00
parent 5116b13d0c
commit fb11868aa9

View file

@ -27,18 +27,31 @@ function admin_gotify_users_legacy_json_path(): string
}
/**
* Key for encrypting Gotify tokens at rest.
* Prefers app_secret config; falls back to deterministic derivation from local config.
* Encryption keys for Gotify tokens at rest.
* Primary key is app_secret (if set) or a deterministic local fallback.
* If app_secret is set, we also try the legacy fallback key for migration.
*/
function admin_gotify_key(): string
function admin_gotify_keys(): array
{
$secret = trim((string) (function_exists('admin_cfg') ? admin_cfg('app_secret', '') : ''));
if ($secret === '') {
$secret = (string) (function_exists('admin_cfg') ? admin_cfg('system_password', '') : '')
$fallbackSecret = (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';
$fallbackKey = hash('sha256', $fallbackSecret, true);
$secret = trim((string) (function_exists('admin_cfg') ? admin_cfg('app_secret', '') : ''));
if ($secret === '') {
return [$fallbackKey];
}
return hash('sha256', $secret, true);
$primary = hash('sha256', $secret, true);
// Include fallback for smooth migration when app_secret is added later.
if (hash_equals($primary, $fallbackKey)) return [$primary];
return [$primary, $fallbackKey];
}
function admin_gotify_key(): string
{
$keys = admin_gotify_keys();
return $keys[0];
}
function admin_gotify_encrypt(string $plaintext): string
@ -61,14 +74,18 @@ function admin_gotify_decrypt(string $value): string
{
$value = trim($value);
if ($value === '') return '';
$keys = admin_gotify_keys();
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 : '';
foreach ($keys as $key) {
$plain = sodium_crypto_secretbox_open($cipher, $nonce, $key);
if (is_string($plain)) return $plain;
}
return '';
}
if (str_starts_with($value, 'o1:')) {
$raw = base64_decode(substr($value, 3), true);
@ -76,8 +93,11 @@ function admin_gotify_decrypt(string $value): string
$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 : '';
foreach ($keys as $key) {
$plain = openssl_decrypt($cipher, 'aes-256-gcm', $key, OPENSSL_RAW_DATA, $iv, $tag);
if (is_string($plain)) return $plain;
}
return '';
}
// Legacy plaintext token - returned as-is; will be migrated on next save.
return $value;
@ -135,8 +155,30 @@ function admin_gotify_user_get(string $screenName): ?array
$token = admin_gotify_decrypt($storedToken);
if ($token === '') return null;
// Transparently migrate legacy plaintext tokens to encrypted storage.
if (!admin_gotify_is_encrypted($storedToken)) {
// Transparently migrate legacy plaintext or old-key encrypted tokens.
$needsMigration = !admin_gotify_is_encrypted($storedToken);
if (!$needsMigration && admin_gotify_is_encrypted($storedToken)) {
$primary = admin_gotify_key();
$probe = null;
if (str_starts_with($storedToken, 's1:') && function_exists('sodium_crypto_secretbox_open')) {
$raw = base64_decode(substr($storedToken, 3), true);
if ($raw !== false && strlen($raw) >= SODIUM_CRYPTO_SECRETBOX_NONCEBYTES + SODIUM_CRYPTO_SECRETBOX_MACBYTES) {
$nonce = substr($raw, 0, SODIUM_CRYPTO_SECRETBOX_NONCEBYTES);
$cipher = substr($raw, SODIUM_CRYPTO_SECRETBOX_NONCEBYTES);
$probe = sodium_crypto_secretbox_open($cipher, $nonce, $primary);
}
} elseif (str_starts_with($storedToken, 'o1:')) {
$raw = base64_decode(substr($storedToken, 3), true);
if ($raw !== false && strlen($raw) >= 28) {
$iv = substr($raw, 0, 12);
$tag = substr($raw, 12, 16);
$cipher = substr($raw, 28);
$probe = openssl_decrypt($cipher, 'aes-256-gcm', $primary, OPENSSL_RAW_DATA, $iv, $tag);
}
}
$needsMigration = !is_string($probe);
}
if ($needsMigration) {
$row['token'] = admin_gotify_encrypt($token);
$row['updated_at'] = (string) ($row['updated_at'] ?? date('c'));
$all[$key] = $row;