|
| 1 | +import os |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from conftest import assert_bash_exec, assert_complete, bash_env_saved |
| 6 | + |
| 7 | + |
| 8 | +@pytest.mark.bashcomp(cmd=None, temp_cwd=True) |
| 9 | +class TestUnitQuoteCompgen: |
| 10 | + @pytest.fixture(scope="class") |
| 11 | + def functions(self, bash): |
| 12 | + assert_bash_exec( |
| 13 | + bash, |
| 14 | + '_comp__test_quote_compgen() { local ret; _comp_quote_compgen "$1"; printf %s "$ret"; }', |
| 15 | + ) |
| 16 | + |
| 17 | + @pytest.mark.parametrize( |
| 18 | + "funcname", "_comp__test_quote_compgen quote_readline".split() |
| 19 | + ) |
| 20 | + def test_exec(self, bash, functions, funcname): |
| 21 | + assert_bash_exec(bash, "%s '' >/dev/null" % funcname) |
| 22 | + |
| 23 | + @pytest.mark.parametrize( |
| 24 | + "funcname", "_comp__test_quote_compgen quote_readline".split() |
| 25 | + ) |
| 26 | + def test_env_non_pollution(self, bash, functions, funcname): |
| 27 | + """Test environment non-pollution, detected at teardown.""" |
| 28 | + assert_bash_exec( |
| 29 | + bash, "foo() { %s meh >/dev/null; }; foo; unset -f foo" % funcname |
| 30 | + ) |
| 31 | + |
| 32 | + @pytest.mark.parametrize( |
| 33 | + "funcname", "_comp__test_quote_compgen quote_readline".split() |
| 34 | + ) |
| 35 | + def test_1(self, bash, functions, funcname): |
| 36 | + output = assert_bash_exec( |
| 37 | + bash, "%s '';echo" % funcname, want_output=True |
| 38 | + ) |
| 39 | + assert output.strip() == "''" |
| 40 | + |
| 41 | + @pytest.mark.parametrize( |
| 42 | + "funcname", "_comp__test_quote_compgen quote_readline".split() |
| 43 | + ) |
| 44 | + def test_2(self, bash, functions, funcname): |
| 45 | + output = assert_bash_exec( |
| 46 | + bash, "%s foo;echo" % funcname, want_output=True |
| 47 | + ) |
| 48 | + assert output.strip() == "foo" |
| 49 | + |
| 50 | + @pytest.mark.parametrize( |
| 51 | + "funcname", "_comp__test_quote_compgen quote_readline".split() |
| 52 | + ) |
| 53 | + def test_3(self, bash, functions, funcname): |
| 54 | + output = assert_bash_exec( |
| 55 | + bash, '%s foo\\"bar;echo' % funcname, want_output=True |
| 56 | + ) |
| 57 | + assert output.strip() == 'foo\\"bar' |
| 58 | + |
| 59 | + @pytest.mark.parametrize( |
| 60 | + "funcname", "_comp__test_quote_compgen quote_readline".split() |
| 61 | + ) |
| 62 | + def test_4(self, bash, functions, funcname): |
| 63 | + output = assert_bash_exec( |
| 64 | + bash, "%s '$(echo x >&2)';echo" % funcname, want_output=True |
| 65 | + ) |
| 66 | + assert output.strip() == "\\$\\(echo\\ x\\ \\>\\&2\\)" |
| 67 | + |
| 68 | + def test_github_issue_189_1(self, bash, functions): |
| 69 | + """Test error messages on a certain command line |
| 70 | +
|
| 71 | + Reported at https://github.com/scop/bash-completion/issues/189 |
| 72 | +
|
| 73 | + Syntax error messages should not be shown by completion on the |
| 74 | + following line: |
| 75 | +
|
| 76 | + $ ls -- '${[TAB] |
| 77 | + $ rm -- '${[TAB] |
| 78 | +
|
| 79 | + """ |
| 80 | + assert_bash_exec(bash, "_comp__test_quote_compgen $'\\'${' >/dev/null") |
| 81 | + |
| 82 | + def test_github_issue_492_1(self, bash, functions): |
| 83 | + """Test unintended code execution on a certain command line |
| 84 | +
|
| 85 | + Reported at https://github.com/scop/bash-completion/pull/492 |
| 86 | +
|
| 87 | + Arbitrary commands could be unintendedly executed by |
| 88 | + _comp_quote_compgen. In the following example, the command "touch |
| 89 | + 1.txt" would be unintendedly created before the fix. The file "1.txt" |
| 90 | + should not be created by completion on the following line: |
| 91 | +
|
| 92 | + $ echo '$(touch file.txt)[TAB] |
| 93 | +
|
| 94 | + """ |
| 95 | + assert_bash_exec( |
| 96 | + bash, "_comp__test_quote_compgen $'\\'$(touch 1.txt)' >/dev/null" |
| 97 | + ) |
| 98 | + assert not os.path.exists("./1.txt") |
| 99 | + |
| 100 | + def test_github_issue_492_2(self, bash, functions): |
| 101 | + """Test the file clear by unintended redirection on a certain command line |
| 102 | +
|
| 103 | + Reported at https://github.com/scop/bash-completion/pull/492 |
| 104 | +
|
| 105 | + The file "1.0" should not be created by completion on the following |
| 106 | + line: |
| 107 | +
|
| 108 | + $ awk '$1 > 1.0[TAB] |
| 109 | +
|
| 110 | + """ |
| 111 | + assert_bash_exec( |
| 112 | + bash, "_comp__test_quote_compgen $'\\'$1 > 1.0' >/dev/null" |
| 113 | + ) |
| 114 | + assert not os.path.exists("./1.0") |
| 115 | + |
| 116 | + def test_github_issue_492_3(self, bash, functions): |
| 117 | + """Test code execution through unintended pathname expansions |
| 118 | +
|
| 119 | + When there is a file named "quote=$(COMMAND)" (for _filedir) or |
| 120 | + "ret=$(COMMAND)" (for _comp_quote_compgen), the completion of the word |
| 121 | + '$* results in the execution of COMMAND. |
| 122 | +
|
| 123 | + $ echo '$*[TAB] |
| 124 | +
|
| 125 | + """ |
| 126 | + os.mkdir("./ret=$(echo injected >&2)") |
| 127 | + assert_bash_exec(bash, "_comp__test_quote_compgen $'\\'$*' >/dev/null") |
| 128 | + |
| 129 | + def test_github_issue_492_4(self, bash, functions): |
| 130 | + """Test error messages through unintended pathname expansions |
| 131 | +
|
| 132 | + When "shopt -s failglob" is set by the user, the completion of the word |
| 133 | + containing glob character and special characters (e.g. TAB) results in |
| 134 | + the failure of pathname expansions. |
| 135 | +
|
| 136 | + $ shopt -s failglob |
| 137 | + $ echo a\\ b*[TAB] |
| 138 | +
|
| 139 | + """ |
| 140 | + with bash_env_saved(bash) as bash_env: |
| 141 | + bash_env.shopt("failglob", True) |
| 142 | + assert_bash_exec( |
| 143 | + bash, "_comp__test_quote_compgen $'a\\\\\\tb*' >/dev/null" |
| 144 | + ) |
| 145 | + |
| 146 | + def test_github_issue_526_1(self, bash): |
| 147 | + r"""Regression tests for unprocessed escape sequences after quotes |
| 148 | +
|
| 149 | + Ref [1] https://github.com/scop/bash-completion/pull/492#discussion_r637213822 |
| 150 | + Ref [2] https://github.com/scop/bash-completion/pull/526 |
| 151 | +
|
| 152 | + The escape sequences in the local variable of "value" in |
| 153 | + "_comp_quote_compgen" needs to be unescaped by passing it to printf as |
| 154 | + the format string. This causes a problem in the following case [where |
| 155 | + the spaces after "alpha\" is a TAB character inserted in the command |
| 156 | + string by "C-v TAB"]: |
| 157 | +
|
| 158 | + $ echo alpha\ b[TAB] |
| 159 | +
|
| 160 | + """ |
| 161 | + os.mkdir("./alpha\tbeta") |
| 162 | + assert ( |
| 163 | + assert_complete( |
| 164 | + # Remark on "rendered_cmd": Bash aligns the last character 'b' |
| 165 | + # in the rendered cmd to an "8 x n" boundary using spaces. |
| 166 | + # Here, the command string is assumed to start from column 2 |
| 167 | + # because the width of PS1 (conftest.PS1 = '/@') is 2, |
| 168 | + bash, |
| 169 | + "echo alpha\\\026\tb", |
| 170 | + rendered_cmd="echo alpha\\ b", |
| 171 | + ) |
| 172 | + == "eta/" |
| 173 | + ) |
0 commit comments