Commit 6580741b authored by Ian Clelland's avatar Ian Clelland Committed by Commit Bot

Add ConsoleMessage to IsFeatureEnabled and ReportFeaturePolicyViolation

Report all feature policy violations to the inspector console. This
log message is generated at the same time that a report would be sent
to the ReportingAPI.

By default, a generic message is logged, but the methods will accept a
ConsoleMessage object which will be used in its place if the FP check
determines that use of the feature is not allowed.

Bug: 661280
Change-Id: Icf2e89e3e6e582480b809b64edfa5832a836ebac
Reviewed-on: https://chromium-review.googlesource.com/c/1257545
Commit-Queue: Ian Clelland <iclelland@chromium.org>
Reviewed-by: default avatarDmitry Gozman <dgozman@chromium.org>
Cr-Commit-Position: refs/heads/master@{#600004}
parent fc59078d
CONSOLE ERROR: line 28: Feature policy violation: fullscreen is not allowed in this document.
--------
......
......@@ -7682,11 +7682,12 @@ LazyLoadImageObserver& Document::EnsureLazyLoadImageObserver() {
return *lazy_load_image_observer_;
}
void Document::ReportFeaturePolicyViolation(
mojom::FeaturePolicyFeature feature) const {
void Document::ReportFeaturePolicyViolation(mojom::FeaturePolicyFeature feature,
const String& message) const {
if (!RuntimeEnabledFeatures::FeaturePolicyReportingEnabled())
return;
if (!GetFrame())
LocalFrame* frame = GetFrame();
if (!frame)
return;
const String& feature_name = GetNameForFeature(feature);
FeaturePolicyViolationReportBody* body = new FeaturePolicyViolationReportBody(
......@@ -7701,9 +7702,14 @@ void Document::ReportFeaturePolicyViolation(
column_number = is_null ? 0 : column_number;
// Send the feature policy violation report to the Reporting API.
GetFrame()->GetReportingService()->QueueFeaturePolicyViolationReport(
frame->GetReportingService()->QueueFeaturePolicyViolationReport(
Url(), feature_name, "Feature policy violation", body->sourceFile(),
line_number, column_number);
frame->Console().AddMessage(ConsoleMessage::Create(
kViolationMessageSource, kErrorMessageLevel,
(message.IsEmpty() ? ("Feature policy violation: " + feature_name +
" is not allowed in this document.")
: message)));
}
void Document::SendViolationReport(
......
......@@ -61,6 +61,7 @@
#include "third_party/blink/renderer/core/frame/hosts_using_features.h"
#include "third_party/blink/renderer/core/html/custom/v0_custom_element.h"
#include "third_party/blink/renderer/core/html/parser/parser_synchronization_policy.h"
#include "third_party/blink/renderer/core/inspector/console_message.h"
#include "third_party/blink/renderer/platform/bindings/exception_state.h"
#include "third_party/blink/renderer/platform/bindings/trace_wrapper_member.h"
#include "third_party/blink/renderer/platform/scroll/scroll_types.h"
......@@ -1502,7 +1503,9 @@ class CORE_EXPORT Document : public ContainerNode,
return agent_cluster_id_;
}
void ReportFeaturePolicyViolation(mojom::FeaturePolicyFeature) const override;
void ReportFeaturePolicyViolation(
mojom::FeaturePolicyFeature,
const String& message = g_empty_string) const override;
protected:
Document(const DocumentInit&, DocumentClassFlags = kDefaultDocumentClass);
......
......@@ -121,7 +121,8 @@ void SecurityContext::InitializeFeaturePolicy(
}
bool SecurityContext::IsFeatureEnabled(mojom::FeaturePolicyFeature feature,
ReportOptions report_on_failure) const {
ReportOptions report_on_failure,
const String& message) const {
// The policy should always be initialized before checking it to ensure we
// properly inherit the parent policy.
DCHECK(feature_policy_);
......@@ -129,7 +130,7 @@ bool SecurityContext::IsFeatureEnabled(mojom::FeaturePolicyFeature feature,
if (feature_policy_->IsFeatureEnabled(feature))
return true;
if (report_on_failure == ReportOptions::kReportOnFailure)
ReportFeaturePolicyViolation(feature);
ReportFeaturePolicyViolation(feature, message);
return false;
}
......
......@@ -32,6 +32,7 @@
#include "third_party/blink/public/platform/web_insecure_request_policy.h"
#include "third_party/blink/renderer/core/core_export.h"
#include "third_party/blink/renderer/core/frame/sandbox_flags.h"
#include "third_party/blink/renderer/core/inspector/console_message.h"
#include "third_party/blink/renderer/platform/heap/handle.h"
#include "third_party/blink/renderer/platform/wtf/forward.h"
#include "third_party/blink/renderer/platform/wtf/hash_set.h"
......@@ -131,12 +132,15 @@ class CORE_EXPORT SecurityContext : public GarbageCollectedMixin {
// Tests whether the policy-controlled feature is enabled in this frame.
// Optionally sends a report to any registered reporting observers or
// Report-To endpoints, via ReportFeaturePolicyViolation(), if the feature is
// disabled.
// disabled. The optional ConsoleMessage will be sent to the console if
// present, or else a default message will be used instead.
bool IsFeatureEnabled(
mojom::FeaturePolicyFeature,
ReportOptions report_on_failure = ReportOptions::kDoNotReport) const;
virtual void ReportFeaturePolicyViolation(mojom::FeaturePolicyFeature) const {
}
ReportOptions report_on_failure = ReportOptions::kDoNotReport,
const String& message = g_empty_string) const;
virtual void ReportFeaturePolicyViolation(
mojom::FeaturePolicyFeature,
const String& message = g_empty_string) const {}
// Apply the sandbox flag. In addition, if the origin is not already opaque,
// the origin is updated to a newly created unique opaque origin, setting the
......
......@@ -744,10 +744,10 @@ bool XMLHttpRequest::InitSend(ExceptionState& exception_state) {
if (!async_) {
if (GetExecutionContext()->IsDocument() &&
!GetDocument()->IsFeatureEnabled(mojom::FeaturePolicyFeature::kSyncXHR,
ReportOptions::kReportOnFailure)) {
LogConsoleError(GetExecutionContext(),
"Synchronous requests are disabled by Feature Policy.");
!GetDocument()->IsFeatureEnabled(
mojom::FeaturePolicyFeature::kSyncXHR,
ReportOptions::kReportOnFailure,
"Synchronous requests are disabled by Feature Policy.")) {
HandleNetworkError();
ThrowForLoadFailureIfNeeded(exception_state, String());
return false;
......
......@@ -227,11 +227,9 @@ void Geolocation::StartRequest(GeoNotifier* notifier) {
if (!GetDocument()->IsFeatureEnabled(
mojom::FeaturePolicyFeature::kGeolocation,
ReportOptions::kReportOnFailure)) {
ReportOptions::kReportOnFailure, kFeaturePolicyConsoleWarning)) {
UseCounter::Count(GetDocument(),
WebFeature::kGeolocationDisabledByFeaturePolicy);
GetDocument()->AddConsoleMessage(ConsoleMessage::Create(
kJSMessageSource, kErrorMessageLevel, kFeaturePolicyConsoleWarning));
notifier->SetFatalError(PositionError::Create(
PositionError::kPermissionDenied, kFeaturePolicyErrorMessage));
return;
......
......@@ -103,10 +103,9 @@ ScriptPromise NavigatorWebMIDI::requestMIDIAccess(ScriptState* script_state,
document, WebFeature::kRequestMIDIAccessIframe_ObscuredByFootprinting);
if (!document.IsFeatureEnabled(mojom::FeaturePolicyFeature::kMidiFeature,
ReportOptions::kReportOnFailure)) {
ReportOptions::kReportOnFailure,
kFeaturePolicyConsoleWarning)) {
UseCounter::Count(document, WebFeature::kMidiDisabledByFeaturePolicy);
document.AddConsoleMessage(ConsoleMessage::Create(
kJSMessageSource, kWarningMessageLevel, kFeaturePolicyConsoleWarning));
return ScriptPromise::RejectWithDOMException(
script_state, DOMException::Create(DOMExceptionCode::kSecurityError,
kFeaturePolicyErrorMessage));
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment