• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include "medialibrary_common_utils.h"
16 
17 #include <algorithm>
18 #include <regex>
19 #include <unordered_set>
20 #include "medialibrary_errno.h"
21 #include "medialibrary_db_const.h"
22 #include "medialibrary_tracer.h"
23 #include "media_log.h"
24 #include "openssl/sha.h"
25 
26 namespace OHOS {
27 namespace Media {
28 using namespace std;
29 const vector<string> CHAR2HEX_TABLE = {
30     "00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "0A", "0B", "0C", "0D", "0E", "0F",
31     "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "1A", "1B", "1C", "1D", "1E", "1F",
32     "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "2A", "2B", "2C", "2D", "2E", "2F",
33     "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "3A", "3B", "3C", "3D", "3E", "3F",
34 
35     "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "4A", "4B", "4C", "4D", "4E", "4F",
36     "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "5A", "5B", "5C", "5D", "5E", "5F",
37     "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "6A", "6B", "6C", "6D", "6E", "6F",
38     "70", "71", "72", "73", "74", "75", "76", "77", "78", "79", "7A", "7B", "7C", "7D", "7E", "7F",
39 
40     "80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "8A", "8B", "8C", "8D", "8E", "8F",
41     "90", "91", "92", "93", "94", "95", "96", "97", "98", "99", "9A", "9B", "9C", "9D", "9E", "9F",
42     "A0", "A1", "A2", "A3", "A4", "A5", "A6", "A7", "A8", "A9", "AA", "AB", "AC", "AD", "AE", "AF",
43     "B0", "B1", "B2", "B3", "B4", "B5", "B6", "B7", "B8", "B9", "BA", "BB", "BC", "BD", "BE", "BF",
44 
45     "C0", "C1", "C2", "C3", "C4", "C5", "C6", "C7", "C8", "C9", "CA", "CB", "CC", "CD", "CE", "CF",
46     "D0", "D1", "D2", "D3", "D4", "D5", "D6", "D7", "D8", "D9", "DA", "DB", "DC", "DD", "DE", "DF",
47     "E0", "E1", "E2", "E3", "E4", "E5", "E6", "E7", "E8", "E9", "EA", "EB", "EC", "ED", "EE", "EF",
48     "F0", "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "FA", "FB", "FC", "FD", "FE", "FF"
49 };
50 
Char2Hex(const unsigned char * data,const size_t len,std::string & hexStr)51 void MediaLibraryCommonUtils::Char2Hex(const unsigned char *data, const size_t len, std::string &hexStr)
52 {
53     constexpr int CHAR_WIDTH = 8;
54     constexpr int HEX_WIDTH = 4;
55     constexpr size_t OUT_HEXSTR_SIZE = SHA256_DIGEST_LENGTH * (CHAR_WIDTH / HEX_WIDTH);
56     hexStr = "";
57     hexStr.reserve(OUT_HEXSTR_SIZE);
58     for (size_t i = 0; i < len; i++) {
59         hexStr.append(CHAR2HEX_TABLE[data[i]]);
60     }
61 }
62 
GenKey(const unsigned char * data,const size_t len,std::string & key)63 int32_t MediaLibraryCommonUtils::GenKey(const unsigned char *data, const size_t len, std::string &key)
64 {
65     if (len == 0 || len > LONG_MAX) {
66         return -EINVAL;
67     }
68 
69     unsigned char hash[SHA256_DIGEST_LENGTH] = "";
70     SHA256_CTX ctx;
71     SHA256_Init(&ctx);
72     SHA256_Update(&ctx, data, len);
73     SHA256_Final(hash, &ctx);
74 
75     /* here we translate sha256 hash to hexadecimal. each 8-bit char will be presented by two characters([0-9a-f]) */
76     Char2Hex(hash, SHA256_DIGEST_LENGTH, key);
77     return E_OK;
78 }
79 
GenKeySHA256(const std::vector<uint8_t> & input,std::string & key)80 int32_t MediaLibraryCommonUtils::GenKeySHA256(const std::vector<uint8_t> &input, std::string &key)
81 {
82     return GenKey(input.data(), input.size(), key);
83 }
84 
GenKeySHA256(const std::string & input,std::string & key)85 int32_t MediaLibraryCommonUtils::GenKeySHA256(const std::string &input, std::string &key)
86 {
87     return GenKey((const unsigned char *)input.c_str(), input.size(), key);
88 }
89 
ExtractKeyWord(std::string & str)90 void MediaLibraryCommonUtils::ExtractKeyWord(std::string &str)
91 {
92     if (str.empty()) {
93         return;
94     }
95     // add seprate space symbol,like file_id=?
96     std::regex spacePattern("\\=|\\<>|\\>|\\>=|\\<|\\<=|\\!=",
97         std::regex_constants::ECMAScript | std::regex_constants::icase);
98     str = regex_replace(str, spacePattern, " ");
99     // remove front space of key word
100     auto pos = str.find_first_not_of(" ");
101     if (pos != std::string::npos) {
102         str.erase(0, pos);
103     }
104     // remove back space of key word
105     pos = str.find_first_of(" ");
106     if (pos != std::string::npos) {
107         str = str.substr(0, pos);
108     }
109 }
110 
CheckWhiteList(const std::string & express)111 bool MediaLibraryCommonUtils::CheckWhiteList(const std::string &express)
112 {
113     static const std::unordered_set<std::string> FILE_KEY_WHITE_LIST {
114         // Files table columns
115         MEDIA_DATA_DB_ID,
116         MEDIA_DATA_DB_RELATIVE_PATH,
117         MEDIA_DATA_DB_NAME,
118         MEDIA_DATA_DB_PARENT_ID,
119         MEDIA_DATA_DB_MIME_TYPE,
120         MEDIA_DATA_DB_MEDIA_TYPE,
121         MEDIA_DATA_DB_SIZE,
122         MEDIA_DATA_DB_DATE_ADDED,
123         MEDIA_DATA_DB_DATE_MODIFIED,
124         MEDIA_DATA_DB_DATE_TAKEN,
125         MEDIA_DATA_DB_TITLE,
126         MEDIA_DATA_DB_ARTIST,
127         MEDIA_DATA_DB_AUDIO_ALBUM,
128         MEDIA_DATA_DB_DURATION,
129         MEDIA_DATA_DB_WIDTH,
130         MEDIA_DATA_DB_HEIGHT,
131         MEDIA_DATA_DB_ORIENTATION,
132         MEDIA_DATA_DB_BUCKET_ID,
133         MEDIA_DATA_DB_BUCKET_NAME,
134         DIRECTORY_DB_DIRECTORY_TYPE,
135         MEDIA_DATA_DB_DATE_TRASHED,
136         MEDIA_DATA_DB_BUCKET_ID,
137         MEDIA_DATA_DB_ALBUM_ID,
138         DEVICE_DB_NETWORK_ID,
139         SMARTABLUMASSETS_PARENTID,
140         SMARTALBUM_DB_ID,
141         MEDIA_DATA_DB_FILE_PATH,
142         MEDIA_DATA_DB_IS_TRASH,
143         MEDIA_DATA_DB_RECYCLE_PATH,
144         MEDIA_DATA_DB_OWNER_PACKAGE,
145         MediaColumn::MEDIA_PACKAGE_NAME,
146         MEDIA_DATA_DB_IS_FAV,
147         MEDIA_DATA_DB_TIME_PENDING,
148 
149         // Photos table columns
150         COMPAT_HIDDEN,
151         COMPAT_PHOTO_SYNC_STATUS,
152         COMPAT_FILE_SUBTYPE,
153         COMPAT_CAMERA_SHOT_KEY,
154 
155         // PhotoAlbum table columns
156         COMPAT_ALBUM_SUBTYPE,
157     };
158     return FILE_KEY_WHITE_LIST.find(express) != FILE_KEY_WHITE_LIST.end();
159 }
160 
CheckExpressValidation(std::vector<std::string> & sepratedStr)161 bool MediaLibraryCommonUtils::CheckExpressValidation(std::vector<std::string> &sepratedStr)
162 {
163     for (auto &str : sepratedStr) {
164         ExtractKeyWord(str);
165         if (str.empty() || (str.size() == 1 && str == " ")) {
166             continue;
167         }
168         if (!CheckWhiteList(str)) {
169             MEDIA_ERR_LOG("Failed to check key word: %{public}s", str.c_str());
170             return false;
171         }
172     }
173 
174     return true;
175 }
176 
removeSpecialCondition(std::string & hacker,const std::string & pattern)177 void MediaLibraryCommonUtils::removeSpecialCondition(std::string &hacker, const std::string &pattern)
178 {
179     auto pos = hacker.find(pattern);
180     while (pos != std::string::npos) {
181         hacker.replace(pos, pos + pattern.size(), " ");
182         pos = hacker.find(pattern);
183     }
184 }
185 
removeSpecialCondition(std::string & hacker)186 void MediaLibraryCommonUtils::removeSpecialCondition(std::string &hacker)
187 {
188     const std::string S1 = "not between ? and ?";
189     const std::string S2 = "between ? and ?";
190     const std::string S3 = "limit ?, ?";
191     removeSpecialCondition(hacker, S1);
192     removeSpecialCondition(hacker, S2);
193     removeSpecialCondition(hacker, S3);
194 }
195 
SeprateSelection(std::string & strCondition,std::vector<std::string> & sepratedStr)196 void MediaLibraryCommonUtils::SeprateSelection(std::string &strCondition, std::vector<std::string> &sepratedStr)
197 {
198     // 0. transform to lower
199     std::transform(strCondition.begin(), strCondition.end(), strCondition.begin(), ::tolower);
200     // 1.remove brackets
201     std::regex bracketsPattern("\\(|\\)", std::regex_constants::ECMAScript | std::regex_constants::icase);
202     strCondition = regex_replace(strCondition, bracketsPattern, "");
203 
204     // 2.remove redundant space
205     std::regex spacePattern("\\s+", std::regex_constants::ECMAScript | std::regex_constants::icase);
206     strCondition = regex_replace(strCondition, spacePattern, " ");
207 
208     // 3. remove special condition
209     removeSpecialCondition(strCondition);
210 
211     // 4. seprate core: according bound symbol,for example: and or ..
212     std::regex conditionPattern("\\s*and\\s+|\\s*or\\s+",
213         std::regex_constants::ECMAScript | std::regex_constants::icase);
214     std::sregex_token_iterator iter(strCondition.begin(), strCondition.end(), conditionPattern, -1);
215     decltype(iter) end;
216     while (iter != end) {
217         sepratedStr.push_back(iter->str());
218         ++iter;
219     }
220 }
221 
CheckKeyWord(const std::string & strCondition)222 bool MediaLibraryCommonUtils::CheckKeyWord(const std::string &strCondition)
223 {
224     std::regex pattern("\\s*exec\\s*|\\s*insert\\s*|\\s*delete\\s*|\\s*update\\s*|" \
225                             "\\s*join\\s*|\\s*union\\s*|\\s*master\\s*|\\s*truncate\\s*",
226                     std::regex_constants::ECMAScript | std::regex_constants::icase);
227 
228     if (regex_search(strCondition, pattern)) {
229         return false;
230     }
231 
232     return true;
233 }
234 
CheckIllegalCharacter(const std::string & strCondition)235 bool MediaLibraryCommonUtils::CheckIllegalCharacter(const std::string &strCondition)
236 {
237     /* if strCondition contains ';', it will be sepreate to two clause */
238     if (strCondition.find(';') == std::string::npos) {
239         return true;
240     }
241     /* other check to do */
242     return false;
243 }
244 
CheckWhereClause(const std::string & whereClause)245 bool MediaLibraryCommonUtils::CheckWhereClause(const std::string &whereClause)
246 {
247     MediaLibraryTracer tracer;
248     tracer.Start("CommonUtils::CheckWhereClause");
249     if (whereClause.empty() || (whereClause.size() == 1 && whereClause == " ")) {
250         return true;
251     }
252     /* check whether query condition has illegal character */
253     if (!CheckIllegalCharacter(whereClause)) {
254         MEDIA_ERR_LOG("CheckIllegalCharacter is failed!");
255         return false;
256     }
257 
258     /* check whether query condition has key word */
259     if (!CheckKeyWord(whereClause)) {
260         MEDIA_ERR_LOG("CheckKeyWord is failed!");
261         return false;
262     }
263 
264     std::vector<std::string> sepratedStr;
265     auto args = whereClause;
266     SeprateSelection(args, sepratedStr);
267     /* check every query condition */
268     return CheckExpressValidation(sepratedStr);
269 }
270 
AppendSelections(std::string & selections)271 void MediaLibraryCommonUtils::AppendSelections(std::string &selections)
272 {
273     if (selections.empty()) {
274         return;
275     }
276     selections = "(" + selections + ")";
277 }
278 } // namespace Media
279 } // namespace OHOS
280