From fd1e9b67ddb941bf8b97ec06d7222eb5a6d7d459 Mon Sep 17 00:00:00 2001 From: Defame1297 Date: Mon, 23 Nov 2026 14:51:13 +0000 Subject: [PATCH] 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 --- .../molecule/default/tasks/failed_run.yml | 3 ++ roles/cloudflared/molecule/default/verify.yml | 18 +++++++++ roles/cloudflared/tasks/validate.yml | 38 +++++++++++++++++++ 3 files changed, 59 insertions(+) diff --git a/roles/cloudflared/molecule/default/tasks/failed_run.yml b/roles/cloudflared/molecule/default/tasks/failed_run.yml index abcde37..b94f8b0 100644 --- a/roles/cloudflared/molecule/default/tasks/failed_run.yml +++ b/roles/cloudflared/molecule/default/tasks/failed_run.yml @@ -20,6 +20,9 @@ cloudflared_manage_dns: "{{ cloudflared_test_case.vars.cloudflared_manage_dns | default(false) }}" cloudflared_zone_name: "{{ cloudflared_test_case.vars.cloudflared_zone_name | default('') }}" cloudflared_ingress: "{{ cloudflared_test_case.vars.cloudflared_ingress | default(cloudflared_test_ingress) }}" + cloudflared_user: "{{ cloudflared_test_case.vars.cloudflared_user | default('cloudflared') }}" + cloudflared_install_dir: "{{ cloudflared_test_case.vars.cloudflared_install_dir | default('/usr/local/bin') }}" + cloudflared_config_dir: "{{ cloudflared_test_case.vars.cloudflared_config_dir | default('/etc/cloudflared') }}" # Only reached when the include returned. The message must not contain the expected # text, so the rescue below cannot mistake it for the failure under test. diff --git a/roles/cloudflared/molecule/default/verify.yml b/roles/cloudflared/molecule/default/verify.yml index f55b4a9..8d5c5ae 100644 --- a/roles/cloudflared/molecule/default/verify.yml +++ b/roles/cloudflared/molecule/default/verify.yml @@ -139,6 +139,24 @@ - name: ingress-without-service vars: {cloudflared_ingress: [{hostname: app.example.test}]} expect: each with hostname and service + - name: hostname-uppercase + vars: {cloudflared_ingress: [{hostname: App.example.test, service: "http://127.0.0.1:8080"}]} + expect: must be lowercase letters + - name: hostname-whitespace + vars: {cloudflared_ingress: [{hostname: "app.example.test\n - service: x", service: "http://127.0.0.1:8080"}]} + expect: must be lowercase letters + - name: user-with-percent + vars: {cloudflared_user: "cloud%flared"} + expect: must not contain a newline or % + - name: config-dir-with-newline + vars: {cloudflared_config_dir: "/etc/cloudflared\nExecStartPre=/bin/false"} + expect: must not contain a newline or % + - name: install-dir-relative + vars: {cloudflared_install_dir: usr/local/bin} + expect: the two directories must be absolute paths + - name: tunnel-id-not-uuid + vars: {cloudflared_tunnel_id: not-a-uuid} + expect: must be a lowercase UUID - name: tunnel-not-minted vars: {cloudflared_tunnel_id: ""} expect: run the mint entry point first diff --git a/roles/cloudflared/tasks/validate.yml b/roles/cloudflared/tasks/validate.yml index 6199108..af69983 100644 --- a/roles/cloudflared/tasks/validate.yml +++ b/roles/cloudflared/tasks/validate.yml @@ -35,6 +35,34 @@ 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 @@ -50,6 +78,16 @@ 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