Skip to content

Conversation

itssimon
Copy link
Member

@itssimon itssimon commented Jun 23, 2025

Summary by CodeRabbit

  • New Features

    • Added support for masking sensitive fields within JSON request and response bodies using configurable patterns.
    • Introduced a new configuration option to specify which body fields to mask.
  • Refactor

    • Centralized and improved the masking logic for requests and responses, ensuring consistent application at write time.
  • Tests

    • Expanded and reorganized tests to cover new masking features, configuration options, and exclusion logic for improved reliability.

@itssimon itssimon self-assigned this Jun 23, 2025
Copy link

coderabbitai bot commented Jun 23, 2025

Walkthrough

The changes introduce enhanced masking capabilities for request and response bodies in the request logger, allowing recursive masking of sensitive JSON fields via configurable regex patterns. Masking logic is centralized and deferred to file write time. The test suite is refactored and expanded to cover new masking features, configuration options, and exclusion rules.

Changes

Files/Groups Change Summary
src/common/requestLogger.ts Added support for recursive masking of body fields using regex patterns, centralized masking logic, deferred masking to write time, and updated configuration and helper methods.
tests/common/requestLogger.test.ts Refactored and expanded tests to cover new masking features, configuration options, and exclusions, improving structure and maintainability.

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

npm error Exit handler never called!
npm error This is an error with npm itself. Please report this error at:
npm error https://github.com/npm/cli/issues
npm error A complete log of this run can be found in: /.npm/_logs/2025-06-23T09_23_35_354Z-debug-0.log


🪧 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.
    • Explain this complex logic.
    • 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 explain this code block.
    • @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 explain its main purpose.
    • @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.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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 sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai auto-generate unit tests to generate unit tests 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.

Copy link

@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: 0

🧹 Nitpick comments (2)
src/common/requestLogger.ts (2)

226-242: Consider adding circular reference protection.

While the recursive masking implementation is correct, it could throw a stack overflow error if the JSON contains circular references. Consider adding a visited set or maximum depth limit.


405-412: Consider a cleaner approach for base64 serialization.

While overriding toJSON on Buffer instances works, it modifies the object prototype which could have side effects. Consider wrapping the body in an object with explicit base64 encoding.

-// Set up body serialization for JSON
-[finalItem.request.body, finalItem.response.body].forEach((body) => {
-  if (body) {
-    // @ts-expect-error Override Buffer's default JSON serialization
-    body.toJSON = function () {
-      return this.toString("base64");
-    };
-  }
-});
+// Convert body buffers to base64 strings
+if (finalItem.request.body) {
+  finalItem.request.body = finalItem.request.body.toString("base64");
+}
+if (finalItem.response.body) {
+  finalItem.response.body = finalItem.response.body.toString("base64");
+}
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 62f2de8 and 26b4738.

📒 Files selected for processing (2)
  • src/common/requestLogger.ts (8 hunks)
  • tests/common/requestLogger.test.ts (1 hunks)
🔇 Additional comments (8)
src/common/requestLogger.ts (5)

58-68: Good selection of sensitive field patterns.

The regex patterns cover common sensitive fields with appropriate case-insensitive matching and character variations.


198-204: Smart tri-state return for JSON content type detection.

The method correctly handles missing content-type headers by returning null, which allows the caller to decide whether to attempt JSON parsing.


244-324: Well-structured centralized masking implementation.

The method correctly applies masking in the right order: callbacks → size limits → field masking. Good error handling for user callbacks and JSON parsing failures.


356-361: Good defensive validation for size values.

Setting negative sizes to undefined prevents logging invalid data.


363-377: Clean refactoring to defer masking.

Good separation of concerns by deferring masking to write time, which improves performance and maintainability.

tests/common/requestLogger.test.ts (3)

23-27: Good test hygiene with afterEach cleanup.

Properly closing the logger after each test prevents resource leaks and test interference.


29-71: Well-structured test helper functions.

The factory functions and getLoggedItems helper improve test readability and maintainability.


216-278: Excellent comprehensive test coverage for body field masking.

The test thoroughly validates the recursive masking feature with nested structures, arrays, and various data types. Good verification of both masked and non-masked fields.

Copy link

codecov bot commented Jun 23, 2025

Codecov Report

Attention: Patch coverage is 82.63889% with 25 lines in your changes missing coverage. Please review.

Project coverage is 88.56%. Comparing base (62f2de8) to head (26b4738).

Files with missing lines Patch % Lines
src/common/requestLogger.ts 82.63% 19 Missing and 6 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #118      +/-   ##
==========================================
+ Coverage   88.42%   88.56%   +0.14%     
==========================================
  Files          31       31              
  Lines        2557     2642      +85     
  Branches      354      375      +21     
==========================================
+ Hits         2261     2340      +79     
  Misses        281      281              
- Partials       15       21       +6     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@itssimon itssimon merged commit b497639 into main Jun 24, 2025
60 of 61 checks passed
@itssimon itssimon deleted the body-field-masking branch June 24, 2025 08:52
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.

1 participant