@@ -18,7 +18,7 @@ import {
1818 type PreviewAutomationWaitForInput ,
1919} from "@t3tools/contracts" ;
2020import { constVoid } from "effect/Function" ;
21- import type { CDPSession , Frame , Locator , Page } from "playwright-core" ;
21+ import type { CDPSession , ConsoleMessage , Frame , Locator , Page } from "playwright-core" ;
2222import * as NodeCrypto from "node:crypto" ;
2323import { BrowserControlInterrupted } from "./SessionControl.ts" ;
2424
@@ -149,6 +149,54 @@ const SNAPSHOT_SCRIPT = `(() => {
149149})()` ;
150150
151151const MASKED_VALUE = "••••••••" ;
152+
153+ /**
154+ * A `data:` URL carries its content, which can be a whole file, and URLs and diagnostics
155+ * reach tool results and provider logs. Keeps only the media type: the payload can hold any
156+ * character and parameters are page-chosen too.
157+ */
158+ export const withoutDataPayload = ( url : string ) => {
159+ // Read the scheme as a browser would: it ignores leading controls and spaces, and tabs and
160+ // newlines anywhere. A snapshot's YAML value may be quoted and escaped; an escape that isn't a
161+ // tab or newline still decodes to something that can't start a scheme.
162+ const value = url
163+ . replace ( / ^ [ " ' ] / , "" )
164+ . replace ( / \\ ( x [ \d a - f ] { 2 } | u [ \d a - f ] { 4 } | .) / gi, ( _escape , code : string ) =>
165+ code . length > 1
166+ ? String . fromCharCode ( Number . parseInt ( code . slice ( 1 ) , 16 ) )
167+ : ( { t : "\t" , n : "\n" , r : "\r" , '"' : '"' , "\\" : "\\" } [ code ] ?? "\u0001" ) ,
168+ )
169+ . replace ( / [ \t \n \r ] / g, "" )
170+ // eslint-disable-next-line no-control-regex -- URL parsing ignores these controls.
171+ . replace ( / ^ [ \u0000 - \u0020 ] + / , "" ) ;
172+ const parsed = / ^ d a t a : ( [ ^ , ; ] * ) / i. exec ( value ) ;
173+ if ( ! parsed ) return url ;
174+ const type = parsed [ 1 ] ! . trim ( ) ;
175+ const mediaType = / ^ [ \w . + - ] { 1 , 64 } \/ [ \w . + - ] { 1 , 64 } $ / . test ( type ) ? type : "" ;
176+ return `data:${ mediaType } ,…` ;
177+ } ;
178+
179+ /**
180+ * Chromium's own console messages, which carry no arguments, quote `data:` URLs whole, as in a
181+ * blocked navigation or a CSP violation. Everything after the first one goes, since Chromium
182+ * accepts any type and a payload can hold any character. What a page logs itself is its own
183+ * text, like anything else it shows, so it stays.
184+ */
185+ export const consoleText = ( message : ConsoleMessage ) =>
186+ message . args ( ) . length > 0
187+ ? message . text ( )
188+ : message . text ( ) . replace ( / (?< ! [ a - z \d ] ) d a t a : [ ^ , ; ] * [ , ; ] [ \s \S ] * / i, withoutDataPayload ) ;
189+
190+ // Split on LF alone: a value keeps any U+2028 that a multiline regex would treat as a line end.
191+ const withoutTreeDataPayloads = ( tree : string ) =>
192+ tree
193+ . split ( "\n" )
194+ . map ( ( line ) => {
195+ const url = / ^ ( \s * - \/ u r l : ) ( .* ) $ / s. exec ( line ) ;
196+ return url ? url [ 1 ] + withoutDataPayload ( url [ 2 ] ! ) : line ;
197+ } )
198+ . join ( "\n" ) ;
199+
152200// The broker waits 15s for the host; a snapshot, retry included, answers well before.
153201const SNAPSHOT_TIMEOUT_MS = 10_000 ;
154202// How long a frame in another process gets to answer; one that crashed never does.
@@ -534,7 +582,10 @@ const capture = async (
534582 const [ page , data ] = await pageAndScreenshot ;
535583 if ( state . generation !== generation ) throw pageChanged ( ) ;
536584 const mask = ( text : string ) => ( dotted ? text . replace ( dotted , MASKED_VALUE ) : text ) ;
537- const accessibilityTree = ( dotted ? maskTree ( tree , dotted ) : tree )
585+ // Redacted before the cut, so a payload crossing it can't leave its start showing, and before
586+ // masking, which could hide the `data:` scheme behind a password value's dots.
587+ const redacted = withoutTreeDataPayloads ( tree ) ;
588+ const accessibilityTree = ( dotted ? maskTree ( redacted , dotted ) : redacted )
538589 . slice ( 0 , MAX_VISIBLE_TEXT_LENGTH )
539590 . replace ( / \[ r e f = ( (?: f \d + ) ? e \d + ) \] / g, ( _match , nativeRef : string ) => {
540591 const ref = `t3-${ generation } -${ nativeRef } ` ;
0 commit comments