Repository navigation
Fix/client side proxy frames - #891
Conversation
| {% if not proxy_frames(env) %} | ||
| // `via.proxy_frames=0` disables iframe proxying server-side (see | ||
| // `modify_tag_attrs` in hooks.py). Make wombat leave iframes alone too, so | ||
| // that frames which page scripts (re)insert aren't proxied either. |
There was a problem hiding this comment.
var rewriteUrl at the top level of an inline script becomes window.rewriteUrl on the proxied page. Generic name, and we're injecting into third-party pages we don't control, so a collision with the site's own code is plausible.
An IIFE keeps it contained at no cost:
(function () {
if (window._WBWombat && typeof window._WBWombat.prototype.rewriteUrl === "function") {
...
return rewriteUrl.apply(this, arguments);
};
}
})();
| var rewriteUrl = window._WBWombat.prototype.rewriteUrl; | ||
| window._WBWombat.prototype.rewriteUrl = function (url, useRel, mod, doc) { | ||
| if (mod === "if_" || mod === "fr_") { | ||
| return url; |
There was a problem hiding this comment.
rewriteUrl does two things in order: it completes the URL against the original site, and then it proxies it. We only want to skip the second step, but return url skips both.
That's a problem for a relative src, because the page isn't where it thinks it is. Its address is Via's, with the original URL inside the path. When a script sets a relative frame URL (for example: iframe.src = "/media/player" ), the raw string is handed straight back, so the browser completes it against the proxy document and the frame ends up requesting that path from the Via host instead of the original site. It resolves to nothing there, and the error response renders inside the frame. Before this change that iframe loaded, so it's a regression.
It doesn't show up in the Studio case because that embed URL is already absolute.
Fix is to do step one and skip step two:
if (mod === "if_" || mod === "fr_") {
if (typeof url !== "string") {
return url;
}
try {
return new URL(url, this.wb_info.url).href;
} catch (e) {
return url;
}
}
Worth a test for the relative case.
Context
Hypothesis LMS assignments built from Canvas Pages show the page through Via. By
default Via rewrites every iframe in a page so it also loads through Via.
Canvas Pages ask Via not to do that with
via.proxy_frames=0(hypothesis/lms#6045, Feb 2024), so embedded players load directly from their
own sites.
That flag was only honoured server-side, in the HTML viahtml rewrites.
wombat.js, pywb's client-side rewriter, kept proxying any iframe a page script
inserted or re-inserted after load. Page-builder runtimes such as DesignPlus
rebuild the page that way, so on those pages a public-link Canvas Studio embed
ended up proxied anyway, and Via's JavaScript rewriting breaks Studio's player
(same failure mode as Kanopy in product-backlog#1760).
This is one of the two causes behind product-backlog#1748 and #1749. The other,
toolbar (plug-icon) embeds that use Canvas's
external_tools/retrieveroute,cannot work inside a frame at all and is handled in hypothesis/lms#7479 by
turning them into a link.
Changes
Honour
via.proxy_frames=0in client-side rewriting too. Aproxy_framestemplate helper reads the flag the same way the server-side hook does. When it
is off,
head_insert.htmlwraps wombat'srewriteUrlso that iframe and frameURLs (modifiers
if_andfr_) are returned unchanged. Every iframe-rewritingpath in wombat goes through that method, so this covers iframes added via
innerHTML, the
srcproperty,setAttributeandinsertAdjacentHTML. Allother resources are rewritten as before, and pages without the flag are
untouched.
Guard the override against pywb changes. The wrapper only installs if
_WBWombat.prototype.rewriteUrlis a function. If a future pywb renames it,behaviour falls back to today's instead of throwing on the first rewrite.
Testing
Unit test for the helper (flag "0" and junk disable, "1" and absent keep the
default), and a functional test that the override is in the served page only
when the flag is "0".
Verified locally against a page that re-inserts its iframes after load, served
through local viahtml with the flag:
/proxy/if_/...andthe Studio player broke
Not covered: the server-side flag being ignored under load (shared
hooks.contextwith gevent), which is a separate change.