From 62ecdd28c2d9fff1b4db663d5a9c79484aee1d23 Mon Sep 17 00:00:00 2001 From: Alex Schwartz Date: Thu, 20 Nov 2025 15:34:47 -0500 Subject: [PATCH 1/8] fix: inject HTML before ` + +describe('http/util/rewriter', () => { + describe('.top-level tests', () => { + it('just a script tag', async () => { + expect(await html('', {} as any, injected)) + .toEqual(`${injected} `) + }) + + it('script tag before a html tag', async () => { + expect(await html('Test HTML', {} as any, injected)) + .toEqual(`${injected} Test HTML`) + }) + + it('script tag after a html tag', async () => { + expect(await html('Test HTML', {} as any, injected)) + .toEqual(` ${injected} Test HTML`) + }) + + it('script tag inside head tag with no body tag', async () => { + expect(await html('', {} as any, injected)) + .toEqual(` ${injected}`) + }) + + it('script tag inside head tag with a body tag', async () => { + expect(await html('Example Body', {} as any, injected)) + .toEqual(` ${injected}Example Body`) + }) + + it('script tag inside body tag with no head tag', async () => { + expect(await html('', {} as any, injected)) + .toEqual(` ${injected} `) + }) + + it('script tag inside body tag with a head tag', async () => { + expect(await html('Original Head', {} as any, injected)) + .toEqual(` ${injected}Original Head`) + }) + }) +}) From 304fece11ba4570210774c3c7b4d0dab9501fcf6 Mon Sep 17 00:00:00 2001 From: Alex Schwartz Date: Thu, 20 Nov 2025 17:32:24 -0500 Subject: [PATCH 5/8] hopefully fix lint --- .../proxy/test/unit/http/util/rewriter.spec.ts | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/packages/proxy/test/unit/http/util/rewriter.spec.ts b/packages/proxy/test/unit/http/util/rewriter.spec.ts index 57738317ef3..8860fca92f5 100644 --- a/packages/proxy/test/unit/http/util/rewriter.spec.ts +++ b/packages/proxy/test/unit/http/util/rewriter.spec.ts @@ -1,5 +1,4 @@ import { describe, expect, it } from 'vitest' -import _ from 'lodash' import { html } from '../../../../lib/http/util/rewriter' const injected = `` @@ -8,37 +7,37 @@ describe('http/util/rewriter', () => { describe('.top-level tests', () => { it('just a script tag', async () => { expect(await html('', {} as any, injected)) - .toEqual(`${injected} `) + .toEqual(`${injected} `) }) it('script tag before a html tag', async () => { expect(await html('Test HTML', {} as any, injected)) - .toEqual(`${injected} Test HTML`) + .toEqual(`${injected} Test HTML`) }) it('script tag after a html tag', async () => { expect(await html('Test HTML', {} as any, injected)) - .toEqual(` ${injected} Test HTML`) + .toEqual(` ${injected} Test HTML`) }) it('script tag inside head tag with no body tag', async () => { expect(await html('', {} as any, injected)) - .toEqual(` ${injected}`) + .toEqual(` ${injected}`) }) it('script tag inside head tag with a body tag', async () => { expect(await html('Example Body', {} as any, injected)) - .toEqual(` ${injected}Example Body`) + .toEqual(` ${injected}Example Body`) }) it('script tag inside body tag with no head tag', async () => { expect(await html('', {} as any, injected)) - .toEqual(` ${injected} `) + .toEqual(` ${injected} `) }) it('script tag inside body tag with a head tag', async () => { expect(await html('Original Head', {} as any, injected)) - .toEqual(` ${injected}Original Head`) + .toEqual(` ${injected}Original Head`) }) }) }) From 536ef85bb36323b701117c4ded8e43000722c266 Mon Sep 17 00:00:00 2001 From: Alex Schwartz Date: Thu, 20 Nov 2025 17:38:59 -0500 Subject: [PATCH 6/8] Fix mocked --- packages/proxy/lib/http/util/rewriter.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/proxy/lib/http/util/rewriter.ts b/packages/proxy/lib/http/util/rewriter.ts index 31ef78b353a..910e5dcd484 100644 --- a/packages/proxy/lib/http/util/rewriter.ts +++ b/packages/proxy/lib/http/util/rewriter.ts @@ -98,8 +98,8 @@ const isScriptTopLevel = (scriptMatch, htmlMatch) => { return scriptIndex < htmlIndex } -export async function html (html: string, opts: SecurityOpts & InjectionOpts) { - const htmlToInject = await Promise.resolve(getHtmlToInject(opts)) +export async function html (html: string, opts: SecurityOpts & InjectionOpts, mockedHtmlToInject: string = "") { + const htmlToInject = mockedHtmlToInject || await Promise.resolve(getHtmlToInject(opts)) // strip clickjacking and framebusting // from the HTML if we've been told to From be98238ca93eb411343fa3e509fbe4ae427a0a6e Mon Sep 17 00:00:00 2001 From: Alex Schwartz Date: Thu, 20 Nov 2025 18:11:51 -0500 Subject: [PATCH 7/8] fix lint --- packages/proxy/lib/http/util/rewriter.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/proxy/lib/http/util/rewriter.ts b/packages/proxy/lib/http/util/rewriter.ts index 910e5dcd484..15bca54e3d8 100644 --- a/packages/proxy/lib/http/util/rewriter.ts +++ b/packages/proxy/lib/http/util/rewriter.ts @@ -98,7 +98,7 @@ const isScriptTopLevel = (scriptMatch, htmlMatch) => { return scriptIndex < htmlIndex } -export async function html (html: string, opts: SecurityOpts & InjectionOpts, mockedHtmlToInject: string = "") { +export async function html (html: string, opts: SecurityOpts & InjectionOpts, mockedHtmlToInject: string = '') { const htmlToInject = mockedHtmlToInject || await Promise.resolve(getHtmlToInject(opts)) // strip clickjacking and framebusting From 51eee9443077bf883b05dd9319ac90bdc34e8356 Mon Sep 17 00:00:00 2001 From: Alex Schwartz Date: Thu, 20 Nov 2025 18:52:24 -0500 Subject: [PATCH 8/8] empty run semantic PR test