ansible-sshd/tests/tests_hostkeys_unsafe_path.yml
Jakub Jelen d10f2ada11
fix: use quote with command, shell and validate with variable (#298)
* robustness: quote test backup/restore file names

This avoids issues if file names are not safepaths.

* security: use quote with command, shell and validate with variable

Skip quotation only if variable is checked.

Add test suit to excercise some quote use cases.

* robustness: fail if systemd.unit could have something in need of quote

Ensure systemd.unit contents is robust. This disables possibility to
have something that needs to be quoted there. But as ansible lacks
proper way to quote systemd unit files (see man systemd.syntax, rules
are not shell rules), it is better to fail such configs. If you are
trying to do that, you are doing it wrong anyway or have malicious
intent.

Also ensure similar issue with sysctl.conf.

Issue can be seen with `tests_hostkeys_unsafe_path.yml`, when adding
following to role params:

       sshd_install_service: true
       sshd_config_file: "{{ ansible_facts.env.TMPDIR }}/sshd.d/foo.conf"
       sshd_binary: "{{ ansible_facts.env.TMPDIR }}/sshd"
       __sshd_runtime_directory: "{{ ansible_facts.env.TMPDIR }}/run"

* tests: Quote also the source filename

Signed-off-by: Jakub Jelen <jjelen@redhat.com>

* tests: Add more negative test cases

Signed-off-by: Jakub Jelen <jjelen@redhat.com>

* tests: Skip the test with unsafe TMPDIR as it does not work on CentOS8

Signed-off-by: Jakub Jelen <jjelen@redhat.com>

* Move the variable checks to separate file ...

... and explain better why this is problematic

Drops also the check for internal variables as the user should not
bother with these.

Signed-off-by: Jakub Jelen <jjelen@redhat.com>

---------

Signed-off-by: Jakub Jelen <jjelen@redhat.com>
Co-authored-by: Markus Linnala <Markus.Linnala@knowit.fi>
2024-09-12 07:24:22 +01:00

70 lines
2 KiB
YAML

---
- name: Test quote with unsafe input
hosts: all
environment:
TMPDIR: "{{ __tmpdir }}"
vars:
__sshd_test_backup_files:
- /etc/ssh/sshd_config
- /etc/ssh/sshd_config.d/00-ansible_system_role.conf
__badflag_file: /tmp/BADFLAG
# Avoid / in TMPDIR file name
__badflag: >-
$(touch -- "$(echo {{ __badflag_file | b64encode }} | base64 -d)")
# Iterate w/o quote, w/ ' and w/ "
__tmpdir: >-
/tmp/a {{ __badflag }} ' {{ __badflag }} '" {{ __badflag }} "b
tasks:
- name: Ensure BADFLAG does not exist
ansible.builtin.file:
path: /tmp/BADFLAG
state: absent
- name: Assert TMPDIR is correctly set
ansible.builtin.assert:
that:
- __tmpdir != ''
- ansible_facts.env.TMPDIR == __tmpdir
- name: "Backup configuration files"
ansible.builtin.include_tasks: tasks/backup.yml
- name: Create BAD TMPDIR
ansible.builtin.file:
state: directory
path: "{{ ansible_facts.env.TMPDIR }}"
mode: '0755'
- name: Configure sshd with BAD config
ansible.builtin.include_role:
name: ansible-sshd
vars:
sshd_skip_defaults: true
sshd_verify_hostkeys: []
when:
- ansible_facts['os_family'] != 'RedHat' or ansible_facts['distribution_major_version'] | int != 8
- name: Verify the options are correctly set
tags: tests::verify
block:
- name: Flush handlers
ansible.builtin.meta: flush_handlers
- name: Get status BADFLAG
ansible.builtin.stat:
path: "{{ __badflag_file }}"
register: badflag
- name: Ensure BADFLAG does not exist
ansible.builtin.assert:
that:
- not badflag.stat.exists
- name: Remove BAD TMPDIR
ansible.builtin.file:
state: absent
path: "{{ ansible_facts.env.TMPDIR }}"
- name: "Restore configuration files"
ansible.builtin.include_tasks: tasks/restore.yml