Commit 7554b55c authored by arthursonzogni's avatar arthursonzogni Committed by Commit Bot

Refactor InitializeContentSecurityPolicy().

(This is a pure refactor. Changes in behavior aren't expected)

- Use early returns, it avoid using too many nested if/then/else. There
  was up to 4 level of indentation in this function.

- Remove unnecessary DCHECK. Checking a pointer to be non-null before
  dereferencing it is useless, because it would have immediately crash
  anyway.

Bug: 1001982
Change-Id: Id54de8441bb0b19b91a70d22435dff5fcd6c5260
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1806674Reviewed-by: default avatarDaniel Cheng <dcheng@chromium.org>
Commit-Queue: Arthur Sonzogni <arthursonzogni@chromium.org>
Cr-Commit-Position: refs/heads/master@{#697538}
parent 426279be
...@@ -697,44 +697,43 @@ class Document::SecurityContextInit : public FeaturePolicyParserDelegate { ...@@ -697,44 +697,43 @@ class Document::SecurityContextInit : public FeaturePolicyParserDelegate {
frame ? frame->Loader().GetLastOriginDocumentCSP() : nullptr; frame ? frame->Loader().GetLastOriginDocumentCSP() : nullptr;
KURL url; KURL url;
if (initializer.ShouldSetURL()) { if (initializer.ShouldSetURL())
url = initializer.Url(); url = initializer.Url().IsEmpty() ? BlankURL() : initializer.Url();
if (url.IsEmpty())
url = BlankURL(); // Alias certain security properties from |owner_document|. Used for the
} // case of about:blank pages inheriting the security properties of their
// requestor context.
if (initializer.HasSecurityContext() && !initializer.OriginToCommit() && //
initializer.OwnerDocument()) { // Note that this is currently somewhat broken; Blink always inherits from
// Alias certain security properties from |owner_document|. Used for // the parent or opener, even though it should actually be inherited from
// the case of about:blank pages inheriting the security properties of // the request initiator.
// their requestor context. if (url.IsEmpty() && initializer.HasSecurityContext() &&
// Note that this is currently somewhat broken; Blink always inherits !initializer.OriginToCommit() && initializer.OwnerDocument()) {
// from the parent or opener, even though it should actually be
// inherited from the request initiator.
if (url.IsEmpty()) {
last_origin_document_csp = last_origin_document_csp =
initializer.OwnerDocument()->GetContentSecurityPolicy(); initializer.OwnerDocument()->GetContentSecurityPolicy();
} }
}
csp_ = initializer.GetContentSecurityPolicy(); csp_ = initializer.GetContentSecurityPolicy();
if (!csp_ && initializer.ImportsController()) { if (!csp_) {
if (initializer.ImportsController()) {
// If this document is an HTML import, grab a reference to its master // If this document is an HTML import, grab a reference to its master
// document's Content Security Policy. We don't bind the CSP's delegate // document's Content Security Policy. We don't bind the CSP's delegate
// in 'InitSecurityPolicy' in this case, as we can't rebind the // in 'InitSecurityPolicy' in this case, as we can't rebind the master
// master document's policy object: The Content Security Policy's delegate // document's policy object: The Content Security Policy's delegate
// needs to remain set to the master document. // needs to remain set to the master document.
csp_ = csp_ = initializer.ImportsController()
initializer.ImportsController()->Master()->GetContentSecurityPolicy(); ->Master()
} else { ->GetContentSecurityPolicy();
if (!csp_) { return;
}
csp_ = MakeGarbageCollected<ContentSecurityPolicy>(); csp_ = MakeGarbageCollected<ContentSecurityPolicy>();
bind_csp_immediately_ = true; bind_csp_immediately_ = true;
} }
// We should inherit the navigation initiator CSP if the document is // We should inherit the navigation initiator CSP if the document is loaded
// loaded using a local-scheme url. // using a local-scheme url.
if (last_origin_document_csp && if (last_origin_document_csp &&
(url.IsEmpty() || url.ProtocolIsAbout() || url.ProtocolIsData() || (url.IsEmpty() || url.ProtocolIsAbout() || url.ProtocolIsData() ||
url.ProtocolIs("blob") || url.ProtocolIs("filesystem"))) { url.ProtocolIs("blob") || url.ProtocolIs("filesystem"))) {
...@@ -742,26 +741,25 @@ class Document::SecurityContextInit : public FeaturePolicyParserDelegate { ...@@ -742,26 +741,25 @@ class Document::SecurityContextInit : public FeaturePolicyParserDelegate {
} }
if (document_classes & kPluginDocumentClass) { if (document_classes & kPluginDocumentClass) {
// TODO(andypaicu): This should inherit the origin document's plugin
// types but because this could be a OOPIF document it might not have
// access. In this situation we fallback on using the parent/opener.
if (last_origin_document_csp) { if (last_origin_document_csp) {
csp_->CopyPluginTypesFrom(last_origin_document_csp); csp_->CopyPluginTypesFrom(last_origin_document_csp);
} else if (frame) { return;
}
// TODO(andypaicu): This should inherit the origin document's plugin types
// but because this could be a OOPIF document it might not have access. In
// this situation we fallback on using the parent/opener:
if (frame) {
Frame* inherit_from = frame->Tree().Parent() Frame* inherit_from = frame->Tree().Parent()
? frame->Tree().Parent() ? frame->Tree().Parent()
: frame->Client()->Opener(); : frame->Client()->Opener();
if (inherit_from && frame != inherit_from) { if (inherit_from && frame != inherit_from) {
DCHECK(
inherit_from->GetSecurityContext() &&
inherit_from->GetSecurityContext()->GetContentSecurityPolicy());
csp_->CopyPluginTypesFrom( csp_->CopyPluginTypesFrom(
inherit_from->GetSecurityContext()->GetContentSecurityPolicy()); inherit_from->GetSecurityContext()->GetContentSecurityPolicy());
} }
} }
} }
} }
}
void InitializeSandboxFlags(const DocumentInit& initializer) { void InitializeSandboxFlags(const DocumentInit& initializer) {
sandbox_flags_ = initializer.GetSandboxFlags() | csp_->GetSandboxMask(); sandbox_flags_ = initializer.GetSandboxFlags() | csp_->GetSandboxMask();
......
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