From f8fe53cba90f5b39ae4666aee97d053a421c61ef Mon Sep 17 00:00:00 2001 From: Yorhel Date: Sun, 16 Mar 2025 15:03:32 +0100 Subject: [PATCH] json_format: Add html_safe option --- FU/Util.pm | 27 ++++++++++++++++++++------- c/jsonfmt.c | 29 ++++++++++++++++------------- t/json_format.t | 1 + 3 files changed, 37 insertions(+), 20 deletions(-) diff --git a/FU/Util.pm b/FU/Util.pm index cd34fd3..a9cca1d 100644 --- a/FU/Util.pm +++ b/FU/Util.pm @@ -212,13 +212,6 @@ roughly similar to: JSON::PP->new->allow_nonref->core_bools->convert_blessed->encode($scalar); -Some modules escape the slash character in encoded strings to prevent a -potential XSS vulnerability when embedding JSON inside C<< >> tags. This function does I do that because it might not even -be sufficient. The following is probably an improvement: - - json_format($data) =~ s{>, C<< > >> and C<< & >> as Unicode escapes. +Commonly used to embed data inside a HTML page: + + $html = ''; + +This option does NOT make it safe to include the encoded JSON as an attribute +value. There is no way to do that without violating JSON specs, so you should +use entity escaping instead. + +Some JSON modules escape the forward slash (C) character instead, but that +is, at best, B sufficient for embedding inside a C<<