Skip to content

Commit c765b19

Browse files
committed
🐛 remove javascript attrs integrity
1 parent 26be9cf commit c765b19

File tree

3 files changed

+26
-16
lines changed

3 files changed

+26
-16
lines changed

src/inject.ts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,10 @@ window.proxyPrefixPathRegexp = new RegExp("${proxyPrefixPathRegexpStr}");
555555
// interceptElementAttribute(element, "src", "mp_");
556556
// interceptElementSrcset(element);
557557
} else if (element instanceof HTMLScriptElement) {
558+
if (element.hasAttribute("integrity")) {
559+
element.removeAttribute("integrity");
560+
}
561+
558562
if (element.getAttribute("type") === "module") {
559563
interceptElementAttribute(element, "src", "esm_");
560564
} else {
@@ -672,6 +676,13 @@ window.proxyPrefixPathRegexp = new RegExp("${proxyPrefixPathRegexpStr}");
672676
const elements = doc.querySelectorAll("*");
673677
elements.forEach((element) => {
674678
if (element instanceof HTMLElement) {
679+
if (
680+
element instanceof HTMLScriptElement &&
681+
element.hasAttribute("integrity")
682+
) {
683+
element.removeAttribute("integrity");
684+
}
685+
675686
const urlAttributes = ["src", "href", "action", "data-src"];
676687
urlAttributes.forEach((attr) => {
677688
if (element.hasAttribute(attr)) {
@@ -968,6 +979,10 @@ window.proxyPrefixPathRegexp = new RegExp("${proxyPrefixPathRegexpStr}");
968979
value: string
969980
): void {
970981
try {
982+
if (name === "integrity" && this instanceof HTMLScriptElement) {
983+
return;
984+
}
985+
971986
const urlAttributes = ["src", "href", "action", "data-src"];
972987
if (urlAttributes.includes(name) && typeof value === "string") {
973988
let mod = "mp_";
@@ -988,19 +1003,6 @@ window.proxyPrefixPathRegexp = new RegExp("${proxyPrefixPathRegexpStr}");
9881003
return originalSetAttribute.call(this, name, rewrittenValue);
9891004
}
9901005

991-
// if (name === "srcset" && typeof value === "string") {
992-
// const parts = value.split(",").map((part) => {
993-
// const [url, ...descriptors] = part.trim().split(/\s+/);
994-
// if (url && !url.startsWith("data:")) {
995-
// const rewrittenUrl = rewriteUrl(url, "mp_");
996-
// return [rewrittenUrl, ...descriptors].join(" ");
997-
// }
998-
// return part;
999-
// });
1000-
1001-
// return originalSetAttribute.call(this, name, parts.join(", "));
1002-
// }
1003-
10041006
if (
10051007
name === "style" &&
10061008
typeof value === "string" &&

src/inject/proxy.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,11 +390,17 @@ export class SlaxEnv {
390390
private wrapDOMConstructor(origCtor: Function): Function {
391391
const proxyThis = this;
392392

393+
const ctorName = origCtor.name;
394+
393395
function wrappedConstructor(this: any, ...args: any[]): any {
394396
if (!(this instanceof wrappedConstructor)) {
395397
return new (origCtor as any)(...args);
396398
}
397399

400+
if (ctorName === "XMLHttpRequest") {
401+
return new (origCtor as any)(...args);
402+
}
403+
398404
const newObj = new (origCtor as any)(...args);
399405

400406
if (newObj instanceof Node) {

src/rewrite.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -503,8 +503,10 @@ export function completeHtmlRewrite(
503503
attrs.includes("type='module'") ||
504504
attrs.includes("type=module");
505505

506-
if (attrs.includes(" src=")) {
507-
const processedAttrs = attrs.replace(
506+
let processedAttrs = attrs.replace(/\s+integrity=["'][^"']+["']/gi, "");
507+
508+
if (processedAttrs.includes(" src=")) {
509+
processedAttrs = processedAttrs.replace(
508510
/src\s*=\s*["']([^"']+)["']/gi,
509511
function (srcMatch: string, src: string) {
510512
if (src.startsWith("#") || src.startsWith("data:")) {
@@ -522,7 +524,7 @@ export function completeHtmlRewrite(
522524
return `<script${processedAttrs}>${content}</script>`;
523525
}
524526

525-
return `<script${attrs}>${rewriteJS(
527+
return `<script${processedAttrs}>${rewriteJS(
526528
content,
527529
baseUrl,
528530
timestamp,

0 commit comments

Comments
 (0)