vanilla_respondage/views/subviews/security_macros.twig
Tammo ter Hark d8ee93a1c0 fix(security): sanitize theme option values at emission (S1) and escape imagefile option HTML (S2)
S1 (HIGH, SECURITY_REVIEW_20261129.md): LimeSurvey renders theme twig with
autoescape OFF, so stored option values are untrusted on the public survey
pages. New views/subviews/security_macros.twig provides validators (color,
length, keyword, fontslug, pxnum, iconcode) applied at every emission point:
the <style> block in custom_header.twig (incl. the resolved values of the
cm()/cmbg() chooser macros), the inline style="color:..." and font-<slug>
class attributes in all survey subviews, and the <body> font class +
data-thememode in the newly overridden layout_global.twig and
layout_survey_list.twig. Crafted values fall back to the configured
defaults; legitimate picker/enum/slug values pass unchanged.

S2 (MEDIUM): options/options.twig escapes group/preview/filepath/filename
with |e('html_attr')/|e('html') when building the imagefile <option> HTML.

Verified: twig compile of all changed templates, malicious-value renders
neutralized + legit renders unchanged, live surveys 216664/282267 render
correct colors with no twig errors, jsdom custom_js tests pass (options_*
tests still need /tmp fixtures, pre-existing), export_public.sh exit 0.

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
2026-11-30 11:27:38 +01:00

58 lines
2.7 KiB
Twig

{#
Security macros — S1 of SECURITY_REVIEW_20261129.md (approved by the owner).
LimeSurvey renders theme twig with autoescape OFF, so every theme option value
emitted into a <style> block or an HTML attribute is untrusted input: the
stored options JSON can be set via API/DB/import with arbitrary content, and
the survey pages are public (stored XSS for every respondent). These macros
validate AT EMISSION; crafted values fall back to the given default, so
legitimate values (hex colors from pickers, enum values from buttons,
font slugs, px/rem sizes) pass through unchanged while `</style>`, quotes,
braces, semicolons, parens etc. can never reach the output.
Import in a view with: {% import './subviews/security_macros.twig' as sec %}
('./' resolves against the theme's views/ root, like every other include).
NOTE: twig macros only see globals + their own arguments, so macros defined
in custom_header.twig itself (cm/cmbg) cannot call sec.* — they inline the
same validation instead.
#}
{# A CSS color value: #rgb/#rrggbb/#rrggbbaa hex, or a plain color keyword
(transparent, lightyellow, currentColor, ...). Anything else -> fallback. #}
{% macro color(value, fallback = '') -%}
{%- set v = value|default('') -%}
{{- (v matches '/^#[0-9a-fA-F]{3,8}$/' or v matches '/^[a-zA-Z]{3,20}$/') ? v : fallback|default('') -}}
{%- endmacro %}
{# A CSS length/percentage: 16px, 1.2rem, 2em, 90%, ... #}
{% macro length(value, fallback = '') -%}
{%- set v = value|default('') -%}
{{- v matches '/^[0-9]+([.][0-9]+)?(px|rem|em|%)$/' ? v : fallback|default('') -}}
{%- endmacro %}
{# A single CSS keyword/enum: solid, dashed, italic, fixed, tile, cover, ... #}
{% macro keyword(value, fallback = '') -%}
{%- set v = value|default('') -%}
{{- v matches '/^[a-zA-Z-]{1,30}$/' ? v : fallback|default('') -}}
{%- endmacro %}
{# A font-select slug, emitted as the class token font-<slug> inside a class
attribute: [a-zA-Z0-9_-] only; invalid values become "base" (no override). #}
{% macro fontslug(value) -%}
{%- set v = value|default('') -%}
{{- v matches '/^[a-zA-Z0-9_-]{1,40}$/' ? v : 'base' -}}
{%- endmacro %}
{# A unitless px number (the "px" is appended in the template): digits only. #}
{% macro pxnum(value, fallback = '0') -%}
{%- set v = value|default('') -%}
{{- v matches '/^[0-9]{1,4}$/' ? v : fallback|default('0') -}}
{%- endmacro %}
{# A check-icon unicode codepoint for the CSS content property (e.g. EB7A):
hex digits only; anything else -> fallback. #}
{% macro iconcode(value, fallback = 'EB7A') -%}
{%- set v = value|default('') -%}
{{- v matches '/^[0-9a-fA-F]{1,6}$/' ? v : fallback|default('EB7A') -}}
{%- endmacro %}