diff --git a/binding.gyp b/binding.gyp index c8ed28e..5936db2 100644 --- a/binding.gyp +++ b/binding.gyp @@ -5,15 +5,11 @@ "sources": [ "extension_whitelist_parser.cc", "extension_whitelist_parser.h", - "extension_whitelist_data.cc", - "extension_whitelist_data.h", ], "include_dirs": [ ".", - './node_modules/hashset-cpp' ], "dependencies": [ - "./node_modules/hashset-cpp/binding.gyp:hashset-cpp" ], "conditions": [ ['OS=="win"', { @@ -34,17 +30,12 @@ "sources": [ "extension_whitelist_parser.cc", "extension_whitelist_parser.h", - "extension_whitelist_data.cc", - "extension_whitelist_data.h", "./node_addon/EWParserWrap.h", "./node_addon/EWParserWrap.cc", "./node_addon/addon.cpp", - "./node_modules/hashset-cpp/hashFn.cc", - "./node_modules/hashset-cpp/hashFn.h" ], "include_dirs": [ ".", - './node_modules/hashset-cpp' ], "conditions": [ ['OS=="win"', { diff --git a/brave/BUILD.gn b/brave/BUILD.gn index 66b0765..95b4c67 100644 --- a/brave/BUILD.gn +++ b/brave/BUILD.gn @@ -18,11 +18,8 @@ source_set("extension-whitelist") { sources = [ "../extension_whitelist_parser.cc", "../extension_whitelist_parser.h", - "../extension_whitelist_data.cc", - "../extension_whitelist_data.h", ] deps = [ - rebase_path("hashset-cpp/brave:hashset-cpp", dep_base), ] } diff --git a/extension_whitelist_data.cc b/extension_whitelist_data.cc deleted file mode 100644 index df020a0..0000000 --- a/extension_whitelist_data.cc +++ /dev/null @@ -1,16 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -#include "./extension_whitelist_data.h" -#include "hashFn.h" - -static HashFn sHashFn(19); - -uint64_t ST_EXTENSION_WHITELIST_DATA::GetHash() const { - if (!sExtensionID) { - return 0; - } - - return sHashFn(sExtensionID, static_cast(strlen(sExtensionID))); -} diff --git a/extension_whitelist_data.h b/extension_whitelist_data.h deleted file mode 100644 index e7a3bfe..0000000 --- a/extension_whitelist_data.h +++ /dev/null @@ -1,94 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -#ifndef EXTENSION_WHITELIST_DATA_H_ -#define EXTENSION_WHITELIST_DATA_H_ - -#include -#include -#include - -struct ST_EXTENSION_WHITELIST_DATA { -public: - ST_EXTENSION_WHITELIST_DATA(): - sExtensionID(nullptr) { - } - - ST_EXTENSION_WHITELIST_DATA(const ST_EXTENSION_WHITELIST_DATA &other) { - if (nullptr == other.sExtensionID) { - return; - } - - sExtensionID = new char[strlen(other.sExtensionID) + 1]; - strcpy(sExtensionID, other.sExtensionID); - } - - ~ST_EXTENSION_WHITELIST_DATA() { - if (nullptr != sExtensionID) { - delete []sExtensionID; - } - } - - uint64_t GetHash() const; - - bool operator==(const ST_EXTENSION_WHITELIST_DATA &rhs) const { - int extensionLen = static_cast(strlen(sExtensionID)); - int rhsExtensionIDLen = static_cast(strlen(rhs.sExtensionID)); - - if (extensionLen != rhsExtensionIDLen) { - return false; - } - - return !memcmp(sExtensionID, rhs.sExtensionID, extensionLen); - } - - // Nothing needs to be updated when an extension is added multiple times - void Update(const ST_EXTENSION_WHITELIST_DATA &) {} - - uint32_t Serialize(char* buffer) { - uint32_t size = 0; - - char sz[32]; - uint32_t dataLenSize = 1 + snprintf(sz, sizeof(sz), "%x", (unsigned int)strlen(sExtensionID)); - - if (buffer) { - memcpy(buffer + size, sz, dataLenSize); - } - size += dataLenSize; - - if (buffer) { - memcpy(buffer + size, sExtensionID, strlen(sExtensionID)); - } - size += static_cast(strlen(sExtensionID)); - - return size; - } - - uint32_t Deserialize(char *buffer, uint32_t bufferSize) { - uint32_t size = 0; - - if (!buffer || 0 == bufferSize) { - return size; - } - unsigned int extensionLength = 0; - sscanf(buffer, "%x", &extensionLength); - if (sExtensionID) { - delete []sExtensionID; - } - size = static_cast(strlen(buffer) + 1); - sExtensionID = new char[extensionLength + 1]; - if (!sExtensionID) { - return size; - } - memcpy(sExtensionID, buffer + size, extensionLength); - sExtensionID[extensionLength] = '\0'; - size += extensionLength; - - return size; - } - - char* sExtensionID; -}; - -#endif // EXTENSION_WHITELIST_DATA_H_ diff --git a/extension_whitelist_parser.cc b/extension_whitelist_parser.cc index f8d8b45..5509adc 100644 --- a/extension_whitelist_parser.cc +++ b/extension_whitelist_parser.cc @@ -2,108 +2,123 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -#include "./extension_whitelist_parser.h" -#include "hash_set.h" +#include "extension_whitelist_parser.h" +#include +#include -ExtensionWhitelistParser::ExtensionWhitelistParser() { - mBlacklist.reset(new HashSet(256, false)); - mWhitelist.reset(new HashSet(256, false)); +namespace { +std::string Serialize(std::unordered_set& set, uint32_t* size) { + *size = set.size() * EXTENSION_ID_LEN; + std::string buffer; + size_t offset = 0; + for (std::string e : set) + buffer += e; + + return buffer; } -ExtensionWhitelistParser::~ExtensionWhitelistParser() { +void Deserialize(std::unordered_set& set, const char* buffer, uint32_t size) { + set.clear(); + const char *p = buffer; + for (uint32_t i = 0; i < size / EXTENSION_ID_LEN; i++) { + std::string s(p, EXTENSION_ID_LEN); + set.insert(s); + p += EXTENSION_ID_LEN; + } +} } +ExtensionWhitelistParser::ExtensionWhitelistParser() {} + +ExtensionWhitelistParser::~ExtensionWhitelistParser() {} + void ExtensionWhitelistParser::addToBlacklist(const char *extensionID) { - if (nullptr == extensionID) - return; - ST_EXTENSION_WHITELIST_DATA extensionData; - extensionData.sExtensionID = new char[strlen(extensionID) + 1]; - if (nullptr == extensionData.sExtensionID) + if (!extensionID || strlen(extensionID) != EXTENSION_ID_LEN) return; - strcpy(extensionData.sExtensionID, extensionID); - mBlacklist->Add(extensionData); + + std::string extensionData(extensionID); + if (extensionData.length() != EXTENSION_ID_LEN) { + throw std::runtime_error("Invalid extension length given!"); + } + mBlacklist.insert(extensionData); } void ExtensionWhitelistParser::addToWhitelist(const char *extensionID) { - if (nullptr == extensionID) - return; - ST_EXTENSION_WHITELIST_DATA extensionData; - extensionData.sExtensionID = new char[strlen(extensionID) + 1]; - if (nullptr == extensionData.sExtensionID) + if (!extensionID || strlen(extensionID) != EXTENSION_ID_LEN) return; - strcpy(extensionData.sExtensionID, extensionID); - mWhitelist->Add(extensionData); + + std::string extensionData(extensionID); + if (extensionData.length() != EXTENSION_ID_LEN) { + throw std::runtime_error("Invalid extension length given!"); + } + mWhitelist.insert(extensionData); } bool ExtensionWhitelistParser::isBlacklisted(const char *extensionID) { - ST_EXTENSION_WHITELIST_DATA extensionData; - extensionData.sExtensionID = new char[strlen(extensionID) + 1]; - if (nullptr == extensionData.sExtensionID) + if (!extensionID || strlen(extensionID) != EXTENSION_ID_LEN) return false; - strcpy(extensionData.sExtensionID, extensionID); - return mBlacklist->Exists(extensionData); + + std::string extensionData(extensionID); + return mBlacklist.find(extensionData) != mBlacklist.end(); } bool ExtensionWhitelistParser::isWhitelisted(const char *extensionID) { - ST_EXTENSION_WHITELIST_DATA extensionData; - extensionData.sExtensionID = new char[strlen(extensionID) + 1]; - if (nullptr == extensionData.sExtensionID) + if (!extensionID || strlen(extensionID) != EXTENSION_ID_LEN) return false; - strcpy(extensionData.sExtensionID, extensionID); - return mWhitelist->Exists(extensionData); + + std::string extensionData(extensionID); + return mWhitelist.find(extensionData) != mWhitelist.end(); } // Returns a newly allocated buffer, caller must manually delete[] the buffer char* ExtensionWhitelistParser::serialize(unsigned int* totalSize) { *totalSize = 0; uint32_t blacklistSize = 0; - char* blacklist = mBlacklist->Serialize(&blacklistSize); + std::string blacklist = ::Serialize(mBlacklist, &blacklistSize); uint32_t whitelistSize = 0; - char* whitelist = mWhitelist->Serialize(&whitelistSize); - *totalSize = sizeof(blacklistSize) + blacklistSize + 1 + - sizeof(whitelistSize) + whitelistSize + 1; + std::string whitelist = ::Serialize(mWhitelist, &whitelistSize); + *totalSize = UINT32_SERIALIZE_MAX + blacklistSize + 1 + + UINT32_SERIALIZE_MAX + whitelistSize + 1; unsigned int pos = 0; char* result = new char[*totalSize]; - if (!result) { - delete []blacklist; - delete []whitelist; - return nullptr; - } memset(result, 0, *totalSize); - char sz[32]; - uint32_t dataLenSize = 1 + snprintf(sz, sizeof(sz), "%x", blacklistSize); - memcpy(result + pos, sz, dataLenSize); + uint32_t dataLenSize = 1 + sprintf(result + pos, "%" PRIu32, blacklistSize); pos += dataLenSize; - memcpy(result + pos, blacklist, blacklistSize); - pos += blacklistSize; - dataLenSize = 1 + snprintf(sz, sizeof(sz), "%x", whitelistSize); - memcpy(result + pos, sz, dataLenSize); + memcpy(result + pos, blacklist.c_str(), blacklistSize); + pos += 1 + blacklistSize; + dataLenSize = 1 + sprintf(result + pos, "%" PRIu32, whitelistSize); pos += dataLenSize; - memcpy(result + pos, whitelist, whitelistSize); - delete []blacklist; - delete []whitelist; - return result; + memcpy(result + pos, whitelist.c_str(), whitelistSize); + return result; } -bool ExtensionWhitelistParser::deserialize(char *buffer, size_t) { +bool ExtensionWhitelistParser::deserialize(const char* buffer, size_t) { return deserialize(buffer); } -bool ExtensionWhitelistParser::deserialize(char *buffer) { +bool ExtensionWhitelistParser::deserialize(const char* buffer) { if (!buffer) return false; - uint32_t blacklistSize = 0; - unsigned int pos = 0; - sscanf(buffer, "%x", &blacklistSize); - pos += static_cast(strlen(buffer) + 1); - if (!mBlacklist->Deserialize(buffer + pos, blacklistSize)) + const char *p = buffer; + uint32_t blacklistSize = 0, whitelistSize = 0; + int x, r; + + r = sscanf(p, "%" SCNu32 "%n", &blacklistSize, &x); + if (r != 1 || r == EOF) return false; - pos += blacklistSize; - uint32_t whitelistSize = 0; - sscanf(buffer + pos, "%x", &whitelistSize); - pos += static_cast(strlen(buffer + pos) + 1); - if (!mWhitelist->Deserialize(buffer + pos, whitelistSize)) { + if (blacklistSize % EXTENSION_ID_LEN != 0) + throw std::runtime_error("deserialize: blacklist not divisible by 32!"); + p += x + 1; + ::Deserialize(mBlacklist, p, blacklistSize); + p += blacklistSize + 1; + + r = sscanf(p, "%" SCNu32 "%n", &whitelistSize, &x); + if (r != 1 || r == EOF) return false; - } + if (whitelistSize % EXTENSION_ID_LEN != 0) + throw std::runtime_error("deserialize: whitelist not divisible by 32!"); + p += x + 1; + ::Deserialize(mWhitelist, p, whitelistSize); + return true; } diff --git a/extension_whitelist_parser.h b/extension_whitelist_parser.h index d2835c9..9ea1148 100644 --- a/extension_whitelist_parser.h +++ b/extension_whitelist_parser.h @@ -5,15 +5,22 @@ #ifndef EXTENSION_WHITELIST_PARSER_H_ #define EXTENSION_WHITELIST_PARSER_H_ +#include +#include +#include #include - -#include "./extension_whitelist_data.h" +#include #define EXTENSION_DAT_FILE "ExtensionWhitelist.dat" #define EXTENSION_DAT_FILE_VERSION "1" +#define EXTENSION_ID_LEN 32 + +#define UINT32_SERIALIZE_MAX 10 -template -class HashSet; +namespace { +std::string Serialize(std::unordered_set& set, uint32_t* size); +void Deserialize(std::unordered_set& set, const char* buffer, uint32_t size); +} class ExtensionWhitelistParser { public: @@ -31,12 +38,12 @@ class ExtensionWhitelistParser { // Deserializes the buffer, a size is not needed since a serialized // buffer is self described - bool deserialize(char *buffer); - bool deserialize(char *buffer, size_t); + bool deserialize(const char* buffer); + bool deserialize(const char* buffer, size_t); private: - std::unique_ptr > mBlacklist; - std::unique_ptr > mWhitelist; + std::unordered_set mBlacklist; + std::unordered_set mWhitelist; }; #endif // EXTENSION_WHITELIST_PARSER_H_ diff --git a/package-lock.json b/package-lock.json index fd8c83a..d0622be 100644 --- a/package-lock.json +++ b/package-lock.json @@ -586,11 +586,6 @@ "resolved": "https://registry.npmjs.org/has-unicode/-/has-unicode-2.0.1.tgz", "integrity": "sha1-4Ob+aijPUROIVeCG0Wkedx3iqLk=" }, - "hashset-cpp": { - "version": "2.2.1", - "resolved": "https://registry.npmjs.org/hashset-cpp/-/hashset-cpp-2.2.1.tgz", - "integrity": "sha512-pO16ApkT0SEtXmCy9FgcwwPnTUbdRzL000gcpOtJyIY3OOSYvh4eQaZdYjlU+Y1AOzy3am6msgHZZ5Yok19aLw==" - }, "he": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/he/-/he-1.2.0.tgz", diff --git a/package.json b/package.json index 63c708f..e35bca0 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,6 @@ }, "scripts": { "install": "node-gyp rebuild", - "preinstall": "npm install hashset-cpp", "data-files": "node ./scripts/gen_data_file.js", "test": "mocha" }, @@ -24,7 +23,6 @@ }, "homepage": "https://github.com/brave/extension-whitelist#readme", "dependencies": { - "hashset-cpp": "^2.2.1", "mkdirp": "^1.0.3", "node-gyp": "^5.0.3" },