Skip to content

Wallos: Open-source, self-hostable personal subscription tracker. Visualize your recurring expenses, manage your budget, and save money.

fix(admin): let the SMTP test button see a managed password

Miguel Ribeirocbd4ec17a6c5
diff --git a/endpoints/notifications/testemailnotifications.php b/endpoints/notifications/testemailnotifications.php
index b71ccb6..4def5d0 100644
--- a/endpoints/notifications/testemailnotifications.php
+++ b/endpoints/notifications/testemailnotifications.php
@@ -7,10 +7,27 @@ use PHPMailer\PHPMailer\Exception;
require_once '../../includes/connect_endpoint.php';
require_once '../../includes/validate_endpoint.php';
require_once '../../includes/ssrf_helper.php';
+require_once '../../includes/instance_config.php';
$postData = file_get_contents("php://input");
$data = json_decode($postData, true);
+// smtppassword is rendered blank on purpose when the deployment owns it - a
+// mounted secret reaching the page source would have left the place it was
+// mounted into - but this endpoint otherwise reads exactly what the browser
+// posted, so a blank managed password was tested as if mail were
+// unconfigured: the admin page called it managed, the test said it failed,
+// and nothing explained why. Any field that came back empty and is
+// env-managed is filled in from the effective configuration instead.
+$data = wallos_fill_managed_form_fields($data, [
+ 'smtpaddress' => 'smtp_address',
+ 'smtpport' => 'smtp_port',
+ 'encryption' => 'encryption',
+ 'smtpusername' => 'smtp_username',
+ 'smtppassword' => 'smtp_password',
+ 'fromemail' => 'from_email',
+], wallos_get_effective_admin_configuration($db)['managed_fields'], wallos_get_admin_settings($db));
+
if (
!isset($data["smtpaddress"]) || $data["smtpaddress"] == "" ||
!isset($data["smtpport"]) || $data["smtpport"] == ""
diff --git a/includes/instance_config.php b/includes/instance_config.php
index ce74ee7..75b7d48 100644
--- a/includes/instance_config.php
+++ b/includes/instance_config.php
@@ -4,7 +4,7 @@
Instance settings the deployment can own.
Wallos keeps its instance SMTP and its public URL in the admin table, entered
- through the admin page. That works, and it keeps working exactly as before —
+ through the admin page. That works, and it keeps working exactly as before -
but it means the credentials of the mail server live in the database, and that
somebody has to type them in again by hand after every fresh volume. In a
container deployment the mail server is not a user preference; it is part of
@@ -13,7 +13,7 @@
The mechanism here is the one this project already uses for OIDC: read the
variable, remember which field the environment owns, and show that field in
the admin page without letting anybody edit it there. A secret may come from a
- file instead — OIDC_CLIENT_SECRET_FILE already does this — because a mounted
+ file instead - OIDC_CLIENT_SECRET_FILE already does this - because a mounted
file is how Docker and Kubernetes hand a secret to a container without putting
it in the process environment where every `docker inspect` can read it.
@@ -227,7 +227,7 @@ function wallos_get_effective_admin_configuration($db)
* The admin row every consumer should read.
*
* A convenience over the function above for the callers that only want the
- * values — the mail jobs, the pages that ask whether mail is configured at all.
+ * values - the mail jobs, the pages that ask whether mail is configured at all.
*
* @param SQLite3 $db
* @return array
@@ -236,3 +236,37 @@ function wallos_get_admin_settings($db)
{
return wallos_get_effective_admin_configuration($db)['settings'];
}
+
+/**
+ * Fills in any posted form field the environment owns that came back empty.
+ *
+ * A managed field is either disabled-but-populated in a page, or - a secret
+ * such as smtp_password - rendered blank on purpose, because a mounted secret
+ * reaching the page source would have left the place it was mounted into.
+ * An endpoint that reads exactly what a form posted then tests a blank managed
+ * secret as though nothing were configured, when it is: the page calls the
+ * field managed, the test fails, and nothing explains why.
+ *
+ * Only an empty posted value is replaced. A field the browser genuinely sent a
+ * value for - including one somebody typed into a field the page disabled,
+ * which a script can still do - keeps what was posted, since deciding what the
+ * server trusts is `savesmtpsettings.php`'s job, not this one's.
+ *
+ * @param array<string, mixed> $posted Form field name => posted value.
+ * @param array<string, string> $fieldColumns Form field name => admin column.
+ * @param array<string, string> $managedFields Admin column => variable name that owns it, from wallos_get_effective_admin_configuration().
+ * @param array<string, mixed> $settings The effective admin settings (column => value) the fallback reads from.
+ * @return array<string, mixed> $posted, with any empty managed field filled in.
+ */
+function wallos_fill_managed_form_fields($posted, $fieldColumns, $managedFields, $settings)
+{
+ foreach ($fieldColumns as $field => $column) {
+ $value = isset($posted[$field]) ? trim((string) $posted[$field]) : '';
+
+ if ($value === '' && isset($managedFields[$column])) {
+ $posted[$field] = $settings[$column] ?? '';
+ }
+ }
+
+ return $posted;
+}
diff --git a/tests/cases/instance_config_test.php b/tests/cases/instance_config_test.php
index 295bca1..8ca22d1 100644
--- a/tests/cases/instance_config_test.php
+++ b/tests/cases/instance_config_test.php
@@ -258,7 +258,7 @@ wallos_test('a fresh installation that configured nothing by hand can still send
// passwordreset.php refuses to run unless smtp_address and server_url are
// both set, and on a fresh volume the admin row has neither. An operator
// who put them in the compose file has configured this installation, and
- // the page has to agree — otherwise the feature is configured and
+ // the page has to agree - otherwise the feature is configured and
// invisible, which is worse than not configured at all.
$db = wallos_test_open_database();
@@ -338,3 +338,73 @@ wallos_test('every reader of the instance mail settings goes through one place',
$path . ' does not read the admin row directly any more');
}
});
+
+wallos_test('a managed field a form posted empty is filled in from the effective settings', function () {
+ // What the admin page does with the password field specifically: the input
+ // is rendered blank when the deployment owns it, so the browser posts an
+ // empty string for a field that is, in fact, configured.
+ $fieldColumns = ['smtppassword' => 'smtp_password', 'fromemail' => 'from_email'];
+ $managedFields = ['smtp_password' => 'WALLOS_SMTP_PASSWORD'];
+ $settings = ['smtp_password' => 'from-the-environment', 'from_email' => '[email protected]'];
+
+ $filled = wallos_fill_managed_form_fields(
+ ['smtppassword' => '', 'fromemail' => ''],
+ $fieldColumns,
+ $managedFields,
+ $settings
+ );
+
+ assert_same('from-the-environment', $filled['smtppassword'],
+ 'the empty managed field is filled in from the effective settings');
+ assert_same('', $filled['fromemail'],
+ 'a field the environment does not own is left exactly as posted, empty or not');
+});
+
+wallos_test('a managed field is left alone when the form actually posted a value', function () {
+ // Deciding what the server trusts belongs to the save endpoint, not this
+ // helper: a script can still post a value into a disabled field, and this
+ // is not the place that rejects it.
+ $filled = wallos_fill_managed_form_fields(
+ ['smtppassword' => 'typed-by-hand'],
+ ['smtppassword' => 'smtp_password'],
+ ['smtp_password' => 'WALLOS_SMTP_PASSWORD'],
+ ['smtp_password' => 'from-the-environment']
+ );
+
+ assert_same('typed-by-hand', $filled['smtppassword'],
+ 'a posted value is never overwritten, managed field or not');
+});
+
+wallos_test('an unmanaged field that was posted empty stays empty', function () {
+ // Nothing here should manufacture a value for an installation that has not
+ // asked for any of this - every field is unmanaged, so nothing changes.
+ $filled = wallos_fill_managed_form_fields(
+ ['smtppassword' => ''],
+ ['smtppassword' => 'smtp_password'],
+ [],
+ ['smtp_password' => 'unused']
+ );
+
+ assert_same('', $filled['smtppassword'], 'no field is managed, so nothing is filled in');
+});
+
+wallos_test('the SMTP test endpoint fills in managed fields before it validates or authenticates', function () {
+ // The defect this exists for: an installation with WALLOS_SMTP_PASSWORD set
+ // shows the password field as managed and blank, and pressing Test posted
+ // that blank straight to PHPMailer - failing against a mail server that
+ // was, in fact, configured correctly.
+ $source = file_get_contents(WALLOS_ROOT . '/endpoints/notifications/testemailnotifications.php');
+
+ assert_contains('instance_config.php', $source, 'the endpoint reads the instance configuration');
+ assert_contains('wallos_fill_managed_form_fields($data', $source,
+ 'posted fields are passed through the managed-field fallback');
+
+ $fallbackAt = strpos($source, 'wallos_fill_managed_form_fields($data');
+ $fillAllFieldsAt = strpos($source, 'fill_all_fields');
+ $smtpAuthAt = strpos($source, '$smtpAuth =');
+
+ assert_true($fallbackAt !== false && $fallbackAt < $fillAllFieldsAt,
+ 'the fallback runs before the endpoint can reject the request for looking unconfigured');
+ assert_true($fallbackAt < $smtpAuthAt,
+ 'the fallback runs before smtpAuth is decided, or a managed password never turns auth on');
+});