D1 export: `quote()` in d1.js turns a literal `\n` into a newline when the value also has a real newline

Đang mở Phù hợp với người mới
#8,678 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

@Jeremiah-D đang làm issue này rồi.

Từ ngày 20/11/2026.

  • #8699 của @Jeremiah-D — đang mở

Đá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
85/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
javascript
Lĩnh vực
database, testing

Hướng nghiên cứu

Đọc src/workerd/server/d1/d1.js và theo dõi quote() để xem cách chọn chuỗi escape cho ký tự xuống dòng và ký tự về đầu dòng. So sánh cách xử lý va chạm được mô tả cho shell.c.in và dumpSql.ts của Miniflare; sau đó kiểm tra d1-export-test.js. Thêm một trường hợp xuất chứa cả ký tự xuống dòng thật lẫn \n dạng literal (và trường hợp tương ứng với ký tự về đầu dòng), rồi xác nhận rằng bản xuất giữ nguyên cả hai giá trị khi được nhập.

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

Mô tả

The new built-in D1 (src/workerd/server/d1/d1.js, from #8486) has the same export escaping bug that was reported against D1 in cloudflare/workers-sdk#9042 (internal ticket CFSQL-1294). Remote D1 has since fixed it, and cloudflare/workers-sdk#15334 fixes it in Miniflare's dumpSql.ts. But cloudflare/workers-sdk#16955 replaces dumpSql.ts with this code, so once that lands wrangler d1 export --local will have the bug again.

The problem is in quote(), which uses the same logic as the old dumpSql.ts (where this reproduces on wrangler 4.154.0):

let out = `'${text.replace(/'|(\n)|(\r)/g, (_, n, r) => {
  if (n) return ((lf = true), '\\n');
  if (r) return ((cr = true), '\\r');
  return "''";
})}'`;
if (cr) out = `replace(${out},'\\r',char(13))`;
if (lf) out = `replace(${out},'\\n',char(10))`;

Each real newline becomes the two characters \n, but a \n already in the value is left as-is. The replace() wrapper can't tell them apart, so on import both turn into newlines.

Example: the value 'a' || char(10) || 'b\nc' (hex 610A625C6E63) exports as

replace('a\nb\nc','\n',char(10))

which reads back as 610A620A63. The backslash-n is gone. \r has the same problem.

The comment says this follows shell.c.in's output_quoted_escaped_string, but the shell version first checks whether the value already contains the escape and picks one it doesn't (\n, then \012, then (\n0), (\n1), ...). Remote D1 export does the same thing now, e.g. the value above exports from remote as:

replace('a\012b\nc','\012',char(10))

So the fix is probably to port that collision check, like cloudflare/workers-sdk#15334 does for Miniflare. The export test in d1-export-test.js only has newlines with no literal backslash-n, which is why it doesn't catch this. A row with both would.

Ngôn ngữ chính
C++
Star
9.3k
Fork
845
Merge trung bình
1 ngày 23 giờ
Pull request đã merge (30 ngày)
558

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

Mở trong Codespaces

Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.

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 cloudflare/workerd

Tất cả issue của cloudflare/workerd

Issue tương tự

Thêm issue về C++

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.