Debian Codegraph @

src/luks/tests/get-hash-validation

raw
1 #!/bin/bash -ex
2 #
3 # Copyright (c) 2026 Red Hat, Inc.
4 # Author: Sergio Arroutbi <[email protected]>
5 #
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18
19 # Test the hash validation used in clevis_luks_get_hash().
20 # This test does not require root or LUKS devices — it exercises the
21 # validation logic directly.
22
23 TEST=$(basename "${0}")
24 . luks-common-test-functions
25
26 # is_valid_hash() mirrors the validation logic in clevis_luks_get_hash():
27 # case "${hash}" in *[!a-zA-Z0-9_-]*|"") return 1;; esac
28 is_valid_hash() {
29 local hash="${1}"
30 case "${hash}" in
31 *[!a-zA-Z0-9_-]*|"") return 1;;
32 esac
33 return 0
34 }
35
36 # Test valid hashes — these must pass validation.
37 for h in sha256 sha512 sha384 sha1 sha224 ripemd160 \
38 SHA256 SHA512 whirlpool blake2b-256 sha3_256; do
39 if ! is_valid_hash "${h}"; then
40 error "${TEST}: valid hash '${h}' was rejected."
41 fi
42 done
43
44 # Test invalid hashes — injection attempts and malformed values.
45 # Each is tested individually to handle special characters safely.
46
47 if is_valid_hash 'sha256; rm -rf /'; then
48 error "${TEST}: 'sha256; rm -rf /' should be rejected (semicolon)."
49 fi
50
51 if is_valid_hash 'sha256$(reboot)'; then
52 error "${TEST}: 'sha256\$(reboot)' should be rejected (command substitution)."
53 fi
54
55 if is_valid_hash 'sha256`reboot`'; then
56 error "${TEST}: 'sha256\`reboot\`' should be rejected (backticks)."
57 fi
58
59 if is_valid_hash 'sha256 --force'; then
60 error "${TEST}: 'sha256 --force' should be rejected (space)."
61 fi
62
63 if is_valid_hash 'sha256|cat /etc/shadow'; then
64 error "${TEST}: 'sha256|cat /etc/shadow' should be rejected (pipe)."
65 fi
66
67 if is_valid_hash 'sha256&bg_cmd'; then
68 error "${TEST}: 'sha256&bg_cmd' should be rejected (ampersand)."
69 fi
70
71 if is_valid_hash '--hash sha256'; then
72 error "${TEST}: '--hash sha256' should be rejected (space)."
73 fi
74
75 if is_valid_hash ''; then
76 error "${TEST}: empty string should be rejected."
77 fi