1/*
2 * Copyright (C) 2016 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "StylePendingResources.h"
28
29#include "CSSCursorImageValue.h"
30#include "CachedResourceLoader.h"
31#include "ContentData.h"
32#include "CursorData.h"
33#include "CursorList.h"
34#include "Document.h"
35#include "RenderStyle.h"
36#include "SVGURIReference.h"
37#include "Settings.h"
38#include "StyleCachedImage.h"
39#include "StyleGeneratedImage.h"
40#include "TransformFunctions.h"
41
42namespace WebCore {
43namespace Style {
44
45// <https://html.spec.whatwg.org/multipage/urls-and-fetching.html#cors-settings-attributes>
46enum class LoadPolicy { NoCORS, Anonymous };
47static void loadPendingImage(Document& document, const StyleImage* styleImage, const Element* element, LoadPolicy loadPolicy = LoadPolicy::NoCORS)
48{
49 if (!styleImage || !styleImage->isPending())
50 return;
51
52 bool isInUserAgentShadowTree = element && element->isInUserAgentShadowTree();
53 ResourceLoaderOptions options = CachedResourceLoader::defaultCachedResourceOptions();
54 options.contentSecurityPolicyImposition = isInUserAgentShadowTree ? ContentSecurityPolicyImposition::SkipPolicyCheck : ContentSecurityPolicyImposition::DoPolicyCheck;
55
56 if (loadPolicy == LoadPolicy::Anonymous && !isInUserAgentShadowTree && document.settings().useAnonymousModeWhenFetchingMaskImages()) {
57 options.mode = FetchOptions::Mode::Cors;
58 options.credentials = FetchOptions::Credentials::SameOrigin;
59 options.storedCredentialsPolicy = StoredCredentialsPolicy::DoNotUse;
60 options.sameOriginDataURLFlag = SameOriginDataURLFlag::Set;
61 }
62
63 const_cast<StyleImage&>(*styleImage).load(document.cachedResourceLoader(), options);
64}
65
66void loadPendingResources(RenderStyle& style, Document& document, const Element* element)
67{
68 for (auto* backgroundLayer = &style.backgroundLayers(); backgroundLayer; backgroundLayer = backgroundLayer->next())
69 loadPendingImage(document, backgroundLayer->image(), element);
70
71 for (auto* contentData = style.contentData(); contentData; contentData = contentData->next()) {
72 if (is<ImageContentData>(*contentData)) {
73 auto& styleImage = downcast<ImageContentData>(*contentData).image();
74 loadPendingImage(document, &styleImage, element);
75 }
76 }
77
78 if (auto* cursorList = style.cursors()) {
79 for (size_t i = 0; i < cursorList->size(); ++i)
80 loadPendingImage(document, cursorList->at(i).image(), element);
81 }
82
83 loadPendingImage(document, style.listStyleImage(), element);
84 loadPendingImage(document, style.borderImageSource(), element);
85 loadPendingImage(document, style.maskBoxImageSource(), element);
86
87 if (auto* reflection = style.boxReflect())
88 loadPendingImage(document, reflection->mask().image(), element);
89
90 // Masking operations may be sensitive to timing attacks that can be used to reveal the pixel data of
91 // the image used as the mask. As a means to mitigate such attacks CSS mask images and shape-outside
92 // images are retreived in "Anonymous" mode, which uses a potentially CORS-enabled fetch.
93 for (auto* maskLayer = &style.maskLayers(); maskLayer; maskLayer = maskLayer->next())
94 loadPendingImage(document, maskLayer->image(), element, LoadPolicy::Anonymous);
95
96 if (style.shapeOutside())
97 loadPendingImage(document, style.shapeOutside()->image(), element, LoadPolicy::Anonymous);
98}
99
100}
101}
102