-
Notifications
You must be signed in to change notification settings - Fork 7
Add masking for request and response body fields #118
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
WalkthroughThe 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
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
npm error Exit handler never called! 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed 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)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this 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
📒 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.
Codecov ReportAttention: Patch coverage is
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. 🚀 New features to boost your workflow:
|
Summary by CodeRabbit
New Features
Refactor
Tests