This source file includes following definitions.
- create
- m_databaseInfoTableName
- reset
- resetDeletes
- addWhitelistedFunctions
- createTable
- createTempTable
- dropTable
- dropTempTable
- allowAlterTable
- createIndex
- createTempIndex
- dropIndex
- dropTempIndex
- createTrigger
- createTempTrigger
- dropTrigger
- dropTempTrigger
- createView
- createTempView
- dropView
- dropTempView
- createVTable
- dropVTable
- allowDelete
- allowInsert
- allowUpdate
- allowTransaction
- allowRead
- allowReindex
- allowAnalyze
- allowPragma
- allowAttach
- allowDetach
- allowFunction
- disable
- enable
- allowWrite
- setPermissions
- denyBasedOnTableName
- updateDeletesBasedOnTableName
#include "config.h"
#include "modules/webdatabase/DatabaseAuthorizer.h"
#include "wtf/PassRefPtr.h"
namespace WebCore {
PassRefPtrWillBeRawPtr<DatabaseAuthorizer> DatabaseAuthorizer::create(const String& databaseInfoTableName)
{
return adoptRefWillBeNoop(new DatabaseAuthorizer(databaseInfoTableName));
}
DatabaseAuthorizer::DatabaseAuthorizer(const String& databaseInfoTableName)
: m_securityEnabled(false)
, m_databaseInfoTableName(databaseInfoTableName)
{
reset();
addWhitelistedFunctions();
}
void DatabaseAuthorizer::reset()
{
m_lastActionWasInsert = false;
m_lastActionChangedDatabase = false;
m_permissions = ReadWriteMask;
}
void DatabaseAuthorizer::resetDeletes()
{
m_hadDeletes = false;
}
void DatabaseAuthorizer::addWhitelistedFunctions()
{
m_whitelistedFunctions.add("sqlite_rename_table");
m_whitelistedFunctions.add("sqlite_rename_trigger");
m_whitelistedFunctions.add("glob");
m_whitelistedFunctions.add("abs");
m_whitelistedFunctions.add("changes");
m_whitelistedFunctions.add("coalesce");
m_whitelistedFunctions.add("glob");
m_whitelistedFunctions.add("ifnull");
m_whitelistedFunctions.add("hex");
m_whitelistedFunctions.add("last_insert_rowid");
m_whitelistedFunctions.add("length");
m_whitelistedFunctions.add("like");
m_whitelistedFunctions.add("lower");
m_whitelistedFunctions.add("ltrim");
m_whitelistedFunctions.add("max");
m_whitelistedFunctions.add("min");
m_whitelistedFunctions.add("nullif");
m_whitelistedFunctions.add("quote");
m_whitelistedFunctions.add("replace");
m_whitelistedFunctions.add("round");
m_whitelistedFunctions.add("rtrim");
m_whitelistedFunctions.add("soundex");
m_whitelistedFunctions.add("sqlite_source_id");
m_whitelistedFunctions.add("sqlite_version");
m_whitelistedFunctions.add("substr");
m_whitelistedFunctions.add("total_changes");
m_whitelistedFunctions.add("trim");
m_whitelistedFunctions.add("typeof");
m_whitelistedFunctions.add("upper");
m_whitelistedFunctions.add("zeroblob");
m_whitelistedFunctions.add("date");
m_whitelistedFunctions.add("time");
m_whitelistedFunctions.add("datetime");
m_whitelistedFunctions.add("julianday");
m_whitelistedFunctions.add("strftime");
m_whitelistedFunctions.add("avg");
m_whitelistedFunctions.add("count");
m_whitelistedFunctions.add("group_concat");
m_whitelistedFunctions.add("sum");
m_whitelistedFunctions.add("total");
m_whitelistedFunctions.add("match");
m_whitelistedFunctions.add("snippet");
m_whitelistedFunctions.add("offsets");
m_whitelistedFunctions.add("optimize");
m_whitelistedFunctions.add("regexp");
}
int DatabaseAuthorizer::createTable(const String& tableName)
{
if (!allowWrite())
return SQLAuthDeny;
m_lastActionChangedDatabase = true;
return denyBasedOnTableName(tableName);
}
int DatabaseAuthorizer::createTempTable(const String& tableName)
{
if (!allowWrite())
return SQLAuthDeny;
return denyBasedOnTableName(tableName);
}
int DatabaseAuthorizer::dropTable(const String& tableName)
{
if (!allowWrite())
return SQLAuthDeny;
return updateDeletesBasedOnTableName(tableName);
}
int DatabaseAuthorizer::dropTempTable(const String& tableName)
{
if (!allowWrite())
return SQLAuthDeny;
return updateDeletesBasedOnTableName(tableName);
}
int DatabaseAuthorizer::allowAlterTable(const String&, const String& tableName)
{
if (!allowWrite())
return SQLAuthDeny;
m_lastActionChangedDatabase = true;
return denyBasedOnTableName(tableName);
}
int DatabaseAuthorizer::createIndex(const String&, const String& tableName)
{
if (!allowWrite())
return SQLAuthDeny;
m_lastActionChangedDatabase = true;
return denyBasedOnTableName(tableName);
}
int DatabaseAuthorizer::createTempIndex(const String&, const String& tableName)
{
if (!allowWrite())
return SQLAuthDeny;
return denyBasedOnTableName(tableName);
}
int DatabaseAuthorizer::dropIndex(const String&, const String& tableName)
{
if (!allowWrite())
return SQLAuthDeny;
return updateDeletesBasedOnTableName(tableName);
}
int DatabaseAuthorizer::dropTempIndex(const String&, const String& tableName)
{
if (!allowWrite())
return SQLAuthDeny;
return updateDeletesBasedOnTableName(tableName);
}
int DatabaseAuthorizer::createTrigger(const String&, const String& tableName)
{
if (!allowWrite())
return SQLAuthDeny;
m_lastActionChangedDatabase = true;
return denyBasedOnTableName(tableName);
}
int DatabaseAuthorizer::createTempTrigger(const String&, const String& tableName)
{
if (!allowWrite())
return SQLAuthDeny;
return denyBasedOnTableName(tableName);
}
int DatabaseAuthorizer::dropTrigger(const String&, const String& tableName)
{
if (!allowWrite())
return SQLAuthDeny;
return updateDeletesBasedOnTableName(tableName);
}
int DatabaseAuthorizer::dropTempTrigger(const String&, const String& tableName)
{
if (!allowWrite())
return SQLAuthDeny;
return updateDeletesBasedOnTableName(tableName);
}
int DatabaseAuthorizer::createView(const String&)
{
return (!allowWrite() ? SQLAuthDeny : SQLAuthAllow);
}
int DatabaseAuthorizer::createTempView(const String&)
{
return (!allowWrite() ? SQLAuthDeny : SQLAuthAllow);
}
int DatabaseAuthorizer::dropView(const String&)
{
if (!allowWrite())
return SQLAuthDeny;
m_hadDeletes = true;
return SQLAuthAllow;
}
int DatabaseAuthorizer::dropTempView(const String&)
{
if (!allowWrite())
return SQLAuthDeny;
m_hadDeletes = true;
return SQLAuthAllow;
}
int DatabaseAuthorizer::createVTable(const String& tableName, const String& moduleName)
{
if (!allowWrite())
return SQLAuthDeny;
if (!equalIgnoringCase(moduleName, "fts3"))
return SQLAuthDeny;
m_lastActionChangedDatabase = true;
return denyBasedOnTableName(tableName);
}
int DatabaseAuthorizer::dropVTable(const String& tableName, const String& moduleName)
{
if (!allowWrite())
return SQLAuthDeny;
if (!equalIgnoringCase(moduleName, "fts3"))
return SQLAuthDeny;
return updateDeletesBasedOnTableName(tableName);
}
int DatabaseAuthorizer::allowDelete(const String& tableName)
{
if (!allowWrite())
return SQLAuthDeny;
return updateDeletesBasedOnTableName(tableName);
}
int DatabaseAuthorizer::allowInsert(const String& tableName)
{
if (!allowWrite())
return SQLAuthDeny;
m_lastActionChangedDatabase = true;
m_lastActionWasInsert = true;
return denyBasedOnTableName(tableName);
}
int DatabaseAuthorizer::allowUpdate(const String& tableName, const String&)
{
if (!allowWrite())
return SQLAuthDeny;
m_lastActionChangedDatabase = true;
return denyBasedOnTableName(tableName);
}
int DatabaseAuthorizer::allowTransaction()
{
return m_securityEnabled ? SQLAuthDeny : SQLAuthAllow;
}
int DatabaseAuthorizer::allowRead(const String& tableName, const String&)
{
if (m_permissions & NoAccessMask && m_securityEnabled)
return SQLAuthDeny;
return denyBasedOnTableName(tableName);
}
int DatabaseAuthorizer::allowReindex(const String&)
{
return (!allowWrite() ? SQLAuthDeny : SQLAuthAllow);
}
int DatabaseAuthorizer::allowAnalyze(const String& tableName)
{
return denyBasedOnTableName(tableName);
}
int DatabaseAuthorizer::allowPragma(const String&, const String&)
{
return m_securityEnabled ? SQLAuthDeny : SQLAuthAllow;
}
int DatabaseAuthorizer::allowAttach(const String&)
{
return m_securityEnabled ? SQLAuthDeny : SQLAuthAllow;
}
int DatabaseAuthorizer::allowDetach(const String&)
{
return m_securityEnabled ? SQLAuthDeny : SQLAuthAllow;
}
int DatabaseAuthorizer::allowFunction(const String& functionName)
{
if (m_securityEnabled && !m_whitelistedFunctions.contains(functionName))
return SQLAuthDeny;
return SQLAuthAllow;
}
void DatabaseAuthorizer::disable()
{
m_securityEnabled = false;
}
void DatabaseAuthorizer::enable()
{
m_securityEnabled = true;
}
bool DatabaseAuthorizer::allowWrite()
{
return !(m_securityEnabled && (m_permissions & ReadOnlyMask || m_permissions & NoAccessMask));
}
void DatabaseAuthorizer::setPermissions(int permissions)
{
m_permissions = permissions;
}
int DatabaseAuthorizer::denyBasedOnTableName(const String& tableName) const
{
if (!m_securityEnabled)
return SQLAuthAllow;
if (equalIgnoringCase(tableName, m_databaseInfoTableName))
return SQLAuthDeny;
return SQLAuthAllow;
}
int DatabaseAuthorizer::updateDeletesBasedOnTableName(const String& tableName)
{
int allow = denyBasedOnTableName(tableName);
if (allow)
m_hadDeletes = true;
return allow;
}
}