Skip to content

Bearer-token redaction is bypassed for the log file and Sentry #124

Closed

Description

@zhollis21

Problem

ErrorReportService.Record redacts Bearer tokens out of the string it returns to the UI, but hands
the raw exception to the two sinks that actually persist and transmit:

var summary = $"{context}: {ex.Message}";
var details = $"{context}{Environment.NewLine}{ex}";

summary = Redact(summary);                     // computed, redacted... then never used
details = Redact(details);                     // redacted, returned to the caller

_logger.LogError(ex, "{Context}", context);    // RAW exception -> file log + logcat
SentrySdk.CaptureException(ex);                // RAW exception -> off-device

return details;

So redaction covers the on-screen error panel — the least sensitive sink, on the user's own device —
while the rotating file log and Sentry receive the unredacted text.

summary being computed, redacted and then discarded suggests this was wiring that got lost rather
than a deliberate choice.

The vector is live

AniListClient embeds up to 500 characters of the raw HTTP response body into the exception message:

// src/AniSprinkles.Core/Services/AniListClient.cs:864
var fallback = content?.Length > 500 ? content[..500] + "..." : content;
throw new AniListApiException(
    kind,
    apiMessage ?? $"AniList request failed ({(int)response.StatusCode}) for {operationName}. {fallback}");

An auth-failure body that echoes the credential (Invalid token: Bearer eyJ...) therefore lands in
AniListApiException.Message, and from there in both the log file and Sentry.

The redaction regex — Bearer\s+[A-Za-z0-9\-\._~\+\/]+=* — is written to match exactly that shape in
free text, which suggests this response-body path is the case it was added for.

Two things that are not the problem, checked while investigating:

  • LoggingHandler logs method and URI only, never headers.
  • SendDefaultPii = false in MauiProgram governs IP/username, not exception message content.

Why this matters more than it looks

#112 proposes a button that lets a user share the diagnostic log file. If tokens can reach that file
unredacted, that feature turns a passive gap into active exfiltration. This should land before
#112 does.

Proposal

Redact at the source rather than at each sink, so the token never enters the exception and every
downstream consumer is safe by construction:

  1. Redact the server-derived text in AniListClient before constructing AniListApiException, at
    all three sites: apiMessage (:856), fallback (:864), and graphQl.Errors[0].Message
    (:879) — the last is the HTTP-200 GraphQL error path, missed in the original write-up. Move the
    regex somewhere both it and ErrorReportService can share.
    Classification must run on the raw text and redaction only on the message that gets stored:
    AniListErrorClassifier matches on "Invalid token" / outage markers, none of which the Bearer
    regex touches, but keeping the order explicit means a future pattern cannot silently break it.
  2. Add a Sentry BeforeSend scrubber as defence in depth, for anything that reaches Sentry by a path
    that does not go through ErrorReportService.
  3. Delete the dead summary local, or use it — whichever the redesign settles on.

Sink-by-sink sanitising was considered and is worse: it means remembering to redact at every future
LogError(ex, ...) call site, and redacting the exception before handing it to Sentry costs the
stack trace that makes Sentry grouping work.

Acceptance

  • A response body containing Bearer <token> cannot reach the file log, logcat, or Sentry.
  • Regression tests for both server-message paths — the 4xx-with-token-in-body path and the
    200-with-GraphQL-error path — asserting the thrown exception's Message carries no raw token and
    its Kind is unchanged.
  • ErrorReportService has no unused redaction result.
  • Stack traces and Sentry grouping are unaffected.

Notes

Pre-existing; not introduced by #123, where ErrorReportService.cs moved as a 100%-similarity rename
with no content change. Found while triaging a Copilot review comment on that PR.

Activity

  1. added
    bug
    p2-high
    on Aug 22, 2026
  2. zhollis21 commented on Aug 23, 2026

    @zhollis21
    OwnerAuthor

    Verification notes (checked against origin/main @ 57d4506):

    • Confirmed as written. src/AniSprinkles.Core/Services/ErrorReportService.cs:20-30 — summary is
      built, redacted, and never read; _logger.LogError(ex, "{Context}", context) and
      SentrySdk.CaptureException(ex) both receive the raw exception.
    • A third server-derived message path the body does not name: AniListClient.cs:879 builds the
      exception message from graphQl.Errors[0].Message on an HTTP-200 GraphQL error response. That
      reaches AniListApiException.Message by the same route as the 4xx apiMessage (:856) and
      fallback (:864), so redaction at the source needs all three sites, not two.
    • ErrorReportService.Record is the only SentrySdk.CaptureException call site under src/; the
      other 27 SentrySdk.* calls are all AddBreadcrumb.
    • Re-confirmed the two non-problems: LoggingHandler logs method + URI only
      (LoggingHandler.cs:18,35,54), and its Sentry breadcrumbs use GetLeftPart(UriPartial.Path).
      AuthService never puts a token in a log message or an exception.
    • AGENTS.md:133 currently reads "ErrorReportService redacts Bearer tokens from all error output
      before logging or Sentry capture", which the code does not do. That line needs correcting with
      the fix.
    • No way to get diagnostics off a device, and Release logs only warnings anyway #112 is still open, so the ordering the body asks for is still available.
  3. zhollis21 commented on Aug 23, 2026

    @zhollis21
    OwnerAuthor

    Updated during planning: the proposal and acceptance now cover the third server-derived message site, graphQl.Errors[0].Message at AniListClient.cs:879 (the HTTP-200 GraphQL error path), which the original write-up missed. Also noted that classification has to run on the raw text with redaction applied only to the stored message, so AniListErrorClassifier keeps matching "Invalid token" and the outage markers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugp2-high

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions