From 200e0a00800b4c9a13deba86a26622ecba249098 Mon Sep 17 00:00:00 2001 From: Emery Hemingway Date: Sun, 5 Dec 2021 16:34:12 +0000 Subject: [PATCH] Do not leak null characters into the #"" encoding --- src/preserves.nim | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/preserves.nim b/src/preserves.nim index a22cee3..614c917 100644 --- a/src/preserves.nim +++ b/src/preserves.nim @@ -1039,7 +1039,11 @@ proc concat[E](result: var string; pr: Preserve[E]) = of pkString: result.add escapeJson(pr.string) of pkByteString: - if sequtils.any(pr.bytes, proc (b: byte): bool = b.char in {'\20'..'\21', '#'..'[', ']'..'~'}): + if pr.bytes.allIt(char(it) in {' '..'!', '#'..'~'}): + result.add("#\"") + result.add(cast[string](pr.bytes)) + result.add('"') + else: if pr.bytes.len > 64: result.add("#[") #]# result.add(base64.encode(pr.bytes)) @@ -1051,10 +1055,6 @@ proc concat[E](result: var string; pr: Preserve[E]) = result.add(alphabet[int(b shr 4)]) result.add(alphabet[int(b and 0xf)]) result.add('"') - else: - result.add("#\"") - result.add(cast[string](pr.bytes)) - result.add('"') of pkSymbol: result.add(escapeJsonUnquoted(pr.symbol)) of pkRecord: