Files
Defame1297 fd1e9b67dd fix(cloudflared): refuse unsafe hostnames, names, paths and tunnel ids
Ingress hostnames must be lowercase, with an optional leading *. and no
whitespace: they land unquoted in config.yml. The service account names
and directories must hold no newline or %, which the unit would read as
directives, and the directories must be absolute. A set tunnel id must
be a lowercase UUID, the shape the stale-credentials search relies on.
Each refusal has a negative case in the scenario.

Assisted-by: Claude Code:claude-opus-5-5
2026-11-23 14:51:13 +00:00

120 lines
5.4 KiB
YAML

---
# Input checks. Imported first in main.yml with tags: always, so a bad input fails before any
# change, whatever tags the run selects.
- name: Require a plain cloudflared_version
ansible.builtin.assert:
that:
- cloudflared_version is string
- cloudflared_version is match('^[0-9]{4}\\.[0-9]+\\.[0-9]+$')
fail_msg: >-
cloudflared_version must be a release tag such as 2026.11.1 (YYYY.M.P), with no leading v: it is
used in the download URL and compared with what the binary reports.
quiet: true
- name: Require a sha256 for the binary
ansible.builtin.assert:
that:
- cloudflared_sha256_amd64 is string
- cloudflared_sha256_amd64 is match('^[0-9a-fA-F]{64}$')
fail_msg: >-
cloudflared_sha256_amd64 must be the 64-digit hex sha256 of the cloudflared-linux-amd64 asset of
release {{ cloudflared_version }}.
quiet: true
# The items land in config.yml and, with cloudflared_manage_dns, become DNS records.
- name: Require a hostname and a service for every ingress rule
ansible.builtin.assert:
that:
- cloudflared_ingress is sequence
- cloudflared_ingress is not string
- cloudflared_ingress is not mapping
- cloudflared_ingress | reject('mapping') | list | length == 0
- cloudflared_ingress | selectattr('hostname', 'defined') | selectattr('service', 'defined') | list | length == cloudflared_ingress | length
fail_msg: >-
cloudflared_ingress must be a list of mappings, each with hostname and service (and optionally
originRequest).
quiet: true
# A hostname lands unquoted in config.yml and is compared with DNS record names.
- name: Require plain lowercase ingress hostnames
ansible.builtin.assert:
that:
- cloudflared_ingress | map(attribute='hostname') | reject('string') | list | length == 0
- cloudflared_ingress | map(attribute='hostname') | reject('match', '^(\\*\\.)?[a-z0-9_.-]+\\Z') | list | length == 0
fail_msg: >-
Every cloudflared_ingress hostname must be lowercase letters, digits, dots, hyphens or underscores,
with an optional leading *. and no whitespace or newline.
quiet: true
# The values land in the systemd unit and in file paths: a newline or % would inject or expand
# unit directives.
- name: Require plain service account names and absolute directories
ansible.builtin.assert:
that:
- cloudflared_user is string
- cloudflared_group is string
- cloudflared_install_dir is string
- cloudflared_config_dir is string
- (cloudflared_user ~ cloudflared_group ~ cloudflared_install_dir ~ cloudflared_config_dir) is not search('[\\r\\n%]')
- cloudflared_install_dir is match('/')
- cloudflared_config_dir is match('/')
fail_msg: >-
cloudflared_user, cloudflared_group, cloudflared_install_dir and cloudflared_config_dir must not contain a
newline or %, and the two directories must be absolute paths.
quiet: true
# The role never creates the tunnel on its main entry point. Defined and empty (or null) counts
# as missing, as in the mint entry point, so the two cannot disagree.
- name: Require the minted tunnel secrets
ansible.builtin.assert:
that:
- cloudflared_tunnel_id | default('', true) | string | length > 0
- cloudflared_credentials_json | default('', true) | string | length > 0
fail_msg: >-
cloudflared_tunnel_id and cloudflared_credentials_json are not defined or empty: run the mint entry
point first (include_role with tasks_from: mint and -e cloudflared_mint_confirm=true), persist
cloudflared_minted_tunnel_id and cloudflared_minted_credentials_json into your inventory as these two
variables, and run the role again.
quiet: true
when: not (cloudflared_remove | bool)
# The id names the credentials file, and stale credentials are found by the UUID shape.
- name: Require a UUID tunnel id
ansible.builtin.assert:
that:
- >-
cloudflared_tunnel_id | default('', true) | string | length == 0 or
cloudflared_tunnel_id | string is match('^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\\Z')
fail_msg: cloudflared_tunnel_id must be a lowercase UUID, as the mint entry point returns it.
quiet: true
# Every Cloudflare call below needs them: the tunnel check on every run, the tunnel deletion
# of a removal with cloudflared_remove_data.
- name: Require the Cloudflare API credentials
ansible.builtin.assert:
that:
- cloudflared_account_id | default('', true) | string | length > 0
- cloudflared_tunnel_api_token | default('', true) | string | length > 0
fail_msg: >-
cloudflared_account_id and cloudflared_tunnel_api_token (vaulted) have no role default and must be set
and non-empty.
quiet: true
when: >-
not (cloudflared_remove | bool) or
((cloudflared_remove_data | bool) and (cloudflared_tunnel_id | default('', true) | string | length) > 0)
- name: Require the zone while the role manages DNS
ansible.builtin.assert:
that:
- cloudflared_zone_name is string
- cloudflared_zone_name | length > 0
fail_msg: >-
cloudflared_manage_dns is true, so cloudflared_zone_name must name the DNS zone of the
cloudflared_ingress hostnames.
quiet: true
when:
- cloudflared_manage_dns | bool
- >-
not (cloudflared_remove | bool) or
((cloudflared_remove_data | bool) and (cloudflared_tunnel_id | default('', true) | string | length) > 0)