1 | /* |
2 | * Copyright (C) 2009 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. ``AS IS'' AND ANY |
14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
24 | */ |
25 | |
26 | #include "config.h" |
27 | #include "CanvasRenderingContext.h" |
28 | |
29 | #include "CachedImage.h" |
30 | #include "CanvasPattern.h" |
31 | #include "HTMLCanvasElement.h" |
32 | #include "HTMLImageElement.h" |
33 | #include "HTMLVideoElement.h" |
34 | #include "Image.h" |
35 | #include "ImageBitmap.h" |
36 | #include "OffscreenCanvas.h" |
37 | #include "SecurityOrigin.h" |
38 | #include <wtf/HashSet.h> |
39 | #include <wtf/IsoMallocInlines.h> |
40 | #include <wtf/Lock.h> |
41 | #include <wtf/NeverDestroyed.h> |
42 | #include <wtf/URL.h> |
43 | |
44 | namespace WebCore { |
45 | |
46 | WTF_MAKE_ISO_ALLOCATED_IMPL(CanvasRenderingContext); |
47 | |
48 | HashSet<CanvasRenderingContext*>& CanvasRenderingContext::instances(const LockHolder&) |
49 | { |
50 | static NeverDestroyed<HashSet<CanvasRenderingContext*>> instances; |
51 | return instances; |
52 | } |
53 | |
54 | Lock& CanvasRenderingContext::instancesMutex() |
55 | { |
56 | static LazyNeverDestroyed<Lock> mutex; |
57 | static std::once_flag initializeMutex; |
58 | std::call_once(initializeMutex, [] { |
59 | mutex.construct(); |
60 | }); |
61 | return mutex.get(); |
62 | } |
63 | |
64 | CanvasRenderingContext::CanvasRenderingContext(CanvasBase& canvas) |
65 | : m_canvas(canvas) |
66 | { |
67 | LockHolder lock(instancesMutex()); |
68 | instances(lock).add(this); |
69 | } |
70 | |
71 | CanvasRenderingContext::~CanvasRenderingContext() |
72 | { |
73 | LockHolder lock(instancesMutex()); |
74 | ASSERT(instances(lock).contains(this)); |
75 | instances(lock).remove(this); |
76 | } |
77 | |
78 | void CanvasRenderingContext::ref() |
79 | { |
80 | m_canvas.refCanvasBase(); |
81 | } |
82 | |
83 | void CanvasRenderingContext::deref() |
84 | { |
85 | m_canvas.derefCanvasBase(); |
86 | } |
87 | |
88 | bool CanvasRenderingContext::wouldTaintOrigin(const CanvasPattern* pattern) |
89 | { |
90 | if (m_canvas.originClean() && pattern && !pattern->originClean()) |
91 | return true; |
92 | return false; |
93 | } |
94 | |
95 | bool CanvasRenderingContext::wouldTaintOrigin(const CanvasBase* sourceCanvas) |
96 | { |
97 | if (m_canvas.originClean() && sourceCanvas && !sourceCanvas->originClean()) |
98 | return true; |
99 | return false; |
100 | } |
101 | |
102 | bool CanvasRenderingContext::wouldTaintOrigin(const HTMLImageElement* element) |
103 | { |
104 | if (!element || !m_canvas.originClean()) |
105 | return false; |
106 | |
107 | auto* cachedImage = element->cachedImage(); |
108 | if (!cachedImage) |
109 | return false; |
110 | |
111 | auto image = makeRefPtr(cachedImage->image()); |
112 | if (!image) |
113 | return false; |
114 | |
115 | if (image->sourceURL().protocolIsData()) |
116 | return false; |
117 | |
118 | if (!image->hasSingleSecurityOrigin()) |
119 | return true; |
120 | |
121 | if (!cachedImage->isCORSSameOrigin()) |
122 | return true; |
123 | |
124 | ASSERT(m_canvas.securityOrigin()); |
125 | ASSERT(cachedImage->origin()); |
126 | ASSERT(m_canvas.securityOrigin()->toString() == cachedImage->origin()->toString()); |
127 | return false; |
128 | } |
129 | |
130 | bool CanvasRenderingContext::wouldTaintOrigin(const HTMLVideoElement* video) |
131 | { |
132 | #if ENABLE(VIDEO) |
133 | // FIXME: This check is likely wrong when a redirect is involved. We need |
134 | // to test the finalURL. Please be careful when fixing this issue not to |
135 | // make currentSrc be the final URL because then the |
136 | // HTMLMediaElement.currentSrc DOM API would leak redirect destinations! |
137 | if (!video || !m_canvas.originClean()) |
138 | return false; |
139 | |
140 | if (!video->hasSingleSecurityOrigin()) |
141 | return true; |
142 | |
143 | if (!(video->player() && video->player()->didPassCORSAccessCheck()) && video->wouldTaintOrigin(*m_canvas.securityOrigin())) |
144 | return true; |
145 | |
146 | #else |
147 | UNUSED_PARAM(video); |
148 | #endif |
149 | |
150 | return false; |
151 | } |
152 | |
153 | bool CanvasRenderingContext::wouldTaintOrigin(const ImageBitmap* imageBitmap) |
154 | { |
155 | if (!imageBitmap || !m_canvas.originClean()) |
156 | return false; |
157 | |
158 | return !imageBitmap->originClean(); |
159 | } |
160 | |
161 | bool CanvasRenderingContext::wouldTaintOrigin(const URL& url) |
162 | { |
163 | if (!m_canvas.originClean()) |
164 | return false; |
165 | |
166 | if (url.protocolIsData()) |
167 | return false; |
168 | |
169 | return !m_canvas.securityOrigin()->canRequest(url); |
170 | } |
171 | |
172 | void CanvasRenderingContext::checkOrigin(const URL& url) |
173 | { |
174 | if (wouldTaintOrigin(url)) |
175 | m_canvas.setOriginTainted(); |
176 | } |
177 | |
178 | void CanvasRenderingContext::checkOrigin(const TypedOMCSSImageValue&) |
179 | { |
180 | m_canvas.setOriginTainted(); |
181 | } |
182 | |
183 | } // namespace WebCore |
184 | |