TestShellQuote pins the contract of the helper used to defend the SCP code path against remote command injection. If any of these cases regress, an attacker-controlled target path could execute arbitrary commands on the remote host.
(t *testing.T)
| 672 | // attacker-controlled target path could execute arbitrary commands on the |
| 673 | // remote host. |
| 674 | func TestShellQuote(t *testing.T) { |
| 675 | cases := []struct { |
| 676 | in, want string |
| 677 | }{ |
| 678 | {"", "''"}, |
| 679 | {"foo", "'foo'"}, |
| 680 | {"foo bar", "'foo bar'"}, |
| 681 | {"foo/bar.txt", "'foo/bar.txt'"}, |
| 682 | {"foo'bar", `'foo'\''bar'`}, |
| 683 | {"'", `''\'''`}, |
| 684 | {"$(rm -rf /)", "'$(rm -rf /)'"}, |
| 685 | {"`id`", "'`id`'"}, |
| 686 | {"a;b|c&d", "'a;b|c&d'"}, |
| 687 | {`"quoted"`, `'"quoted"'`}, |
| 688 | } |
| 689 | for _, c := range cases { |
| 690 | got := shellQuote(c.in) |
| 691 | assert.Equal(t, c.want, got, "shellQuote(%q)", c.in) |
| 692 | } |
| 693 | } |
| 694 | |
| 695 | // TestWriteFileRejectsInvalidTargetName guards the SCP protocol-injection |
| 696 | // fix: a target filename containing newline/CR/NUL must be rejected before |
nothing calls this directly
no test coverage detected