From 33ba67646bba1d4f9dd88f505a5e84ac77424142 Mon Sep 17 00:00:00 2001 From: Ilya Kreymer Date: Mon, 7 Aug 2017 20:00:30 -0700 Subject: [PATCH] JS proxy fix (#229) * proxy access fixes: - catch proxy access (in case cross-domain, eg. from service worker) - document.location access falls back to defaultView._WB_wombat_location if not available - use obj_to_proxy(), proxy_to_obj() wrappers access, catch exceptions --- pywb/static/wombat.js | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/pywb/static/wombat.js b/pywb/static/wombat.js index 00671605..0f1a8abe 100644 --- a/pywb/static/wombat.js +++ b/pywb/static/wombat.js @@ -899,9 +899,7 @@ var _WBWombat = function($wbwindow, wbinfo) { if (orig_getter) { var new_getter = function() { - var res = orig_getter.call(this); - res = (res && res._WB_wombat_obj_proxy) || res; - return res; + return obj_to_proxy(orig_getter.call(this)); } def_prop(proto, prop, undefined, new_getter); @@ -1720,11 +1718,9 @@ var _WBWombat = function($wbwindow, wbinfo) { var getter = function() { init_iframe_wombat(this); - var res = orig_getter.call(this); - res = (res && res._WB_wombat_obj_proxy) || res; - return res; - }; - + return obj_to_proxy(orig_getter.call(this)); + } + def_prop(obj, prop, orig_setter, getter); obj["_get_" + prop] = orig_getter; } @@ -1906,7 +1902,7 @@ var _WBWombat = function($wbwindow, wbinfo) { function receive_hash_change(event) { - var source = event.source.__WBProxyRealObj__ || event.source; + var source = proxy_to_obj(event.source); if (!event.data || source != $wbwindow.__WB_top_frame) { return; @@ -2023,7 +2019,7 @@ var _WBWombat = function($wbwindow, wbinfo) { source = win.__WB_win_id[event.data.src_id]; } - source = source.__WBProxyRealObj__ || source; + source = proxy_to_obj(source); ne = new MessageEvent("message", {"bubbles": event.bubbles, @@ -2132,7 +2128,7 @@ var _WBWombat = function($wbwindow, wbinfo) { var orig_observe = $wbwindow.MutationObserver.prototype.observe; function observe_deproxy(target, options) { - target = target && target.__WBProxyRealObj__ || target; + target = proxy_to_obj(target); return orig_observe.call(this, target, options); } @@ -2535,6 +2531,22 @@ var _WBWombat = function($wbwindow, wbinfo) { // New Proxy Obj Override Functions // Original Concept by John Berlin (https://github.com/N0taN3rd) //============================================ + function proxy_to_obj(source) { + try { + return source.__WBProxyRealObj__ || source; + } catch (e) { + return source; + } + } + + function obj_to_proxy(obj) { + try { + return (obj && obj._WB_wombat_obj_proxy) || obj; + } catch (e) { + return obj; + } + } + function getAllOwnProps(obj) { var ownProps = []; @@ -2568,7 +2580,7 @@ var _WBWombat = function($wbwindow, wbinfo) { if (prop == '__WBProxyRealObj__') { return obj; } else if (prop == 'location') { - return obj._WB_wombat_location; + return obj._WB_wombat_location || (obj.defaultView && obj._WB_wombat_location); } else if (prop == "_WB_wombat_obj_proxy") { return obj._WB_wombat_obj_proxy; }