Skip to content

Fix/client side proxy frames - #891

Merged
Elimpizza merged 4 commits into
mainfrom
fix/client-side-proxy-frames
Nov 3, 2026
Merged

Elimpizza merged 4 commits into
mainfrom
fix/client-side-proxy-frames

Conversation

@Elimpizza

Copy link
Copy Markdown
Contributor

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/retrieve route,
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=0 in client-side rewriting too. A proxy_frames
template helper reads the flag the same way the server-side hook does. When it
is off, head_insert.html wraps wombat's rewriteUrl so that iframe and frame
URLs (modifiers if_ and fr_) are returned unchanged. Every iframe-rewriting
path in wombat goes through that method, so this covers iframes added via
innerHTML, the src property, setAttribute and insertAdjacentHTML. All
other 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.rewriteUrl is 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:

  • before: the re-inserted iframes were requested through /proxy/if_/... and
    the Studio player broke
  • after: all iframes load directly and the player keeps playing
  • without the flag: iframes are still proxied
  • with the flag: images and scripts inserted by script are still proxied

Not covered: the server-side flag being ignored under load (shared
hooks.context with gevent), which is a separate change.

{% 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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);
     };
   }
 })();

Comment thread viahtml/templates/head_insert.html Outdated
var rewriteUrl = window._WBWombat.prototype.rewriteUrl;
window._WBWombat.prototype.rewriteUrl = function (url, useRel, mod, doc) {
if (mod === "if_" || mod === "fr_") {
return url;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@Elimpizza
Elimpizza marked this pull request as ready for review November 3, 2026 15:30
@Elimpizza
Elimpizza merged commit 2f2abd7 into main Nov 3, 2026
3 checks passed
@Elimpizza
Elimpizza deleted the fix/client-side-proxy-frames branch November 3, 2026 16:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants