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

Aperta Adatta ai principianti
#8,678 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@Jeremiah-D ci sta già lavorando.

Dal 20/11/2026.

  • #8699 di @Jeremiah-D — aperta

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
85/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
javascript
Ambito
database, testing

Direzione di ricerca

Leggi src/workerd/server/d1/d1.js e segui quote() per vedere come vengono scelti gli escape per il carattere di nuova riga e il ritorno a capo. Confronta la gestione delle collisioni descritta per shell.c.in e dumpSql.ts di Miniflare; poi esamina d1-export-test.js. Aggiungi un caso di esportazione che contenga sia un carattere di nuova riga reale sia un \n letterale (e il caso corrispondente per il ritorno a capo) e conferma che l’esportazione conserva entrambi i valori quando vengono importati.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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.

Lingua principale
C++
Stelle
9.3k
Fork
845
Merge medio
1g 23h
PR unite (30g)
558

Preparare l'ambiente

Apri in Codespaces

Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di cloudflare/workerd

Tutte le issue di cloudflare/workerd

Issue simili

Altre issue su C++

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.