2121 * or a copy of a value inside other text.
2222 */
2323import type { IncomingHttpHeaders } from "node:http" ;
24+ import { httpError } from "./errors.ts" ;
2425import {
25- parseJson ,
26+ parseJsonSkipBom ,
27+ isObject ,
2628 isOpen ,
2729 isJsonSpace ,
2830 type Escapes ,
@@ -672,10 +674,6 @@ function bodyStart(text: string, href: string, keyHeaders: string[]): number {
672674 return at ;
673675}
674676
675- /** Tells whether a parsed JSON value is an object that is not an array. */
676- const isObject = ( json : unknown ) : json is Record < string , unknown > =>
677- json !== null && typeof json === "object" && ! Array . isArray ( json ) ;
678-
679677/** Tells whether the field `name` of `json` holds a minted value. */
680678const mintedValue = ( json : Record < string , unknown > , name : string ) => {
681679 const value = json [ name ] ;
@@ -691,7 +689,7 @@ const mintedValue = (json: Record<string, unknown>, name: string) => {
691689 * result, so the caller does not record the body.
692690 */
693691function replaceMinted ( text : string ) : { text : string ; minted : string [ ] } {
694- const json = parseJson ( text ) ;
692+ const json = parseJsonSkipBom ( text ) ;
695693 const none = { text, minted : [ ] } ;
696694 if ( ! isObject ( json ) ) return none ;
697695 // The fields of the top object that can hold a minted value.
@@ -726,7 +724,7 @@ function replaceMinted(text: string): { text: string; minted: string[] } {
726724 // Parse the result again, and fail when a minted value stays. The pass
727725 // replaces only whole strings with strings, so the names of the fields
728726 // do not change. Thus only the names from the first parse can hold one.
729- const again = parseJson ( result ) ;
727+ const again = parseJsonSkipBom ( result ) ;
730728 if ( ! isObject ( again ) || names . some ( ( name ) => mintedValue ( again , name ) ) ) {
731729 throw new Error ( "a minted credential stayed in the body" ) ;
732730 }
@@ -892,7 +890,7 @@ export function toRedact(
892890 */
893891function bodyFields ( request : RequestToRedact ) {
894892 const form = isForm ( request . contentType ) ;
895- const json = form ? undefined : parseJson ( request . body ) ;
893+ const json = form ? undefined : parseJsonSkipBom ( request . body ) ;
896894 const fields = form ? formFields ( request . body ) : jsonFields ( json ) ;
897895 return { form, json, fields, isValue : valueTest ( fields . values ) } ;
898896}
@@ -1001,7 +999,7 @@ export function redactRequestWith(
1001999 }
10021000 const known = new Set ( fields . values ) ;
10031001 const body = redactWith ( values , replaceStrings ( result . slice ( at ) , known ) ) ;
1004- const parsed = parseJson ( body ) ;
1002+ const parsed = parseJsonSkipBom ( body ) ;
10051003 if (
10061004 parsed === undefined ||
10071005 ! showsNoField ( parsed , known ) ||
@@ -1045,11 +1043,9 @@ export function createSecrets(initial: string[], limit: LearnLimit) {
10451043 next . length += part . length ;
10461044 }
10471045 if ( next . values > limit . values || next . length > limit . length ) {
1048- throw Object . assign (
1049- new Error (
1050- `this run cannot learn more credentials (${ limit . values } values or ${ limit . length } characters, with the parts of each value). Start a new run.` ,
1051- ) ,
1052- { status } ,
1046+ throw httpError (
1047+ status ,
1048+ `this run cannot learn more credentials (${ limit . values } values or ${ limit . length } characters, with the parts of each value). Start a new run.` ,
10531049 ) ;
10541050 }
10551051 learned = next ;
0 commit comments