-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathImageViewManagerModule.cpp
More file actions
231 lines (208 loc) · 7.77 KB
/
ImageViewManagerModule.cpp
File metadata and controls
231 lines (208 loc) · 7.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
// NYI:
// implement image cache
// implement prefetch functionality
// implement multi source (parse out most suitable image source from array of
// sources)
#include "pch.h"
#include "ImageViewManagerModule.h"
#include <UI.Xaml.Media.Imaging.h>
#include <Views/Image/ReactImage.h>
#include <cxxreact/JsArgumentHelpers.h>
#ifdef USE_FABRIC
#include <Fabric/WindowsImageManager.h>
#include <Utils/Helpers.h>
#include <wincodec.h>
#include "XamlUtils.h"
#endif // USE_FABRIC
#include <winrt/Windows.Storage.Streams.h>
#include "../../Shared/InputValidation.h"
#include "Unicode.h"
namespace winrt {
using namespace Windows::Foundation;
using namespace Windows::Storage::Streams;
using namespace xaml::Media::Imaging;
} // namespace winrt
namespace Microsoft::ReactNative {
winrt::fire_and_forget GetImageSizeAsync(
const winrt::Microsoft::ReactNative::IReactPropertyBag &properties,
std::string uriString,
winrt::Microsoft::ReactNative::JSValue &&headers,
Mso::Functor<void(int32_t width, int32_t height)> successCallback,
Mso::Functor<void()> errorCallback
#ifdef USE_FABRIC
,
bool useFabric
#endif // USE_FABRIC
) {
bool succeeded{false};
try {
ReactImageSource source;
source.uri = uriString;
if (!headers.IsNull()) {
for (auto &header : headers.AsObject()) {
source.headers.push_back(std::make_pair(header.first, header.second.AsString()));
}
}
winrt::Uri uri{Microsoft::Common::Unicode::Utf8ToUtf16(uriString)};
winrt::hstring scheme{uri.SchemeName()};
bool needsDownload = (scheme == L"http") || (scheme == L"https");
bool inlineData = scheme == L"data";
winrt::IRandomAccessStream memoryStream;
if (needsDownload) {
memoryStream = co_await GetImageStreamAsync(properties, source);
} else if (inlineData) {
memoryStream = co_await GetImageInlineDataAsync(source);
}
#ifdef USE_FABRIC
if (!useFabric) {
#endif // USE_FABRIC
winrt::BitmapImage bitmap;
if (memoryStream) {
co_await bitmap.SetSourceAsync(memoryStream);
}
if (bitmap) {
successCallback(bitmap.PixelWidth(), bitmap.PixelHeight());
succeeded = true;
}
#ifdef USE_FABRIC
} else {
auto result = wicBitmapSourceFromStream(memoryStream);
if (!std::get<std::shared_ptr<facebook::react::ImageErrorInfo>>(result)) {
auto imagingFactory = std::get<winrt::com_ptr<IWICImagingFactory>>(result);
auto wicBmpSource = std::get<winrt::com_ptr<IWICBitmapSource>>(result);
UINT width, height;
if (SUCCEEDED(wicBmpSource->GetSize(&width, &height))) {
successCallback(width, height);
succeeded = true;
}
}
}
#endif // USE_FABRIC
} catch (winrt::hresult_error const &) {
}
if (!succeeded)
errorCallback();
co_return;
}
void ImageLoader::Initialize(React::ReactContext const &reactContext) noexcept {
m_context = reactContext;
}
void ImageLoader::getSize(std::string uri, React::ReactPromise<std::vector<double>> &&result) noexcept {
// VALIDATE URI - file:// abuse PROTECTION (P0 Critical - CVSS 7.8)
try {
if (uri.find("data:") == 0) {
// Validate data URI size to prevent DoS through memory exhaustion
::Microsoft::ReactNative::InputValidation::SizeValidator::ValidateSize(
uri.length(), ::Microsoft::ReactNative::InputValidation::SizeValidator::MAX_DATA_URI_SIZE, "Data URI");
} else {
// Allow http/https only for non-data URIs
::Microsoft::ReactNative::InputValidation::URLValidator::ValidateURL(uri, {"http", "https"});
}
} catch (const ::Microsoft::ReactNative::InputValidation::ValidationException &ex) {
result.Reject(ex.what());
return;
}
m_context.UIDispatcher().Post(
[context = m_context, uri = std::move(uri), result = std::move(result)]() mutable noexcept {
GetImageSizeAsync(
context.Properties().Handle(),
std::move(uri),
{},
[result](double width, double height) noexcept {
result.Resolve(std::vector<double>{width, height});
},
[result]() noexcept { result.Reject("Failed"); }
#ifdef USE_FABRIC
,
IsFabricEnabled(context.Properties().Handle())
#endif // USE_FABRIC
);
});
}
void ImageLoader::getSizeWithHeaders(
std::string uri,
React::JSValue &&headers,
React::ReactPromise<Microsoft::ReactNativeSpecs::ImageLoaderIOSSpec_getSizeWithHeaders_returnType>
&&result) noexcept {
// SDL Compliance: Validate URI for SSRF (P0 Critical - CVSS 7.8)
try {
if (uri.find("data:") == 0) {
// Validate data URI size to prevent DoS through memory exhaustion
::Microsoft::ReactNative::InputValidation::SizeValidator::ValidateSize(
uri.length(), ::Microsoft::ReactNative::InputValidation::SizeValidator::MAX_DATA_URI_SIZE, "Data URI");
} else {
// Allow http/https only for non-data URIs
::Microsoft::ReactNative::InputValidation::URLValidator::ValidateURL(uri, {"http", "https"});
}
} catch (const ::Microsoft::ReactNative::InputValidation::ValidationException &ex) {
result.Reject(ex.what());
return;
}
m_context.UIDispatcher().Post([context = m_context,
uri = std::move(uri),
headers = std::move(headers),
result = std::move(result)]() mutable noexcept {
GetImageSizeAsync(
context.Properties().Handle(),
std::move(uri),
std::move(headers),
[result](double width, double height) noexcept {
result.Resolve(Microsoft::ReactNativeSpecs::ImageLoaderIOSSpec_getSizeWithHeaders_returnType{width, height});
},
[result]() noexcept { result.Reject("Failed"); }
#ifdef USE_FABRIC
,
IsFabricEnabled(context.Properties().Handle())
#endif // USE_FABRIC
);
});
}
void ImageLoader::prefetchImage(std::string uri, React::ReactPromise<bool> &&result) noexcept {
// VALIDATE URI - file:// abuse PROTECTION (P0 Critical - CVSS 7.8)
try {
if (uri.find("data:") == 0) {
// Validate data URI size to prevent DoS through memory exhaustion
::Microsoft::ReactNative::InputValidation::SizeValidator::ValidateSize(
uri.length(), ::Microsoft::ReactNative::InputValidation::SizeValidator::MAX_DATA_URI_SIZE, "Data URI");
} else {
// Allow http/https only for non-data URIs
::Microsoft::ReactNative::InputValidation::URLValidator::ValidateURL(uri, {"http", "https"});
}
} catch (const ::Microsoft::ReactNative::InputValidation::ValidationException &ex) {
result.Reject(ex.what());
return;
}
// NYI
result.Resolve(true);
}
void ImageLoader::prefetchImageWithMetadata(
std::string uri,
std::string queryRootName,
double rootTag,
React::ReactPromise<bool> &&result) noexcept {
// SDL Compliance: Validate URI for SSRF (P0 Critical - CVSS 7.8)
try {
if (uri.find("data:") == 0) {
// Validate data URI size to prevent DoS through memory exhaustion
::Microsoft::ReactNative::InputValidation::SizeValidator::ValidateSize(
uri.length(), ::Microsoft::ReactNative::InputValidation::SizeValidator::MAX_DATA_URI_SIZE, "Data URI");
} else {
// Allow http/https only for non-data URIs
::Microsoft::ReactNative::InputValidation::URLValidator::ValidateURL(uri, {"http", "https"});
}
} catch (const ::Microsoft::ReactNative::InputValidation::ValidationException &ex) {
result.Reject(ex.what());
return;
}
// NYI
result.Resolve(true);
}
void ImageLoader::queryCache(
std::vector<std::string> const &uris,
React::ReactPromise<React::JSValue> &&result) noexcept {
// NYI
result.Resolve(React::JSValueObject{});
}
} // namespace Microsoft::ReactNative