From 046db49b5025570fb8616bcca491e5f77d5279d3 Mon Sep 17 00:00:00 2001 From: Felipe Lalanne Date: Tue, 31 Mar 2026 12:25:07 -0300 Subject: [PATCH] fix: Escape quotes and backslashes when quoting whitespace QuotedWhitespace adds a quote (`"`) around whitespaced strings, but it doesn't escape existing quotes inside the string leading to potentially wrong metadata being passed to entities created via quadlets. This escapes existing quotes and backslashes to ensure the generated output matches the intention of the user. Change-type: patch Fixes: containers/podlet#202 Signed-off-by: Felipe Lalanne --- src/serde/quadlet.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/serde/quadlet.rs b/src/serde/quadlet.rs index dc3fb07..d9b23d4 100644 --- a/src/serde/quadlet.rs +++ b/src/serde/quadlet.rs @@ -37,6 +37,8 @@ impl Display for QuoteWhitespace<'_> { for char in self.0.chars() { match char { '\n' => f.write_str(r"\n")?, + '"' => f.write_str(r#"\""#)?, + '\\' => f.write_str(r"\\")?, char => f.write_char(char)?, } } @@ -917,5 +919,11 @@ mod tests { let quoted = QuoteWhitespace("test1\ntest2"); assert_eq!(quoted.to_string(), r#""test1\ntest2""#); + + let quoted = QuoteWhitespace(r#"key={"foo": "bar"}"#); + assert_eq!(quoted.to_string(), r#""key={\"foo\": \"bar\"}""#); + + let quoted = QuoteWhitespace(r"path=C:\Users\test dir"); + assert_eq!(quoted.to_string(), r#""path=C:\\Users\\test dir""#); } }