move CSP code to a helper funciton, apply to stylesheet property

This commit is contained in:
Peter Snyder
2021-12-10 15:06:52 -06:00
parent 548ffac408
commit 3c5fb368e1
@@ -29,6 +29,16 @@ namespace speedreader {
namespace {
std::string WrapStylesheetWithCSP(const std::string &stylesheet) {
const std::string style_hash = crypto::SHA256HashString(stylesheet);
const std::string style_hash_b64 =
base::Base64Encode(base::as_bytes(base::make_span(style_hash)));
return "<meta http-equiv=\"Content-Security-Policy\" content=\""
"script-src 'none'; style-src 'sha256-" + style_hash_b64 + "'\">\n"
"<style id=\"brave_speedreader_style\">" + stylesheet + "</style>";
}
std::string GetDistilledPageStylesheet(const base::FilePath& stylesheet_path) {
std::string stylesheet;
const bool success = base::ReadFileToString(stylesheet_path, &stylesheet);
@@ -39,13 +49,7 @@ std::string GetDistilledPageStylesheet(const base::FilePath& stylesheet_path) {
IDR_SPEEDREADER_STYLE_DESKTOP);
}
const std::string style_hash = crypto::SHA256HashString(stylesheet);
const std::string style_hash_b64 =
base::Base64Encode(base::as_bytes(base::make_span(style_hash)));
return "<meta http-equiv=\"Content-Security-Policy\" content=\""
"script-src 'none'; style-src 'sha256-" + style_hash_b64 + "'\">\n"
"<style id=\"brave_speedreader_style\">" + stylesheet + "</style>";
return WrapStylesheetWithCSP(stylesheet);
}
} // namespace
@@ -55,11 +59,9 @@ SpeedreaderRewriterService::SpeedreaderRewriterService(
: component_(new speedreader::SpeedreaderComponent(delegate)),
speedreader_(new speedreader::SpeedReader) {
// Load the built-in stylesheet as the default
content_stylesheet_ =
"<style id=\"brave_speedreader_style\">" +
content_stylesheet_ = WrapStylesheetWithCSP(
ui::ResourceBundle::GetSharedInstance().LoadDataResourceString(
IDR_SPEEDREADER_STYLE_DESKTOP) +
"</style>";
IDR_SPEEDREADER_STYLE_DESKTOP));
// Check the paths from the component as observer may register
// later than the paths were available in the component.