Downgrade to SemiStrict in production

This commit is contained in:
Olivier 'reivilibre
2025-10-28 17:02:05 +00:00
parent 81e2f9a628
commit 00e051e67a
6 changed files with 46 additions and 7 deletions

View File

@@ -160,8 +160,14 @@ impl Options {
)?;
// Load and compile the templates
let templates =
templates_from_config(&config.templates, &site_config, &url_builder).await?;
let templates = templates_from_config(
&config.templates,
&site_config,
&url_builder,
// Don't use strict mode in production yet
false,
)
.await?;
shutdown.register_reloadable(&templates);
let http_client = mas_http::reqwest_client();

View File

@@ -65,8 +65,14 @@ impl Options {
&account_config,
&captcha_config,
)?;
let templates =
templates_from_config(&template_config, &site_config, &url_builder).await?;
let templates = templates_from_config(
&template_config,
&site_config,
&url_builder,
// Use strict mode in template checks
true,
)
.await?;
templates.check_render(clock.now(), &mut rng)?;
Ok(ExitCode::SUCCESS)

View File

@@ -52,8 +52,14 @@ impl Options {
)?;
// Load and compile the templates
let templates =
templates_from_config(&config.templates, &site_config, &url_builder).await?;
let templates = templates_from_config(
&config.templates,
&site_config,
&url_builder,
// Don't use strict mode on task workers for now
false,
)
.await?;
let mailer = mailer_from_config(&config.email, &templates)?;
test_mailer_in_background(&mailer, Duration::from_secs(30));

View File

@@ -232,6 +232,7 @@ pub async fn templates_from_config(
config: &TemplatesConfig,
site_config: &SiteConfig,
url_builder: &UrlBuilder,
strict: bool,
) -> Result<Templates, anyhow::Error> {
Templates::load(
config.path.clone(),
@@ -240,6 +241,7 @@ pub async fn templates_from_config(
config.translations_path.clone(),
site_config.templates_branding(),
site_config.templates_features(),
strict,
)
.await
.with_context(|| format!("Failed to load the templates at {}", config.path))

View File

@@ -176,6 +176,8 @@ impl TestState {
workspace_root.join("translations"),
site_config.templates_branding(),
site_config.templates_features(),
// Strict mode in testing
true,
)
.await?;

View File

@@ -71,6 +71,9 @@ pub struct Templates {
vite_manifest_path: Utf8PathBuf,
translations_path: Utf8PathBuf,
path: Utf8PathBuf,
/// Whether template rendering is in strict mode (for testing,
/// until this can be rolled out in production.)
strict: bool,
}
/// There was an issue while loading the templates
@@ -151,6 +154,7 @@ impl Templates {
translations_path: Utf8PathBuf,
branding: SiteBranding,
features: SiteFeatures,
strict: bool,
) -> Result<Self, TemplateLoadingError> {
let (translator, environment) = Self::load_(
&path,
@@ -159,6 +163,7 @@ impl Templates {
&translations_path,
branding.clone(),
features,
strict,
)
.await?;
Ok(Self {
@@ -170,6 +175,7 @@ impl Templates {
translations_path,
branding,
features,
strict,
})
}
@@ -180,6 +186,7 @@ impl Templates {
translations_path: &Utf8Path,
branding: SiteBranding,
features: SiteFeatures,
strict: bool,
) -> Result<(Arc<Translator>, Arc<minijinja::Environment<'static>>), TemplateLoadingError> {
let path = path.to_owned();
let span = tracing::Span::current();
@@ -206,7 +213,14 @@ impl Templates {
let mut loaded: HashSet<_> = HashSet::new();
let mut env = minijinja::Environment::new();
// Don't allow use of undefined variables
env.set_undefined_behavior(UndefinedBehavior::Strict);
env.set_undefined_behavior(if strict {
UndefinedBehavior::Strict
} else {
// For now, allow semi-strict, because we don't have total test coverage of
// tests and some tests rely on if conditions against sometimes-undefined
// variables
UndefinedBehavior::SemiStrict
});
let root = path.canonicalize_utf8()?;
info!(%root, "Loading templates from filesystem");
for entry in walkdir::WalkDir::new(&root)
@@ -277,6 +291,7 @@ impl Templates {
&self.translations_path,
self.branding.clone(),
self.features,
self.strict,
)
.await?;
@@ -526,6 +541,8 @@ mod tests {
translations_path,
branding,
features,
// Use strict mode in tests
true,
)
.await
.unwrap();