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
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/safe_browsing/content/renderer/threat_dom_details.h"
#include <algorithm>
#include <map>
#include <string>
#include <unordered_set>
#include "base/compiler_specific.h"
#include "base/functional/bind.h"
#include "base/metrics/field_trial_params.h"
#include "base/metrics/histogram_macros.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "components/safe_browsing/core/common/features.h"
#include "content/public/renderer/render_frame.h"
#include "third_party/blink/public/platform/web_string.h"
#include "third_party/blink/public/web/web_document.h"
#include "third_party/blink/public/web/web_element.h"
#include "third_party/blink/public/web/web_element_collection.h"
#include "third_party/blink/public/web/web_frame.h"
#include "third_party/blink/public/web/web_local_frame.h"
namespace safe_browsing {
// A map for keeping track of the identity of DOM Elements, used to generate
// unique IDs for each element and lookup elements IDs by parent Element, to
// maintain proper parent/child relationships.
// They key is a WebNode from the DOM, which is basically a pointer so can be
// copied into the map when inserting new elements.
// The values are indices into the resource vector, and are used to retrieve IPC
// messages generated by ThreatDOMDetails.
using ElementToNodeMap = std::map<blink::WebNode, size_t>;
// The name of the param containing the tags and attributes list.
const char kTagAndAttributeParamName[] = "tag_attribute_csv";
namespace {
void GetDefaultTagAndAttributeList(
std::vector<TagAndAttributesItem>* tag_and_attributes_list) {
tag_and_attributes_list->clear();
// These entries must be sorted by tag name.
// These tags are related to identifying Google ads.
tag_and_attributes_list->push_back(
TagAndAttributesItem("div", {"data-google-query-id", "id"}));
tag_and_attributes_list->push_back(TagAndAttributesItem("iframe", {"id"}));
}
void ParseTagAndAttributeParams(
std::vector<TagAndAttributesItem>* tag_and_attributes_list) {
DCHECK(tag_and_attributes_list);
// If the feature is disabled we just use the default list. Otherwise the list
// from the Finch param will be the one used.
if (!base::FeatureList::IsEnabled(kThreatDomDetailsTagAndAttributeFeature)) {
GetDefaultTagAndAttributeList(tag_and_attributes_list);
return;
}
tag_and_attributes_list->clear();
const std::string& tag_attribute_csv_param =
base::GetFieldTrialParamValueByFeature(
kThreatDomDetailsTagAndAttributeFeature, kTagAndAttributeParamName);
if (tag_attribute_csv_param.empty()) {
return;
}
std::vector<std::string> split =
base::SplitString(tag_attribute_csv_param, ",", base::TRIM_WHITESPACE,
base::SPLIT_WANT_NONEMPTY);
// If we don't have the right number of pairs in the csv then don't bother
// parsing further.
if (split.size() % 2 != 0) {
return;
}
for (size_t i = 0; i < split.size(); i += 2) {
const std::string& tag_name = split[i];
const std::string& attribute = split[i + 1];
auto item_iter = std::ranges::find(*tag_and_attributes_list, tag_name,
&TagAndAttributesItem::tag_name);
if (item_iter == tag_and_attributes_list->end()) {
TagAndAttributesItem item;
item.tag_name = tag_name;
item.attributes.push_back(attribute);
tag_and_attributes_list->push_back(item);
} else {
item_iter->attributes.push_back(attribute);
}
}
std::sort(tag_and_attributes_list->begin(), tag_and_attributes_list->end(),
[](const TagAndAttributesItem& a, const TagAndAttributesItem& b) {
return a.tag_name < b.tag_name;
});
}
mojom::ThreatDOMDetailsNode* GetNodeForElement(
const blink::WebNode& element,
const safe_browsing::ElementToNodeMap& element_to_node_map,
std::vector<mojom::ThreatDOMDetailsNodePtr>* resources) {
DCHECK_GT(element_to_node_map.count(element), 0u);
size_t resource_index = element_to_node_map.at(element);
return (*resources)[resource_index].get();
}
std::string TruncateAttributeString(const std::string& input) {
if (input.length() <= ThreatDOMDetails::kMaxAttributeStringLength) {
return input;
}
std::string truncated;
base::TruncateUTF8ToByteSize(
input, ThreatDOMDetails::kMaxAttributeStringLength - 3, &truncated);
truncated.append("...");
return truncated;
}
// Handler for the various HTML elements that we extract URLs from.
void HandleElement(
const blink::WebElement& element,
const std::vector<TagAndAttributesItem>& tag_and_attributes_list,
mojom::ThreatDOMDetailsNode* summary_node,
std::vector<mojom::ThreatDOMDetailsNodePtr>* resources,
safe_browsing::ElementToNodeMap* element_to_node_map) {
// Retrieve the link and resolve the link in case it's relative.
blink::WebURL full_url =
element.GetDocument().CompleteURL(element.GetAttribute("src"));
const GURL& child_url = GURL(full_url);
if (!child_url.is_empty() && child_url.is_valid()) {
summary_node->children.push_back(child_url);
}
mojom::ThreatDOMDetailsNodePtr child_node =
mojom::ThreatDOMDetailsNode::New();
child_node->url = child_url;
child_node->tag_name = element.TagName().Utf8();
child_node->parent = summary_node->url;
// The body of an iframe may be in a different renderer. Look up the routing
// ID of the local or remote frame and store it with the iframe node. If this
// element is not a frame then the result of the lookup will be null.
blink::WebFrame* subframe = blink::WebFrame::FromFrameOwnerElement(element);
if (subframe) {
child_node->child_frame_token = subframe->GetFrameToken();
}
// Populate the element's attributes, but only collect the ones that are
// configured in the finch study.
const auto& tag_attribute_iter = std::ranges::find(
tag_and_attributes_list, base::ToLowerASCII(child_node->tag_name),
&TagAndAttributesItem::tag_name);
if (tag_attribute_iter != tag_and_attributes_list.end()) {
const std::vector<std::string> attributes_to_collect =
tag_attribute_iter->attributes;
for (const std::string& attribute : attributes_to_collect) {
blink::WebString attr_webstring = blink::WebString::FromASCII(attribute);
if (!element.HasAttribute(attr_webstring)) {
continue;
}
mojom::AttributeNameValuePtr attribute_name_value =
mojom::AttributeNameValue::New(
attribute, TruncateAttributeString(
element.GetAttribute(attr_webstring).Ascii()));
child_node->attributes.push_back(std::move(attribute_name_value));
if (child_node->attributes.size() == ThreatDOMDetails::kMaxAttributes) {
break;
}
}
}
// Update the ID mapping. First generate the ID for the current node.
// Then, if its parent is available, set the current node's parent ID, and
// also update the parent's children with the current node's ID.
const int child_id = static_cast<int>(element_to_node_map->size()) + 1;
child_node->node_id = child_id;
blink::WebNode cur_parent_element = element.ParentNode();
while (!cur_parent_element.IsNull()) {
if (element_to_node_map->count(cur_parent_element) > 0) {
mojom::ThreatDOMDetailsNode* parent_node = GetNodeForElement(
cur_parent_element, *element_to_node_map, resources);
child_node->parent_node_id = parent_node->node_id;
parent_node->child_node_ids.push_back(child_id);
// TODO(lpz): Consider also updating the URL-level parent/child mapping
// here. Eg: child_node.parent=parent_node.url, and
// parent_node.children.push_back(child_url).
break;
} else {
// It's possible that the direct parent of this node wasn't handled, so it
// isn't represented in |element_to_node_map|. Try walking up the
// hierarchy to see if a parent further up was handled.
cur_parent_element = cur_parent_element.ParentNode();
}
}
// Add the child node to the list of resources.
resources->push_back(std::move(child_node));
// .. and remember which index it was inserted at so we can look it up later.
(*element_to_node_map)[element] = resources->size() - 1;
}
bool ShouldHandleElement(
const blink::WebElement& element,
const std::vector<TagAndAttributesItem>& tag_and_attributes_list) {
// Fenced frames are always handled.
// TODO(crbug.com/40900693): Update this to support getting the URL of
// fenced frames loaded with a config.
if (element.HasHTMLTagName("fencedframe")) {
return true;
}
// Resources with a SRC are always handled.
if ((element.HasHTMLTagName("iframe") || element.HasHTMLTagName("frame") ||
element.HasHTMLTagName("embed") || element.HasHTMLTagName("script")) &&
element.HasAttribute("src")) {
return true;
}
const auto& tag_attribute_iter = std::ranges::find(
tag_and_attributes_list, base::ToLowerASCII(element.TagName().Ascii()),
&TagAndAttributesItem::tag_name);
if (tag_attribute_iter == tag_and_attributes_list.end()) {
return false;
}
const std::vector<std::string>& valid_attributes =
tag_attribute_iter->attributes;
for (const std::string& attribute : valid_attributes) {
if (element.HasAttribute(blink::WebString::FromASCII(attribute))) {
return true;
}
}
return false;
}
} // namespace
TagAndAttributesItem::TagAndAttributesItem() = default;
TagAndAttributesItem::TagAndAttributesItem(
const std::string& tag_name_param,
const std::vector<std::string>& attributes_param)
: tag_name(tag_name_param), attributes(attributes_param) {}
TagAndAttributesItem::TagAndAttributesItem(const TagAndAttributesItem& item)
: tag_name(item.tag_name), attributes(item.attributes) {}
TagAndAttributesItem::~TagAndAttributesItem() = default;
uint32_t ThreatDOMDetails::kMaxNodes = 500;
uint32_t ThreatDOMDetails::kMaxAttributes = 100;
uint32_t ThreatDOMDetails::kMaxAttributeStringLength = 100;
// static
ThreatDOMDetails* ThreatDOMDetails::Create(
content::RenderFrame* render_frame,
service_manager::BinderRegistry* registry) {
// Private constructor and public static Create() method to facilitate
// stubbing out this class for binary-size reduction purposes.
return new ThreatDOMDetails(render_frame, registry);
}
void ThreatDOMDetails::OnThreatReporterReceiver(
mojo::PendingReceiver<mojom::ThreatReporter> receiver) {
threat_reporter_receivers_.Add(this, std::move(receiver));
}
ThreatDOMDetails::ThreatDOMDetails(content::RenderFrame* render_frame,
service_manager::BinderRegistry* registry)
: content::RenderFrameObserver(render_frame) {
ParseTagAndAttributeParams(&tag_and_attributes_list_);
// Base::Unretained() is safe here because both the registry and the
// ThreatDOMDetails are scoped to the same render frame.
registry->AddInterface(base::BindRepeating(
&ThreatDOMDetails::OnThreatReporterReceiver, base::Unretained(this)));
}
ThreatDOMDetails::~ThreatDOMDetails() = default;
void ThreatDOMDetails::GetThreatDOMDetails(
GetThreatDOMDetailsCallback callback) {
std::vector<mojom::ThreatDOMDetailsNodePtr> resources;
ExtractResources(&resources);
// Notify the browser.
std::move(callback).Run(std::move(resources));
}
void ThreatDOMDetails::ExtractResources(
std::vector<mojom::ThreatDOMDetailsNodePtr>* resources) {
blink::WebLocalFrame* frame = render_frame()->GetWebFrame();
if (!frame)
return;
mojom::ThreatDOMDetailsNodePtr details_node =
mojom::ThreatDOMDetailsNode::New();
blink::WebDocument document = frame->GetDocument();
details_node->url = GURL(document.Url());
if (document.IsNull()) {
// Nothing in this frame. Just report its URL.
resources->push_back(std::move(details_node));
return;
}
ElementToNodeMap element_to_node_map;
blink::WebElementCollection elements = document.All();
blink::WebElement element = elements.FirstItem();
for (; !element.IsNull(); element = elements.NextItem()) {
if (ShouldHandleElement(element, tag_and_attributes_list_)) {
HandleElement(element, tag_and_attributes_list_, details_node.get(),
resources, &element_to_node_map);
if (resources->size() >= kMaxNodes) {
// We have reached kMaxNodes, exit early.
break;
}
}
}
resources->push_back(std::move(details_node));
}
void ThreatDOMDetails::OnDestruct() {
delete this;
}
} // namespace safe_browsing
|