| 1 | /* |
| 2 | * Open Chinese Convert |
| 3 | * |
| 4 | * Copyright 2015-2026 Carbo Kuo and contributors |
| 5 | * |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | */ |
| 18 | |
| 19 | #include "Conversion.hpp" |
| 20 | #include "DictGroupTestBase.hpp" |
| 21 | |
| 22 | namespace opencc { |
| 23 | |
| 24 | class ConversionTest : public DictGroupTestBase { |
| 25 | protected: |
| 26 | ConversionTest() |
| 27 | : input(utf8("太后的头发干燥")), expected(utf8("太后的頭髮乾燥")) {} |
| 28 | |
| 29 | virtual void SetUp() { |
| 30 | dict = CreateDictGroupForConversion(); |
| 31 | conversion = ConversionPtr(new Conversion(dict)); |
| 32 | } |
| 33 | |
| 34 | DictPtr dict; |
| 35 | ConversionPtr conversion; |
| 36 | const std::string input; |
| 37 | const std::string expected; |
| 38 | }; |
| 39 | |
| 40 | TEST_F(ConversionTest, ConvertString) { |
| 41 | const std::string converted = conversion->Convert(input); |
| 42 | EXPECT_EQ(expected, converted); |
| 43 | } |
| 44 | |
| 45 | TEST_F(ConversionTest, ConvertCString) { |
| 46 | const std::string converted = conversion->Convert(input.c_str()); |
| 47 | EXPECT_EQ(expected, converted); |
| 48 | } |
| 49 | |
| 50 | TEST_F(ConversionTest, PreserveDictGroupPriority) { |
| 51 | LexiconPtr firstLexicon(new Lexicon); |
| 52 | firstLexicon->Add(DictEntryFactory::New("a", "X")); |
| 53 | firstLexicon->Sort(); |
| 54 | LexiconPtr secondLexicon(new Lexicon); |
| 55 | secondLexicon->Add(DictEntryFactory::New("ab", "Y")); |
| 56 | secondLexicon->Sort(); |
| 57 | |
| 58 | DictPtr firstDict(new TextDict(firstLexicon)); |
| 59 | DictPtr secondDict(new TextDict(secondLexicon)); |
| 60 | DictPtr dictGroup(new DictGroup(std::list<DictPtr>{firstDict, secondDict})); |
| 61 | Conversion groupConversion(dictGroup); |
| 62 | |
| 63 | EXPECT_EQ("Xbc", groupConversion.Convert("abc")); |
| 64 | } |
| 65 | |
| 66 | TEST_F(ConversionTest, PreserveNestedDictGroupPriority) { |
| 67 | LexiconPtr firstLexicon(new Lexicon); |
| 68 | firstLexicon->Add(DictEntryFactory::New("a", "X")); |
| 69 | firstLexicon->Sort(); |
| 70 | LexiconPtr secondLexicon(new Lexicon); |
| 71 | secondLexicon->Add(DictEntryFactory::New("ab", "Y")); |
| 72 | secondLexicon->Sort(); |
| 73 | LexiconPtr thirdLexicon(new Lexicon); |
| 74 | thirdLexicon->Add(DictEntryFactory::New("abc", "Z")); |
| 75 | thirdLexicon->Sort(); |
| 76 | |
| 77 | DictPtr firstDict(new TextDict(firstLexicon)); |
| 78 | DictPtr secondDict(new TextDict(secondLexicon)); |
| 79 | DictPtr thirdDict(new TextDict(thirdLexicon)); |
| 80 | DictPtr nestedGroup(new DictGroup(std::list<DictPtr>{firstDict, secondDict})); |
| 81 | DictPtr dictGroup(new DictGroup(std::list<DictPtr>{nestedGroup, thirdDict})); |
| 82 | Conversion groupConversion(dictGroup); |
| 83 | |
| 84 | EXPECT_EQ("Xbc", groupConversion.Convert("abc")); |
| 85 | } |
| 86 | |
| 87 | TEST_F(ConversionTest, PreserveIdeographicDescriptionSequenceComponents) { |
| 88 | LexiconPtr lexicon(new Lexicon); |
| 89 | lexicon->Add(DictEntryFactory::New(utf8("钅"), utf8("釒"))); |
| 90 | lexicon->Add(DictEntryFactory::New(utf8("只"), utf8("隻"))); |
| 91 | lexicon->Sort(); |
| 92 | |
| 93 | DictPtr componentDict(new TextDict(lexicon)); |
| 94 | Conversion componentConversion(componentDict); |
| 95 | |
| 96 | EXPECT_EQ(utf8("⿰钅只隻"), componentConversion.Convert(utf8("⿰钅只只"))); |
| 97 | } |
| 98 | |
| 99 | TEST_F(ConversionTest, PreserveComplexIdeographicDescriptionSequenceComponents) { |
| 100 | LexiconPtr lexicon(new Lexicon); |
| 101 | lexicon->Add(DictEntryFactory::New(utf8("長"), utf8("长"))); |
| 102 | lexicon->Add(DictEntryFactory::New(utf8("馬"), utf8("马"))); |
| 103 | lexicon->Add(DictEntryFactory::New(utf8("心"), utf8("芯"))); |
| 104 | lexicon->Sort(); |
| 105 | |
| 106 | DictPtr componentDict(new TextDict(lexicon)); |
| 107 | Conversion componentConversion(componentDict); |
| 108 | |
| 109 | EXPECT_EQ( |
| 110 | utf8("⿺⻍⿳穴⿲月⿱⿲幺言幺⿲長馬長刂心长"), |
| 111 | componentConversion.Convert( |
| 112 | utf8("⿺⻍⿳穴⿲月⿱⿲幺言幺⿲長馬長刂心長"))); |
| 113 | } |
| 114 | |
| 115 | TEST_F(ConversionTest, DoesNotPreserveIsolatedIdeographicDescriptionCharacters) { |
| 116 | LexiconPtr lexicon(new Lexicon); |
| 117 | lexicon->Add(DictEntryFactory::New(utf8("表"), utf8("錶"))); |
| 118 | lexicon->Add(DictEntryFactory::New(utf8("证"), utf8("證"))); |
| 119 | lexicon->Sort(); |
| 120 | |
| 121 | DictPtr dict(new TextDict(lexicon)); |
| 122 | Conversion idcDescriptionConversion(dict); |
| 123 | |
| 124 | EXPECT_EQ( |
| 125 | utf8("錶意文字描述字元包括這些字元:⿰⿱⿲⿳⿴⿵⿶⿷⿸⿹⿺⿻"), |
| 126 | idcDescriptionConversion.Convert( |
| 127 | utf8("表意文字描述字元包括這些字元:⿰⿱⿲⿳⿴⿵⿶⿷⿸⿹⿺⿻"))); |
| 128 | EXPECT_EQ(utf8("证證"), idcDescriptionConversion.Convert(utf8("证证"))); |
| 129 | } |
| 130 | |
| 131 | TEST_F(ConversionTest, TruncatedUtf8Sequence) { |
| 132 | // This test specifically triggers the information disclosure vulnerability |
| 133 | // in the old code. The bug occurs when a string ends with an incomplete |
| 134 | // UTF-8 sequence. |
| 135 | // |
| 136 | // Background: UTF8Util::NextCharLength() examines only the first byte to |
| 137 | // determine the expected character length (1-6 bytes), but doesn't verify |
| 138 | // that enough bytes actually remain before the null terminator. |
| 139 | // |
| 140 | // Trigger condition: When the expected UTF-8 character length exceeds |
| 141 | // the actual remaining bytes before null, the old code would: |
| 142 | // 1. Call FromSubstr with a length crossing the null terminator |
| 143 | // 2. Advance pstr beyond the null terminator |
| 144 | // 3. Continue reading heap memory on next iteration |
| 145 | // 4. Output leaked heap data to conversion result (INFORMATION DISCLOSURE) |
| 146 | |
| 147 | // Construct a string ending with a truncated 3-byte UTF-8 sequence: |
| 148 | // - Normal text: "干" (valid 3-byte UTF-8: 0xE5 0xB9 0xB2) |
| 149 | // - Followed by: 0xE5 0xB9 (incomplete 3-byte sequence - missing last byte) |
| 150 | std::string malformed; |
| 151 | malformed += utf8("干"); // Valid character |
| 152 | malformed += '\xE5'; // Start of 3-byte UTF-8 (NextCharLength returns 3) |
| 153 | malformed += '\xB9'; // Second byte |
| 154 | // Missing third byte - only 2 bytes remain but NextCharLength expects 3 |
| 155 | // Old code would jump over null, read heap memory, and leak it in output |
| 156 | |
| 157 | // The fixed code should handle this gracefully without information disclosure |
| 158 | EXPECT_NO_THROW({ |
| 159 | const std::string converted = conversion->Convert(malformed); |
| 160 | // Should convert "干" to "幹" (first candidate in dict) and preserve incomplete sequence |
| 161 | std::string expected; |
| 162 | expected += utf8("幹"); // Converted from "干" (dict has ["幹", "乾", "干"]) |
| 163 | expected += '\xE5'; // Incomplete sequence preserved as-is |
| 164 | expected += '\xB9'; |
| 165 | EXPECT_EQ(expected, converted); |
| 166 | // Should NOT contain garbage heap data beyond the input |
| 167 | // (ASan would catch any out-of-bounds reads during conversion) |
| 168 | }); |
| 169 | } |
| 170 | |
| 171 | } // namespace opencc |