Mail review stage 7a: mail passwords, the OAuth client secret and the mail tokens are stored encrypted with a key from config.php's backup key, kept as typed only when there is no key, with a warning in Admin > Settings > Mail when they can't be read (database 4.3.1); pictures from other sites in client email are held back on a ticket, its quick look and its work order until Show pictures is clicked; the sign-in page no longer loads the mail server's password; 26.12.3 changelog

This commit is contained in:
johnnyq
2026-11-30 21:34:43 -05:00
parent 6cec0b8574
commit b3ba5d7275
21 changed files with 339 additions and 65 deletions

View File

@@ -0,0 +1,26 @@
<?php
/*
* ITFlow - Database update to version 4.3.1 (from 4.3.0)
* Included by admin/database_updates.php - do not access directly
*
* The mail passwords, OAuth secret and tokens are encrypted (functions/mail_secret.php): the password and secret
* columns made wide enough for an encrypted value, then those saved as typed encrypted - when config.php has a
* backup key, or one can be added to it. Without one they stay as they are, and the nightly job tries again.
*
* Re-runnable: a column is widened only while it is narrower, and a value is encrypted only once.
*/
defined('FROM_DB_UPDATER') || die("Direct file access is not allowed");
foreach (['config_smtp_password', 'config_imap_password', 'config_mail_oauth_client_secret'] as $itflow_column) {
$itflow_width = mysqli_fetch_row(mysqli_query($mysqli, "SELECT CHARACTER_MAXIMUM_LENGTH FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'settings' AND COLUMN_NAME = '$itflow_column' LIMIT 1"));
if ($itflow_width && intval($itflow_width[0]) < 500) {
mysqli_query($mysqli, "ALTER TABLE `settings` MODIFY `$itflow_column` varchar(500) DEFAULT NULL");
}
}
if (function_exists('mailSecretsSealAll')) {
mailSecretsSealAll(true);
}

View File

@@ -85,14 +85,12 @@ $refresh_token = (string) $json['refresh_token'];
$access_token = (string) $json['access_token'];
$expires_at = date('Y-m-d H:i:s', time() + intval($json['expires_in'] ?? 3600));
$refresh_token_esc = mysqli_real_escape_string($mysqli, $refresh_token);
$access_token_esc = mysqli_real_escape_string($mysqli, $access_token);
$expires_at_esc = mysqli_real_escape_string($mysqli, $expires_at);
// Only the token - Sending and Receiving stay as they were set; connecting for one doesn't turn on the other
// Only the token - Sending and Receiving stay as they were set; connecting for one doesn't turn on the other. Sealed (functions/mail_secret.php)
mysqli_query($mysqli, "UPDATE settings SET
config_mail_oauth_refresh_token = '$refresh_token_esc',
config_mail_oauth_access_token = '$access_token_esc',
" . mailSecretSql('config_mail_oauth_refresh_token', $refresh_token) . ",
" . mailSecretSql('config_mail_oauth_access_token', $access_token) . ",
config_mail_oauth_access_token_expires_at = '$expires_at_esc'
WHERE company_id = 1
");

View File

@@ -136,7 +136,7 @@ if (isset($_POST['edit_mail_oauth_settings']) || isset($_POST['oauth_connect_mic
}
$config_mail_oauth_client_id = $after['config_mail_oauth_client_id'];
$config_mail_oauth_client_secret = $after['config_mail_oauth_client_secret'];
$config_mail_oauth_client_secret = mailSecretOpen($after['config_mail_oauth_client_secret']);
$config_mail_oauth_tenant_id = $after['config_mail_oauth_tenant_id'];
if ($config_mail_oauth_client_id === '' || $config_mail_oauth_client_secret === '' || $config_mail_oauth_tenant_id === '') {
flashAlert("The app's client ID, secret and tenant ID first - then connect.", 'error');
@@ -402,16 +402,12 @@ if (isset($_POST['test_email_imap'])) {
$expires_at = date('Y-m-d H:i:s', time() + intval($json['expires_in'] ?? 3600));
$refresh_token_to_save = $json['refresh_token'] ?? null;
$token_esc = mysqli_real_escape_string($mysqli, $password);
$expires_at_esc = mysqli_real_escape_string($mysqli, $expires_at);
$refresh_sql = '';
if (!empty($refresh_token_to_save)) {
$refresh_token_esc = mysqli_real_escape_string($mysqli, $refresh_token_to_save);
$refresh_sql = ", config_mail_oauth_refresh_token = '{$refresh_token_esc}'";
}
// Sealed (functions/mail_secret.php)
$refresh_sql = !empty($refresh_token_to_save) ? ', ' . mailSecretSql('config_mail_oauth_refresh_token', $refresh_token_to_save) : '';
mysqli_query($mysqli, "UPDATE settings SET config_mail_oauth_access_token = '{$token_esc}', config_mail_oauth_access_token_expires_at = '{$expires_at_esc}'{$refresh_sql} WHERE company_id = 1");
mysqli_query($mysqli, "UPDATE settings SET " . mailSecretSql('config_mail_oauth_access_token', $password) . ", config_mail_oauth_access_token_expires_at = '{$expires_at_esc}'{$refresh_sql} WHERE company_id = 1");
}
}
@@ -513,20 +509,16 @@ if (isset($_POST['test_oauth_token_refresh'])) {
redirect($mail_tab_redirect);
}
$new_access_token = escapeSql($json['access_token']);
$new_access_token = (string) $json['access_token'];
$new_expires_at = date('Y-m-d H:i:s', time() + intval($json['expires_in'] ?? 3600));
$new_refresh_token = !empty($json['refresh_token']) ? escapeSql($json['refresh_token']) : '';
$new_refresh_token = !empty($json['refresh_token']) ? (string) $json['refresh_token'] : '';
$new_access_token_esc = mysqli_real_escape_string($mysqli, $new_access_token);
$new_expires_at_esc = mysqli_real_escape_string($mysqli, $new_expires_at);
$refresh_sql = '';
if (!empty($new_refresh_token)) {
$new_refresh_token_esc = mysqli_real_escape_string($mysqli, $new_refresh_token);
$refresh_sql = ", config_mail_oauth_refresh_token = '$new_refresh_token_esc'";
}
// Sealed (functions/mail_secret.php) - and as Microsoft or Google sent them: escapeSql() then mysqli_real_escape_string() escaped them twice
$refresh_sql = $new_refresh_token !== '' ? ', ' . mailSecretSql('config_mail_oauth_refresh_token', $new_refresh_token) : '';
mysqli_query($mysqli, "UPDATE settings SET config_mail_oauth_access_token = '$new_access_token_esc', config_mail_oauth_access_token_expires_at = '$new_expires_at_esc'$refresh_sql WHERE company_id = 1");
mysqli_query($mysqli, "UPDATE settings SET " . mailSecretSql('config_mail_oauth_access_token', $new_access_token) . ", config_mail_oauth_access_token_expires_at = '$new_expires_at_esc'$refresh_sql WHERE company_id = 1");
$provider_label = $provider === 'microsoft_oauth' ? 'Microsoft 365' : 'Google Workspace';
logAudit("Settings", "Edit", "$session_name tested OAuth token refresh for $provider_label mail settings");

View File

@@ -178,6 +178,11 @@ if (!$config_module_enable_ticketing) {
</li>
</ul>
<?php if (($mail_secret_problem = mailSecretProblem()) !== '') { ?>
<!-- The passwords and tokens - kept as typed, or saved under another key (functions/mail_secret.php) -->
<div class="alert alert-warning mt-3 mb-0" data-mail-secret-problem><i class="fas fa-fw fa-key me-2"></i><?= escapeHtml($mail_secret_problem) ?></div>
<?php } ?>
<div class="tab-content">
<!-- ============================ SENDING / SMTP ============================ -->