1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "extensions/utility/unpacker.h"
#include <stddef.h>
#include <algorithm>
#include <set>
#include <tuple>
#include <utility>
#include "base/files/file_enumerator.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/i18n/rtl.h"
#include "base/json/json_file_value_serializer.h"
#include "base/numerics/safe_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/threading/thread.h"
#include "base/values.h"
#include "content/public/child/image_decoder_utils.h"
#include "extensions/common/constants.h"
#include "extensions/common/extension.h"
#include "extensions/common/extension_l10n_util.h"
#include "extensions/common/extension_utility_messages.h"
#include "extensions/common/extensions_client.h"
#include "extensions/common/file_util.h"
#include "extensions/common/manifest.h"
#include "extensions/common/manifest_constants.h"
#include "extensions/common/manifest_handlers/default_locale_handler.h"
#include "extensions/strings/grit/extensions_strings.h"
#include "ipc/ipc_message_utils.h"
#include "net/base/file_stream.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/gfx/geometry/size.h"
namespace extensions {
namespace {
namespace errors = manifest_errors;
namespace keys = manifest_keys;
// A limit to stop us passing dangerously large canvases to the browser.
const int kMaxImageCanvas = 4096 * 4096;
static const base::FilePath::CharType* kAllowedThemeFiletypes[] = {
FILE_PATH_LITERAL(".bmp"), FILE_PATH_LITERAL(".gif"),
FILE_PATH_LITERAL(".jpeg"), FILE_PATH_LITERAL(".jpg"),
FILE_PATH_LITERAL(".json"), FILE_PATH_LITERAL(".png"),
FILE_PATH_LITERAL(".webp")};
SkBitmap DecodeImage(const base::FilePath& path) {
// Read the file from disk.
std::string file_contents;
if (!base::PathExists(path) ||
!base::ReadFileToString(path, &file_contents)) {
return SkBitmap();
}
// Decode the image using WebKit's image decoder.
const unsigned char* data =
reinterpret_cast<const unsigned char*>(file_contents.data());
SkBitmap bitmap =
content::DecodeImage(data, gfx::Size(), file_contents.length());
if (bitmap.computeSize64() > kMaxImageCanvas)
return SkBitmap();
return bitmap;
}
bool PathContainsParentDirectory(const base::FilePath& path) {
const base::FilePath::StringType kSeparators(base::FilePath::kSeparators);
const base::FilePath::StringType kParentDirectory(
base::FilePath::kParentDirectory);
const size_t npos = base::FilePath::StringType::npos;
const base::FilePath::StringType& value = path.value();
for (size_t i = 0; i < value.length();) {
i = value.find(kParentDirectory, i);
if (i != npos) {
if ((i == 0 || kSeparators.find(value[i - 1]) == npos) &&
(i + 1 < value.length() || kSeparators.find(value[i + 1]) == npos)) {
return true;
}
++i;
}
}
return false;
}
bool WritePickle(const IPC::Message& pickle, const base::FilePath& dest_path) {
int size = base::checked_cast<int>(pickle.size());
const char* data = static_cast<const char*>(pickle.data());
int bytes_written = base::WriteFile(dest_path, data, size);
return (bytes_written == size);
}
} // namespace
struct Unpacker::InternalData {
DecodedImages decoded_images;
};
Unpacker::Unpacker(const base::FilePath& working_dir,
const base::FilePath& extension_dir,
const std::string& extension_id,
Manifest::Location location,
int creation_flags)
: working_dir_(working_dir),
extension_dir_(extension_dir),
extension_id_(extension_id),
location_(location),
creation_flags_(creation_flags) {
internal_data_.reset(new InternalData());
}
Unpacker::~Unpacker() {
}
// static
bool Unpacker::ShouldExtractFile(bool is_theme,
const base::FilePath& file_path) {
if (is_theme) {
const base::FilePath::StringType extension =
base::ToLowerASCII(file_path.FinalExtension());
// Allow filenames with no extension.
if (extension.empty())
return true;
return std::find(kAllowedThemeFiletypes,
kAllowedThemeFiletypes + arraysize(kAllowedThemeFiletypes),
extension) !=
(kAllowedThemeFiletypes + arraysize(kAllowedThemeFiletypes));
}
return !base::FilePath::CompareEqualIgnoreCase(file_path.FinalExtension(),
FILE_PATH_LITERAL(".exe"));
}
// static
bool Unpacker::IsManifestFile(const base::FilePath& file_path) {
CHECK(!file_path.IsAbsolute());
return base::FilePath::CompareEqualIgnoreCase(file_path.value(),
kManifestFilename);
}
// static
std::unique_ptr<base::DictionaryValue> Unpacker::ReadManifest(
const base::FilePath& extension_dir,
std::string* error) {
DCHECK(error);
base::FilePath manifest_path = extension_dir.Append(kManifestFilename);
if (!base::PathExists(manifest_path)) {
*error = errors::kInvalidManifest;
return nullptr;
}
JSONFileValueDeserializer deserializer(manifest_path);
std::unique_ptr<base::Value> root = deserializer.Deserialize(NULL, error);
if (!root.get()) {
return nullptr;
}
if (!root->IsType(base::Value::Type::DICTIONARY)) {
*error = errors::kInvalidManifest;
return nullptr;
}
return base::DictionaryValue::From(std::move(root));
}
bool Unpacker::ReadAllMessageCatalogs() {
base::FilePath locales_path = extension_dir_.Append(kLocaleFolder);
// Not all folders under _locales have to be valid locales.
base::FileEnumerator locales(locales_path, false,
base::FileEnumerator::DIRECTORIES);
std::set<std::string> all_locales;
extension_l10n_util::GetAllLocales(&all_locales);
base::FilePath locale_path;
while (!(locale_path = locales.Next()).empty()) {
if (extension_l10n_util::ShouldSkipValidation(locales_path, locale_path,
all_locales))
continue;
base::FilePath messages_path = locale_path.Append(kMessagesFilename);
if (!ReadMessageCatalog(messages_path))
return false;
}
return true;
}
bool Unpacker::Run() {
// Parse the manifest.
std::string error;
parsed_manifest_ = ReadManifest(extension_dir_, &error);
if (!parsed_manifest_.get()) {
SetError(error);
return false;
}
scoped_refptr<Extension> extension(
Extension::Create(extension_dir_, location_, *parsed_manifest_,
creation_flags_, extension_id_, &error));
if (!extension.get()) {
SetError(error);
return false;
}
std::vector<InstallWarning> warnings;
if (!file_util::ValidateExtension(extension.get(), &error, &warnings)) {
SetError(error);
return false;
}
extension->AddInstallWarnings(warnings);
// Decode any images that the browser needs to display.
std::set<base::FilePath> image_paths =
ExtensionsClient::Get()->GetBrowserImagePaths(extension.get());
for (const base::FilePath& path : image_paths) {
if (!AddDecodedImage(path))
return false; // Error was already reported.
}
// Parse all message catalogs (if any).
parsed_catalogs_.reset(new base::DictionaryValue);
if (!LocaleInfo::GetDefaultLocale(extension.get()).empty()) {
if (!ReadAllMessageCatalogs())
return false; // Error was already reported.
}
return DumpImagesToFile() && DumpMessageCatalogsToFile();
}
bool Unpacker::DumpImagesToFile() {
IPC::Message pickle; // We use a Message so we can use WriteParam.
IPC::WriteParam(&pickle, internal_data_->decoded_images);
base::FilePath path = working_dir_.AppendASCII(kDecodedImagesFilename);
if (!WritePickle(pickle, path)) {
SetError("Could not write image data to disk.");
return false;
}
return true;
}
bool Unpacker::DumpMessageCatalogsToFile() {
IPC::Message pickle;
IPC::WriteParam(&pickle, *parsed_catalogs_.get());
base::FilePath path =
working_dir_.AppendASCII(kDecodedMessageCatalogsFilename);
if (!WritePickle(pickle, path)) {
SetError("Could not write message catalogs to disk.");
return false;
}
return true;
}
bool Unpacker::AddDecodedImage(const base::FilePath& path) {
// Make sure it's not referencing a file outside the extension's subdir.
if (path.IsAbsolute() || PathContainsParentDirectory(path)) {
SetUTF16Error(l10n_util::GetStringFUTF16(
IDS_EXTENSION_PACKAGE_IMAGE_PATH_ERROR,
base::i18n::GetDisplayStringInLTRDirectionality(
path.LossyDisplayName())));
return false;
}
SkBitmap image_bitmap = DecodeImage(extension_dir_.Append(path));
if (image_bitmap.isNull()) {
SetUTF16Error(l10n_util::GetStringFUTF16(
IDS_EXTENSION_PACKAGE_IMAGE_ERROR,
base::i18n::GetDisplayStringInLTRDirectionality(
path.BaseName().LossyDisplayName())));
return false;
}
internal_data_->decoded_images.push_back(std::make_tuple(image_bitmap, path));
return true;
}
bool Unpacker::ReadMessageCatalog(const base::FilePath& message_path) {
std::string error;
JSONFileValueDeserializer deserializer(message_path);
std::unique_ptr<base::DictionaryValue> root =
base::DictionaryValue::From(deserializer.Deserialize(NULL, &error));
if (!root.get()) {
base::string16 messages_file = message_path.LossyDisplayName();
if (error.empty()) {
// If file is missing, Deserialize will fail with empty error.
SetError(base::StringPrintf("%s %s", errors::kLocalesMessagesFileMissing,
base::UTF16ToUTF8(messages_file).c_str()));
} else {
SetError(base::StringPrintf(
"%s: %s", base::UTF16ToUTF8(messages_file).c_str(), error.c_str()));
}
return false;
}
base::FilePath relative_path;
// message_path was created from temp_install_dir. This should never fail.
if (!extension_dir_.AppendRelativePath(message_path, &relative_path)) {
NOTREACHED();
return false;
}
std::string dir_name = relative_path.DirName().MaybeAsASCII();
if (dir_name.empty()) {
NOTREACHED();
return false;
}
parsed_catalogs_->Set(dir_name, root.release());
return true;
}
void Unpacker::SetError(const std::string& error) {
SetUTF16Error(base::UTF8ToUTF16(error));
}
void Unpacker::SetUTF16Error(const base::string16& error) {
error_message_ = error;
}
} // namespace extensions
|