D1 export: `quote()` in d1.js turns a literal `\n` into a newline when the value also has a real newline
Maintainers usually reply within 1 day
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
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
Starts the project's dev container in your browser, under your own GitHub account.
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from cloudflare/workerd
-
Build fails on macOS 27: dyld refuses stripped Rust proc-macros (mis-aligned LINKEDIT string pool)Open
Difficulty 2/5 1-3 hours Newbie friendliness 73/100
cloudflare/workerd#8963 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
cloudflare/workerd#8807 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
cloudflare/workerd#8575 ·
Maintainers usually reply within 1 day
-
fix(types): ForwardableEmailMessage omits the documented canBeForwarded propertyPossibly taken @itsrayeedscodespace claimed this 19 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
cloudflare/workerd#8290 ·
Maintainers usually reply within 1 day
-
Streaming TextDecoder drops U+FEFF at the start of a chunk that follows an ASCII-only chunkMay be free again @oliarmak-web claimed this 19 days ago, and no pull request is open. Open
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
cloudflare/workerd#8279 · 1 comment ·
Maintainers usually reply within 1 day
All issues in cloudflare/workerd
Similar issues
-
Android Bug Core Drape
Difficulty 2/5 1-3 hours Newbie friendliness 77/100
organicmaps/organicmaps#14449 ·
Maintainers usually reply within 1 day
-
comp-codecs
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
ClickHouse/ClickHouse#135159 ·
Maintainers usually reply within 1 day
-
difficulty:easy func:python os:windows prio:high type:bug
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
NatronGitHub/Natron#2029 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 67/100
ardazishvili/Sota#114 ·
Maintainers usually reply within 1 day
-
`Rect::containsPoint` is not `const`, so a `const Rect` cannot be asked whether it holds a pointOpenNeeds: Triage :mag:
Difficulty 1/5 Under an hour Newbie friendliness 82/100
react/react-native#60349 ·
Maintainers usually reply within 1 day