fix: normalize smart quotes in function execution body (#3169)

* fix: normalize smart quotes in function execution body

Safari substitutes curly quotes while typing, which makes JSON bodies
invalid (console#2872). Normalize typographic quotes on submit and
disable spellcheck on the body textarea.

Co-authored-by: chiragaggarwal5k <chiragaggarwal5k@gmail.com>

* fix: only repair smart quotes when they invalidate JSON

Address Greptile P1: unconditional normalization could rewrite intentional
typographic quotes in valid JSON string values or plain-text bodies.
Only normalize when the original body fails JSON.parse and the repaired
body succeeds.

Co-authored-by: chiragaggarwal5k <chiragaggarwal5k@gmail.com>

* fix: repair only curly double quotes in JSON bodies

Narrow Greptile follow-up: JSON delimiters are double quotes, so leave
apostrophes/single smart quotes untouched. Still only rewrite when the
original body is invalid JSON and the repaired body parses.

Co-authored-by: chiragaggarwal5k <chiragaggarwal5k@gmail.com>

---------

Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Co-authored-by: chiragaggarwal5k <chiragaggarwal5k@gmail.com>
This commit is contained in:
authored and GitHub committed 2026-08-19 10:49:13 +05:30
1 parent a723540585
commit bdfadb14ff
3 files changed
+83 -2

No files matched your search

+44 -1
View File
@@ -1,4 +1,10 @@
import { singular, camelize, capitalize } from '$lib/helpers/string';
import {
singular,
camelize,
capitalize,
normalizeSmartQuotes,
repairSmartQuotedJson
} from '$lib/helpers/string';
import { expect, test } from 'vitest';
/*
@@ -97,3 +103,40 @@ test('capitalize should handle strings with no lowercase letters', () => {
test('capitalize should handle strings with only one character', () => {
expect(capitalize('a')).toBe('A');
});
/*
NORMALIZE SMART QUOTES
*/
test('normalizeSmartQuotes replaces curly double quotes only', () => {
const curly = '{ \u201Ctest\u201D: \u2018value\u2019 }';
expect(normalizeSmartQuotes(curly)).toBe('{ "test": \u2018value\u2019 }');
});
test('normalizeSmartQuotes leaves ASCII quotes unchanged', () => {
expect(normalizeSmartQuotes('{ "test": \'value\' }')).toBe('{ "test": \'value\' }');
});
test('normalizeSmartQuotes handles empty input', () => {
expect(normalizeSmartQuotes('')).toBe('');
});
test('repairSmartQuotedJson fixes curly structural quotes', () => {
const curly = '{ \u201Ctest\u201D: \u201Cvalue\u201D }';
expect(repairSmartQuotedJson(curly)).toBe('{ "test": "value" }');
});
test('repairSmartQuotedJson leaves valid JSON with curly quotes in values', () => {
const valid = '{ "quote": "He said \u201Chello\u201D" }';
expect(repairSmartQuotedJson(valid)).toBe(valid);
});
test('repairSmartQuotedJson preserves apostrophes when repairing delimiters', () => {
const curly = '{ \u201Cname\u201D: \u201CO\u2019Brien\u201D }';
expect(repairSmartQuotedJson(curly)).toBe('{ "name": "O\u2019Brien" }');
});
test('repairSmartQuotedJson leaves non-JSON bodies unchanged', () => {
const plain = 'Hello \u201Cworld\u201D';
expect(repairSmartQuotedJson(plain)).toBe(plain);
});
+33
View File
@@ -127,3 +127,36 @@ export function hash(input: string | string[], delimiter: string = ','): string
}
return Math.abs(hash).toString(36);
}
/**
* Replace typographic / “smart” double quotes with ASCII `"`.
* Safari (and some other editors) substitute curly quotes while typing, which
* breaks JSON request bodies. Single quotes / apostrophes are left alone —
* JSON string delimiters are always double quotes.
*/
export function normalizeSmartQuotes(str: string): string {
if (!str) return str;
return str.replace(/[\u201C\u201D\u201E\u201F\u2033\u2036]/g, '"');
}
/**
* Fix Safari-style smart double quotes when they make an otherwise-JSON body
* invalid. Leaves already-valid JSON and non-JSON bodies unchanged so
* intentional typographic characters in string values or plain text are
* preserved. Only double quotes are rewritten (JSON delimiters).
*/
export function repairSmartQuotedJson(str: string): string {
if (!str) return str;
try {
JSON.parse(str);
return str;
} catch {
const normalized = normalizeSmartQuotes(str);
try {
JSON.parse(normalized);
return normalized;
} catch {
return str;
}
}
}
@@ -25,6 +25,7 @@
toLocaleTimeISO
} from '$lib/helpers/date';
import { last } from '$lib/helpers/array';
import { repairSmartQuotedJson } from '$lib/helpers/string';
import {
Accordion,
Alert,
@@ -100,7 +101,9 @@
.forProject(page.params.region, page.params.project)
.functions.createExecution({
functionId: func.$id,
body,
// Safari may substitute curly/smart quotes while typing JSON.
// Only rewrite when that made the body invalid JSON.
body: repairSmartQuotedJson(body),
async: true,
xpath: path,
method,
@@ -179,6 +182,7 @@
<InputTextarea
placeholder="Enter request body here..."
id="body"
spellcheck={false}
bind:value={body} />
</Layout.Stack>
</Accordion>
@@ -294,6 +298,7 @@
<InputTextarea
placeholder="Enter request body here..."
id="body"
spellcheck={false}
bind:value={body} />
</Layout.Stack>
</Accordion>