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

This commit is contained in:
Treehugger Robot 2019-10-11 20:11:49 +00:00 committed by Gerrit Code Review
commit 536e8ded6b
2 changed files with 37 additions and 0 deletions

View file

@ -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];
}

View file

@ -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(