Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(360)

Unified Diff: ios/web/web_state/ui/web_view_js_utils.mm

Issue 2335483004: Limit depth of parsing of dictionaries returned by JS evaluation. (Closed)
Patch Set: Addressed comment. Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « ios/web/web_state/ui/web_view_js_utils.h ('k') | ios/web/web_state/ui/web_view_js_utils_unittest.mm » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ios/web/web_state/ui/web_view_js_utils.mm
diff --git a/ios/web/web_state/ui/web_view_js_utils.mm b/ios/web/web_state/ui/web_view_js_utils.mm
index 77817a1f0176d12abcd8c32d0ba8760a0764672f..8ab4867e6224bd0f00479d07d99df1b22c38565b 100644
--- a/ios/web/web_state/ui/web_view_js_utils.mm
+++ b/ios/web/web_state/ui/web_view_js_utils.mm
@@ -8,20 +8,28 @@
#import <WebKit/WebKit.h>
#include "base/logging.h"
+#include "base/mac/foundation_util.h"
#include "base/mac/scoped_nsobject.h"
#include "base/memory/ptr_util.h"
#include "base/strings/sys_string_conversions.h"
#include "base/values.h"
-namespace web {
-
-NSString* const kJSEvaluationErrorDomain = @"JSEvaluationError";
+namespace {
-std::unique_ptr<base::Value> ValueResultFromWKResult(id wk_result) {
+// Converts result of WKWebView script evaluation to base::Value, parsing
+// |wk_result| up to a depth of |max_depth|.
+std::unique_ptr<base::Value> ValueResultFromWKResult(id wk_result,
+ int max_depth) {
if (!wk_result)
return nullptr;
std::unique_ptr<base::Value> result;
+
+ if (max_depth < 0) {
+ DLOG(WARNING) << "JS maximum recursion depth exceeded.";
+ return result;
+ }
+
CFTypeID result_type = CFGetTypeID(wk_result);
if (result_type == CFStringGetTypeID()) {
result.reset(new base::StringValue(base::SysNSStringToUTF16(wk_result)));
@@ -40,10 +48,13 @@ std::unique_ptr<base::Value> ValueResultFromWKResult(id wk_result) {
std::unique_ptr<base::DictionaryValue> dictionary =
base::MakeUnique<base::DictionaryValue>();
for (id key in wk_result) {
- DCHECK([key respondsToSelector:@selector(UTF8String)]);
- const std::string& path([key UTF8String]);
- dictionary->Set(path,
- ValueResultFromWKResult([wk_result objectForKey:key]));
+ NSString* obj_c_string = base::mac::ObjCCast<NSString>(key);
+ const std::string path = base::SysNSStringToUTF8(obj_c_string);
+ std::unique_ptr<base::Value> value = ValueResultFromWKResult(
+ [wk_result objectForKey:obj_c_string], max_depth - 1);
+ if (value) {
+ dictionary->Set(path, std::move(value));
+ }
}
result = std::move(dictionary);
} else {
@@ -52,6 +63,17 @@ std::unique_ptr<base::Value> ValueResultFromWKResult(id wk_result) {
return result;
}
+} // namespace
+
+namespace web {
+
+NSString* const kJSEvaluationErrorDomain = @"JSEvaluationError";
+int const kMaximumParsingRecursionDepth = 6;
+
+std::unique_ptr<base::Value> ValueResultFromWKResult(id wk_result) {
+ return ::ValueResultFromWKResult(wk_result, kMaximumParsingRecursionDepth);
+}
+
void ExecuteJavaScript(WKWebView* web_view,
NSString* script,
JavaScriptResultBlock completion_handler) {
« no previous file with comments | « ios/web/web_state/ui/web_view_js_utils.h ('k') | ios/web/web_state/ui/web_view_js_utils_unittest.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698