1 /*
2  * Copyright 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.providers.telephony;
18 
19 import android.util.Log;
20 
21 public class SqlQueryChecker {
22     private static final String SELECT_TOKEN = "select";
23 
checkToken(String token)24     static void checkToken(String token) {
25         if (SELECT_TOKEN.equalsIgnoreCase(token)) {
26             throw new IllegalArgumentException("SELECT token not allowed in query");
27         }
28     }
29 
30     /**
31      * Check the query parameters to see if they contain subqueries. Throws an
32      * {@link IllegalArgumentException} if they do. See
33      * {@link android.content.ContentProvider#query} for the definitions of the arguments.
34      */
checkQueryParametersForSubqueries(String[] projection, String selection, String sortOrder)35     static void checkQueryParametersForSubqueries(String[] projection,
36             String selection, String sortOrder) {
37         Log.v("MmsProvider", "inside checkQueryParametersForSubqueries");
38         if (projection != null) {
39             for (String proj : projection) {
40                 Log.v("MmsProvider", "checkQueryParametersForSubqueries checking proj: " + proj);
41                 SQLiteTokenizer.tokenize(proj, SQLiteTokenizer.OPTION_NONE,
42                         SqlQueryChecker::checkToken);
43             }
44         }
45         Log.v("MmsProvider", "checkQueryParametersForSubqueries checking sel: " + selection);
46         SQLiteTokenizer.tokenize(selection, SQLiteTokenizer.OPTION_NONE,
47                 SqlQueryChecker::checkToken);
48         Log.v("MmsProvider", "checkQueryParametersForSubqueries checking sort: " + sortOrder);
49         SQLiteTokenizer.tokenize(sortOrder, SQLiteTokenizer.OPTION_NONE,
50                 SqlQueryChecker::checkToken);
51     }
52 }
53