From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gate001.proxmox.com (gate001.proxmox.com [45.144.208.40]) by lore.proxmox.com (Postfix) with ESMTPS id C9DA01FF0C2 for ; Thu, 22 Oct 2026 14:18:30 +0200 (CEST) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id 6EE5D217D2; Thu, 22 Oct 2026 14:17:23 +0200 (CEST) From: Lukas Wagner To: pbs-devel@lists.proxmox.com, pve-devel@lists.proxmox.com Subject: [PATCH proxmox v5 19/42] notify: matcher: ensure that field names match SAFE_ID_REGEX Date: Thu, 22 Oct 2026 14:15:44 +0200 Message-ID: <20261022121608.271515-20-l.wagner@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261022121608.271515-1-l.wagner@proxmox.com> References: <20261022121608.271515-1-l.wagner@proxmox.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1792671372784 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.269 Adjusted score from AWL reputation of From: address DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment (newer systems) RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Message-ID-Hash: KNXYJXSNQWS3APN7LWMGADPGHBOXXPW4 X-Message-ID-Hash: KNXYJXSNQWS3APN7LWMGADPGHBOXXPW4 X-MailFrom: l.wagner@proxmox.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.10 Precedence: list List-Id: Proxmox VE development discussion List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: The old 'match-field' options used their schema to validate the name, so it also makes sense to add additional checks for the JSON-based expression variant. Suggested-by: Michael Köppl Signed-off-by: Lukas Wagner --- Notes: New in v5 proxmox-notify/src/matcher/field.rs | 25 ++++++++++++---- proxmox-notify/src/matcher/mod.rs | 45 +++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 5 deletions(-) diff --git a/proxmox-notify/src/matcher/field.rs b/proxmox-notify/src/matcher/field.rs index cd23ff9c..ac7da37d 100644 --- a/proxmox-notify/src/matcher/field.rs +++ b/proxmox-notify/src/matcher/field.rs @@ -1,3 +1,4 @@ +use proxmox_schema::api_types::SAFE_ID_REGEX; use regex::Regex; use serde::{Deserialize, Serialize}; @@ -55,11 +56,25 @@ impl MatchDirective for FieldMatcher { } fn validate(&self) -> Result<(), Error> { - if let FieldMatcher::Exact { values, .. } = self { - if values.is_empty() { - return Err(Error::Generic( - "field matcher must contain values to match".to_string(), - )); + match self { + FieldMatcher::Exact { field, values } => { + if values.is_empty() { + return Err(Error::Generic( + "field matcher must contain values to match".to_string(), + )); + } + if !SAFE_ID_REGEX.is_match(field) { + return Err(Error::Generic( + "field name does not match regex pattern".to_string(), + )); + } + } + FieldMatcher::Regex { field, .. } => { + if !SAFE_ID_REGEX.is_match(field) { + return Err(Error::Generic( + "field name does not match regex pattern".to_string(), + )); + } } } diff --git a/proxmox-notify/src/matcher/mod.rs b/proxmox-notify/src/matcher/mod.rs index a3b97952..c74e3f28 100644 --- a/proxmox-notify/src/matcher/mod.rs +++ b/proxmox-notify/src/matcher/mod.rs @@ -534,4 +534,49 @@ mod tests { assert!(config.ensure_valid().is_err()); } } + + #[test] + fn test_invalid_fields() { + let exact_field_matcher_template = r#" + { + "match": { + "type": "field", + "field": "%FIELD%", + "values": ["abc"] + } + }"#; + + let regex_field_matcher_template = r#" + { + "match": { + "type": "field", + "field": "%FIELD%", + "regex": "abc" + } + }"#; + + for (field, valid) in [ + ("some-field", true), + ("some_field", true), + ("some-", true), + ("some.field", true), + ("-some", false), + ("some%field", false), + ("", false), + ] { + for tpl in [exact_field_matcher_template, regex_field_matcher_template] { + let mut config = MatcherConfig { + name: "matcher".to_string(), + expression: Some(tpl.replace("%FIELD%", field)), + ..Default::default() + }; + + assert_eq!( + config.ensure_valid().is_ok(), + valid, + "{field} should be {valid}" + ); + } + } + } } -- 2.47.3