@@ -16,18 +16,29 @@ protected function obfuscateBody(string $message): string
1616 {
1717 $ replacement = config ('http-client-global-logger.obfuscate.replacement ' );
1818
19- foreach (config ('http-client-global-logger.obfuscate.body_keys ' ) as $ key ) {
19+ $ bodyKeys = config ('http-client-global-logger.obfuscate.body_keys ' );
20+
21+ // For each key, replace JSON-style and query param style
22+ foreach ($ bodyKeys as $ key ) {
2023 $ quoted = preg_quote ($ key , '/ ' );
21- // JSON-style: "key":"value"
24+
25+ // NOTES:
26+ // No multiline (/m) or ungreedy (/U) flags are needed; you only want to match within each line.
27+ // Each match is replaced in-place without modifying the rest of the string.
28+
29+ // 1. JSON-style: "key":"value"
2230 $ message = preg_replace (
23- '/(?<= " ' .$ quoted .'": ")[^"]*(?= ")/mU ' ,
24- $ replacement ,
31+ '/(" ' .$ quoted .'"\s*:\s* ")[^"]*(")/ ' ,
32+ ' $1 ' . $ replacement. ' $2 ' ,
2533 $ message
2634 );
27- // form-style: key=value (until & or end)
28- $ message = preg_replace (
29- '/(?<=\b ' . $ quoted .'=)[^&]*(?=&|$)/ ' ,
30- $ replacement ,
35+
36+ // 2. Form/query-style: key=value (stopping at & or end)
37+ // Using preg_replace_callback, so we don’t accidentally re-use the matched value or cause duplicates.
38+ // The pattern (\bkey=)[^&\s]* will match only the value, and not cross line breaks or ampersands.
39+ $ message = preg_replace_callback (
40+ '/(\b ' .$ quoted .'=)[^&\s]*/ ' ,
41+ fn ($ matches ) => $ matches [1 ].$ replacement ,
3142 $ message
3243 );
3344 }
0 commit comments