vanilla_respondage/views/subviews/security_macros.twig

58 lines
2.7 KiB
Twig
Raw Permalink Normal View History

{#
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 %}