From c67228115493ab6e99fdb4d597f0dc8ec740591e Mon Sep 17 00:00:00 2001 From: Carmelo Messina Date: Thu, 12 Sep 2024 19:18:40 +0200 Subject: [PATCH] Experimental user scripts support: fix unsafe-buffer-usage errors (#1365) --- .../Experimental-user-scripts-support.patch | 42 ++++++++++--------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/build/patches/Experimental-user-scripts-support.patch b/build/patches/Experimental-user-scripts-support.patch index 3c9ef6d2f..97fb6f52c 100644 --- a/build/patches/Experimental-user-scripts-support.patch +++ b/build/patches/Experimental-user-scripts-support.patch @@ -74,7 +74,7 @@ License: GPL-3.0-only - https://spdx.org/licenses/GPL-3.0-only.html .../user-script-ui/user-scripts-ui.js | 9 + .../browser/ui/user_scripts_ui.cc | 146 ++++ .../user_scripts/browser/ui/user_scripts_ui.h | 37 + - .../browser/user_script_loader.cc | 706 +++++++++++++++ + .../browser/user_script_loader.cc | 704 +++++++++++++++ .../user_scripts/browser/user_script_loader.h | 168 ++++ .../browser/user_script_pref_info.cc | 34 + .../browser/user_script_pref_info.h | 72 ++ @@ -93,7 +93,7 @@ License: GPL-3.0-only - https://spdx.org/licenses/GPL-3.0-only.html components/user_scripts/common/host_id.cc | 31 + components/user_scripts/common/host_id.h | 35 + .../user_scripts/common/script_constants.h | 33 + - components/user_scripts/common/url_pattern.cc | 802 ++++++++++++++++++ + components/user_scripts/common/url_pattern.cc | 808 ++++++++++++++++++ components/user_scripts/common/url_pattern.h | 302 +++++++ .../user_scripts/common/url_pattern_set.cc | 335 ++++++++ .../user_scripts/common/url_pattern_set.h | 160 ++++ @@ -104,7 +104,7 @@ License: GPL-3.0-only - https://spdx.org/licenses/GPL-3.0-only.html components/user_scripts/common/view_type.cc | 37 + components/user_scripts/common/view_type.h | 48 ++ components/user_scripts/renderer/BUILD.gn | 65 ++ - .../renderer/extension_frame_helper.cc | 95 +++ + .../renderer/extension_frame_helper.cc | 95 ++ .../renderer/extension_frame_helper.h | 90 ++ .../user_scripts/renderer/injection_host.cc | 12 + .../user_scripts/renderer/injection_host.h | 41 + @@ -135,7 +135,7 @@ License: GPL-3.0-only - https://spdx.org/licenses/GPL-3.0-only.html .../Experimental-user-scripts-support.inc | 13 + ipc/ipc_message_start.h | 1 + tools/gritsettings/resource_ids.spec | 6 + - 108 files changed, 9476 insertions(+), 2 deletions(-) + 108 files changed, 9480 insertions(+), 2 deletions(-) create mode 100644 components/user_scripts/README.md create mode 100755 components/user_scripts/android/BUILD.gn create mode 100644 components/user_scripts/android/java/res/layout/accept_script_item.xml @@ -2832,7 +2832,7 @@ diff --git a/components/user_scripts/browser/user_script_loader.cc b/components/ new file mode 100755 --- /dev/null +++ b/components/user_scripts/browser/user_script_loader.cc -@@ -0,0 +1,706 @@ +@@ -0,0 +1,704 @@ +/* + This file is part of Bromite. + @@ -2931,9 +2931,7 @@ new file mode 100755 + if (index == std::string_view::npos) + return false; + -+ std::string temp(line.data() + index + prefix.length(), -+ line.length() - index - prefix.length()); -+ ++ std::string_view temp = line.substr(index + prefix.length()); + if (temp.empty() || !base::IsAsciiWhitespace(temp[0])) + return false; + @@ -2961,8 +2959,8 @@ new file mode 100755 + if (line_end == std::string::npos) + line_end = script_text.length() - 1; + -+ line = std::string_view(script_text.data() + line_start, -+ line_end - line_start); ++ line = script_text.substr(line_start, ++ line_end - line_start); + + if (!*found_metadata) { + if (base::StartsWith(line, kUserScriptBegin)) @@ -3070,8 +3068,8 @@ new file mode 100755 + } + + auto buffer = std::vector(length); -+ int bytes_read = infile.Read(0, buffer.data(), length); -+ if (bytes_read == -1) { ++ std::optional bytes_read = infile.Read(0, base::as_writable_byte_span(buffer)); ++ if (!bytes_read.has_value()) { + *error = u"Could not read source file."; + return false; + } @@ -4772,7 +4770,7 @@ diff --git a/components/user_scripts/common/url_pattern.cc b/components/user_scr new file mode 100755 --- /dev/null +++ b/components/user_scripts/common/url_pattern.cc -@@ -0,0 +1,802 @@ +@@ -0,0 +1,808 @@ +// 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. @@ -4902,8 +4900,9 @@ new file mode 100755 + +// static +bool URLPattern::IsValidSchemeForExtensions(std::string_view scheme) { ++ auto validSchemes = base::span(kValidSchemes); + for (size_t i = 0; i < std::size(kValidSchemes); ++i) { -+ if (scheme == kValidSchemes[i]) ++ if (scheme == validSchemes[i]) + return true; + } + return false; @@ -4911,9 +4910,10 @@ new file mode 100755 + +// static +int URLPattern::GetValidSchemeMaskForExtensions() { ++ auto validSchemeMasks = base::span(kValidSchemeMasks); + int result = 0; + for (size_t i = 0; i < std::size(kValidSchemeMasks); ++i) -+ result |= kValidSchemeMasks[i]; ++ result |= validSchemeMasks[i]; + return result; +} + @@ -5159,8 +5159,10 @@ new file mode 100755 + if (valid_schemes_ == SCHEME_ALL) + return true; + ++ auto validSchemes = base::span(kValidSchemes); ++ auto validSchemeMasks = base::span(kValidSchemeMasks); + for (size_t i = 0; i < std::size(kValidSchemes); ++i) { -+ if (scheme == kValidSchemes[i] && (valid_schemes_ & kValidSchemeMasks[i])) ++ if (scheme == validSchemes[i] && (valid_schemes_ & validSchemeMasks[i])) + return true; + } + @@ -5546,9 +5548,10 @@ new file mode 100755 + return result; + } + ++ auto validSchemes = base::span(kValidSchemes); + for (size_t i = 0; i < std::size(kValidSchemes); ++i) { -+ if (MatchesScheme(kValidSchemes[i])) { -+ result.push_back(kValidSchemes[i]); ++ if (MatchesScheme(validSchemes[i])) { ++ result.push_back(validSchemes[i]); + } + } + @@ -5573,7 +5576,8 @@ new file mode 100755 +// static +const char* URLPattern::GetParseResultString( + URLPattern::ParseResult parse_result) { -+ return kParseResultMessages[static_cast(parse_result)]; ++ auto parseResultMessages = base::span(kParseResultMessages); ++ return parseResultMessages[static_cast(parse_result)]; +} diff --git a/components/user_scripts/common/url_pattern.h b/components/user_scripts/common/url_pattern.h new file mode 100755