Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1oAt97-0000eu-HG for mharc-grub-devel@gnu.org; Mon, 11 Jul 2022 09:08:17 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:55530) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oAt95-0000ZG-Ie for grub-devel@gnu.org; Mon, 11 Jul 2022 09:08:15 -0400 Received: from mail-qv1-xf2d.google.com ([2607:f8b0:4864:20::f2d]:39478) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1oAt93-0001lx-K7 for grub-devel@gnu.org; Mon, 11 Jul 2022 09:08:15 -0400 Received: by mail-qv1-xf2d.google.com with SMTP id nd6so299597qvb.6 for ; Mon, 11 Jul 2022 06:08:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:date:mime-version:user-agent:subject:content-language:to :references:from:in-reply-to:content-transfer-encoding; bh=6Yl7JmZ/OEMDoMis7vbAvepP5l5pi832+rknP/+vp3c=; b=EnmYGPrqCJjBafzzHdzGFE3yLg6WsKuE+Alfr6zYNqTJzsgINFgnkEQxuAFy59T90C 37K6fa7gjjyPKe0HdTl5W/1Dr9nRZ5FQ6Guq0J1R79ZM8NIjLraWFQFF//xydVlS77yT PdBJ8JzgJX28XNPIBcyKP26lLlkJfyZtYVC9aUMhL4s9V1j6FRLNQtNFCFwe3UfCO5O2 cyLqiugfJul0jW6nAkBMt5lLdETJ/sEOwBeb2amQNt52hBkBCTSNAfPIAnywBTWUdghF 4I24r5w5FGf4dB6BAggQBTCwW5z1BAFkd4SCp+r37lYLqnnYcuvwJpF+PjZM7EtrF/d2 Gkpg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:references:from:in-reply-to :content-transfer-encoding; bh=6Yl7JmZ/OEMDoMis7vbAvepP5l5pi832+rknP/+vp3c=; b=fF7PGriosoyGg0VdcGeEkBKhgHtk/5ENJHCSA50/yfZHuZtglQ39UnSQfHPMJtzpW+ tjiArBqMzcg5Kbf1+6n7ZCkb+Hgp850cTOtWrGfwT9Q4QRKp9ET7okKA3O4c/mVogYY3 skjGnLsQMJTvRDOzLXZmjWsdDnew3XyEBgwQJNy718FoMlbguJk5qzJu3YSYQSHV7x5p RF87t0AGHntLlBojzxzVeEesPWIubLh6uLEIdBPgkTp5oMiNHYsFOIJMWKOrBrHstR9z uOzTpr7Aff5pY+EL/aQPD26lZo310oykWtur8x118N/TINpvGrhXBW2q614yNjRG/FrF 2B9A== X-Gm-Message-State: AJIora/Ls6paEePhpBh2xlwSdQunUtejSR6tMwWLtkvv5a/3fbKfoEx4 TGmtDl1b4UOaxM86ONuSqI2H05MRtT4= X-Google-Smtp-Source: AGRyM1tIsGA/WgpRD+DSDoGso6GauRCFzu/JzJvI3t4jm9PawSAzfgG8ZQrIQSw/O/bc8GBKZd7tDw== X-Received: by 2002:ad4:5aee:0:b0:473:6a3f:bd4c with SMTP id c14-20020ad45aee000000b004736a3fbd4cmr4323980qvh.105.1657544891046; Mon, 11 Jul 2022 06:08:11 -0700 (PDT) Received: from ?IPV6:2602:47:d930:400:4a:b95f:f603:b11d? ([2602:47:d930:400:4a:b95f:f603:b11d]) by smtp.gmail.com with ESMTPSA id i5-20020ac87645000000b00304efba3d84sm5272826qtr.25.2022.07.11.06.08.09 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 11 Jul 2022 06:08:10 -0700 (PDT) Message-ID: Date: Mon, 11 Jul 2022 09:08:09 -0400 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.9.1 Subject: Re: [PATCH v5 1/2] json: Add function to unescape JSON-encoded strings Content-Language: en-US To: grub-devel@gnu.org References: From: Nicholas Vinson In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Received-SPF: pass client-ip=2607:f8b0:4864:20::f2d; envelope-from=nvinson234@gmail.com; helo=mail-qv1-xf2d.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_ENVFROM_END_DIGIT=0.25, FREEMAIL_FROM=0.001, NICE_REPLY_A=-0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 11 Jul 2022 13:08:15 -0000 On 7/11/22 06:44, Patrick Steinhardt wrote: > JSON strings require certain characters to be encoded, either by using a > single reverse solidus character "\" for a set of popular characters, or > by using a Unicode representation of "\uXXXXX". The jsmn library doesn't > handle unescaping for us, so we must implement this functionality for > ourselves. > > Add a new function `grub_json_unescape ()` that takes a potentially > escaped JSON string as input and returns a new unescaped string. > > Signed-off-by: Patrick Steinhardt > --- > grub-core/lib/json/json.c | 118 ++++++++++++++++++++++++++++++++++++++ > grub-core/lib/json/json.h | 12 ++++ > 2 files changed, 130 insertions(+) > > diff --git a/grub-core/lib/json/json.c b/grub-core/lib/json/json.c > index 1c20c75ea..1eadd1ce9 100644 > --- a/grub-core/lib/json/json.c > +++ b/grub-core/lib/json/json.c > @@ -262,3 +262,121 @@ grub_json_getint64 (grub_int64_t *out, const grub_json_t *parent, const char *ke > > return GRUB_ERR_NONE; > } > + > +grub_err_t > +grub_json_unescape (char **out, grub_size_t *outlen, const char *in, grub_size_t inlen) > +{ > + grub_err_t ret = GRUB_ERR_NONE; > + grub_size_t inpos, resultpos; > + char *result; > + > + if (out == NULL || outlen == NULL) > + return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("output parameters are not set")); > + > + result = grub_calloc (1, inlen + 1); > + if (result == NULL) > + return GRUB_ERR_OUT_OF_MEMORY; > + > + for (inpos = resultpos = 0; inpos < inlen; inpos++) > + { > + if (in[inpos] == '\\') > + { > + inpos++; > + if (inpos >= inlen) > + { > + ret = grub_error (GRUB_ERR_BAD_ARGUMENT, N_("expected escaped character")); > + goto err; > + } > + > + switch (in[inpos]) > + { > + case '"': > + result[resultpos++] = '"'; > + break; > + > + case '/': > + result[resultpos++] = '/'; > + break; > + > + case '\\': > + result[resultpos++] = '\\'; > + break; > + > + case 'b': > + result[resultpos++] = '\b'; > + break; > + > + case 'f': > + result[resultpos++] = '\f'; > + break; > + > + case 'r': > + result[resultpos++] = '\r'; > + break; > + > + case 'n': > + result[resultpos++] = '\n'; > + break; > + > + case 't': > + result[resultpos++] = '\t'; > + break; > + > + case 'u': > + { > + char values[4] = {0}; > + unsigned i; > + > + inpos++; > + if (inpos + ARRAY_SIZE(values) > inlen) > + { > + ret = grub_error (GRUB_ERR_BAD_ARGUMENT, N_("unicode sequence too short")); > + goto err; > + } I recommend relaxing these errors a little bit. While technically correct, I'm thinking it might serve GRUB better to be a bit more forgiving. Perhaps something like: if the input is '\u????' where ? is not a hex digit, set the value to 'u', and process ???? separately. if ???? is less than ARRAY_SIZE chars, left-pad ???? with 0s* until it's the correct length then convert the value. (* I don't mean to literally pad. I mean to treat the short number as if it was left-padded with 0s) > + > + for (i = 0; i < ARRAY_SIZE(values); i++) > + { > + char c = in[inpos++]; > + > + if (c >= '0' && c <= '9') > + values[i] = c - '0'; > + else if (c >= 'A' && c <= 'F') > + values[i] = c - 'A' + 10; > + else if (c >= 'a' && c <= 'f') > + values[i] = c - 'a' + 10; > + else > + { With a similar argument here. Treat the short ???? as suggested above and resume normal processing at the non-hex digit position. > + ret = grub_error (GRUB_ERR_BAD_ARGUMENT, > + N_("unicode sequence with invalid character '%c'"), c); > + goto err; > + } > + } > + > + if (values[0] != 0 || values[1] != 0) > + result[resultpos++] = values[0] << 4 | values[1]; > + result[resultpos++] = values[2] << 4 | values[3]; > + > + /* Offset the increment that's coming in via the loop increment. */ > + inpos--; > + > + break; > + } > + > + default: > + ret = grub_error (GRUB_ERR_BAD_ARGUMENT, N_("unrecognized escaped character '%c'"), in[inpos]); > + goto err; And the final prong of the suggestion would be here. Treat the improper escape as if it had not been escaped at all or as '\\?' where '?' is the escaped character. Thanks, Nicholas Vinson > + } > + } > + else > + result[resultpos++] = in[inpos]; > + } > + > + *out = result; > + *outlen = resultpos; > + > + err: > + if (ret != GRUB_ERR_NONE) > + grub_free (result); > + > + return ret; > +} > diff --git a/grub-core/lib/json/json.h b/grub-core/lib/json/json.h > index 4ea2a22d8..626074c35 100644 > --- a/grub-core/lib/json/json.h > +++ b/grub-core/lib/json/json.h > @@ -125,4 +125,16 @@ extern grub_err_t EXPORT_FUNC(grub_json_getint64) (grub_int64_t *out, > const grub_json_t *parent, > const char *key); > > +/* > + * Unescape escaped characters and Unicode sequences in the > + * given JSON-encoded string. Returns a newly allocated string > + * passed back via the `out` parameter that has a length of > + * `*outlen`. > + * > + * See https://datatracker.ietf.org/doc/html/rfc8259#section-7 for more > + * information on escaping in JSON. > + */ > +extern grub_err_t EXPORT_FUNC(grub_json_unescape) (char **out, grub_size_t *outlen, > + const char *in, grub_size_t inlen); > + > #endif > > > _______________________________________________ > Grub-devel mailing list > Grub-devel@gnu.org > https://lists.gnu.org/mailman/listinfo/grub-devel