Content-type Header Not Handled Correctly for JSON and Form Data with Charset Information

Đang mở
#1,646 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
55/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
javascript, node.js
Lĩnh vực
backend

Hướng nghiên cứu

Bắt đầu trong examples/middleware/bodyDecoder-middleware.js tại các dòng 47-53 và tái hiện request bằng cách sử dụng Content-type: application/json; charset=utf-8 hoặc dạng tương đương của header form-data. Xác minh rằng cả hai content type có tham số đều được decode và serialize chính xác, trong khi các content type thuần hiện có vẫn tiếp tục hoạt động.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

Description

In the file bodyDecoder-middleware.js, lines 47-53, the server incorrectly handles Content-type: application/json; charset=utf-8 headers and similarly formatted headers for form data (application/x-www-form-urlencoded). The existing code uses strict string comparison, causing issues when the Content-type header contains additional properties like charset.

Existing Code

// bodyDecoder-middleware.js, lines 47-53
if (contentType === 'application/json') {
  bodyData = JSON.stringify(req.body);
}

if (contentType === 'application/x-www-form-urlencoded') {
  bodyData = queryString.stringify(req.body);
}

This code only works for requests that send a pure Content-type: application/json or Content-type: application/x-www-form-urlencoded without any additional properties like charset.

Expected behavior

The code should be able to handle Content-type headers with additional parameters.

Standard Practice According to MDN

According to MDN's documentation on Content-Type, the header can contain additional parameters (like charset), separated by a semicolon. Therefore, using startsWith would align better with this standard practice.

Solution

A possible solution is to change the string comparison to check if the Content-type starts with the desired value. Below is the proposed change:

if (contentType.startsWith('application/json')) {
  bodyData = JSON.stringify(req.body);
}

if (contentType.startsWith('application/x-www-form-urlencoded')) {
  bodyData = queryString.stringify(req.body);
}

Steps to Reproduce

  1. Send a POST request with a Content-type: application/json; charset=utf-8 header and JSON payload.
  2. Observe that the existing code does not handle the body as expected.

Thank you for taking the time to look into this issue.

Ngôn ngữ chính
JavaScript
Star
14.1k
Fork
2k
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Chuẩn bị môi trường

Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của http-party/node-http-proxy

Tất cả issue của http-party/node-http-proxy

Issue tương tự

Thêm issue về JavaScript

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.