CVE-2023-49284: Interpretation Conflict (CWE-436)
value: fish is a smart and user-friendly command line shell for macOS, Linux, and the rest of the family. fish shell uses certain Unicode non-characters internally for marking wildcards and expansions. It will incorrectly allow these markers to be read on command substitution output, rather than transforming them into a safe internal representation. While this may cause unexpected behavior with direct input (for example, echo \UFDD2HOME has the same output as echo $HOME), this may become a minor security problem if the output is being fed from an external program into a command substitution where this output may not be expected. This design flaw was introduced in very early versions of fish, predating the version control system, and is thought to be present in every version of fish released in the last 15 years or more, although with different characters. Code execution does not appear to be possible, but denial of service (through large brace expansion) or information disclosure (such as variable expansion) is potentially possible under certain circumstances. fish shell 3.6.2 has been released to correct this issue. Users are advised to upgrade. There are no known workarounds for this vulnerability.
CWE description: Product A handles inputs or steps differently than Product B, which causes A to perform incorrect actions based on its perception of B's state.
CVSS v3 base score: 6.6
Published: 2023-12-05T00:15Z
Repository: https://github.com/fish-shell/fish-shell
Language: C++
Files changed: src/common.cpp, tests/checks/basic.fish
Commit date: 2023-12-04 23:00:01 +0800
Commit message: Encode all ENCODE_DIRECT codepoints with encode_direct

Diff: diff --git a/src/common.cpp b/src/common.cpp
index a1cc7c63c..370ecacd5 100644
--- a/src/common.cpp
+++ b/src/common.cpp
@@ -333,13 +333,11 @@ static wcstring str2wcs_internal(const char *in, const size_t in_len) {
             // two UTF-16 chars. Something that doesn't work with our simple use of std::mbrtowc().
             use_encode_direct = true;
         } else {
             ret = std::mbrtowc(&wc, &in[in_pos], in_len - in_pos, &state);
             // Determine whether to encode this character with our crazy scheme.
-            if (wc >= ENCODE_DIRECT_BASE && wc < ENCODE_DIRECT_BASE + 256) {
-                use_encode_direct = true;
-            } else if (wc == INTERNAL_SEPARATOR) {
+            if (fish_reserved_codepoint(wc)) {
                 use_encode_direct = true;
             } else if (ret == static_cast<size_t>(-2)) {
                 // Incomplete sequence.
                 use_encode_direct = true;
             } else if (ret == static_cast<size_t>(-1)) {
@@ -1311,10 +1309,13 @@ maybe_t<size_t> read_unquoted_escape(const wchar_t *input, wcstring *result, boo
 
         break;
     }
 
     if (result_char_or_none.has_value()) {
+        if (fish_reserved_codepoint(*result_char_or_none)) {
+            return none();
+        }
         result->push_back(*result_char_or_none);
     }
 
     return in_pos;
 }
diff --git a/tests/checks/basic.fish b/tests/checks/basic.fish
index 60a4e18a2..314b78cc0 100644
--- a/tests/checks/basic.fish
+++ b/tests/checks/basic.fish
@@ -585,10 +585,18 @@ $fish -c 'echo \U'
 $fish -c 'echo \x'
 # CHECKERR: fish: Incomplete escape sequence '\x'
 # CHECKERR: echo \x
 # CHECKERR:      ^^
 
+$fish -c 'echo \ufdd2"fart"'
+# CHECKERR: fish: Invalid token '\ufdd2"fart"'
+# CHECKERR: echo \ufdd2"fart"
+# CHECKERR: ^~~~~~~~~~~^
+
+echo (sh -c 'printf $\'\ufdd2foo\'') | string escape
+# CHECK: \Xef\Xb7\X92foo
+
 printf '%s\n' "#!/bin/sh" 'echo $0' > $tmpdir/argv0.sh
 chmod +x $tmpdir/argv0.sh
 cd $tmpdir
 ./argv0.sh
 # CHECK: ./argv0.sh

Vulnerable code:             if (wc >= ENCODE_DIRECT_BASE && wc < ENCODE_DIRECT_BASE + 256) {
                use_encode_direct = true;
            } else if (wc == INTERNAL_SEPARATOR) {
Fixed code:             if (fish_reserved_codepoint(wc)) {
        if (fish_reserved_codepoint(*result_char_or_none)) {
            return none();
        }
$fish -c 'echo \ufdd2"fart"'
# CHECKERR: fish: Invalid token '\ufdd2"fart"'
# CHECKERR: echo \ufdd2"fart"
# CHECKERR: ^~~~~~~~~~~^

echo (sh -c 'printf $\'\ufdd2foo\'') | string escape
# CHECK: \Xef\Xb7\X92foo