Bug 1313627 - Get the firstPartyDomain from the nodePrincipal of the document in nsDocShell::CanAccessItem() if the first party isolation is on. r=smaug

This commit is contained in:
Tim Huang 2016-11-10 14:20:38 +08:00
Родитель 3ffb31894f
Коммит 950b86072e
3 изменённых файлов: 52 добавлений и 12 удалений

Просмотреть файл

@ -324,6 +324,7 @@ OriginAttributes::SetFromGenericAttributes(const GenericOriginAttributes& aAttrs
mFirstPartyDomain = aAttrs.mFirstPartyDomain;
}
/* static */
bool
OriginAttributes::IsFirstPartyEnabled()
{

Просмотреть файл

@ -61,13 +61,13 @@ public:
void SetFromGenericAttributes(const GenericOriginAttributes& aAttrs);
// check if "privacy.firstparty.isolate" is enabled.
static bool IsFirstPartyEnabled();
protected:
OriginAttributes() {}
explicit OriginAttributes(const OriginAttributesDictionary& aOther)
: OriginAttributesDictionary(aOther) {}
// check if "privacy.firstparty.isolate" is enabled.
bool IsFirstPartyEnabled();
};
class PrincipalOriginAttributes;

Просмотреть файл

@ -3573,8 +3573,54 @@ nsDocShell::CanAccessItem(nsIDocShellTreeItem* aTargetItem,
return false;
}
if (static_cast<nsDocShell*>(targetDS.get())->GetOriginAttributes() !=
static_cast<nsDocShell*>(accessingDS.get())->GetOriginAttributes()) {
nsCOMPtr<nsIDocShellTreeItem> accessingRoot;
aAccessingItem->GetSameTypeRootTreeItem(getter_AddRefs(accessingRoot));
nsCOMPtr<nsIDocShell> accessingRootDS = do_QueryInterface(accessingRoot);
nsCOMPtr<nsIDocShellTreeItem> targetRoot;
aTargetItem->GetSameTypeRootTreeItem(getter_AddRefs(targetRoot));
nsCOMPtr<nsIDocShell> targetRootDS = do_QueryInterface(targetRoot);
DocShellOriginAttributes targetOA =
static_cast<nsDocShell*>(targetDS.get())->GetOriginAttributes();
DocShellOriginAttributes accessingOA =
static_cast<nsDocShell*>(accessingDS.get())->GetOriginAttributes();
// When the first party isolation is on, the top-level docShell may not have
// the firstPartyDomain in its originAttributes, but its document will have
// it. So we get the firstPartyDomain from the nodePrincipal of the document
// before we compare the originAttributes.
if (OriginAttributes::IsFirstPartyEnabled()) {
if (accessingDS == accessingRootDS &&
aAccessingItem->ItemType() == nsIDocShellTreeItem::typeContent &&
!accessingDS->GetIsMozBrowserOrApp()) {
nsCOMPtr<nsIDocument> accessingDoc = aAccessingItem->GetDocument();
if (accessingDoc) {
nsCOMPtr<nsIPrincipal> accessingPrincipal = accessingDoc->NodePrincipal();
accessingOA.mFirstPartyDomain =
BasePrincipal::Cast(accessingPrincipal)->OriginAttributesRef().mFirstPartyDomain;
}
}
if (targetDS == targetRootDS &&
aTargetItem->ItemType() == nsIDocShellTreeItem::typeContent &&
!targetDS->GetIsMozBrowserOrApp()) {
nsCOMPtr<nsIDocument> targetDoc = aAccessingItem->GetDocument();
if (targetDoc) {
nsCOMPtr<nsIPrincipal> targetPrincipal = targetDoc->NodePrincipal();
targetOA.mFirstPartyDomain =
BasePrincipal::Cast(targetPrincipal)->OriginAttributesRef().mFirstPartyDomain;
}
}
}
if (targetOA != accessingOA) {
return false;
}
@ -3584,10 +3630,6 @@ nsDocShell::CanAccessItem(nsIDocShellTreeItem* aTargetItem,
return false;
}
nsCOMPtr<nsIDocShellTreeItem> accessingRoot;
aAccessingItem->GetSameTypeRootTreeItem(getter_AddRefs(accessingRoot));
if (aTargetItem == accessingRoot) {
// A frame can navigate its root.
return true;
@ -3605,9 +3647,6 @@ nsDocShell::CanAccessItem(nsIDocShellTreeItem* aTargetItem,
parent.swap(target);
} while (target);
nsCOMPtr<nsIDocShellTreeItem> targetRoot;
aTargetItem->GetSameTypeRootTreeItem(getter_AddRefs(targetRoot));
if (aTargetItem != targetRoot) {
// target is a subframe, not in accessor's frame hierarchy, and all its
// ancestors have origins different from that of the accessor. Don't