|
1 | 1 | from unittest import TestCase |
2 | 2 | from unittest.mock import MagicMock, patch |
3 | 3 |
|
4 | | -from pyinfra.api import Config, State |
| 4 | +from pyinfra.api import Config, State, HiddenValue |
5 | 5 | from pyinfra.connectors.util import ( |
6 | 6 | CommandOutput, |
7 | 7 | OutputLine, |
@@ -147,6 +147,40 @@ def test_command_env_injection_attempt(self): |
147 | 147 | == "sh -c 'export '\"'\"'KEY=value\"; id; echo \"'\"'\"' && uptime'" |
148 | 148 | ) |
149 | 149 |
|
| 150 | + def test_command_env_hidden_value(self): |
| 151 | + command = make_unix_command( |
| 152 | + "uptime", |
| 153 | + _env={ |
| 154 | + "KEY": HiddenValue("super-secret"), |
| 155 | + }, |
| 156 | + ) |
| 157 | + |
| 158 | + raw = command.get_raw_value() |
| 159 | + masked = command.get_masked_value() |
| 160 | + assert "super-secret" in raw |
| 161 | + assert "super-secret" not in masked |
| 162 | + assert "*MASKED*" in masked |
| 163 | + assert "KEY=*MASKED*" in masked |
| 164 | + |
| 165 | + def test_command_env_hidden_value_with_shell_chars(self): |
| 166 | + command = make_unix_command( |
| 167 | + "uptime", |
| 168 | + _env={ |
| 169 | + "KEY": HiddenValue("abc; id; echo bad"), |
| 170 | + }, |
| 171 | + ) |
| 172 | + |
| 173 | + raw = command.get_raw_value() |
| 174 | + masked = command.get_masked_value() |
| 175 | + |
| 176 | + assert "abc; id; echo bad" in raw |
| 177 | + assert "abc; id; echo bad" not in masked |
| 178 | + assert "*MASKED*" in masked |
| 179 | + assert "KEY=*MASKED*" in masked |
| 180 | + |
| 181 | + # The raw command should still quote the assignment as one shell token. |
| 182 | + assert "'\"'\"'KEY=abc; id; echo bad'\"'\"'" in raw |
| 183 | + |
150 | 184 | def test_command_chdir(self): |
151 | 185 | command = make_unix_command("uptime", _chdir="/opt/somedir") |
152 | 186 | assert command.get_raw_value() == "sh -c 'cd /opt/somedir && uptime'" |
|
0 commit comments