manifest_fuzzer.cc 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Copyright 2021 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include <stddef.h>
  5. #include <stdint.h>
  6. #include <string>
  7. #include <utility>
  8. #include <vector>
  9. #include <fuzzer/FuzzedDataProvider.h>
  10. #include "base/at_exit.h"
  11. #include "base/json/json_reader.h"
  12. #include "base/values.h"
  13. #include "extensions/common/extensions_client.h"
  14. #include "extensions/common/install_warning.h"
  15. #include "extensions/common/manifest.h"
  16. #include "extensions/common/mojom/manifest.mojom-shared.h"
  17. #include "extensions/test/test_extensions_client.h"
  18. #include "third_party/abseil-cpp/absl/types/optional.h"
  19. namespace extensions {
  20. namespace {
  21. const mojom::ManifestLocation kLocations[] = {
  22. mojom::ManifestLocation::kInternal,
  23. mojom::ManifestLocation::kExternalPref,
  24. mojom::ManifestLocation::kExternalRegistry,
  25. mojom::ManifestLocation::kUnpacked,
  26. mojom::ManifestLocation::kComponent,
  27. mojom::ManifestLocation::kExternalPrefDownload,
  28. mojom::ManifestLocation::kExternalPolicyDownload,
  29. mojom::ManifestLocation::kCommandLine,
  30. mojom::ManifestLocation::kExternalPolicy,
  31. mojom::ManifestLocation::kExternalComponent,
  32. };
  33. struct Environment {
  34. Environment() { ExtensionsClient::Set(&extensions_client); }
  35. // Singleton objects needed for the tested code.
  36. base::AtExitManager at_exit;
  37. TestExtensionsClient extensions_client;
  38. };
  39. } // namespace
  40. extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
  41. static Environment env;
  42. FuzzedDataProvider fuzzed_data_provider(data, size);
  43. std::string extension_id = fuzzed_data_provider.ConsumeRandomLengthString();
  44. if (extension_id.empty())
  45. extension_id.resize(1);
  46. absl::optional<base::Value> parsed_json = base::JSONReader::Read(
  47. fuzzed_data_provider.ConsumeRemainingBytesAsString());
  48. if (!parsed_json || !parsed_json->is_dict())
  49. return 0;
  50. for (auto location : kLocations) {
  51. Manifest manifest(location,
  52. base::DictionaryValue::From(
  53. base::Value::ToUniquePtrValue(parsed_json->Clone())),
  54. extension_id);
  55. std::string error;
  56. std::vector<InstallWarning> install_warning;
  57. manifest.ValidateManifest(&error, &install_warning);
  58. }
  59. return 0;
  60. }
  61. } // namespace extensions