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

Open Beginner friendly
#8,678 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

@Jeremiah-D is already working on this.

Since Nov 20, 2026.

  • #8699 by @Jeremiah-D — open

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
85/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
javascript
Domain
database, testing

Research direction

Read src/workerd/server/d1/d1.js and follow quote() to see how newline and carriage return escapes are chosen. Compare the collision handling described for shell.c.in and Miniflare's dumpSql.ts; then inspect d1-export-test.js. Add an export case containing both a real newline and a literal \n (and the corresponding carriage return case), and confirm the export preserves both values when imported.

Written by the indexing model from the issue text.

Description

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.

Dominant language
C++
Stars
9.3k
Forks
845
Avg merge
1d 23h
Merged PRs (30d)
558

Getting set up

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from cloudflare/workerd

All issues in cloudflare/workerd

Similar issues

More C++ issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.