Data filtering in iOS

Overview

Data filtering, or Sanitizing, is a feature which lets you modify or discard specific chunks of debug data. General description can be found here

Usage

Filter is a callback you assign to the AppSpectorConfig property httpSanitizer for HTTP Monitor or logSanitizer for Logs Monitor. This callback accepts an event as an argument and should return its modified version. To discard the event, set event.blocked property to true.

Examples

  • Remove authentication token from request headers:
[config.httpSanitizer setFilter:^ASHTTPEvent *(ASHTTPEvent *event) {
    if ([event.request.allHTTPHeaderFields.allKeys containsObject:@"YOUR-AUTH-HEADER"]) {
        [event.request setValue:@"redacted" forHTTPHeaderField:@"YOUR-AUTH-HEADER"];
    }

    return event;
}];
config.httpSanitizer.filter = { event in
    if event.request
        .allHTTPHeaderFields?
        .keys
        .contains("YOUR-AUTH-HEADER") == .some(true) {
        event.request.setValue("redacted",
                               forHTTPHeaderField: "YOUR-AUTH-HEADER")
    }
    return event
}
  • Set log level to WARN for all messages with word token:
[config.logSanitizer setFilter:^ASLogMonitorEvent *(ASLogMonitorEvent *event) {
    if ([event.message rangeOfString:@"token"].location != NSNotFound) {
        event.level = ASLogEventLevelWarn;
    }

    return event;
}];
config.logSanitizer.filter = { event in
    if event.message.range(of: "token") != .none {
        event.level = .error
    }
    return event
}
  • Ignore the HTTP event (note that it won't block the request itself, but it will not be processed by HTTP Monitor):
[config.httpSanitizer setFilter:^ASHTTPEvent *(ASHTTPEvent *event) {
    if ([event.request.allHTTPHeaderFields.allKeys containsObject:@"YOUR-AUTH-HEADER"]) {
        event.blocked = @YES
    }
    return event;
}];
config.httpSanitizer.filter = { event in
    if event.request
        .allHTTPHeaderFields?
        .keys
        .contains("YOUR-AUTH-HEADER") == .some(true) {
        event.blocked = true
    }
    return event
}