From cb78edff6f505e84ab141fcf41e81ca8874f2bef Mon Sep 17 00:00:00 2001 From: Defame1297 Date: Sun, 6 Dec 2026 21:35:12 +0000 Subject: [PATCH] fix(pihole)!: refuse a non-empty app password and render app_pwhash empty Released pihole-FTL checks an app_pwhash that does not start with `$` as a legacy double-SHA256 hash and, on a match, upgrades it to a BALLOON hash written into the main webserver.api.pwhash. One login with the app password the role rendered therefore replaced the web password. FTL master fixes this, no release has it yet, and the role does not pin FTL. validate.yml now refuses a non-empty config.webserver.api.app_password, and the template always renders app_pwhash = "", which FTL treats as app passwords disabled. The key stays in the defaults; only a value is refused. Molecule verifies the rendered empty hash and the new refusal. BREAKING CHANGE: a non-empty config.webserver.api.app_password now fails the role, and app_pwhash always renders empty. Refs: #383 Assisted-by: Claude Code --- changelogs/fragments/383-pihole-app-password.yml | 7 +++++++ roles/pihole/README.md | 3 ++- roles/pihole/defaults/main.yml | 2 +- roles/pihole/meta/argument_specs.yml | 6 +++++- roles/pihole/molecule/default/verify.yml | 9 ++++++++- roles/pihole/tasks/validate.yml | 11 +++++++++++ roles/pihole/templates/pihole.toml.j2 | 6 +++--- 7 files changed, 37 insertions(+), 7 deletions(-) create mode 100644 changelogs/fragments/383-pihole-app-password.yml diff --git a/changelogs/fragments/383-pihole-app-password.yml b/changelogs/fragments/383-pihole-app-password.yml new file mode 100644 index 0000000..97ce452 --- /dev/null +++ b/changelogs/fragments/383-pihole-app-password.yml @@ -0,0 +1,7 @@ +--- +breaking_changes: + - >- + pihole - A non-empty ``config.webserver.api.app_password`` is now refused, and ``app_pwhash`` + always renders empty, which disables app passwords. Released pihole-FTL upgrades a + legacy-hashed app password on login by writing the new hash into the main ``pwhash``, so one + login with the app password replaced the web password. diff --git a/roles/pihole/README.md b/roles/pihole/README.md index 5eb2f6d..6a08a31 100644 --- a/roles/pihole/README.md +++ b/roles/pihole/README.md @@ -58,13 +58,14 @@ The input asserts carry `always` (and `assert`), so they run whatever tags are s ## Tests -The `default` molecule scenario runs in CI only, on two platforms: `${MOLECULE_IMAGE}` (Ubuntu 24.04) and `${MOLECULE_IMAGE_2604}` (Ubuntu 26.04). It replaces the upstream repository with a local stub whose installer only logs its arguments and creates the database file, so it tests the merge, the asserts, the user, the configuration directory and the rendered `pihole.toml` (owner, group and mode), the clone's checked-out version, the installer sequence (install once, then reconfigure from the handler) and idempotence, not Pi-hole itself: no pihole-FTL service runs, so that the service loaded the configuration is not verified. It runs `check` before the converge (a fresh host) and after it (a configured host). Negative tests in `verify.yml` cover a host without Ubuntu facts, a missing `pihole_web_password`, an unset interface, empty upstreams, a non-string password and the placeholder password. +The `default` molecule scenario runs in CI only, on two platforms: `${MOLECULE_IMAGE}` (Ubuntu 24.04) and `${MOLECULE_IMAGE_2604}` (Ubuntu 26.04). It replaces the upstream repository with a local stub whose installer only logs its arguments and creates the database file, so it tests the merge, the asserts, the user, the configuration directory and the rendered `pihole.toml` (owner, group and mode), the clone's checked-out version, the installer sequence (install once, then reconfigure from the handler) and idempotence, not Pi-hole itself: no pihole-FTL service runs, so that the service loaded the configuration is not verified. It runs `check` before the converge (a fresh host) and after it (a configured host). Negative tests in `verify.yml` cover a host without Ubuntu facts, a missing `pihole_web_password`, an unset interface, empty upstreams, a non-string password, the placeholder password and a non-empty app password. ## Notes - Idempotent: yes. - Merge model: the role sets an internal fact to `pihole_default | combine(pihole_overrides | default({}), recursive=True)`. The input is only read, never overwritten, so each run recomputes the merge. Dictionaries are not merged across variable sources, so define `pihole_overrides` in one place only. - Secrets: the password is hashed twice with SHA-256 in the template (`pwhash`). The merge and both template tasks use `no_log`, and the templates `diff: false`, so a `pihole.toml` change shows no diff. `pihole_testing` masks the web password, app password and TOTP secret in its output. `totp_secret` and `app_password` default to empty strings. +- App passwords: `config.webserver.api.app_password` must stay empty, and the role fails otherwise; `app_pwhash` always renders empty, which disables app passwords. Released pihole-FTL upgrades a legacy-hashed (double SHA-256) app password on login by writing the new hash into the main `pwhash`, so one login with the app password would replace the web password. - Install flow: the role creates the `pihole` system user (nologin), the configuration directory and, on first install, `pihole.toml`, because the unattended installer needs it first. It clones `repo.version` and runs the installer with `--unattended` only when `config.files.database` does not exist yet. A change to `pihole.toml` notifies the `reconfigure pihole` handler, which runs the installer with `--reconfigure --unattended`. - Updates: the role does not upgrade an installed Pi-hole; changing `repo.version` only moves the clone. - Check mode: `--check` is the supported preview; a play-level `check_mode: true` is not supported. On a host where the configuration directory does not exist yet, the `pihole.toml` template tasks are skipped under `--check`, because the directory they write into is not created. diff --git a/roles/pihole/defaults/main.yml b/roles/pihole/defaults/main.yml index abdfdd4..d90fddd 100644 --- a/roles/pihole/defaults/main.yml +++ b/roles/pihole/defaults/main.yml @@ -308,7 +308,7 @@ pihole_default: password: "{{ pihole_web_password }}" # Pi-hole 2FA TOTP secret. totp_secret: "" - # Pi-hole application password (to circumvent 2FA for legacy 3rd-party services) + # Pi-hole application password: must stay empty, app_pwhash always renders empty (README Notes) app_password: "" # Should application password API sessions be allowed to modify config settings? app_sudo: false diff --git a/roles/pihole/meta/argument_specs.yml b/roles/pihole/meta/argument_specs.yml index a0ffda9..b319e6e 100644 --- a/roles/pihole/meta/argument_specs.yml +++ b/roles/pihole/meta/argument_specs.yml @@ -47,7 +47,11 @@ argument_specs: C(config.dns.interface) must be set (string, for example C(eth0)): its default is an empty placeholder and the role fails until it is set. C(config.dns.upstreams) (list of IP addresses or hostnames, optionally with C(#port)) must stay non-empty. C(config.webserver.api.password) must stay a string. - C(config.webserver.api.totp_secret) and C(config.webserver.api.app_password) are also credentials. + C(config.webserver.api.totp_secret) is also a credential. + - >- + C(config.webserver.api.app_password) must stay empty and C(app_pwhash) always renders empty, which + disables app passwords: released pihole-FTL replaces the main web password on a login with a + legacy-hashed app password. - >- Other keys the tasks read are C(repo.remote), C(repo.version) (a tag, default C(v6.4)), C(repo.path) (default C(~/.cache/pi-hole) of the connecting user), C(user), C(group) (both default C(pihole)) and diff --git a/roles/pihole/molecule/default/verify.yml b/roles/pihole/molecule/default/verify.yml index 284c2bd..b76a069 100644 --- a/roles/pihole/molecule/default/verify.yml +++ b/roles/pihole/molecule/default/verify.yml @@ -56,7 +56,7 @@ database: passwd key: pihole - - name: The config carries the overridden upstreams and a hashed password + - name: The config carries the overridden upstreams, a hashed password and no app password ansible.builtin.assert: that: - "'9.9.9.9' in pihole_test_toml_content" @@ -66,6 +66,7 @@ - "'\"eth0\"' not in pihole_test_toml_content" - "'molecule-test-password' not in pihole_test_toml_content" - pihole_test_toml_content is search('(?m)^\\s*pwhash = \"[0-9a-f]{64}\"') + - pihole_test_toml_content is search('(?m)^\\s*app_pwhash = \"\"$') quiet: true vars: pihole_test_toml_content: "{{ pihole_test_toml.content | b64decode }}" @@ -167,3 +168,9 @@ dns: {interface: molecule0} webserver: {api: {password: changetoyourownpassword}} expect: placeholder changetoyourownpassword + - name: app-password-set + overrides: + config: + dns: {interface: molecule0} + webserver: {api: {password: molecule-test-password, app_password: molecule-test-app-password}} + expect: app_password must stay empty diff --git a/roles/pihole/tasks/validate.yml b/roles/pihole/tasks/validate.yml index 32b40b2..80f18e9 100644 --- a/roles/pihole/tasks/validate.yml +++ b/roles/pihole/tasks/validate.yml @@ -53,3 +53,14 @@ The web password is the placeholder changetoyourownpassword: set pihole_web_password, or pihole_overrides.config.webserver.api.password, to a secret of your own. quiet: true + + # Released pihole-FTL upgrades a legacy-hashed app password on login by writing the new hash + # into the main pwhash, so the role renders app_pwhash empty, which disables app passwords. + - name: Check that no app password is set + ansible.builtin.assert: + that: + - (__pihole_candidate.config.webserver.api.app_password | default('', true)) == '' + fail_msg: >- + config.webserver.api.app_password must stay empty: released pihole-FTL replaces the main + web password when someone logs in with an app password the role has hashed (see #541). + quiet: true diff --git a/roles/pihole/templates/pihole.toml.j2 b/roles/pihole/templates/pihole.toml.j2 index 14c6be4..cc94787 100644 --- a/roles/pihole/templates/pihole.toml.j2 +++ b/roles/pihole/templates/pihole.toml.j2 @@ -838,9 +838,9 @@ # # Possible values are: # - # NOTE! I am assuming this should be double-hashed just like the regular password, but - # this assumption has not been tested! - app_pwhash = "{{ __pihole_effective.config.webserver.api.app_password | hash('sha256') | hash('sha256') }}" + # Always empty, which disables app passwords: the role refuses app_password, because + # released pihole-FTL rewrites the main pwhash on a login with a legacy-hashed app password. + app_pwhash = "" # Should application password API sessions be allowed to modify config settings? # Setting this to true allows third-party applications using the application password