Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewriting Global + Live Proxy Fixes #158

Merged
merged 7 commits into from
Jan 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions dist/sw.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/wombat.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/wombatWorkers.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/live-proxy/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@
<script src="./index.js"></script>
</head>
<body>
<iframe id="content"></iframe>
<iframe sandbox="allow-downloads allow-modals allow-orientation-lock allow-pointer-lock allow-popups allow-popups-to-escape-sandbox allow-presentation allow-scripts allow-same-origin allow-forms" id="content"></iframe>
</body>
</html>
32 changes: 26 additions & 6 deletions examples/live-proxy/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,17 @@ class WabacLiveProxy
{
constructor() {
this.url = "";
this.ts = "";
}

async init() {
window.addEventListener("load", () => {
const iframe = document.querySelector("#content");
if (iframe) {
iframe.addEventListener("load", () => this.onIframeLoad(iframe.contentWindow.location.href));
}
});

const scope = "./";

// also add inject of custom.js as a script into each replayed page
Expand All @@ -34,11 +42,12 @@ class WabacLiveProxy
file: {"sourceUrl": `proxy:${proxyPrefix}`},
skipExisting: false,
extraConfig: {
"prefix": proxyPrefix,
"isLive": true,
"prefix": proxyPrefix,
"isLive": false,
"baseUrl": baseUrl.href,
"baseUrlHashReplay": true,
"noPostToGet": true
"noPostToGet": true,
"archivePrefix": "https://web.archive.org/web/"
},
};

Expand Down Expand Up @@ -69,20 +78,31 @@ class WabacLiveProxy
const m = window.location.hash.slice(1).match(/\/?(?:([\d]+)\/)?(.*)/);

const url = m && m[2] || "https://example.com/";
const ts = m && m[1] || "";

// don't change if same url
if (url === this.url) {
if (url === this.url && ts === this.ts) {
return;
}

let iframeUrl = `/w/liveproxy/mp_/${url}`;
let iframeUrl = ts ? `/w/liveproxy/${ts}mp_/${url}` : `/w/liveproxy/mp_/${url}`;

const iframe = document.querySelector("#content");
iframe.src = iframeUrl;

this.url = url;
this.ts = ts;

window.location.hash = ts ? `#${ts}/${url}` : `#${url}`;
}

onIframeLoad(url) {
const m = url.match(/liveproxy\/([\d]+)?\w\w_\/(.*)/);

this.ts = m[1] || "";
this.url = m[2] || "";

window.location.hash = `#${url}`;
window.location.hash = this.ts ? `#${this.ts}/${this.url}` : `#${this.url}`;
}
}

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "@webrecorder/wabac",
"version": "2.16.13",
"version": "2.17.0",
"main": "index.js",
"type": "module",
"license": "AGPL-3.0-or-later",
"dependencies": {
"@peculiar/asn1-ecc": "^2.3.4",
"@peculiar/asn1-schema": "^2.3.3",
"@peculiar/x509": "^1.9.2",
"@webrecorder/wombat": "^3.6.1",
"@webrecorder/wombat": "^3.7.0",
"acorn": "^8.10.0",
"auto-js-ipfs": "^2.1.1",
"base64-js": "^1.5.1",
Expand Down
1 change: 1 addition & 0 deletions src/liveproxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ export class LiveProxy {
date: new Date(),
noRW: false,
isLive: this.isLive,
archivePrefix: this.archivePrefix,
});

if (clonedResponse) {
Expand Down
8 changes: 7 additions & 1 deletion src/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const decoder = new TextDecoder();
class ArchiveResponse
{

static fromResponse({url, response, date, noRW, isLive}) {
static fromResponse({url, response, date, noRW, isLive, archivePrefix}) {
const payload = response.body ? new AsyncIterReader(response.body.getReader(), false) : null;
const status = Number(response.headers.get("x-redirect-status") || response.status);
const statusText = response.headers.get("x-redirect-statusText") || response.statusText;
Expand All @@ -21,6 +21,12 @@ class ArchiveResponse
if (origLoc.startsWith(self.location.origin)) {
origLoc = origLoc.slice(self.location.origin.length);
}
if (archivePrefix && origLoc.startsWith(archivePrefix)) {
const inx = origLoc.indexOf("/http");
if (inx > 0) {
origLoc = origLoc.slice(inx + 1);
}
}
headers.set("location", origLoc);
headers.delete("x-orig-location");
headers.delete("x-redirect-status");
Expand Down
14 changes: 8 additions & 6 deletions src/rewrite/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,17 @@ class HTMLRewriter
const tagName = tag.tagName;

for (let attr of tag.attrs) {
const name = attr.name;
const value = attr.value;
const name = attr.name || "";
const value = attr.value || "";

// js attrs
if (name.startsWith("on") && value.startsWith("javascript:") && name.slice(2, 3) != "-") {
// js attrs with javascript:
if (value.startsWith("javascript:")) {
attr.value = "javascript:" + rewriter.rewriteJS(value.slice("javascript:".length), {inline: true});
}
} else if (name.startsWith("on") && name.slice(2, 3) != "-") {
// js attrs
attr.value = rewriter.rewriteJS(value, {inline: true});
// css attrs
else if (name === "style") {
} else if (name === "style") {
attr.value = rewriter.rewriteCSS(attr.value);
}

Expand Down
2 changes: 1 addition & 1 deletion src/rewrite/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ class Rewriter {

// JS
rewriteJS(text, opts) {
const noUrlProxyRewrite = opts && !opts.rewriteUrl && opts.isModule === undefined;
const noUrlProxyRewrite = opts && !opts.rewriteUrl && opts.isModule === undefined && !opts.inline;
const dsRules = noUrlProxyRewrite ? baseRules : this.dsRules;
const dsRewriter = dsRules.getRewriter(this.baseUrl);

Expand Down
40 changes: 28 additions & 12 deletions src/rewrite/jsrewriter.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,22 +176,35 @@ if (!self.__WB_pmw) { self.__WB_pmw = function(obj) { this.__WB_source = obj; re
return false;
}

parseLetConstGlobals(text) {
parseGlobals(text) {
const res = acorn.parse(text, {ecmaVersion: "latest"});

const names = [];

const excludeOverrides = new Set();

for (const expr of res.body) {
const { type, kind, declarations } = expr;
if (type === "VariableDeclaration" && (kind === "const" || kind === "let")) {
const decl = declarations.length && declarations[0];
if (decl && decl.type === "VariableDeclarator") {
const name = decl.id && decl.id.name;
names.push(`self.${name} = ${name};`);
if (type === "VariableDeclaration") {
for (const decl of declarations) {
if (decl && decl.type === "VariableDeclarator" && decl.id && decl.id.type === "Identifier") {
const name = decl.id.name;

if (GLOBAL_OVERRIDES.includes(name)) {
excludeOverrides.add(name);
} else if (kind === "const" || kind === "let") {
names.push(`self.${name} = ${name};`);
}
}
}
}
}

if (excludeOverrides.size) {
const filteredGlobals = GLOBAL_OVERRIDES.filter(x => !excludeOverrides.has(x));
this.firstBuff = this.initLocalDecl(filteredGlobals);
}

if (names.length) {
return "\n" + names.join("\n");
} else {
Expand Down Expand Up @@ -227,20 +240,23 @@ if (!self.__WB_pmw) { self.__WB_pmw = function(obj) { this.__WB_source = obj; re

const wrapGlobals = GLOBALS_RX.exec(text);

if (opts.inline) {
newText = newText.replace(/\n/g, " ") ;
}

if (wrapGlobals) {
let hoistGlobals = "";
if (newText) {
try {
hoistGlobals = this.parseLetConstGlobals(newText);
hoistGlobals = this.parseGlobals(newText);
} catch (e) {
console.warn("acorn parsing failed on: " + newText);
console.warn(`acorn parsing failed, script len ${newText.length}`);
}
}
newText = this.firstBuff + newText + hoistGlobals + this.lastBuff;
}

if (opts && opts.inline) {
newText = newText.replace(/\n/g, " ") ;
if (opts.inline) {
newText = newText.replace(/\n/g, " ") ;
}
}

return newText;
Expand Down
21 changes: 21 additions & 0 deletions test/rewriteHTML.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ ${text}

}

function wrapScriptInline(text) {
return wrapScript(text).replace(/\n/g, " ").replace(/["]/g, "&quot;").replace(/[&][&]/g, "&amp;&amp;");
}


function wrapScriptModule(text) {
return `\
Expand Down Expand Up @@ -275,6 +279,12 @@ test("srcset", rewriteHtml,
"<img srcset=\"\">",
"<img srcset=\"\">");

// background attr
test("background", rewriteHtml,
"<td background=\"https://example.com/\"></td>",
"<td background=\"http://localhost:8080/prefix/20201226101010mp_/https://example.com/\"></td>"
);

// SCRIPT Tag
// pywb diff: no script url rewriting!
test("script proxy wrapped", rewriteHtml,
Expand All @@ -289,6 +299,17 @@ test("script not wrapped", rewriteHtml,
{useBaseRules: true}
);

// inline attr rewrite
test("inline attr rewrite", rewriteHtml,
"<body onload=\"window.location.href = '/path.html'\"></body>",
`<body onload="${wrapScriptInline("window.location.href = '/path.html'")}"></body>`
);

test("inline attr rewrite with javascript: prefix", rewriteHtml,
"<body onload=\"javascript:window.location.href = '/path.html'\"></body>",
`<body onload="javascript:${wrapScriptInline("window.location.href = '/path.html'")}"></body>`
);

// no rewriting if no props
test("script", rewriteHtml,
"<script>var foo = \"http://example.com/a/b/c.html\"</script>",
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -398,10 +398,10 @@
resolved "https://registry.yarnpkg.com/@webpack-cli/serve/-/serve-1.5.2.tgz#ea584b637ff63c5a477f6f21604b5a205b72c9ec"
integrity sha512-vgJ5OLWadI8aKjDlOH3rb+dYyPd2GTZuQC/Tihjct6F9GpXGZINo3Y/IVuZVTM1eDQB+/AOsjPUWH/WySDaXvw==

"@webrecorder/wombat@^3.6.1":
version "3.6.1"
resolved "https://registry.yarnpkg.com/@webrecorder/wombat/-/wombat-3.6.1.tgz#55e23a2c0df28ad8e6d67b87fffd913a101f6982"
integrity sha512-yJ5NBf/ERCBz07cFA4phIQCpj6U/28HWFk67V+yg3S3GSAAmlFP8dC9g1z+JO77PKuAe/qlrzO6CcS0VyUr5Ww==
"@webrecorder/wombat@^3.7.0":
version "3.7.0"
resolved "https://registry.yarnpkg.com/@webrecorder/wombat/-/wombat-3.7.0.tgz#809e47c62b6acd6d5435708909d0d88c1090b0c3"
integrity sha512-xSDjuywIp8tmjIW5RqgeISgXhabqbbt1LcgrgBLUdqz1JHzJTGJ/OjmiNgV8bT+f6vtIbL+mtU8qxc86n/BgtA==
dependencies:
warcio "^2.2.0"

Expand Down
Loading