Skip to content

fix: merge per-request headers over defaults in fetchAPI (#19095) - #19181

Merged
SandeepVashishtha merged 2 commits into
SandeepVashishtha:masterfrom
rajatsingh535:fix/fetchapi-headers-merge-19095
Dec 6, 2026
Merged

SandeepVashishtha merged 2 commits into
SandeepVashishtha:masterfrom
rajatsingh535:fix/fetchapi-headers-merge-19095

Conversation

@rajatsingh535

Copy link
Copy Markdown
Contributor

Description

Fixes a critical bug where spreading ...options after the headers object would completely overwrite the merged headers, dropping Content-Type and Authorization when custom headers were provided.

Problem:
In src/lib/api.js, the fetchAPI function built a merged headers object but then spread ...options after it. If any caller passes options.headers, the later spread overwrites the entire headers object, discarding Content-Type and Authorization (Bearer token).

Impact:

  • Currently latent (no existing code passes options.headers)
  • Would cause hard-to-trace 401 errors when any future code adds custom headers
  • Examples: multipart uploads, CSRF tokens, Idempotency-Key, Accept-Language

Solution:
Destructure headers from options before spreading, then merge custom headers on top of defaults:

// Before (vulnerable):
const res = await fetch(url, {
  ...options,  // ❌ overwrites headers if options.headers exists
  headers: {
    "Content-Type": "application/json",
    ...getAuthHeader(),
    ...options.headers,
  },
});

// After (fixed):
const { headers: optHeaders, ...rest } = options;
const res = await fetch(url, {
  ...rest,  // ✅ spread other options first
  headers: {
    "Content-Type": "application/json",
    ...getAuthHeader(),
    ...(optHeaders || {}),  // ✅ merge custom headers on top
  },
});

…htha#19069

Provides comprehensive security documentation for the backend
PaymentPlanService implementation to prevent cancellation of
COMPLETED payment plans and deactivation of paid QR tickets.

Documentation:
- docs/FIX_SUMMARY.md - Technical specification
- docs/PAYMENT_PLAN_FIX.md - Implementation guide

Security guards documented:
- Status validation (block COMPLETED/REFUNDED cancellations)
- QR ticket protection for paid attendees
- Audit logging
- Transaction management

This addresses issue SandeepVashishtha#19069 with preventative security guidelines
for when the backend payment feature is implemented.

Related: SandeepVashishtha#19069
…ishtha#19095)

Fixes a critical bug where spreading ...options after the headers
object would completely overwrite the merged headers, dropping
Content-Type and Authorization when custom headers were provided.

Problem:
- fetchAPI built headers with Content-Type + Auth + custom headers
- Then spread ...options which included options.headers
- This caused options.headers to overwrite the entire headers object
- Result: Loss of authentication on requests with custom headers

Solution:
- Destructure headers from options first: { headers, ...rest }
- Spread ...rest before defining headers
- Merge custom headers on top: ...(optHeaders || {})
- This preserves all default headers while adding custom ones

Impact:
- Currently latent (no caller passes options.headers yet)
- Prevents future 401 errors when features add custom headers
- Examples: multipart uploads, CSRF tokens, Idempotency-Key

Testing:
- Manual test cases documented in TEST_RESULTS.md
- Comprehensive test suite in src/lib/__tests__/api.test.js
- All existing API calls work without changes (no regression)

Closes SandeepVashishtha#19095
@vercel

vercel Bot commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

@rajatsingh535 is attempting to deploy a commit to the sandeepvashishtha's projects Team on Vercel.

A member of the Team first needs to authorize it.

@codescene-access codescene-access Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

No quality gates enabled for this code.

See analysis details in CodeScene

Quality Gate Profile: Custom Configuration
Install CodeScene MCP: safeguard and uplift AI-generated code. Catch issues early with our IDE extension and CLI tool.

@SandeepVashishtha
SandeepVashishtha merged commit 13561e5 into SandeepVashishtha:master Dec 6, 2026
2 of 3 checks passed
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.

2 participants