2 * Copyright (C) 2007 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14 * its contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include "DatabaseAuthorizer.h"
32 #include "PlatformString.h"
36 DatabaseAuthorizer::DatabaseAuthorizer()
37 : m_securityEnabled(false)
42 void DatabaseAuthorizer::reset()
44 m_lastActionWasInsert = false;
45 m_lastActionIncreasedSize = false;
48 int DatabaseAuthorizer::createTable(const String& tableName)
50 m_lastActionIncreasedSize = true;
51 return denyBasedOnTableName(tableName);
54 int DatabaseAuthorizer::createTempTable(const String& tableName)
56 return denyBasedOnTableName(tableName);
59 int DatabaseAuthorizer::dropTable(const String& tableName)
61 return denyBasedOnTableName(tableName);
64 int DatabaseAuthorizer::dropTempTable(const String& tableName)
66 return denyBasedOnTableName(tableName);
69 int DatabaseAuthorizer::allowAlterTable(const String& databaseName, const String& tableName)
71 m_lastActionIncreasedSize = true;
72 return denyBasedOnTableName(tableName);
75 int DatabaseAuthorizer::createIndex(const String& indexName, const String& tableName)
77 m_lastActionIncreasedSize = true;
78 return denyBasedOnTableName(tableName);
81 int DatabaseAuthorizer::createTempIndex(const String& indexName, const String& tableName)
83 return denyBasedOnTableName(tableName);
86 int DatabaseAuthorizer::dropIndex(const String& indexName, const String& tableName)
88 return denyBasedOnTableName(tableName);
91 int DatabaseAuthorizer::dropTempIndex(const String& indexName, const String& tableName)
93 return denyBasedOnTableName(tableName);
96 int DatabaseAuthorizer::createTrigger(const String& triggerName, const String& tableName)
98 m_lastActionIncreasedSize = true;
99 return denyBasedOnTableName(tableName);
102 int DatabaseAuthorizer::createTempTrigger(const String& triggerName, const String& tableName)
104 return denyBasedOnTableName(tableName);
107 int DatabaseAuthorizer::dropTrigger(const String& triggerName, const String& tableName)
109 return denyBasedOnTableName(tableName);
112 int DatabaseAuthorizer::dropTempTrigger(const String& triggerName, const String& tableName)
114 return denyBasedOnTableName(tableName);
117 int DatabaseAuthorizer::createVTable(const String& tableName, const String& moduleName)
119 m_lastActionIncreasedSize = true;
120 return m_securityEnabled ? SQLAuthDeny : SQLAuthAllow;
123 int DatabaseAuthorizer::dropVTable(const String& tableName, const String& moduleName)
125 return m_securityEnabled ? SQLAuthDeny : SQLAuthAllow;
128 int DatabaseAuthorizer::allowDelete(const String& tableName)
130 return denyBasedOnTableName(tableName);
133 int DatabaseAuthorizer::allowInsert(const String& tableName)
135 m_lastActionIncreasedSize = true;
136 m_lastActionWasInsert = true;
137 return denyBasedOnTableName(tableName);
140 int DatabaseAuthorizer::allowUpdate(const String& tableName, const String& columnName)
142 m_lastActionIncreasedSize = true;
143 return denyBasedOnTableName(tableName);
146 int DatabaseAuthorizer::allowRead(const String& tableName, const String& columnName)
148 return denyBasedOnTableName(tableName);
151 int DatabaseAuthorizer::allowAnalyze(const String& tableName)
153 return denyBasedOnTableName(tableName);
156 int DatabaseAuthorizer::allowPragma(const String& pragmaName, const String& firstArgument)
158 return m_securityEnabled ? SQLAuthDeny : SQLAuthAllow;
161 int DatabaseAuthorizer::allowAttach(const String& filename)
163 return m_securityEnabled ? SQLAuthDeny : SQLAuthAllow;
166 int DatabaseAuthorizer::allowDetach(const String& databaseName)
168 return m_securityEnabled ? SQLAuthDeny : SQLAuthAllow;
171 int DatabaseAuthorizer::allowFunction(const String& functionName)
173 // FIXME: Are there any of these we need to prevent? One might guess current_date, current_time, current_timestamp because
174 // they would violate the "sandbox environment" part of 4.11.3, but scripts can generate the local client side information via
175 // javascript directly, anyways. Are there any other built-ins we need to be worried about?
179 void DatabaseAuthorizer::disable()
181 m_securityEnabled = false;
184 void DatabaseAuthorizer::enable()
186 m_securityEnabled = true;
189 int DatabaseAuthorizer::denyBasedOnTableName(const String& tableName)
191 if (!m_securityEnabled)
194 // Sadly, normal creates and drops end up affecting sqlite_master in an authorizer callback, so
195 // it will be tough to enforce all of the following policies
196 //if (equalIgnoringCase(tableName, "sqlite_master") || equalIgnoringCase(tableName, "sqlite_temp_master") ||
197 // equalIgnoringCase(tableName, "sqlite_sequence") || equalIgnoringCase(tableName, Database::databaseInfoTableName()))
198 // return SQLAuthDeny;
200 if (equalIgnoringCase(tableName, Database::databaseInfoTableName()))
207 } // namespace WebCore