Skip to content

Instantly share code, notes, and snippets.

@uazo
Created August 28, 2023 08:07
Show Gist options
  • Save uazo/fce34cf7b6331b79dc558091cbc9ae35 to your computer and use it in GitHub Desktop.
Save uazo/fce34cf7b6331b79dc558091cbc9ae35 to your computer and use it in GitHub Desktop.
From: uazo <uazo@users.noreply.github.com>
Date: Mon, 28 Aug 2023 07:57:50 +0000
Subject: Anti anti-adblocker
---
.../blink/renderer/core/fetch/fetch_manager.cc | 16 +++++++++++-----
.../renderer/core/html/html_image_loader.cc | 13 +++++++++++--
.../renderer/core/html/html_link_element.cc | 7 ++++---
.../blink/renderer/core/html/html_link_element.h | 2 +-
.../blink/renderer/core/loader/image_loader.cc | 5 ++++-
.../blink/renderer/core/loader/link_loader.cc | 4 ++--
.../renderer/core/loader/link_loader_client.h | 2 +-
.../core/script/classic_pending_script.cc | 2 +-
.../blink/renderer/core/script/pending_script.cc | 2 +-
.../blink/renderer/core/svg/svg_image_loader.cc | 10 +++++++++-
.../blink/renderer/core/svg/svg_use_element.cc | 2 ++
.../core/xmlhttprequest/xml_http_request.cc | 10 +++++++---
.../core/xmlhttprequest/xml_http_request.h | 2 +-
.../renderer/platform/loader/fetch/resource.h | 6 ++++++
.../platform/loader/fetch/resource_error.cc | 4 ++++
.../platform/loader/fetch/resource_error.h | 1 +
.../platform/loader/fetch/resource_fetcher.cc | 2 +-
17 files changed, 67 insertions(+), 23 deletions(-)
diff --git a/third_party/blink/renderer/core/fetch/fetch_manager.cc b/third_party/blink/renderer/core/fetch/fetch_manager.cc
--- a/third_party/blink/renderer/core/fetch/fetch_manager.cc
+++ b/third_party/blink/renderer/core/fetch/fetch_manager.cc
@@ -290,6 +290,7 @@ class FetchManager::Loader final
// of the usual "Failed to fetch" TypeError.
void Failed(const String& message,
DOMException* dom_exception,
+ absl::optional<ResourceError> error,
absl::optional<String> devtools_request_id = absl::nullopt,
absl::optional<base::UnguessableToken> issue_id = absl::nullopt);
void NotifyFinished();
@@ -556,6 +557,7 @@ void FetchManager::Loader::DidFail(uint64_t identifier,
network::mojom::blink::TrustTokenOperationStatus::kOk) {
Failed(String(),
TrustTokenErrorToDOMException(error.TrustTokenOperationError()),
+ error,
IdentifiersFactory::SubresourceRequestId(identifier));
return;
}
@@ -564,12 +566,12 @@ void FetchManager::Loader::DidFail(uint64_t identifier,
? absl::optional<base::UnguessableToken>(
error.CorsErrorStatus()->issue_id)
: absl::nullopt;
- Failed(String(), nullptr,
+ Failed(String(), nullptr, error,
IdentifiersFactory::SubresourceRequestId(identifier), issue_id);
}
void FetchManager::Loader::DidFailRedirectCheck(uint64_t identifier) {
- Failed(String(), nullptr,
+ Failed(String(), nullptr, /*error*/ absl::nullopt,
IdentifiersFactory::SubresourceRequestId(identifier));
}
@@ -774,7 +776,7 @@ void FetchManager::Loader::FileIssueAndPerformNetworkError(
void FetchManager::Loader::PerformNetworkError(
const String& message,
absl::optional<base::UnguessableToken> issue_id) {
- Failed(message, nullptr, absl::nullopt, issue_id);
+ Failed(message, nullptr, /*error*/ absl::nullopt, absl::nullopt, issue_id);
}
void FetchManager::Loader::PerformHTTPFetch() {
@@ -917,6 +919,7 @@ void FetchManager::Loader::PerformDataFetch() {
void FetchManager::Loader::Failed(
const String& message,
DOMException* dom_exception,
+ absl::optional<ResourceError> error,
absl::optional<String> devtools_request_id,
absl::optional<base::UnguessableToken> issue_id) {
if (failed_ || finished_)
@@ -941,8 +944,11 @@ void FetchManager::Loader::Failed(
if (resolver_) {
ScriptState* state = resolver_->GetScriptState();
ScriptState::Scope scope(state);
+ bool should_send_error_to_javascript = true;
+ if (error && error->ShouldSendErrorToJavascript())
+ should_send_error_to_javascript = false;
if (dom_exception) {
- resolver_->Reject(dom_exception);
+ if (should_send_error_to_javascript) resolver_->Reject(dom_exception);
} else {
v8::Local<v8::Value> value = exception_.Get(state->GetIsolate());
exception_.Reset();
@@ -960,7 +966,7 @@ void FetchManager::Loader::Failed(
V8String(state->GetIsolate(),
IdentifiersFactory::IdFromToken(*issue_id)));
}
- resolver_->Reject(value);
+ if (should_send_error_to_javascript) resolver_->Reject(value);
SendHistogram(FetchManagerLoaderCheckPoint::kFailed);
}
}
diff --git a/third_party/blink/renderer/core/html/html_image_loader.cc b/third_party/blink/renderer/core/html/html_image_loader.cc
--- a/third_party/blink/renderer/core/html/html_image_loader.cc
+++ b/third_party/blink/renderer/core/html/html_image_loader.cc
@@ -28,6 +28,7 @@
#include "third_party/blink/renderer/core/html/html_object_element.h"
#include "third_party/blink/renderer/core/html/parser/html_parser_idioms.h"
#include "third_party/blink/renderer/core/loader/resource/image_resource_content.h"
+#include "third_party/blink/renderer/platform/loader/fetch/resource_error.h"
#include "third_party/blink/renderer/platform/loader/fetch/resource_loading_log.h"
namespace blink {
@@ -49,8 +50,16 @@ void HTMLImageLoader::DispatchLoadEvent() {
// An <object> considers a 404 to be an error and should fire onerror.
error_occurred = (GetContent()->GetResponse().HttpStatusCode() >= 400);
}
- GetElement()->DispatchEvent(*Event::Create(
- error_occurred ? event_type_names::kError : event_type_names::kLoad));
+ bool should_send_error_to_javascript = true;
+ ImageResourceContent* content = GetContent();
+ if (content && content->GetResourceError()) {
+ should_send_error_to_javascript =
+ content->GetResourceError()->ShouldSendErrorToJavascript();
+ }
+ if (should_send_error_to_javascript) {
+ GetElement()->DispatchEvent(*Event::Create(
+ error_occurred ? event_type_names::kError : event_type_names::kLoad));
+ }
}
void HTMLImageLoader::NoImageResourceToLoad() {
diff --git a/third_party/blink/renderer/core/html/html_link_element.cc b/third_party/blink/renderer/core/html/html_link_element.cc
--- a/third_party/blink/renderer/core/html/html_link_element.cc
+++ b/third_party/blink/renderer/core/html/html_link_element.cc
@@ -291,11 +291,12 @@ void HTMLLinkElement::LinkLoaded() {
DispatchEvent(*Event::Create(event_type_names::kLoad));
}
-void HTMLLinkElement::LinkLoadingErrored() {
+void HTMLLinkElement::LinkLoadingErrored(bool should_send_error_to_javascript) {
if (rel_attribute_.IsLinkPrefetch()) {
UseCounter::Count(GetDocument(), WebFeature::kLinkPrefetchErrorEvent);
}
- DispatchEvent(*Event::Create(event_type_names::kError));
+ if (should_send_error_to_javascript)
+ DispatchEvent(*Event::Create(event_type_names::kError));
}
bool HTMLLinkElement::SheetLoaded() {
@@ -315,7 +316,7 @@ void HTMLLinkElement::DispatchPendingEvent(
if (link_->HasLoaded())
LinkLoaded();
else
- LinkLoadingErrored();
+ LinkLoadingErrored(/*should_send_error_to_javascript*/ true);
// Checks Document's load event synchronously here for performance.
// This is safe because dispatchPendingEvent() is called asynchronously.
diff --git a/third_party/blink/renderer/core/html/html_link_element.h b/third_party/blink/renderer/core/html/html_link_element.h
--- a/third_party/blink/renderer/core/html/html_link_element.h
+++ b/third_party/blink/renderer/core/html/html_link_element.h
@@ -151,7 +151,7 @@ class CORE_EXPORT HTMLLinkElement final : public HTMLElement,
// From LinkLoaderClient
void LinkLoaded() override;
- void LinkLoadingErrored() override;
+ void LinkLoadingErrored(bool should_send_error_to_javascript) override;
Member<LinkResource> link_;
Member<LinkLoader> link_loader_;
diff --git a/third_party/blink/renderer/core/loader/image_loader.cc b/third_party/blink/renderer/core/loader/image_loader.cc
--- a/third_party/blink/renderer/core/loader/image_loader.cc
+++ b/third_party/blink/renderer/core/loader/image_loader.cc
@@ -818,7 +818,10 @@ void ImageLoader::ImageNotifyFinished(ImageResourceContent* content) {
if (error && error->IsAccessCheck())
CrossSiteOrCSPViolationOccurred(AtomicString(error->FailingURL()));
- DispatchErrorEvent();
+ bool should_send_error_to_javascript =
+ content->GetResourceError()->ShouldSendErrorToJavascript();
+ if (should_send_error_to_javascript)
+ DispatchErrorEvent();
return;
}
diff --git a/third_party/blink/renderer/core/loader/link_loader.cc b/third_party/blink/renderer/core/loader/link_loader.cc
--- a/third_party/blink/renderer/core/loader/link_loader.cc
+++ b/third_party/blink/renderer/core/loader/link_loader.cc
@@ -89,7 +89,7 @@ void LinkLoader::NotifyFinished(Resource* resource) {
(resource->IsLinkPreload() &&
resource->IntegrityDisposition() ==
ResourceIntegrityDisposition::kFailed)) {
- client_->LinkLoadingErrored();
+ client_->LinkLoadingErrored(resource->ShouldSendErrorToJavascript());
} else {
client_->LinkLoaded();
}
@@ -101,7 +101,7 @@ void LinkLoader::NotifyModuleLoadFinished(ModuleScript* module) {
// and return." [spec text]
// Step 15. "Fire an event named load at the link element." [spec text]
if (!module)
- client_->LinkLoadingErrored();
+ client_->LinkLoadingErrored(/*should_send_error_to_javascript*/ true);
else
client_->LinkLoaded();
}
diff --git a/third_party/blink/renderer/core/loader/link_loader_client.h b/third_party/blink/renderer/core/loader/link_loader_client.h
--- a/third_party/blink/renderer/core/loader/link_loader_client.h
+++ b/third_party/blink/renderer/core/loader/link_loader_client.h
@@ -46,7 +46,7 @@ class CORE_EXPORT LinkLoaderClient : public GarbageCollectedMixin {
virtual bool ShouldLoadLink() = 0;
virtual void LinkLoaded() = 0;
- virtual void LinkLoadingErrored() = 0;
+ virtual void LinkLoadingErrored(bool should_send_error_to_javascript) = 0;
// There is no notification for cancellation.
virtual bool IsLinkCreatedByParser() = 0;
diff --git a/third_party/blink/renderer/core/script/classic_pending_script.cc b/third_party/blink/renderer/core/script/classic_pending_script.cc
--- a/third_party/blink/renderer/core/script/classic_pending_script.cc
+++ b/third_party/blink/renderer/core/script/classic_pending_script.cc
@@ -367,7 +367,7 @@ void ClassicPendingScript::NotifyFinished(Resource* resource) {
bool error_occurred =
resource->ErrorOccurred() || integrity_failure || mime_type_failure;
if (error_occurred) {
- AdvanceReadyState(kErrorOccurred);
+ AdvanceReadyState(kErrorOccurred); //
return;
}
diff --git a/third_party/blink/renderer/core/script/pending_script.cc b/third_party/blink/renderer/core/script/pending_script.cc
--- a/third_party/blink/renderer/core/script/pending_script.cc
+++ b/third_party/blink/renderer/core/script/pending_script.cc
@@ -212,7 +212,7 @@ void PendingScript::ExecuteScriptBlockInternal(
// <spec step="2">If the script's script is null, fire an event named error at
// the element, and return.</spec>
if (!script) {
- element->DispatchErrorEvent();
+ //element->DispatchErrorEvent();
return;
}
diff --git a/third_party/blink/renderer/core/svg/svg_image_loader.cc b/third_party/blink/renderer/core/svg/svg_image_loader.cc
--- a/third_party/blink/renderer/core/svg/svg_image_loader.cc
+++ b/third_party/blink/renderer/core/svg/svg_image_loader.cc
@@ -21,6 +21,7 @@
#include "third_party/blink/renderer/core/svg/svg_image_loader.h"
#include "third_party/blink/renderer/core/dom/events/event.h"
+#include "third_party/blink/renderer/platform/loader/fetch/resource_error.h"
#include "third_party/blink/renderer/core/svg/svg_image_element.h"
namespace blink {
@@ -29,7 +30,14 @@ SVGImageLoader::SVGImageLoader(SVGImageElement* node) : ImageLoader(node) {}
void SVGImageLoader::DispatchLoadEvent() {
if (GetContent()->ErrorOccurred()) {
- GetElement()->DispatchEvent(*Event::Create(event_type_names::kError));
+ bool should_send_error_to_javascript = true;
+ if (GetContent()->GetResourceError()) {
+ should_send_error_to_javascript =
+ GetContent()->GetResourceError()->ShouldSendErrorToJavascript();
+ }
+ if (should_send_error_to_javascript) {
+ GetElement()->DispatchEvent(*Event::Create(event_type_names::kError));
+ }
} else {
auto* image_element = To<SVGImageElement>(GetElement());
image_element->SendSVGLoadEventToSelfAndAncestorChainIfPossible();
diff --git a/third_party/blink/renderer/core/svg/svg_use_element.cc b/third_party/blink/renderer/core/svg/svg_use_element.cc
--- a/third_party/blink/renderer/core/svg/svg_use_element.cc
+++ b/third_party/blink/renderer/core/svg/svg_use_element.cc
@@ -624,6 +624,8 @@ void SVGUseElement::NotifyFinished(Resource* resource) {
const AtomicString& event_name =
is_error ? event_type_names::kError : event_type_names::kLoad;
DCHECK(!pending_event_.IsActive());
+
+ if (resource->ShouldSendErrorToJavascript())
pending_event_ = PostCancellableTask(
*GetDocument().GetTaskRunner(TaskType::kDOMManipulation), FROM_HERE,
WTF::BindOnce(&SVGUseElement::QueueOrDispatchPendingEvent,
diff --git a/third_party/blink/renderer/core/xmlhttprequest/xml_http_request.cc b/third_party/blink/renderer/core/xmlhttprequest/xml_http_request.cc
--- a/third_party/blink/renderer/core/xmlhttprequest/xml_http_request.cc
+++ b/third_party/blink/renderer/core/xmlhttprequest/xml_http_request.cc
@@ -1313,12 +1313,14 @@ void XMLHttpRequest::DispatchProgressEventFromSnapshot(
response_.ExpectedContentLength());
}
-void XMLHttpRequest::HandleNetworkError() {
+void XMLHttpRequest::HandleNetworkError(bool should_send_error_to_javascript) {
DVLOG(1) << this << " handleNetworkError()";
InternalAbort();
- HandleRequestError(DOMExceptionCode::kNetworkError, event_type_names::kError);
+ if (should_send_error_to_javascript) {
+ HandleRequestError(DOMExceptionCode::kNetworkError, event_type_names::kError);
+ }
}
void XMLHttpRequest::HandleDidCancel() {
@@ -1705,7 +1707,9 @@ void XMLHttpRequest::DidFail(uint64_t, const ResourceError& error) {
TrustTokenErrorToDOMException(error.TrustTokenOperationError());
}
- HandleNetworkError();
+ bool should_send_error_to_javascript =
+ error.ShouldSendErrorToJavascript();
+ HandleNetworkError(should_send_error_to_javascript);
}
void XMLHttpRequest::DidFailRedirectCheck(uint64_t) {
diff --git a/third_party/blink/renderer/core/xmlhttprequest/xml_http_request.h b/third_party/blink/renderer/core/xmlhttprequest/xml_http_request.h
--- a/third_party/blink/renderer/core/xmlhttprequest/xml_http_request.h
+++ b/third_party/blink/renderer/core/xmlhttprequest/xml_http_request.h
@@ -277,7 +277,7 @@ class CORE_EXPORT XMLHttpRequest final
void DispatchProgressEventFromSnapshot(const AtomicString&);
// Handles didFail() call not caused by cancellation or timeout.
- void HandleNetworkError();
+ void HandleNetworkError(bool should_send_error_to_javascript = true);
// Handles didFail() call for cancellations. For example, the
// ResourceLoader handling the load notifies m_loader of an error
// cancellation when the frame containing the XHR navigates away.
diff --git a/third_party/blink/renderer/platform/loader/fetch/resource.h b/third_party/blink/renderer/platform/loader/fetch/resource.h
--- a/third_party/blink/renderer/platform/loader/fetch/resource.h
+++ b/third_party/blink/renderer/platform/loader/fetch/resource.h
@@ -167,6 +167,12 @@ class PLATFORM_EXPORT Resource : public GarbageCollected<Resource>,
DCHECK(error_);
return *error_;
}
+ bool ShouldSendErrorToJavascript() {
+ if (error_)
+ return error_->ShouldSendErrorToJavascript();
+ else
+ return true;
+ }
uint64_t InspectorId() const { return LastResourceRequest().InspectorId(); }
diff --git a/third_party/blink/renderer/platform/loader/fetch/resource_error.cc b/third_party/blink/renderer/platform/loader/fetch/resource_error.cc
--- a/third_party/blink/renderer/platform/loader/fetch/resource_error.cc
+++ b/third_party/blink/renderer/platform/loader/fetch/resource_error.cc
@@ -214,6 +214,10 @@ bool ResourceError::WasBlockedByResponse() const {
return error_code_ == net::ERR_BLOCKED_BY_RESPONSE;
}
+bool ResourceError::ShouldSendErrorToJavascript() const {
+ return error_code_ != net::ERR_BLOCKED_BY_ADMINISTRATOR;
+}
+
namespace {
blink::ResourceRequestBlockedReason
BlockedByResponseReasonToResourceRequestBlockedReason(
diff --git a/third_party/blink/renderer/platform/loader/fetch/resource_error.h b/third_party/blink/renderer/platform/loader/fetch/resource_error.h
--- a/third_party/blink/renderer/platform/loader/fetch/resource_error.h
+++ b/third_party/blink/renderer/platform/loader/fetch/resource_error.h
@@ -104,6 +104,7 @@ class PLATFORM_EXPORT ResourceError final {
bool IsCancelledFromHttpError() const {
return is_cancelled_from_http_error_;
}
+ bool ShouldSendErrorToJavascript() const;
absl::optional<ResourceRequestBlockedReason> GetResourceRequestBlockedReason()
const;
diff --git a/third_party/blink/renderer/platform/loader/fetch/resource_fetcher.cc b/third_party/blink/renderer/platform/loader/fetch/resource_fetcher.cc
--- a/third_party/blink/renderer/platform/loader/fetch/resource_fetcher.cc
+++ b/third_party/blink/renderer/platform/loader/fetch/resource_fetcher.cc
@@ -2324,7 +2324,7 @@ void ResourceFetcher::HandleLoaderError(Resource* resource,
use_counter_->CountUse(
mojom::WebFeature::kCertificateTransparencyRequiredErrorOnResourceLoad);
}
- resource->FinishAsError(error, freezable_task_runner_.get());
+ resource->FinishAsError(error, freezable_task_runner_.get()); // starting point: this is the source of truth
if (resource_load_observer_) {
DCHECK(!IsDetached());
resource_load_observer_->DidFailLoading(
--
2.25.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment