From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <pbs-devel-bounces@lists.proxmox.com>
Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68])
	by lore.proxmox.com (Postfix) with ESMTPS id 93E5F1FF166
	for <inbox@lore.proxmox.com>; Fri, 13 Sep 2024 15:10:37 +0200 (CEST)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
	by firstgate.proxmox.com (Proxmox) with ESMTP id CF0A3133A9;
	Fri, 13 Sep 2024 15:10:39 +0200 (CEST)
From: Gabriel Goller <g.goller@proxmox.com>
To: pbs-devel@lists.proxmox.com
Date: Fri, 13 Sep 2024 15:10:30 +0200
Message-Id: <20240913131033.396324-5-g.goller@proxmox.com>
X-Mailer: git-send-email 2.39.2
In-Reply-To: <20240913131033.396324-1-g.goller@proxmox.com>
References: <20240913131033.396324-1-g.goller@proxmox.com>
MIME-Version: 1.0
X-SPAM-LEVEL: Spam detection results:  0
 AWL -0.044 Adjusted score from AWL reputation of From: address
 BAYES_00                 -1.9 Bayes spam probability is 0 to 1%
 DMARC_MISSING             0.1 Missing DMARC policy
 KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment
 SPF_HELO_NONE           0.001 SPF: HELO does not publish an SPF Record
 SPF_PASS               -0.001 SPF: sender matches SPF record
 URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See
 http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more
 information. [docs.rs]
Subject: [pbs-devel] [PATCH proxmox v4 4/7] rest-server: add custom
 handlebars escape fn
X-BeenThere: pbs-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Proxmox Backup Server development discussion
 <pbs-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pbs-devel>, 
 <mailto:pbs-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pbs-devel/>
List-Post: <mailto:pbs-devel@lists.proxmox.com>
List-Help: <mailto:pbs-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel>, 
 <mailto:pbs-devel-request@lists.proxmox.com?subject=subscribe>
Reply-To: Proxmox Backup Server development discussion
 <pbs-devel@lists.proxmox.com>
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: 7bit
Errors-To: pbs-devel-bounces@lists.proxmox.com
Sender: "pbs-devel" <pbs-devel-bounces@lists.proxmox.com>

Add a custom handlebars escape function. It's nearly identical to the
default `html_escape` fn [0], but it does not escape the '='. This is
needed to support base64 encoded values.

[0]: https://docs.rs/handlebars/latest/handlebars/fn.html_escape.html

Signed-off-by: Gabriel Goller <g.goller@proxmox.com>
---
 proxmox-rest-server/src/api_config.rs | 28 ++++++++++++++++++++++++++-
 1 file changed, 27 insertions(+), 1 deletion(-)

diff --git a/proxmox-rest-server/src/api_config.rs b/proxmox-rest-server/src/api_config.rs
index ddc37f2253a6..c37e49d1f04f 100644
--- a/proxmox-rest-server/src/api_config.rs
+++ b/proxmox-rest-server/src/api_config.rs
@@ -62,7 +62,7 @@ impl ApiConfig {
             privileged_addr: None,
 
             #[cfg(feature = "templates")]
-            templates: Default::default(),
+            templates: templates::Templates::with_escape_fn(),
         }
     }
 
@@ -335,6 +335,32 @@ mod templates {
     }
 
     impl Templates {
+        pub fn with_escape_fn() -> Templates {
+            let mut registry = Handlebars::new();
+            // This is the same as the default `html_escape` fn in
+            // handlebars, **but** it does not escape the '='. This
+            // is to preserve base64 values.
+            registry.register_escape_fn(|value| {
+                let mut output = String::new();
+                for c in value.chars() {
+                    match c {
+                        '<' => output.push_str("&lt;"),
+                        '>' => output.push_str("&gt;"),
+                        '"' => output.push_str("&quot;"),
+                        '&' => output.push_str("&amp;"),
+                        '\'' => output.push_str("&#x27;"),
+                        '`' => output.push_str("&#x60;"),
+                        _ => output.push(c),
+                    }
+                }
+                output
+            });
+            Self {
+                templates: RwLock::new(registry),
+                template_files: RwLock::new(HashMap::new()),
+            }
+        }
+
         pub fn register<P>(&self, name: &str, path: P) -> Result<(), Error>
         where
             P: Into<PathBuf>,
-- 
2.39.2



_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel