Skip to content

fix(lib): prevent data leak and improve HTTP error handling robustness - #29866

Open
adwaitm1301 wants to merge 1 commit into
calcom:mainfrom
adwaitm1301:main
Open

fix(lib): prevent data leak and improve HTTP error handling robustness#29866
adwaitm1301 wants to merge 1 commit into
calcom:mainfrom
adwaitm1301:main

Conversation

@adwaitm1301

Copy link
Copy Markdown

This PR fixes two issues in packages/lib/errors.ts and packages/lib/fetch-wrapper.ts.

Changes:

  • Removed console.log() data leak in handleErrorsJson
  • Removed console.error in handleErrorsRaw
  • Added getResponseBody() helper with JSON .message extraction
  • Fixed getErrorBody() fallback for empty statusText (Node.js HTTP/2)
  • Added 21 unit tests covering both modules

@github-actions

Copy link
Copy Markdown
Contributor

Welcome to Cal.diy, @adwaitm1301! Thanks for opening this pull request.

A few things to keep in mind:

  • This is Cal.diy, not Cal.com. Cal.diy is a community-driven, fully open-source fork of Cal.com licensed under MIT. Your changes here will be part of Cal.diy — they will not be deployed to the Cal.com production app.
  • Please review our Contributing Guidelines if you haven't already.
  • Make sure your PR title follows the Conventional Commits format.

A maintainer will review your PR soon. Thanks for contributing!

@coderabbitai

coderabbitai Bot commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 784dd350-87cf-4d2f-8984-239fdbfcdc36

📥 Commits

Reviewing files that changed from the base of the PR and between 96bb13a and df95bd8.

📒 Files selected for processing (4)
  • packages/lib/errors.test.ts
  • packages/lib/errors.ts
  • packages/lib/fetch-wrapper.test.ts
  • packages/lib/fetch-wrapper.ts
🚧 Files skipped from review as they are similar to previous changes (4)
  • packages/lib/errors.test.ts
  • packages/lib/fetch-wrapper.ts
  • packages/lib/fetch-wrapper.test.ts
  • packages/lib/errors.ts

📝 Walkthrough

Walkthrough

Updated shared response handlers to parse error bodies safely, handle empty and gzip-compressed responses, and report JSON parsing failures. Updated the HTTP wrapper to preserve parsed error messages for failed requests and provide fallbacks for non-JSON bodies. Added Vitest coverage for response handling, unknown error conversion, HTTP methods, successful JSON responses, HTTP errors, and network failures.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the main changes: fixing data leakage and hardening HTTP error handling.
Description check ✅ Passed The description matches the implemented changes in errors.ts and fetch-wrapper.ts and their added tests.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 4

🧹 Nitpick comments (1)
packages/lib/fetch-wrapper.test.ts (1)

28-65: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Assert each helper’s request method and body.

The PUT, PATCH, and DELETE tests pass even if the wrapper sends the wrong method or omits the serialized body. Add mockFetch request assertions for every helper.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/lib/fetch-wrapper.test.ts` around lines 28 - 65, Expand the
assertions in the PUT, PATCH, and DELETE tests alongside the existing POST
assertion to verify mockFetch receives the correct HTTP method and serialized
request body for each helper. Use the request payload already supplied in each
test and preserve the existing response and result assertions.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/lib/errors.ts`:
- Around line 83-85: Replace generic Error instances in packages/lib/errors.ts
at lines 83-85, 90-91, and 99-101 with ErrorWithCode, assigning appropriate
structured codes for non-OK JSON responses, successful-response JSON parse
failures, and non-OK raw responses respectively. Preserve the existing response
status, body, and parse-error context in each error message.
- Around line 61-67: Update the JSON parsing logic in the error-message helper
to safely inspect the parsed value before accessing message. Return
parsed.message only when it is a non-empty string; otherwise consistently return
the raw body, including when the parsed value is null or another incompatible
shape.
- Around line 74-76: Update the response handling in the surrounding
error-parsing function to validate response.ok before entering the gzip branch,
so gzip-encoded 4xx/5xx responses follow the existing error path instead of
being parsed as Type. Preserve the successful gzip parsing behavior and add a
regression test covering a gzip error response.

In `@packages/lib/fetch-wrapper.ts`:
- Around line 3-9: Update getErrorBody to read the response body as text once,
then conditionally parse valid JSON while preserving non-JSON content. Ensure
parsed JSON is validated as a non-null object before returning it, and otherwise
return a message using body || statusText || String(status) so HttpError
creation remains safe.

---

Nitpick comments:
In `@packages/lib/fetch-wrapper.test.ts`:
- Around line 28-65: Expand the assertions in the PUT, PATCH, and DELETE tests
alongside the existing POST assertion to verify mockFetch receives the correct
HTTP method and serialized request body for each helper. Use the request payload
already supplied in each test and preserve the existing response and result
assertions.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: a5918fd6-c67d-4dd5-a690-9ddf541ebffc

📥 Commits

Reviewing files that changed from the base of the PR and between 3894f37 and 96bb13a.

📒 Files selected for processing (4)
  • packages/lib/errors.test.ts
  • packages/lib/errors.ts
  • packages/lib/fetch-wrapper.test.ts
  • packages/lib/fetch-wrapper.ts

Comment thread packages/lib/errors.ts
Comment thread packages/lib/errors.ts
Comment thread packages/lib/errors.ts Outdated
Comment thread packages/lib/fetch-wrapper.ts
- Remove console.log(data) leak from handleErrorsJson that exposed raw error bodies

- Remove console.error({ response }) and response.text().then(console.log) from handleErrorsRaw

- Add getResponseBody() helper: safely extracts body text, tries JSON.parse for .message

- Fix getErrorBody() fallback when response.statusText is empty (Node.js HTTP/2)

- Add comprehensive unit tests (21 tests) for errors.ts and fetch-wrapper.ts
@CLAassistant

CLAassistant commented Jul 29, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants