Skip to content

Commit

Permalink
document-write fix:
Browse files Browse the repository at this point in the history
- ennsure text/* scripts do not get document.close() injection
- also ensure document.close is injected into scripts that have a top-level document.write
- fixes webrecorder/wombat#131

test: disable testBlockloaders by renaming to _testBlockloaders
  • Loading branch information
ikreymer committed Feb 22, 2024
1 parent de61e6d commit 4bc4d52
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 5 deletions.
2 changes: 1 addition & 1 deletion dist/sw.js

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion src/rewrite/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,8 @@ class HTMLRewriter
return "json";
} else if (!scriptType || (scriptType.indexOf("javascript") >= 0 || scriptType.indexOf("ecmascript") >= 0)) {
return "js";
} else if (scriptType.startsWith("text/")) {
return "text";
} else {
return "";
}
Expand Down Expand Up @@ -353,7 +355,7 @@ class HTMLRewriter
break;

case "script":
if (headDone && !isTextEmpty && (!scriptRw || scriptRw === "js")) {
if (headDone && !isTextEmpty && scriptRw === "js") {
rwStream.emitRaw(";document.close();");
}
break;
Expand Down
24 changes: 23 additions & 1 deletion src/rewrite/jsrewriter.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,13 +179,17 @@ if (!self.__WB_pmw) { self.__WB_pmw = function(obj) { this.__WB_source = obj; re
parseGlobals(text) {
const res = acorn.parse(text, {ecmaVersion: "latest"});

let hasDocWrite = false;

const names = [];

const excludeOverrides = new Set();

for (const expr of res.body) {
const { type, kind, declarations } = expr;
const { type } = expr;
// Check global variable declarations
if (type === "VariableDeclaration") {
const { kind, declarations } = expr;
for (const decl of declarations) {
if (decl && decl.type === "VariableDeclarator" && decl.id && decl.id.type === "Identifier") {
const name = decl.id.name;
Expand All @@ -197,6 +201,19 @@ if (!self.__WB_pmw) { self.__WB_pmw = function(obj) { this.__WB_source = obj; re
}
}
}
// Check for document.write() calls
} else if (!hasDocWrite && type === "ExpressionStatement") {
const { expression } = expr;
if (expression && expression.type === "CallExpression") {
const { callee } = expression;
if (callee && callee.type === "MemberExpression") {
const { object, property } = callee;
if (object.type === "Identifier" && object.name === "document" &&
property.type === "Identifier" && property.name === "write") {
hasDocWrite = true;
}
}
}
}
}

Expand All @@ -205,6 +222,11 @@ if (!self.__WB_pmw) { self.__WB_pmw = function(obj) { this.__WB_source = obj; re
this.firstBuff = this.initLocalDecl(filteredGlobals);
}

// top-level document.write(), add document.close()
if (hasDocWrite) {
this.lastBuff = ";document.close();" + this.lastBuff;
}

if (names.length) {
return "\n" + names.join("\n");
} else {
Expand Down
4 changes: 2 additions & 2 deletions test/testBlockloaders.js → test/_testBlockloaders.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";

/*

import test from "ava";

import {createLoader} from "../src/blockloaders.js";
Expand All @@ -26,4 +26,4 @@ test("Load data from IPFS Blockloader", async t => {

t.deepEqual(chunks, expected, "Got chunks from loader");
});
*/

0 comments on commit 4bc4d52

Please sign in to comment.