gradio
6ec23fba - Fix `JSONDecodeError` when loading cached examples with negative number outputs (#13592)

Commit
126 days ago
Fix `JSONDecodeError` when loading cached examples with negative number outputs (#13592) * Don't CSV-escape negative number values sanitize_value_for_csv prepends a quote to strings starting with "-", so cached gr.Number/gr.Slider/gr.JSON outputs like "-0.5678" were written to log.csv as "'-0.5678" and crashed read_from_flag's json.loads on example click (JSONDecodeError). Extend the JSON special case from #12499 to values starting with "-": a leading "-" is only valid JSON for a negative number, which is not a formula injection vector, so it is safe to leave unescaped. Non-JSON strings starting with "-" are still escaped as before. Fixes #13591 * add changeset * add changeset * Validate negative numbers without re-serializing Per review: return the original string unchanged instead of json.dumps(json.loads(value)), so sanitization never reformats values (e.g. "-1e-5" -> "-1e-05"). Only the JSON-number validation gates the pass-through. * Read escaped negative numbers from old caches Caches written before the sanitize fix still contain negative numbers escaped with a leading "'", so they kept crashing with JSONDecodeError even after upgrading, until the cache was regenerated. Add a read-side fallback: read_from_flag for Number, Slider, and JSON now strips the escape character and retries when json.loads fails on a value starting with "'". json.dumps never emits a leading "'", so the fallback cannot misread a legitimate value. * Revert sanitize change, keep read-side fix Per review feedback, leave sanitize_value_for_csv untouched to avoid any security implications in the write path. The read-side fallback alone fixes the crash and covers both existing caches and newly written ones, so drop the write-side tests and fold the stale-cache test into test_caching_negative_number, which now exercises the escaped round trip end to end. * Rename parse_flagged_json to parse_escaped_json --------- Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
Author
Parents
Loading