@stacksjs/stx 0.2.184 → 0.2.185

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -79,6 +79,35 @@ export declare function clearCustomFilters(): void;
79
79
  * HTML escape function to prevent XSS
80
80
  */
81
81
  export declare function escapeHtml(unsafe: string): string;
82
+ /**
83
+ * Escape a *value* for HTML output: the markup characters, and the template's
84
+ * own syntax with them.
85
+ *
86
+ * **A value is data, never a template.** The pipeline runs more than one
87
+ * expression pass over composed output - a loop body is processed per
88
+ * iteration and the finished page again, a section is spliced into its layout,
89
+ * an include into its parent - so whatever one pass emits, the next one reads.
90
+ * Without neutralising the opening braces here, a value of `{{ secret }}` is
91
+ * evaluated against the page's own scope on the second pass, and every field a
92
+ * person can type into becomes server-side template injection: a repository
93
+ * description, an issue title, a branch name.
94
+ *
95
+ * Reproduced in a real application before this existed: a description of
96
+ * `probe {{ 6*7 }} here` rendered as `probe 42 here`, and one of
97
+ * `{{ ownerHandle }}` printed the view's own local variable.
98
+ *
99
+ * **Only a brace that opens template syntax is encoded** - a run of `{`, or a
100
+ * `{` before `!`. A lone brace is left exactly as it was, so a JSON value, a
101
+ * CSS snippet, or prose about `{` renders byte for byte as it always did; this
102
+ * is a security fix and it should not rewrite everybody's output as the price.
103
+ *
104
+ * Character references rather than sentinel tokens, so there is nothing to
105
+ * restore afterwards and no code path that can leak a placeholder into a page.
106
+ * The DOM is unaffected: a reader sees `{{ secret }}`, an attribute parses back
107
+ * to `{`, and `JSON.parse` of a data attribute still works, because the browser
108
+ * decodes references before anything reads them.
109
+ */
110
+ export declare function escapeHtmlValue(unsafe: string): string;
82
111
  /**
83
112
  * Check if a template uses signals (state, derived, effect) in its script blocks
84
113
  * or has reactive attributes (@if, @for) with function call expressions
@@ -57,7 +57,7 @@ export const defaultFilters = {
57
57
  escape: (value, _context) => {
58
58
  if (value === void 0 || value === null)
59
59
  return "";
60
- return escapeHtml(String(value));
60
+ return escapeHtmlValue(String(value));
61
61
  },
62
62
  translate: (value, context, params = {}) => {
63
63
  if (!context || !context.__translations)
@@ -236,6 +236,9 @@ export function clearCustomFilters() {
236
236
  export function escapeHtml(unsafe) {
237
237
  return unsafe.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&#39;");
238
238
  }
239
+ export function escapeHtmlValue(unsafe) {
240
+ return escapeHtml(unsafe).replace(/\{{2,}/g, (match) => "&#123;".repeat(match.length)).replace(/\{(?=!)/g, "&#123;");
241
+ }
239
242
  export function usesSignalsInScript(template) {
240
243
  const { scripts } = stashScriptElements(template);
241
244
  for (const script of scripts) {
@@ -439,7 +442,7 @@ export function processExpressions(template, context, filePath) {
439
442
  return match;
440
443
  if (context.__stx_buildMode === "compile" && !hasSignals && (value === void 0 || expressionReadsServerBinding(trimmedExpr, context)))
441
444
  return createPlaceholder("expr", trimmedExpr);
442
- return value !== void 0 && value !== null ? escapeHtml(String(value)) : "";
445
+ return value !== void 0 && value !== null ? escapeHtmlValue(String(value)) : "";
443
446
  } catch (error) {
444
447
  if (firstVarIsClientSignal)
445
448
  return match;