Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 536e8ded authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge "Only allow alphanumerical characters, '-' and '_' in event trigger names"

parents 0e9a0f98 aaab5966
Loading
Loading
Loading
Loading
+17 −0
Original line number Diff line number Diff line
@@ -16,11 +16,14 @@

#include "action_parser.h"

#include <ctype.h>

#include <android-base/properties.h>
#include <android-base/strings.h>

#if defined(__ANDROID__)
#include "property_service.h"
#include "selinux.h"
#else
#include "host_init_stubs.h"
#endif
@@ -77,6 +80,17 @@ Result<void> ParsePropertyTrigger(const std::string& trigger, Subcontext* subcon
    return {};
}

Result<void> ValidateEventTrigger(const std::string& event_trigger) {
    if (SelinuxGetVendorAndroidVersion() >= __ANDROID_API_R__) {
        for (const char& c : event_trigger) {
            if (c != '_' && c != '-' && !std::isalnum(c)) {
                return Error() << "Illegal character '" << c << "' in '" << event_trigger << "'";
            }
        }
    }
    return {};
}

Result<void> ParseTriggers(const std::vector<std::string>& args, Subcontext* subcontext,
                           std::string* event_trigger,
                           std::map<std::string, std::string>* property_triggers) {
@@ -103,6 +117,9 @@ Result<void> ParseTriggers(const std::vector<std::string>& args, Subcontext* sub
            if (!event_trigger->empty()) {
                return Error() << "multiple event triggers are not allowed";
            }
            if (auto result = ValidateEventTrigger(args[i]); !result) {
                return result;
            }

            *event_trigger = args[i];
        }
+20 −0
Original line number Diff line number Diff line
@@ -93,6 +93,26 @@ pass_test
    EXPECT_TRUE(expect_true);
}

TEST(init, WrongEventTrigger) {
    std::string init_script =
            R"init(
on boot:
pass_test
)init";

    TemporaryFile tf;
    ASSERT_TRUE(tf.fd != -1);
    ASSERT_TRUE(android::base::WriteStringToFd(init_script, tf.fd));

    ActionManager am;

    Parser parser;
    parser.AddSectionParser("on", std::make_unique<ActionParser>(&am, nullptr));

    ASSERT_TRUE(parser.ParseConfig(tf.path));
    ASSERT_EQ(1u, parser.parse_error_count());
}

TEST(init, EventTriggerOrder) {
    std::string init_script =
        R"init(