Prevent overlayable resources from being obfuscated

Once the build process finds one overlayable resource entry is not in
the list of collapse name exemption, the build process outputs the
resource name automatically and gives a warning message to notify the
developers.

Bug: 228192695

Test: atest aapt2_test
Change-Id: I989b75343d803b059c902baa088590fce7237211
This commit is contained in:
felkachang
2022-07-01 15:18:10 +08:00
parent 1fa562ce52
commit 8947def237
2 changed files with 65 additions and 1 deletions

View File

@@ -641,7 +641,23 @@ class PackageFlattener {
local_key_index = (uint32_t)key_pool_.MakeRef(entry.name).index();
} else {
// resource isn't exempt from collapse, add it as obfuscated value
local_key_index = (uint32_t)key_pool_.MakeRef(obfuscated_resource_name).index();
if (entry.overlayable_item) {
// if the resource name of the specific entry is obfuscated and this
// entry is in the overlayable list, the overlay can't work on this
// overlayable at runtime because the name has been obfuscated in
// resources.arsc during flatten operation.
const OverlayableItem& item = entry.overlayable_item.value();
context_->GetDiagnostics()->Warn(android::DiagMessage(item.overlayable->source)
<< "The resource name of overlayable entry "
<< resource_name.to_string() << "'"
<< " shouldn't be obfuscated in resources.arsc");
local_key_index = (uint32_t)key_pool_.MakeRef(entry.name).index();
} else {
// TODO(b/228192695): output the entry.name and Resource id to make
// de-obfuscated possible.
local_key_index = (uint32_t)key_pool_.MakeRef(obfuscated_resource_name).index();
}
}
// Group values by configuration.
for (auto& config_value : entry.values) {

View File

@@ -878,4 +878,52 @@ TEST_F(TableFlattenerTest, FlattenCustomResourceTypes) {
Res_value::TYPE_STRING, (uint32_t)*idx, 0u));
}
TEST_F(TableFlattenerTest, FlattenTypeEntryWithNameCollapseNotInExemption) {
OverlayableItem overlayable_item(std::make_shared<Overlayable>("TestName", "overlay://theme"));
overlayable_item.policies |= PolicyFlags::PUBLIC;
std::string name = "com.app.test:color/overlayable_color";
std::unique_ptr<ResourceTable> table =
test::ResourceTableBuilder()
.AddValue("com.app.test:color/overlayable_color", ResourceId(0x7f010000),
util::make_unique<BinaryPrimitive>(uint8_t(Res_value::TYPE_INT_COLOR_ARGB8),
0xffaabbcc))
.SetOverlayable(name, overlayable_item)
.Build();
TableFlattenerOptions options;
options.collapse_key_stringpool = true;
ResTable res_table;
EXPECT_THAT(Flatten(context_.get(), options, table.get(), &res_table), testing::IsTrue());
EXPECT_THAT(Exists(&res_table, "com.app.test:color/overlayable_color", ResourceId(0x7f010000), {},
Res_value::TYPE_INT_COLOR_ARGB8, 0xffaabbcc, 0u),
testing::IsTrue());
}
TEST_F(TableFlattenerTest, FlattenTypeEntryWithNameCollapseInExemption) {
OverlayableItem overlayable_item(std::make_shared<Overlayable>("TestName", "overlay://theme"));
overlayable_item.policies |= PolicyFlags::PUBLIC;
std::string name = "com.app.test:color/overlayable_color";
std::unique_ptr<ResourceTable> table =
test::ResourceTableBuilder()
.AddValue("com.app.test:color/overlayable_color", ResourceId(0x7f010000),
util::make_unique<BinaryPrimitive>(uint8_t(Res_value::TYPE_INT_COLOR_ARGB8),
0xffaabbcc))
.SetOverlayable(name, overlayable_item)
.Build();
TableFlattenerOptions options;
options.collapse_key_stringpool = true;
options.name_collapse_exemptions.insert(
ResourceName({}, ResourceType::kColor, "overlayable_color"));
ResTable res_table;
EXPECT_THAT(Flatten(context_.get(), options, table.get(), &res_table), testing::IsTrue());
EXPECT_THAT(Exists(&res_table, "com.app.test:color/overlayable_color", ResourceId(0x7f010000), {},
Res_value::TYPE_INT_COLOR_ARGB8, 0xffaabbcc, 0u),
testing::IsTrue());
}
} // namespace aapt