@@ -58,6 +58,7 @@ public static class ExecApprovalSecretRedactor
5858 private static readonly Regex EmptyShellParameterExpansionTailRe = CreateRegex ( @"^[-=?+]\}$" , DefaultRegexOptions ) ;
5959 private static readonly Regex AuthHeaderStartRe = CreateRegex ( @"(?:Authorization|Proxy-Authorization)(?:\\+[""'])?\s*[:=]\s*(?:\\+[""'])?" , IgnoreCaseRegexOptions ) ;
6060 private static readonly Regex StructuredAuthHeaderSerializedKeyRe = CreateRegex ( @"[""'](?:Authorization|Proxy-Authorization)[""']\s*[:=]\s*[""']" , IgnoreCaseRegexOptions ) ;
61+ private static readonly Regex PemLineBreakRe = CreateRegex ( @"\r?\n|\\r\\n|\\n" , DefaultRegexOptions ) ;
6162
6263 internal static IReadOnlyCollection < string > RegisteredSecretValues { get ; set ; } = Array . Empty < string > ( ) ;
6364
@@ -91,6 +92,7 @@ public static bool[] ComputeRedactionBitmap(string text)
9192 }
9293
9394 var bitmap = new bool [ text . Length ] ;
95+ MarkRegisteredSecretValueRedactions ( text , bitmap ) ;
9496 MarkStructuredAuthHeaderRedactions ( text , bitmap ) ;
9597 MarkUrlQueryPairRedactions ( text , bitmap ) ;
9698 MarkFormBodyRedactions ( text , bitmap ) ;
@@ -119,12 +121,7 @@ internal static string MaskToken(string token)
119121
120122 private static string RedactRegisteredSecretValues ( string text )
121123 {
122- if ( RegisteredSecretValues . Count == 0 )
123- {
124- return text ;
125- }
126-
127- var values = RegisteredSecretValues . Where ( value => ! string . IsNullOrEmpty ( value ) ) . OrderByDescending ( value => value . Length ) . ToArray ( ) ;
124+ var values = GetRegisteredSecretValues ( ) ;
128125 if ( values . Length == 0 || ! values . Select ( value => value [ 0 ] ) . Distinct ( ) . Any ( text . Contains ) )
129126 {
130127 return text ;
@@ -134,6 +131,13 @@ private static string RedactRegisteredSecretValues(string text)
134131 return matcher . Replace ( text , match => MaskToken ( match . Value ) ) ;
135132 }
136133
134+ private static string [ ] GetRegisteredSecretValues ( ) =>
135+ RegisteredSecretValues
136+ . Where ( value => ! string . IsNullOrEmpty ( value ) )
137+ . Distinct ( StringComparer . Ordinal )
138+ . OrderByDescending ( value => value . Length )
139+ . ToArray ( ) ;
140+
137141 private static string SliceUtf16Safe ( string value , int start , int ? end = null )
138142 {
139143 var actualStart = start < 0 ? Math . Max ( value . Length + start , 0 ) : Math . Min ( start , value . Length ) ;
@@ -173,7 +177,7 @@ private static string ReplacePattern(string text, RedactRegex pattern)
173177
174178 private static string RedactMatch ( Match match , RedactRegex pattern , string input )
175179 {
176- if ( match . Value . Contains ( "PRIVATE KEY-----" , StringComparison . Ordinal ) )
180+ if ( match . Value . Contains ( "PRIVATE KEY-----" , StringComparison . OrdinalIgnoreCase ) )
177181 {
178182 return RedactPemBlock ( match . Value ) ;
179183 }
@@ -438,8 +442,60 @@ private static string RedactFormBody(string text)
438442
439443 private static string RedactPemBlock ( string block )
440444 {
441- var lines = Regex . Split ( block , @"\r?\n" ) . Where ( line => line . Length > 0 ) . ToArray ( ) ;
442- return lines . Length < 2 ? Mask : lines [ 0 ] + "\n …redacted…\n " + lines [ ^ 1 ] ;
445+ var lines = PemLineBreakRe . Split ( block ) . Where ( line => line . Length > 0 ) . ToArray ( ) ;
446+ if ( lines . Length < 2 )
447+ return Mask ;
448+
449+ var separator = block . Contains ( "\r \n " , StringComparison . Ordinal )
450+ ? "\r \n "
451+ : block . Contains ( '\n ' )
452+ ? "\n "
453+ : block . Contains ( "\\ r\\ n" , StringComparison . Ordinal )
454+ ? "\\ r\\ n"
455+ : "\\ n" ;
456+ return lines [ 0 ] + separator + "…redacted…" + separator + lines [ ^ 1 ] ;
457+ }
458+
459+ internal static bool IsShellReference ( string value ) =>
460+ ShellReferenceBareRe . IsMatch ( value ) || ShellReferenceBracedRe . IsMatch ( value ) ;
461+
462+ internal static string RedactReviewSafeUrlQueryValues ( string text )
463+ {
464+ if ( string . IsNullOrEmpty ( text ) || ! text . Contains ( '?' ) )
465+ {
466+ return text ;
467+ }
468+
469+ return UrlQueryPairRe . Replace ( text , match =>
470+ {
471+ var key = match . Groups [ 2 ] . Value ;
472+ var token = match . Groups [ 3 ] . Value ;
473+ if ( ! IsSensitiveBodyKey ( key ) || ! IsReviewSafeQueryToken ( token ) )
474+ return match . Value ;
475+
476+ return match . Groups [ 1 ] . Value
477+ + key
478+ + "="
479+ + MaskSecretValue ( token , hinted : true ) ;
480+ } ) ;
481+ }
482+
483+ private static bool IsReviewSafeQueryToken ( string token )
484+ {
485+ var value = SplitSecretValueForMask ( token ) . Maskable ;
486+ if ( string . IsNullOrEmpty ( value ) || IsShellReference ( value ) )
487+ return true ;
488+
489+ foreach ( var character in value )
490+ {
491+ if ( ! char . IsAsciiLetterOrDigit ( character )
492+ && character is not ( '-' or '_' or '.' or '~' or '+' or '/' or '=' or ':' ) )
493+ {
494+ return false ;
495+ }
496+ }
497+
498+ return true ;
443499 }
444500
445501 private static bool IsShellReferenceToKey ( string key , string value )
@@ -849,7 +905,7 @@ private static void MarkPatternRedactions(string text, bool[] bitmap, RedactRege
849905
850906 private static void MarkPatternMatchRedaction ( bool [ ] bitmap , string input , Match match , RedactRegex pattern )
851907 {
852- if ( match . Value . Contains ( "PRIVATE KEY-----" , StringComparison . Ordinal ) )
908+ if ( match . Value . Contains ( "PRIVATE KEY-----" , StringComparison . OrdinalIgnoreCase ) )
853909 {
854910 MarkBitmapRange ( bitmap , match . Index , match . Index + match . Length ) ;
855911 return ;
@@ -887,6 +943,23 @@ private static void MarkUrlQueryPairRedactions(string text, bool[] bitmap)
887943 }
888944 }
889945
946+ private static void MarkRegisteredSecretValueRedactions ( string text , bool [ ] bitmap )
947+ {
948+ foreach ( var value in GetRegisteredSecretValues ( ) )
949+ {
950+ var start = 0 ;
951+ while ( start < text . Length )
952+ {
953+ var index = text . IndexOf ( value , start , StringComparison . Ordinal ) ;
954+ if ( index < 0 )
955+ break ;
956+
957+ MarkBitmapRange ( bitmap , index , index + value . Length ) ;
958+ start = index + value . Length ;
959+ }
960+ }
961+ }
962+
890963 private static void MarkEncodedFormPairRedactions ( string text , bool [ ] bitmap , int offset = 0 )
891964 {
892965 if ( string . IsNullOrEmpty ( text ) || ( ! text . Contains ( '%' , StringComparison . Ordinal ) && FormBodyKeyObfuscationRe . Replace ( text , string . Empty ) == text ) )
@@ -1041,6 +1114,18 @@ private static List<RedactRegex> BuildDefaultPatterns()
10411114 var authorizationBasicPattern = @"Authorization(?:\\+)?[""']?[ \t]*[:=](?:[ \t]|\\[trn]|\r?\n[ \t]*)*(?:\\+)?[""']?Basic(?:[ \t]|\\[trn]|\r?\n[ \t]*)+((?>[-A-Za-z0-9._~+/=:]+))(?!…)" ;
10421115 var authorizationBotPattern = @"Authorization(?:\\+)?[""']?[ \t]*[:=](?:[ \t]|\\[trn]|\r?\n[ \t]*)*(?:\\+)?[""']?Bot(?:[ \t]|\\[trn]|\r?\n[ \t]*)+((?>[-A-Za-z0-9._~+/=:]+))(?!…)" ;
10431116 var standaloneBearerPattern = @"\bBearer\s+([-A-Za-z0-9._~+/=]{18,})(?![-A-Za-z0-9._~+/=])" ;
1117+ var pemNewlinePattern = @"(?:\r?\n|\\r\\n|\\n)" ;
1118+ var pemBase64LinePattern =
1119+ @"(?=[A-Za-z0-9+/])(?:[A-Za-z0-9+/]{4})*"
1120+ + @"(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?" ;
1121+ var pemPrivateKeyPattern =
1122+ @"-----BEGIN (?<pemType>(?:[A-Z0-9]+ )*PRIVATE KEY)-----"
1123+ + pemNewlinePattern
1124+ + @"(?:Proc-Type:[ \t]*4,ENCRYPTED" + pemNewlinePattern
1125+ + @"DEK-Info:[ \t]*[A-Z0-9-]{3,32},[A-F0-9]{16,64}"
1126+ + pemNewlinePattern + pemNewlinePattern + @")?"
1127+ + @"(?:" + pemBase64LinePattern + pemNewlinePattern + @"){1,4096}"
1128+ + @"-----END \k<pemType>-----" ;
10441129
10451130 var patterns = new List < RedactRegex >
10461131 {
@@ -1064,13 +1149,7 @@ private static List<RedactRegex> BuildDefaultPatterns()
10641149 Add ( @"(^|[\s,;])(?:" + FormBodyFirstPairKeys + @")=([^&\s]+)(?=&[A-Za-z_][A-Za-z0-9_.-]*=)" , IgnoreCaseRegexOptions ) ,
10651150 Add ( standaloneAssignmentQuotedPattern , IgnoreCaseRegexOptions , shellReferencePreserving : true ) ,
10661151 Add ( standaloneAssignmentPattern , IgnoreCaseRegexOptions , shellReferencePreserving : true ) ,
1067- Add (
1068- @"-----BEGIN (?<pemType>(?:[A-Z0-9]+ )*PRIVATE KEY)-----\r?\n"
1069- + @"(?:Proc-Type:[ \t]*4,ENCRYPTED\r?\n"
1070- + @"DEK-Info:[ \t]*[A-Z0-9-]{3,32},[A-F0-9]{16,64}\r?\n\r?\n)?"
1071- + @"(?:(?:[A-Za-z0-9+/]{4,128}={0,2})\r?\n){1,1024}"
1072- + @"-----END \k<pemType>-----" ,
1073- IgnoreCaseRegexOptions ) ,
1152+ Add ( pemPrivateKeyPattern , IgnoreCaseRegexOptions ) ,
10741153 Add ( @"(?<![A-Za-z0-9_])(sk-[A-Za-z0-9_-]{8,})(?![A-Za-z0-9_])" , IgnoreCaseRegexOptions ) ,
10751154 Add ( @"(ghp_[A-Za-z0-9]{10,})" , IgnoreCaseRegexOptions ) ,
10761155 Add ( @"(github_pat_[A-Za-z0-9_]{10,})" , IgnoreCaseRegexOptions ) ,
0 commit comments