Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

templates: Forbid looping over non-arrays.

Anders Kaseorga80c52d6965b
diff --git a/web/src/templates.ts b/web/src/templates.ts
index 34187f5efcb..fdde7849a9b 100644
--- a/web/src/templates.ts
+++ b/web/src/templates.ts
@@ -10,6 +10,8 @@ import {user_settings} from "./user_settings.ts";
const orig_escape_expression = Handlebars.Utils.escapeExpression;
const orig_is_empty = Handlebars.Utils.isEmpty;
+const orig_each = Handlebars.helpers["each"];
+assert(orig_each !== undefined);
Handlebars.Utils.escapeExpression = (value: unknown): string => {
/* istanbul ignore if */
@@ -42,6 +44,16 @@ Handlebars.Utils.isEmpty = (value: unknown): boolean => {
return orig_is_empty(value);
};
+Handlebars.helpers["each"] = function (context, options): unknown {
+ /* istanbul ignore if */
+ if (!Array.isArray(context)) {
+ blueslip.error(
+ `Cannot loop over a value of type ${Object.prototype.toString.call(context)} in a Zulip Handlebars template`,
+ );
+ }
+ return orig_each.call(this, context, options);
+};
+
// Below, we register Zulip-specific extensions to the Handlebars API.
//
// IMPORTANT: When adding a new Handlebars helper, update the
diff --git a/web/templates/blueslip_stacktrace.hbs b/web/templates/blueslip_stacktrace.hbs
index 0ee27e64271..44841fa14d5 100644
--- a/web/templates/blueslip_stacktrace.hbs
+++ b/web/templates/blueslip_stacktrace.hbs
@@ -24,9 +24,11 @@
</div>
<div class="code-context" style="display: none">
<div class="code-context-content">
- {{~#each context~}}
- <div {{#if focus}}class="focus-line"{{/if}}><span class="line-number">{{ line_number }}</span> {{ line }}</div>
- {{~/each~}}
+ {{~#unless (eq context undefined)~}}
+ {{~#each context~}}
+ <div {{#if focus}}class="focus-line"{{/if}}><span class="line-number">{{ line_number }}</span> {{ line }}</div>
+ {{~/each~}}
+ {{~/unless~}}
</div>
</div>
</div>
diff --git a/web/tests/i18n.test.cjs b/web/tests/i18n.test.cjs
index e6f67e9f696..d8c90eec0d5 100644
--- a/web/tests/i18n.test.cjs
+++ b/web/tests/i18n.test.cjs
@@ -79,8 +79,18 @@ run_test("t_tag", ({mock_template}) => {
run_test("{{#tr}} to tag for translation", ({mock_template}) => {
const args = {
- notification_settings: {},
- settings_object: {},
+ general_settings: [],
+ notification_settings: {
+ desktop_notification_settings: [],
+ mobile_notification_settings: [],
+ email_message_notification_settings: [],
+ other_email_settings: [],
+ },
+ custom_stream_specific_notification_settings: [],
+ email_notifications_batching_period_values: [],
+ settings_object: {
+ available_notification_sounds: [],
+ },
settings_label: {
desktop_icon_count_display:
"Unread count badge (appears in desktop sidebar and browser tab)",
diff --git a/web/tests/templates.test.cjs b/web/tests/templates.test.cjs
index 6cd7f8145ce..581e54bf86f 100644
--- a/web/tests/templates.test.cjs
+++ b/web/tests/templates.test.cjs
@@ -134,7 +134,7 @@ run_test("list_each", ({override}) => {
`,
);
assert.equal(
- require("./templates/list_each.hbs")({stuff: {}}),
+ require("./templates/list_each.hbs")({stuff: []}),
`\
empty