Skip to content

Commit 56f7aa8

Browse files
committed
implement body redaction
1 parent d4b1036 commit 56f7aa8

File tree

2 files changed

+81
-2
lines changed

2 files changed

+81
-2
lines changed

supabase/lib/src/sentry_supabase_client.dart

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,15 @@ import 'operation.dart';
33
import 'package:sentry/sentry.dart';
44
import 'dart:convert';
55

6+
typedef SentrySupabaseRedactRequestBody = String? Function(
7+
String table,
8+
String key,
9+
String value,
10+
);
11+
612
class SentrySupabaseClient extends BaseClient {
713
final bool _breadcrumbs;
14+
final SentrySupabaseRedactRequestBody? _redactRequestBody;
815
final Client _client;
916
final Hub _hub;
1017

@@ -40,9 +47,11 @@ class SentrySupabaseClient extends BaseClient {
4047

4148
SentrySupabaseClient({
4249
required bool breadcrumbs,
50+
SentrySupabaseRedactRequestBody? redactRequestBody,
4351
Client? client,
4452
Hub? hub,
4553
}) : _breadcrumbs = breadcrumbs,
54+
_redactRequestBody = redactRequestBody,
4655
_client = client ?? Client(),
4756
_hub = hub ?? HubAdapter();
4857

@@ -71,7 +80,13 @@ class SentrySupabaseClient extends BaseClient {
7180

7281
final bodyString =
7382
request is Request && request.body.isNotEmpty ? request.body : null;
74-
final body = bodyString != null ? jsonDecode(bodyString) : null;
83+
var body = bodyString != null ? jsonDecode(bodyString) : null;
84+
85+
if (_redactRequestBody != null) {
86+
for (final entry in body?.entries ?? []) {
87+
body[entry.key] = _redactRequestBody(table, entry.key, entry.value);
88+
}
89+
}
7590

7691
if (_breadcrumbs) {
7792
_addBreadcrumb(description, operation, query, body);

supabase/test/sentry_supabase_client_test.dart

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,67 @@ void main() {
183183

184184
expect(fixture.mockHub.addBreadcrumbCalls.length, 0);
185185
});
186+
187+
test('redact request body', () async {
188+
final sentrySupabaseClient = fixture.getSut(
189+
redactRequestBody: (table, key, value) {
190+
switch (key) {
191+
case "password":
192+
return "<redacted>";
193+
case "token":
194+
return "<nope>";
195+
case "secret":
196+
return "<uwatm8>";
197+
case "null-me":
198+
return null;
199+
default:
200+
{
201+
return value;
202+
}
203+
}
204+
},
205+
);
206+
final supabase = SupabaseClient(
207+
supabaseUrl,
208+
supabaseKey,
209+
httpClient: sentrySupabaseClient,
210+
);
211+
212+
try {
213+
await supabase.from("mock-table").insert(
214+
{'user': 'picklerick', 'password': 'whoops', 'null-me': 'foo'});
215+
} catch (e) {
216+
print(e);
217+
}
218+
219+
try {
220+
await supabase
221+
.from("mock-table")
222+
.upsert({'user': 'picklerick', 'token': 'whoops'});
223+
} catch (e) {
224+
print(e);
225+
}
226+
227+
try {
228+
await supabase
229+
.from("mock-table")
230+
.update({'user': 'picklerick', 'secret': 'whoops'}).eq("id", 42);
231+
} catch (e) {
232+
print(e);
233+
}
234+
235+
expect(fixture.mockHub.addBreadcrumbCalls.length, 3);
236+
final inserted = fixture.mockHub.addBreadcrumbCalls[0].$1;
237+
expect(inserted.data?['body'],
238+
{'user': 'picklerick', 'password': '<redacted>', 'null-me': null});
239+
240+
final upserted = fixture.mockHub.addBreadcrumbCalls[1].$1;
241+
expect(upserted.data?['body'], {'user': 'picklerick', 'token': '<nope>'});
242+
243+
final updated = fixture.mockHub.addBreadcrumbCalls[2].$1;
244+
expect(
245+
updated.data?['body'], {'user': 'picklerick', 'secret': '<uwatm8>'});
246+
});
186247
});
187248
}
188249

@@ -193,11 +254,14 @@ class Fixture {
193254
final mockClient = MockClient();
194255
final mockHub = MockHub();
195256

196-
SentrySupabaseClient getSut({bool breadcrumbs = true}) {
257+
SentrySupabaseClient getSut(
258+
{bool breadcrumbs = true,
259+
SentrySupabaseRedactRequestBody? redactRequestBody}) {
197260
return SentrySupabaseClient(
198261
breadcrumbs: breadcrumbs,
199262
client: mockClient,
200263
hub: mockHub,
264+
redactRequestBody: redactRequestBody,
201265
);
202266
}
203267
}

0 commit comments

Comments
 (0)