1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
|
/*
* Copyright (C) 2025 Igalia, S.L. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "WebExtensionRegisteredScriptsSQLiteStore.h"
#include "Logging.h"
#include "WebExtensionSQLiteHelpers.h"
#include <wtf/CrossThreadCopier.h>
#include <wtf/JSONValues.h>
#include <wtf/TZoneMallocInlines.h>
#include <wtf/text/MakeString.h>
namespace WebKit {
#if ENABLE(WK_WEB_EXTENSIONS)
WTF_MAKE_TZONE_ALLOCATED_IMPL(WebExtensionRegisteredScriptsSQLiteStore);
static const SchemaVersion currentSchemaVersion = 2;
static constexpr auto idKey = "id"_s;
static constexpr auto persistAcrossSessionsKey = "persistAcrossSessions"_s;
static String rowFilterStringFromRowKeys(Vector<String> keys)
{
Vector<String> escapedAndQuotedKeys;
for (const auto& key : keys) {
auto keyWithSingleQuotesEscaped = makeStringByReplacingAll(key, "'"_s, "''"_s);
escapedAndQuotedKeys.append(makeString("'"_s, keyWithSingleQuotesEscaped, "'"_s));
}
return makeStringByJoining(escapedAndQuotedKeys.span(), ", "_s);
}
WebExtensionRegisteredScriptsSQLiteStore::WebExtensionRegisteredScriptsSQLiteStore(const String& uniqueIdentifier, const String& directory, bool useInMemoryDatabase)
: WebExtensionSQLiteStore(uniqueIdentifier, directory, useInMemoryDatabase)
{
}
void WebExtensionRegisteredScriptsSQLiteStore::updateScripts(Vector<Ref<JSON::Object>> scripts, CompletionHandler<void(const String& errorMessage)>&& completionHandler)
{
Vector<String> ids;
for (const auto& script : scripts)
ids.append(script->getString(idKey));
deleteScriptsWithIDs(ids, [weakThis = ThreadSafeWeakPtr { *this }, ids = WTFMove(ids), scripts = WTFMove(scripts), completionHandler = WTFMove(completionHandler)](const String& errorMessage) mutable {
RefPtr protectedThis = weakThis.get();
if (!protectedThis) {
completionHandler({ });
return;
}
if (!errorMessage.isEmpty()) {
completionHandler(errorMessage);
return;
}
protectedThis->addScripts(scripts, [completionHandler = WTFMove(completionHandler)](const String& errorMessage) mutable {
completionHandler(errorMessage);
});
});
}
void WebExtensionRegisteredScriptsSQLiteStore::deleteScriptsWithIDs(Vector<String> ids, CompletionHandler<void(const String& errorMessage)>&& completionHandler)
{
if (ids.isEmpty()) {
completionHandler({ });
return;
}
queue().dispatch([weakThis = ThreadSafeWeakPtr { *this }, ids = crossThreadCopy(ids), completionHandler = WTFMove(completionHandler)]() mutable {
RefPtr protectedThis = weakThis.get();
if (!protectedThis) {
completionHandler({ });
return;
}
String errorMessage;
if (!protectedThis->openDatabaseIfNecessary(errorMessage, false)) {
WorkQueue::mainSingleton().dispatch([errorMessage = crossThreadCopy(errorMessage), completionHandler = WTFMove(completionHandler)]() mutable {
completionHandler(errorMessage);
});
return;
}
ASSERT(errorMessage.isEmpty());
DatabaseResult result = SQLiteDatabaseExecute(*(protectedThis->database()), makeString("DELETE FROM registered_scripts WHERE key in ("_s, rowFilterStringFromRowKeys(ids), ")"_s));
if (result != SQLITE_DONE) {
RELEASE_LOG_ERROR(Extensions, "Failed to delete scripts for extension %s.", protectedThis->uniqueIdentifier().utf8().data());
errorMessage = "Failed to delete scripts from registered content scripts storage."_s;
}
String deleteDatabaseErrorMessage = protectedThis->deleteDatabaseIfEmpty();
WorkQueue::mainSingleton().dispatch([deleteDatabaseErrorMessage = crossThreadCopy(deleteDatabaseErrorMessage), errorMessage = crossThreadCopy(errorMessage), completionHandler = WTFMove(completionHandler)]() mutable {
// Errors from opening the database or deleting keys take precedence over an error deleting the database.
completionHandler(!errorMessage.isEmpty() ? errorMessage : deleteDatabaseErrorMessage);
});
});
}
void WebExtensionRegisteredScriptsSQLiteStore::addScripts(Vector<Ref<JSON::Object>> scripts, CompletionHandler<void(const String& errorMessage)>&& completionHandler)
{
// Only save persistent scripts to storage
Vector<Ref<JSON::Object>> persistentScripts;
for (Ref script : scripts) {
if (auto persistent = script->getBoolean(persistAcrossSessionsKey); persistent && persistent.value())
persistentScripts.append(script);
}
if (persistentScripts.isEmpty()) {
completionHandler({ });
return;
}
queue().dispatch([weakThis = ThreadSafeWeakPtr { *this }, persistentScripts, completionHandler = WTFMove(completionHandler)]() mutable {
RefPtr protectedThis = weakThis.get();
if (!protectedThis) {
completionHandler({ });
return;
}
String errorMessage;
if (!protectedThis->openDatabaseIfNecessary(errorMessage, true)) {
WorkQueue::mainSingleton().dispatch([errorMessage = crossThreadCopy(errorMessage), completionHandler = WTFMove(completionHandler)]() mutable {
completionHandler(errorMessage);
});
return;
}
ASSERT(errorMessage.isEmpty());
for (Ref script : persistentScripts)
protectedThis->insertScript(script, *(protectedThis->database()), errorMessage);
WorkQueue::mainSingleton().dispatch([errorMessage = crossThreadCopy(errorMessage), completionHandler = WTFMove(completionHandler)]() mutable {
completionHandler(errorMessage);
});
});
}
void WebExtensionRegisteredScriptsSQLiteStore::getScripts(CompletionHandler<void(Vector<Ref<JSON::Object>> scripts, const String& errorMessage)>&& completionHandler)
{
queue().dispatch([weakThis = ThreadSafeWeakPtr { *this }, completionHandler = WTFMove(completionHandler)]() mutable {
RefPtr protectedThis = weakThis.get();
if (!protectedThis) {
completionHandler({ }, nullString());
return;
}
String errorMessage;
auto scripts = protectedThis->getScriptsWithErrorMessage(errorMessage);
WorkQueue::mainSingleton().dispatch([scripts = WTFMove(scripts), errorMessage = crossThreadCopy(errorMessage), completionHandler = WTFMove(completionHandler)]() mutable {
completionHandler(scripts, errorMessage);
});
});
}
Vector<Ref<JSON::Object>> WebExtensionRegisteredScriptsSQLiteStore::getScriptsWithErrorMessage(String& errorMessage)
{
assertIsCurrent(queue());
if (!openDatabaseIfNecessary(errorMessage, false))
return { };
ASSERT(errorMessage.isEmpty());
if (RefPtr rows = SQLiteDatabaseFetch(*database(), "SELECT * FROM registered_scripts"_s))
return getKeysAndValuesFromRowIterator(*rows);
return { };
}
Vector<Ref<JSON::Object>> WebExtensionRegisteredScriptsSQLiteStore::getKeysAndValuesFromRowIterator(Ref<WebExtensionSQLiteRowEnumerator> rows)
{
Vector<Ref<JSON::Object>> results;
RefPtr row = rows->next();
while (row != nullptr) {
auto script = row->getString(1);
if (RefPtr value = JSON::Value::parseJSON(script)) {
if (RefPtr object = value->asObject())
results.append(*object);
else
RELEASE_LOG_ERROR(Extensions, "Failed to deserialize registered content scripts for extension %s", uniqueIdentifier().utf8().data());
} else
RELEASE_LOG_ERROR(Extensions, "Failed to parse JSON for registered content scripts for extension %s", uniqueIdentifier().utf8().data());
row = rows->next();
}
return results;
}
void WebExtensionRegisteredScriptsSQLiteStore::insertScript(const JSON::Object& script, Ref<WebExtensionSQLiteDatabase> database, String& errorMessage)
{
assertIsCurrent(queue());
auto scriptID = script.getString(idKey);
ASSERT(!scriptID.isEmpty());
auto scriptData = script.toJSONString();
DatabaseResult result = SQLiteDatabaseExecute(database, "INSERT INTO registered_scripts (key, script) VALUES (?, ?)"_s, scriptID, scriptData);
if (result != SQLITE_DONE) {
RELEASE_LOG_ERROR(Extensions, "Failed to insert registered content script for extension %s.", uniqueIdentifier().utf8().data());
errorMessage = "Failed to add content script."_s;
return;
}
}
// MARK: Database Schema
SchemaVersion WebExtensionRegisteredScriptsSQLiteStore::currentDatabaseSchemaVersion()
{
return currentSchemaVersion;
}
DatabaseResult WebExtensionRegisteredScriptsSQLiteStore::createFreshDatabaseSchema()
{
assertIsCurrent(queue());
ASSERT(database());
DatabaseResult result = SQLiteDatabaseExecute(*database(), "CREATE TABLE registered_scripts (key TEXT PRIMARY KEY NOT NULL, script BLOB NOT NULL)"_s);
if (result != SQLITE_DONE)
RELEASE_LOG_ERROR(Extensions, "Failed to create registered_scripts database for extension %s: %s (%d)", uniqueIdentifier().utf8().data(), lastErrorMessage().data(), result);
return result;
}
SchemaVersion WebExtensionRegisteredScriptsSQLiteStore::migrateToCurrentSchemaVersionIfNeeded()
{
assertIsCurrent(queue());
auto currentDatabaseSchemaVersion = databaseSchemaVersion();
if (currentDatabaseSchemaVersion == 1) {
// We need to migrate existing data to the format understood by the new C++ SQLite Store parser
// Older data would be stored in a format dictated by NSKeyedArchiver/NSKeyedUnarchiver, and would need to be converted
// to the JSON data that the new format expects.
// We do bump the schema version, as it's technically a format change, but to avoid unnecessary data loss, we simply migrate the data
// and return the new version without deleting the database.
migrateData();
setDatabaseSchemaVersion(currentSchemaVersion);
return currentSchemaVersion;
}
return WebExtensionSQLiteStore::migrateToCurrentSchemaVersionIfNeeded();
}
DatabaseResult WebExtensionRegisteredScriptsSQLiteStore::resetDatabaseSchema()
{
assertIsCurrent(queue());
ASSERT(database());
DatabaseResult result = SQLiteDatabaseExecute(*database(), "DROP TABLE IF EXISTS registered_scripts"_s);
if (result != SQLITE_DONE)
RELEASE_LOG_ERROR(Extensions, "Failed to reset registered_scripts database schema for extension %s: %s (%d)", uniqueIdentifier().utf8().data(), lastErrorMessage().data(), result);
return result;
}
bool WebExtensionRegisteredScriptsSQLiteStore::isDatabaseEmpty()
{
assertIsCurrent(queue());
ASSERT(database());
RefPtr rows = SQLiteDatabaseFetch(*database(), "SELECT COUNT(*) FROM registered_scripts"_s);
if (RefPtr row = rows->next())
return !row->getInt64(0);
return true;
}
URL WebExtensionRegisteredScriptsSQLiteStore::databaseURL()
{
if (useInMemoryDatabase())
return WebExtensionSQLiteDatabase::inMemoryDatabaseURL();
ASSERT(!directory().isEmpty());
return URL(URL { makeString(directory().string(), "/"_s) }, "RegisteredContentScripts.db"_s);
}
#endif // ENABLE(WK_WEB_EXTENSIONS)
} // namespace WebKit
|