@@ -25,6 +25,7 @@ skip_count=0
2525
2626source /usr/libexec/helper-scripts/get_colors.sh
2727source /usr/libexec/helper-scripts/safe-rm-maybe.bsh
28+ source /usr/libexec/helper-scripts/has.sh
2829
2930cleanup () {
3031 if [ -n " $test_dir " ] && [ -d " $test_dir " ]; then
@@ -38,13 +39,14 @@ test_dir="$(mktemp --directory)"
3839# # grep-find-unicode-wrapper is similar to grep.
3940# # - Exit code 0: if found.
4041# # - Non-zero exit code: if not found.
41- command -v grep-find-unicode-wrapper > /dev/null
42+ has grep-find-unicode-wrapper
43+ has write-long-line-with-unicode
4244
4345expect_detected () {
4446 local description=" $1 "
4547 local file_name=" $2 "
4648
47- if grep-find-unicode-wrapper " $file_name " > /dev/null 2>&1 ; then
49+ if grep-find-unicode-wrapper " $file_name " & > /dev/null ; then
4850 printf " %s\n" " ${green} PASS${nocolor} : Detected: $description "
4951 pass_count=$(( pass_count + 1 ))
5052 else
@@ -57,7 +59,7 @@ expect_clean() {
5759 local description=" $1 "
5860 local file_name=" $2 "
5961
60- if ! grep-find-unicode-wrapper " $file_name " > /dev/null 2>&1 ; then
62+ if ! grep-find-unicode-wrapper " $file_name " & > /dev/null ; then
6163 printf " %s\n" " ${green} PASS${nocolor} : Clean: $description "
6264 pass_count=$(( pass_count + 1 ))
6365 else
@@ -70,15 +72,15 @@ write_file() {
7072 local name=" $1 "
7173 local content=" $2 "
7274 local file_name=" $test_dir /$name "
73- printf ' %s' " $content " | tee -- " $file_name " > /dev/null
75+ printf ' %s' " $content " | tee -- " $file_name " & > /dev/null
7476 printf ' %s' " $file_name "
7577}
7678
7779write_file_binary () {
7880 local name=" $1 "
7981 shift
8082 local file_name=" $test_dir /$name "
81- printf " $@ " | tee -- " $file_name " > /dev/null
83+ printf " $@ " | tee -- " $file_name " & > /dev/null
8284 printf ' %s' " $file_name "
8385}
8486
@@ -121,7 +123,7 @@ old_check3_pattern=$'[\u061C\u200E\u200F\u202A\u202B\u202C\u202D\u202E\u2066\u20
121123check3_false_positive_test () {
122124 local description=" $1 "
123125 local file_name=" $2 "
124- if LC_ALL=C grep --files-with-matches --line-number --binary-files=text " $old_check3_pattern " " $file_name " > /dev/null 2>&1 ; then
126+ if LC_ALL=C grep --files-with-matches --line-number --binary-files=text " $old_check3_pattern " " $file_name " & > /dev/null; then
125127 if [ " $check3_locale_ok " = " true" ]; then
126128 # # UTF-8 locale: old pattern should work, so this is a real failure.
127129 printf ' %s\n' " ${red} FAIL${nocolor} : Old check 3 false positive: $description " >&2
@@ -427,20 +429,23 @@ f="$(write_file_binary "hidden_in_comment.txt" '# This is a normal comment\xE2\x
427429expect_detected " Zero-width space hidden in comment" " $f "
428430
429431# # LTR mark (E2 80 8E) hidden in a string literal.
430- f=" $( write_file_binary " hidden_in_string.txt" ' var x = "hello\xE2\x80\x8Eworld"; ' ) "
432+ f=" $( write_file_binary " hidden_in_string.txt" ' x = "hello\xE2\x80\x8Eworld"' ) "
431433expect_detected " LTR mark hidden in string literal" " $f "
432434
433435# # Trojan Source BiDi attack pattern.
434- f=" $( write_file_binary " trojan_source_example.txt" ' access_level = "user\xE2\x80\xAA\xE2\x81\xA6\xE2\x81\xA9\xE2\x81\xA6admin\xE2\x81\xA9\xE2\x80\xAC"' ) "
436+ # # Embeds BiDi overrides around the word "test" to demonstrate detection.
437+ f=" $( write_file_binary " trojan_source_example.txt" ' access_level = "user\xE2\x80\xAA\xE2\x81\xA6\xE2\x81\xA9\xE2\x81\xA6test\xE2\x81\xA9\xE2\x80\xAC"' ) "
435438expect_detected " Trojan Source BiDi attack pattern" " $f "
436439
437- # # Backspace (\x08) overwrite attack.
438- f=" $( write_file_binary " backspace_overwrite.txt" ' user\x08\x08\x08\x08root' ) "
439- expect_detected " Backspace overwrite (displays 'root' over 'user')" " $f "
440+ # # Backspace (\x08) overwrite: text followed by backspaces then replacement.
441+ # # Would display "BADX" overwriting "GOOD" on a terminal.
442+ f=" $( write_file_binary " backspace_overwrite.txt" ' GOOD\x08\x08\x08\x08BADX' ) "
443+ expect_detected " Backspace overwrite (GOOD overwritten by BADX)" " $f "
440444
441- # # Carriage return (\x0D) overwrite attack.
442- f=" $( write_file_binary " cr_overwrite.txt" ' safe command\x0Drm -rf /' ) "
443- expect_detected " CR overwrite (hides malicious command)" " $f "
445+ # # Carriage return (\x0D) overwrite: second text replaces first on display.
446+ # # Would display "ERROR: You should not see this" on a terminal.
447+ f=" $( write_file_binary " cr_overwrite.txt" ' This line looks safe\x0DERROR: You should not see this' ) "
448+ expect_detected " CR overwrite (hides text behind carriage return)" " $f "
444449
445450# # ===================================================================
446451# # Section 10: Mixed content edge cases
@@ -461,10 +466,7 @@ f="$(write_file_binary "leading_null.txt" '\x00normal text')"
461466expect_detected " NULL byte at start of file" " $f "
462467
463468# # Very long line with suspicious char in the middle (ZWSP = E2 80 8B).
464- python3 -c "
465- import sys
466- sys.stdout.buffer.write(b'a' * 10000 + b'\xe2\x80\x8b' + b'b' * 10000)
467- " | tee -- " $test_dir /long_line.txt" > /dev/null
469+ write-long-line-with-unicode " $test_dir /long_line.txt"
468470expect_detected " Suspicious char buried in 20000-char line" " $test_dir /long_line.txt"
469471
470472# # ===================================================================
0 commit comments