Skip to content

Conversation

@mkysel
Copy link
Collaborator

@mkysel mkysel commented Apr 21, 2025

Fixes #727

Summary by CodeRabbit

  • New Features

    • Standardized and sanitized error messages are now returned for failed gRPC requests, improving consistency and security.
    • A new metric tracks the number of failed gRPC requests by error code, visible via the metrics endpoint.
  • Documentation

    • Added documentation for the new failed gRPC requests metric in the metrics catalog.

@mkysel mkysel requested review from a team as code owners April 21, 2025 14:48
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Apr 21, 2025

Warning

Rate limit exceeded

@mkysel has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 7 minutes and 35 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between df4816b and 35c60d5.

📒 Files selected for processing (3)
  • doc/metrics_catalog.md (1 hunks)
  • pkg/interceptors/server/logging.go (3 hunks)
  • pkg/metrics/api.go (3 hunks)

"""

Walkthrough

The changes introduce a new error sanitization mechanism for gRPC server interceptors to prevent internal error details from being exposed to clients. A sanitizeError function is added to standardize error messages returned by the API, replacing sensitive or internal information with generic messages. Additionally, a new Prometheus metric xmtp_api_failed_grpc_requests is implemented and registered, tracking the number of failed gRPC requests by error code. Documentation is updated to reflect the new metric. Furthermore, error returns in validation functions are converted to gRPC status errors with appropriate codes.

Changes

File(s) Change Summary
pkg/interceptors/server/logging.go Added sanitizeError function; updated unary and stream interceptors to use it for error returns.
pkg/metrics/api.go Introduced apiFailedGRPCRequestsCounter metric and EmitNewFailedGRPCRequest function.
pkg/metrics/metrics.go Registered the new apiFailedGRPCRequestsCounter metric with Prometheus collectors.
doc/metrics_catalog.md Documented the new xmtp_api_failed_grpc_requests metric.
pkg/api/message/service.go Converted validation errors to gRPC status errors with InvalidArgument code in validation funcs.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant gRPCServer
    participant LoggingInterceptor
    participant Metrics

    Client->>gRPCServer: Send gRPC request
    gRPCServer->>LoggingInterceptor: Handle request
    LoggingInterceptor->>LoggingInterceptor: Process request
    LoggingInterceptor->>LoggingInterceptor: If error, call sanitizeError()
    LoggingInterceptor->>Metrics: EmitNewFailedGRPCRequest(code)
    LoggingInterceptor->>gRPCServer: Return sanitized error
    gRPCServer->>Client: Respond with sanitized error
Loading

Assessment against linked issues

Objective Addressed Explanation
Sanitize API error returns to avoid leaking internal details (#727)

Suggested reviewers

  • neekolas
  • fbac
    """

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@macroscopeapp
Copy link

macroscopeapp bot commented Apr 21, 2025

Prevent exposure of internal error details by sanitizing gRPC API error responses in the logging interceptor

  • Implements error sanitization in the LoggingInterceptor through new sanitizeError function in logging.go that maps context errors to gRPC codes and replaces sensitive internal error messages with generic ones
  • Adds new xmtp_api_failed_grpc_requests_counter Prometheus metric in api.go to track failed gRPC requests by error code
  • Updates error handling in service.go to use status.Errorf with codes.InvalidArgument for client validation failures
  • Documents new metric in metrics_catalog.md

📍Where to Start

Start with the sanitizeError function in logging.go which contains the core error sanitization logic used by both unary and stream interceptors.


Macroscope summarized 35c60d5.

@mkysel mkysel changed the title Sanitize API error returns Sanitize gRPC API error responses to prevent exposure of internal error details in the logging interceptor Apr 21, 2025
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🧹 Nitpick comments (2)
pkg/metrics/api.go (1)

84-87: Guardrail: accept only valid gRPC codes or use the typed enum

EmitNewFailedGRPCRequest takes a free‑form string. A mistyped value elsewhere would silently create a new label series, inflating cardinality.
Consider either:

  1. Accepting a codes.Code and calling .String() inside the function, or
  2. Asserting/parsing the provided string against the small, fixed set of gRPC codes.

Example (option 1):

-func EmitNewFailedGRPCRequest(code string) {
-    apiFailedGRPCRequests.With(prometheus.Labels{"code": code}).Inc()
+func EmitNewFailedGRPCRequest(code codes.Code) {
+    apiFailedGRPCRequests.WithLabelValues(code.String()).Inc()
 }

This guarantees label values remain within the expected ~15‑code cardinality.

doc/metrics_catalog.md (1)

3-3: Update catalog after metric rename

After renaming the metric to xmtp_api_failed_grpc_requests_counter, reflect the change here to keep the documentation authoritative.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 1163f75 and 57c9674.

📒 Files selected for processing (4)
  • doc/metrics_catalog.md (1 hunks)
  • pkg/interceptors/server/logging.go (3 hunks)
  • pkg/metrics/api.go (2 hunks)
  • pkg/metrics/metrics.go (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
pkg/interceptors/server/logging.go (1)
pkg/metrics/api.go (1)
  • EmitNewFailedGRPCRequest (84-87)
⏰ Context from checks skipped due to timeout of 90000ms (4)
  • GitHub Check: Push Docker Images to GitHub Packages (xmtpd-cli)
  • GitHub Check: Push Docker Images to GitHub Packages (xmtpd)
  • GitHub Check: Test (Node)
  • GitHub Check: Upgrade Tests
🔇 Additional comments (1)
pkg/metrics/metrics.go (1)

89-90: Follow‑up required if metric is renamed

If you adopt the _counter suffix (see earlier comment), remember to update this reference (apiFailedGRPCRequests) or you will panic at startup with “collector already registered” / “nil reference” errors.

@mkysel mkysel merged commit cd69b97 into main Apr 21, 2025
8 checks passed
@mkysel mkysel deleted the mkysel/sanitize-error-return branch April 21, 2025 15:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Sanitize API error returns

4 participants