Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

fix(mobile): log redaction blanks plain-base64 bearer tokens, Basic auth and sign-in codes in URLs

deed7664d82516d
diff --git a/mobile/lib/logging/redact.dart b/mobile/lib/logging/redact.dart
index fdafab7cf..458a499b8 100644
--- a/mobile/lib/logging/redact.dart
+++ b/mobile/lib/logging/redact.dart
@@ -84,9 +84,22 @@ String redactSecretsInText(String input) {
typedef _Redaction = ({RegExp pattern, String Function(Match) replace});
final List<_Redaction> _textRedactions = [
- // `Bearer <token>` — the shape auth takes if it ever lands in output.
+ // `Bearer <token>` — the shape auth takes if it ever lands in output. The
+ // alphabet is RFC 6750's `b64token`, `+`, `/`, `~` and padding included: a
+ // narrower one cut an opaque plain-base64 token short at its first `+`, and
+ // left all of it on disk when that came before the eighth character.
(
- pattern: RegExp(r'(Bearer\s+)[A-Za-z0-9._\-]{8,}', caseSensitive: false),
+ pattern: RegExp(
+ r'(Bearer\s+)[A-Za-z0-9._~+/\-]{8,}=*',
+ caseSensitive: false,
+ ),
+ replace: (m) => '${m[1]}<redacted>',
+ ),
+ // `Basic <base64 of user:password>` — the other scheme an `Authorization`
+ // header carries, and a password outright. Case-sensitive, unlike the rule
+ // above, so "basic authentication" in prose stays readable.
+ (
+ pattern: RegExp(r'(Basic\s+)[A-Za-z0-9+/]{8,}=*'),
replace: (m) => '${m[1]}<redacted>',
),
// Vendor keys with a well-known prefix (OpenAI `sk-…`, Anthropic `sk-ant-…`).
@@ -103,10 +116,12 @@ final List<_Redaction> _textRedactions = [
),
replace: (m) => '${m[1]}<redacted>',
),
- // A `?…token=…` or `?…key=…` inside a logged URL.
+ // A `?…token=…` or `?…key=…` inside a logged URL — and the two a sign-in
+ // puts there: an OAuth redirect's one-time `code`, which is a credential
+ // until it is spent, and a password (`pass…`, `pwd`).
(
pattern: RegExp(
- r'([?&][^=\s&]*(?:token|key|secret|sig|signature)[^=\s&]*=)[^\s&]+',
+ r'([?&][^=\s&]*(?:token|key|secret|sig|signature|code|pass|pwd)[^=\s&]*=)[^\s&]+',
caseSensitive: false,
),
replace: (m) => '${m[1]}<redacted>',
diff --git a/mobile/test/logging/redact_test.dart b/mobile/test/logging/redact_test.dart
new file mode 100644
index 000000000..2699efc78
--- /dev/null
+++ b/mobile/test/logging/redact_test.dart
@@ -0,0 +1,118 @@
+import 'package:flutter_test/flutter_test.dart';
+import 'package:harness_mobile/logging/redact.dart';
+
+/// What reaches `~/.harness/logs`, where a line outlives whatever it was
+/// minted for by a fortnight. The frames and URLs worth logging are the ones
+/// most likely to carry a credential, so every shape one takes is pinned here.
+void main() {
+ group('a frame, by its keys', () {
+ test('a secret-named key is blanked, however it is spelled or nested', () {
+ final line = redactValue({
+ 'engine': 'codex',
+ 'apiKey': 'sk-live',
+ 'accessToken': 'eyJ…',
+ 'refresh_token': 'r',
+ 'Authorization': 'Bearer x',
+ 'turn': {'urls': 'turn:relay', 'username': 'u', 'credential': 'c'},
+ 'sessionId': 's-1',
+ 'cookie': 'a=b',
+ });
+
+ expect(
+ line,
+ '{engine: codex, apiKey: <redacted>, accessToken: <redacted>, '
+ 'refresh_token: <redacted>, Authorization: <redacted>, '
+ 'turn: {urls: turn:relay, username: u, credential: <redacted>}, '
+ 'sessionId: <redacted>, cookie: <redacted>}',
+ );
+ });
+
+ test('a list is a count, never its contents', () {
+ expect(
+ redactValue({
+ 'agents': [1, 2, 3],
+ 'one': ['x'],
+ }),
+ '{agents: [3 items], one: [1 item]}',
+ );
+ expect(redactValue(null), 'null');
+ });
+
+ test('a long value is clipped so one blob cannot bury the line', () {
+ final line = redactValue({'sdp': 'v' * 500});
+ expect(line, '{sdp: ${'v' * 120}…}');
+ });
+
+ test('the whole line has a budget of its own', () {
+ final frame = {for (var i = 0; i < 40; i++) 'field$i': 'x' * 100};
+ final line = summariseForLog(frame, maxLength: 300);
+ expect(line, hasLength(301));
+ expect(line, endsWith('…'));
+ expect(summariseForLog({'ok': true}), '{ok: true}');
+ });
+ });
+
+ group('free text', () {
+ test('a bearer token is blanked whatever alphabet it is written in', () {
+ expect(
+ redactSecretsInText(
+ 'Authorization: Bearer eyJhbGciOi.J9eyJzdWIi.sig-_x',
+ ),
+ 'Authorization: Bearer <redacted>',
+ );
+ // RFC 6750's b64token takes `+`, `/`, `~` and trailing `=` too — an
+ // opaque token in plain base64 used to leak whole, being cut short of
+ // the eight characters the rule wanted at its first `+`.
+ expect(
+ redactSecretsInText('Bearer abc123+def/ghi~jk=='),
+ 'Bearer <redacted>',
+ );
+ });
+
+ test('basic credentials are blanked as bearer ones are', () {
+ expect(
+ redactSecretsInText('Authorization: Basic dXNlcjpwYXNzd29yZA=='),
+ 'Authorization: Basic <redacted>',
+ );
+ });
+
+ test('vendor keys with a known prefix', () {
+ expect(
+ redactSecretsInText('key sk-ant-api03-AbCdEf123456 in use'),
+ 'key sk-<redacted> in use',
+ );
+ });
+
+ test('the value after any secret-named field, however punctuated', () {
+ expect(
+ redactSecretsInText('{"session_token": "abcdef123456", "ok": 1}'),
+ '{"session_token": "<redacted>", "ok": 1}',
+ );
+ expect(redactSecretsInText('password=hunter22'), 'password=<redacted>');
+ expect(redactSecretsInText('api_key: abcdef'), 'api_key: <redacted>');
+ // Too short to be a secret — `token: null` stays readable.
+ expect(redactSecretsInText('token: null'), 'token: null');
+ });
+
+ test('a credential in a URL query is blanked, and only its value', () {
+ expect(
+ redactSecretsInText('GET https://h.invalid/x?sig=abc&lang=vi'),
+ 'GET https://h.invalid/x?sig=<redacted>&lang=vi',
+ );
+ // An OAuth redirect's one-time code is a credential until it is spent.
+ expect(
+ redactSecretsInText('GET https://h.invalid/cb?code=4/0AbCd&state=s1'),
+ 'GET https://h.invalid/cb?code=<redacted>&state=s1',
+ );
+ expect(
+ redactSecretsInText('POST https://h.invalid/a?user=me&pwd=x1'),
+ 'POST https://h.invalid/a?user=me&pwd=<redacted>',
+ );
+ });
+
+ test('ordinary text is left alone', () {
+ const line = 'GET https://h.invalid/api/machines → 200 (118ms)';
+ expect(redactSecretsInText(line), line);
+ });
+ });
+}