From 54cc702419163c416e5511f487fcb80cb37b2561 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Thu, 23 Apr 2020 16:50:01 +0300 Subject: plugin_hostlist: add fuzzer --- .gitignore | 1 + subprojects/plugin_hostlist/corpus/apple.txt | 1 + subprojects/plugin_hostlist/corpus/banana.txt | 1 + subprojects/plugin_hostlist/corpus/kiwi.txt | 1 + subprojects/plugin_hostlist/corpus/orange.txt | 1 + subprojects/plugin_hostlist/filterlist.cpp | 25 +++++++---- subprojects/plugin_hostlist/filterlist.h | 58 +++++++++++++++++++++++++ subprojects/plugin_hostlist/filterlist.hpp | 58 ------------------------- subprojects/plugin_hostlist/meson.build | 11 +++++ subprojects/plugin_hostlist/plugin/plugin.cpp | 2 +- subprojects/plugin_hostlist/test/filterlist.cpp | 2 +- subprojects/plugin_hostlist/test/rule.cpp | 2 +- 12 files changed, 94 insertions(+), 69 deletions(-) create mode 100644 subprojects/plugin_hostlist/corpus/apple.txt create mode 100644 subprojects/plugin_hostlist/corpus/banana.txt create mode 100644 subprojects/plugin_hostlist/corpus/kiwi.txt create mode 100644 subprojects/plugin_hostlist/corpus/orange.txt create mode 100644 subprojects/plugin_hostlist/filterlist.h delete mode 100644 subprojects/plugin_hostlist/filterlist.hpp diff --git a/.gitignore b/.gitignore index d65f849..1715188 100644 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,7 @@ build* subprojects/* !subprojects/*.wrap !subprojects/plugin_* +subprojects/plugin_*/corpus lang/*.qm tools/src/crashhandler/defaults.go diff --git a/subprojects/plugin_hostlist/corpus/apple.txt b/subprojects/plugin_hostlist/corpus/apple.txt new file mode 100644 index 0000000..3a8973b --- /dev/null +++ b/subprojects/plugin_hostlist/corpus/apple.txt @@ -0,0 +1 @@ +127.0.0.1 localhost.localdomain diff --git a/subprojects/plugin_hostlist/corpus/banana.txt b/subprojects/plugin_hostlist/corpus/banana.txt new file mode 100644 index 0000000..c30aa84 --- /dev/null +++ b/subprojects/plugin_hostlist/corpus/banana.txt @@ -0,0 +1 @@ +0.0.0.0 blockeddomain.com diff --git a/subprojects/plugin_hostlist/corpus/kiwi.txt b/subprojects/plugin_hostlist/corpus/kiwi.txt new file mode 100644 index 0000000..77c325c --- /dev/null +++ b/subprojects/plugin_hostlist/corpus/kiwi.txt @@ -0,0 +1 @@ +# This is a comment, and after it comes a blank line diff --git a/subprojects/plugin_hostlist/corpus/orange.txt b/subprojects/plugin_hostlist/corpus/orange.txt new file mode 100644 index 0000000..583273d --- /dev/null +++ b/subprojects/plugin_hostlist/corpus/orange.txt @@ -0,0 +1 @@ +0.0.0.0 blockeddomain.first blockeddomain.second diff --git a/subprojects/plugin_hostlist/filterlist.cpp b/subprojects/plugin_hostlist/filterlist.cpp index 42be349..a0fd414 100644 --- a/subprojects/plugin_hostlist/filterlist.cpp +++ b/subprojects/plugin_hostlist/filterlist.cpp @@ -6,14 +6,26 @@ * SPDX-License-Identifier: GPL-3.0 */ -#include "filterlist.hpp" +#include "filterlist.h" #include #include using namespace Hostlist; +#ifdef FUZZER +extern "C" int LLVMFuzzerTestOneInput(const char *Data, long long Size) +{ + Filterlist::parseRule(QString::fromLatin1(Data, Size)); + return 0; +} +#endif + std::map Filterlist::parseRule(const QString &line) { + if(line.isEmpty() || line.at(0) == '#') { + return {}; + } + auto parts = line.trimmed().split(' '); if(parts.size() < 2) { return {}; @@ -36,14 +48,11 @@ bool Filterlist::load(QIODevice &from) while(from.bytesAvailable() > 0) { const auto line = from.readLine(512).trimmed(); - if(!line.isEmpty() && line.at(0) != '#') { - auto r = parseRule(line); - if(!r.empty()) { - qDebug("merging in %lu rules", r.size()); - rules.merge(r); - } + auto r = parseRule(line); + if(!r.empty()) { + qDebug("merging in %lu rules", r.size()); + rules.merge(r); } } return true; } - diff --git a/subprojects/plugin_hostlist/filterlist.h b/subprojects/plugin_hostlist/filterlist.h new file mode 100644 index 0000000..7301f20 --- /dev/null +++ b/subprojects/plugin_hostlist/filterlist.h @@ -0,0 +1,58 @@ +/* + * This file is part of smolbote. It's copyrighted by the contributors recorded + * in the version control history of the file, available from its original + * location: https://library.iserlohn-fortress.net/aqua/smolbote.git + * + * SPDX-License-Identifier: GPL-3.0 + */ + +#pragma once + +#include +#include + +namespace Hostlist +{ + +class Filterlist final : public FilterList +{ +public: + typedef uint DomainHash; + struct Rule { + QString domain; + QString redirect; + }; + + Filterlist() = default; + ~Filterlist() = default; + + [[nodiscard]] bool findMatch(const QString &domain) const + { + const auto hash = qHash(domain, 0); + const auto found = rules.find(hash); + if(found != rules.end()) { + return true; + } + return false; + } + int count() const + { + return rules.size(); + } + + [[nodiscard]] bool filter(QWebEngineUrlRequestInfo &info) const + { + return false; + } + [[nodiscard]] bool isUpToDate() const + { + return true; + } + + bool load(QIODevice &device); + [[nodiscard]] static std::map parseRule(const QString &line); + +private: + std::map rules; +}; +} // namespace Hostlist diff --git a/subprojects/plugin_hostlist/filterlist.hpp b/subprojects/plugin_hostlist/filterlist.hpp deleted file mode 100644 index 7301f20..0000000 --- a/subprojects/plugin_hostlist/filterlist.hpp +++ /dev/null @@ -1,58 +0,0 @@ -/* - * This file is part of smolbote. It's copyrighted by the contributors recorded - * in the version control history of the file, available from its original - * location: https://library.iserlohn-fortress.net/aqua/smolbote.git - * - * SPDX-License-Identifier: GPL-3.0 - */ - -#pragma once - -#include -#include - -namespace Hostlist -{ - -class Filterlist final : public FilterList -{ -public: - typedef uint DomainHash; - struct Rule { - QString domain; - QString redirect; - }; - - Filterlist() = default; - ~Filterlist() = default; - - [[nodiscard]] bool findMatch(const QString &domain) const - { - const auto hash = qHash(domain, 0); - const auto found = rules.find(hash); - if(found != rules.end()) { - return true; - } - return false; - } - int count() const - { - return rules.size(); - } - - [[nodiscard]] bool filter(QWebEngineUrlRequestInfo &info) const - { - return false; - } - [[nodiscard]] bool isUpToDate() const - { - return true; - } - - bool load(QIODevice &device); - [[nodiscard]] static std::map parseRule(const QString &line); - -private: - std::map rules; -}; -} // namespace Hostlist diff --git a/subprojects/plugin_hostlist/meson.build b/subprojects/plugin_hostlist/meson.build index 04711fd..f4178c9 100644 --- a/subprojects/plugin_hostlist/meson.build +++ b/subprojects/plugin_hostlist/meson.build @@ -40,3 +40,14 @@ test('filterlist', executable('filterlist', dependencies: [dep_qt5, dep_catch, dep_hostlistfilter]), env: 'HOSTLIST_TXT='+meson.current_source_dir()/'test/hostlist.txt' ) + +# fuzzer +if meson.get_compiler('cpp').has_multi_arguments('-g', '-fsanitize=fuzzer') +executable('hostlist-fuzzer', + sources: 'filterlist.cpp', + include_directories: smolbote_interface, + dependencies: dep_qt5, + cpp_args: [ '-g', '-fsanitize=fuzzer', '-DFUZZER' ], + link_args: [ '-fsanitize=fuzzer' ] +) +endif diff --git a/subprojects/plugin_hostlist/plugin/plugin.cpp b/subprojects/plugin_hostlist/plugin/plugin.cpp index 2399510..28a7706 100644 --- a/subprojects/plugin_hostlist/plugin/plugin.cpp +++ b/subprojects/plugin_hostlist/plugin/plugin.cpp @@ -7,7 +7,7 @@ */ #include "plugin.h" -#include "filterlist.hpp" +#include "filterlist.h" FilterList* HostlistFilterPlugin::load(QIODevice &from) const { diff --git a/subprojects/plugin_hostlist/test/filterlist.cpp b/subprojects/plugin_hostlist/test/filterlist.cpp index fb71068..4aa532b 100644 --- a/subprojects/plugin_hostlist/test/filterlist.cpp +++ b/subprojects/plugin_hostlist/test/filterlist.cpp @@ -1,5 +1,5 @@ #define CATCH_CONFIG_MAIN -#include "filterlist.hpp" +#include "filterlist.h" #include #include diff --git a/subprojects/plugin_hostlist/test/rule.cpp b/subprojects/plugin_hostlist/test/rule.cpp index 6b1ea70..b5ba6e0 100644 --- a/subprojects/plugin_hostlist/test/rule.cpp +++ b/subprojects/plugin_hostlist/test/rule.cpp @@ -1,5 +1,5 @@ #define CATCH_CONFIG_MAIN -#include "filterlist.hpp" +#include "filterlist.h" #include using namespace Hostlist; -- cgit v1.2.1