@@ -17,7 +17,7 @@ use camino::{Utf8Path, Utf8PathBuf};
1717use mas_i18n:: Translator ;
1818use mas_router:: UrlBuilder ;
1919use mas_spa:: ViteManifest ;
20- use minijinja:: Value ;
20+ use minijinja:: { UndefinedBehavior , Value } ;
2121use rand:: Rng ;
2222use serde:: Serialize ;
2323use thiserror:: Error ;
@@ -71,6 +71,9 @@ pub struct Templates {
7171 vite_manifest_path : Utf8PathBuf ,
7272 translations_path : Utf8PathBuf ,
7373 path : Utf8PathBuf ,
74+ /// Whether template rendering is in strict mode (for testing,
75+ /// until this can be rolled out in production.)
76+ strict : bool ,
7477}
7578
7679/// There was an issue while loading the templates
@@ -151,6 +154,7 @@ impl Templates {
151154 translations_path : Utf8PathBuf ,
152155 branding : SiteBranding ,
153156 features : SiteFeatures ,
157+ strict : bool ,
154158 ) -> Result < Self , TemplateLoadingError > {
155159 let ( translator, environment) = Self :: load_ (
156160 & path,
@@ -159,6 +163,7 @@ impl Templates {
159163 & translations_path,
160164 branding. clone ( ) ,
161165 features,
166+ strict,
162167 )
163168 . await ?;
164169 Ok ( Self {
@@ -170,6 +175,7 @@ impl Templates {
170175 translations_path,
171176 branding,
172177 features,
178+ strict,
173179 } )
174180 }
175181
@@ -180,6 +186,7 @@ impl Templates {
180186 translations_path : & Utf8Path ,
181187 branding : SiteBranding ,
182188 features : SiteFeatures ,
189+ strict : bool ,
183190 ) -> Result < ( Arc < Translator > , Arc < minijinja:: Environment < ' static > > ) , TemplateLoadingError > {
184191 let path = path. to_owned ( ) ;
185192 let span = tracing:: Span :: current ( ) ;
@@ -205,6 +212,15 @@ impl Templates {
205212 span. in_scope ( move || {
206213 let mut loaded: HashSet < _ > = HashSet :: new ( ) ;
207214 let mut env = minijinja:: Environment :: new ( ) ;
215+ // Don't allow use of undefined variables
216+ env. set_undefined_behavior ( if strict {
217+ UndefinedBehavior :: Strict
218+ } else {
219+ // For now, allow semi-strict, because we don't have total test coverage of
220+ // tests and some tests rely on if conditions against sometimes-undefined
221+ // variables
222+ UndefinedBehavior :: SemiStrict
223+ } ) ;
208224 let root = path. canonicalize_utf8 ( ) ?;
209225 info ! ( %root, "Loading templates from filesystem" ) ;
210226 for entry in walkdir:: WalkDir :: new ( & root)
@@ -275,6 +291,7 @@ impl Templates {
275291 & self . translations_path ,
276292 self . branding . clone ( ) ,
277293 self . features ,
294+ self . strict ,
278295 )
279296 . await ?;
280297
@@ -524,6 +541,8 @@ mod tests {
524541 translations_path,
525542 branding,
526543 features,
544+ // Use strict mode in tests
545+ true ,
527546 )
528547 . await
529548 . unwrap ( ) ;
0 commit comments