Skip to content

Commit 7b6240f

Browse files
bkudiessCopilot
andcommitted
fix: harden approval redaction boundaries
Co-authored-by: Copilot App <[email protected]>
1 parent 3f44199 commit 7b6240f

6 files changed

Lines changed: 452 additions & 33 deletions

‎src/OpenClaw.Shared/ExecApprovals/ExecApprovalCommandDisplaySanitizer.cs‎

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,20 @@ public static string Sanitize(string text)
2626
return SanitizeInternal(text).Text;
2727
}
2828

29-
public static (string Text, bool Truncated, bool Oversized, bool Redacted) SanitizeWithStatus(string text)
29+
public static (
30+
string Text,
31+
bool Truncated,
32+
bool Oversized,
33+
bool Redacted,
34+
bool UnsafeConcealment) SanitizeWithStatus(string text)
3035
{
3136
var result = SanitizeInternal(text);
32-
return (result.Text, result.Truncated, result.Oversized, result.Redacted);
37+
return (
38+
result.Text,
39+
result.Truncated,
40+
result.Oversized,
41+
result.Redacted,
42+
result.UnsafeConcealment);
3343
}
3444

3545
public static string SanitizeWarningText(string text)
@@ -51,7 +61,8 @@ private static SanitizedDisplayText SanitizeInternal(
5161
oversizedMarker ?? OversizedMarker,
5262
Truncated: false,
5363
Oversized: true,
54-
Redacted: false);
64+
Redacted: false,
65+
UnsafeConcealment: false);
5566
}
5667

5768
var rawRedacted = ExecApprovalSecretRedactor.Redact(text);
@@ -63,14 +74,28 @@ private static SanitizedDisplayText SanitizeInternal(
6374
strippedRedacted,
6475
strippedView.Text,
6576
StringComparison.Ordinal);
77+
var rawMask = redacted
78+
? ExecApprovalSecretRedactor.ComputeRedactionBitmap(text)
79+
: Array.Empty<bool>();
80+
var strippedMask = redacted
81+
? ExecApprovalSecretRedactor.ComputeRedactionBitmap(strippedView.Text)
82+
: Array.Empty<bool>();
83+
var rawReviewSafe = ExecApprovalSecretRedactor.RedactReviewSafeUrlQueryValues(text);
84+
var strippedReviewSafe =
85+
ExecApprovalSecretRedactor.RedactReviewSafeUrlQueryValues(strippedView.Text);
86+
var unsafeConcealment = redacted
87+
&& (!string.Equals(rawRedacted, rawReviewSafe, StringComparison.Ordinal)
88+
|| !string.Equals(
89+
strippedRedacted,
90+
strippedReviewSafe,
91+
StringComparison.Ordinal));
6692

6793
if (strippedRedacted == strippedView.Text)
6894
return TruncateForDisplay(
6995
EscapeInvisibles(rawRedacted, preserveLineBreaks),
70-
redacted);
96+
redacted,
97+
unsafeConcealment);
7198

72-
var rawMask = ExecApprovalSecretRedactor.ComputeRedactionBitmap(text);
73-
var strippedMask = ExecApprovalSecretRedactor.ComputeRedactionBitmap(strippedView.Text);
7499
var bypassDetected = false;
75100
for (var i = 0; i < strippedMask.Length; i++)
76101
{
@@ -84,7 +109,8 @@ private static SanitizedDisplayText SanitizeInternal(
84109
if (!bypassDetected)
85110
return TruncateForDisplay(
86111
EscapeInvisibles(rawRedacted, preserveLineBreaks),
87-
redacted);
112+
redacted,
113+
unsafeConcealment);
88114

89115
var unionMask = (bool[])rawMask.Clone();
90116
for (var i = 0; i < strippedMask.Length; i++)
@@ -95,7 +121,8 @@ private static SanitizedDisplayText SanitizeInternal(
95121

96122
return TruncateForDisplay(
97123
RenderUnionMask(text, unionMask, preserveLineBreaks),
98-
redacted: true);
124+
redacted: true,
125+
unsafeConcealment);
99126
}
100127

101128
private static string NormalizeDisplayLineBreaks(string text)
@@ -197,22 +224,25 @@ private static string RenderUnionMask(string text, bool[] unionMask, bool preser
197224

198225
private static SanitizedDisplayText TruncateForDisplay(
199226
string text,
200-
bool redacted = false)
227+
bool redacted = false,
228+
bool unsafeConcealment = false)
201229
{
202230
if (text.Length <= MaxOutput)
203231
{
204232
return new SanitizedDisplayText(
205233
text,
206234
Truncated: false,
207235
Oversized: false,
208-
Redacted: redacted);
236+
Redacted: redacted,
237+
UnsafeConcealment: unsafeConcealment);
209238
}
210239

211240
return new SanitizedDisplayText(
212241
TruncateUtf16Safe(text, MaxOutput) + TruncationMarker,
213242
Truncated: true,
214243
Oversized: false,
215-
Redacted: redacted);
244+
Redacted: redacted,
245+
UnsafeConcealment: unsafeConcealment);
216246
}
217247

218248
private static string TruncateUtf16Safe(string input, int maxLength)
@@ -283,7 +313,8 @@ private sealed record SanitizedDisplayText(
283313
string Text,
284314
bool Truncated,
285315
bool Oversized,
286-
bool Redacted);
316+
bool Redacted,
317+
bool UnsafeConcealment);
287318

288319
private sealed record StrippedView(string Text, int[] StrippedToOriginal);
289320
}

‎src/OpenClaw.Shared/ExecApprovals/ExecApprovalSecretRedactor.cs‎

Lines changed: 96 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -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),

‎src/OpenClaw.Shared/ExecApprovals/ExecApprovalV2UiPromptHandler.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ public async Task<ExecApprovalPromptOutcome> PromptAsync(
5757
_logger.Warn("[EXEC-APPROVALS] prompt: command exceeds display limit; denying (cannot review in full)");
5858
return ExecApprovalPromptOutcome.Deny;
5959
}
60-
if (commandStatus.Redacted)
60+
if (commandStatus.UnsafeConcealment)
6161
{
62-
_logger.Warn("[EXEC-APPROVALS] prompt: command contains redacted content; denying (cannot review in full)");
62+
_logger.Warn("[EXEC-APPROVALS] prompt: redaction would hide command syntax; denying (cannot review in full)");
6363
return ExecApprovalPromptOutcome.Deny;
6464
}
6565
var commandText = commandStatus.Text;

0 commit comments

Comments
 (0)