json_file_sanitizer.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright 2018 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. #ifndef EXTENSIONS_BROWSER_JSON_FILE_SANITIZER_H_
  5. #define EXTENSIONS_BROWSER_JSON_FILE_SANITIZER_H_
  6. #include <memory>
  7. #include <set>
  8. #include <string>
  9. #include <tuple>
  10. #include "base/files/file_path.h"
  11. #include "base/memory/weak_ptr.h"
  12. #include "base/values.h"
  13. #include "mojo/public/cpp/bindings/remote.h"
  14. #include "services/data_decoder/public/mojom/json_parser.mojom.h"
  15. #include "third_party/abseil-cpp/absl/types/optional.h"
  16. namespace data_decoder {
  17. class DataDecoder;
  18. }
  19. namespace extensions {
  20. // This class takes potentially unsafe JSON files, decodes them in a sandboxed
  21. // process, then reencodes them so that they can later be parsed safely from the
  22. // browser process.
  23. // Note that at this time it this is limited to JSON files that contain a
  24. // unique dictionary as their root and will fail with a kDecodingError if that
  25. // is not the case.
  26. class JsonFileSanitizer {
  27. public:
  28. enum class Status {
  29. kSuccess = 0,
  30. kFileReadError,
  31. kFileDeleteError,
  32. kDecodingError,
  33. kSerializingError,
  34. kFileWriteError,
  35. };
  36. // Callback invoked when the JSON sanitization is is done. If status is an
  37. // error, |error_msg| contains the error message.
  38. using Callback =
  39. base::OnceCallback<void(Status status, const std::string& error_msg)>;
  40. // Creates a JsonFileSanitizer and starts the sanitization of the JSON files
  41. // in |file_paths|.
  42. // |decoder| should be a DataDecoder which can be used to talk to a Data
  43. // Decoder service instance. It must be live on the calling sequence and
  44. // it is not retained beyond the extent of this call.
  45. // |callback| is invoked asynchronously when all JSON files have been
  46. // sanitized or if an error occurred.
  47. // If the returned JsonFileSanitizer instance is deleted before |callback| was
  48. // invoked, then |callback| is never invoked and the sanitization stops
  49. // promptly (some background tasks may still run).
  50. static std::unique_ptr<JsonFileSanitizer> CreateAndStart(
  51. data_decoder::DataDecoder* decoder,
  52. const std::set<base::FilePath>& file_paths,
  53. Callback callback,
  54. const scoped_refptr<base::SequencedTaskRunner>& io_task_runner);
  55. JsonFileSanitizer(const JsonFileSanitizer&) = delete;
  56. JsonFileSanitizer& operator=(const JsonFileSanitizer&) = delete;
  57. ~JsonFileSanitizer();
  58. private:
  59. JsonFileSanitizer(
  60. const std::set<base::FilePath>& file_paths,
  61. Callback callback,
  62. const scoped_refptr<base::SequencedTaskRunner>& io_task_runner);
  63. void Start(data_decoder::DataDecoder* decoder);
  64. void JsonFileRead(const base::FilePath& file_path,
  65. std::tuple<std::string, bool, bool> read_and_delete_result);
  66. void JsonParsingDone(const base::FilePath& file_path,
  67. absl::optional<base::Value> json_value,
  68. const absl::optional<std::string>& error);
  69. void JsonFileWritten(const base::FilePath& file_path,
  70. int expected_size,
  71. int actual_size);
  72. void ReportSuccess();
  73. void ReportError(Status status, const std::string& path);
  74. std::set<base::FilePath> file_paths_;
  75. Callback callback_;
  76. scoped_refptr<base::SequencedTaskRunner> io_task_runner_;
  77. mojo::Remote<data_decoder::mojom::JsonParser> json_parser_;
  78. base::WeakPtrFactory<JsonFileSanitizer> weak_factory_{this};
  79. };
  80. } // namespace extensions
  81. #endif // EXTENSIONS_BROWSER_JSON_FILE_SANITIZER_H_