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>
This commit is contained in:
Tammo ter Hark 2026-11-30 11:27:38 +01:00
commit d8ee93a1c0
3 changed files with 322 additions and 36 deletions

View file

@ -25,7 +25,14 @@
old 4-way <el>textcolor dropdowns map to color1/color2 as well. #}
{% macro cm(mode, custom, opts) -%}
{%- set m = mode|default('') -%}
{{- m matches '/^#[0-9a-fA-F]{3,8}$/' ? m : (m == 'color1' or m == 'primary' ? opts.primarycolor|default('') : (m == 'color2' or m == 'secondary' ? opts.secondarycolor|default('') : (m == 'color3' ? opts.tertiarycolor|default('') : (m == 'defaultbackgroundcolor' ? opts.defaultbackgroundcolor|default('') : (m == 'other' ? custom|default('') : opts.fontcolor|default('')))))) -}}
{%- set r = m matches '/^#[0-9a-fA-F]{3,8}$/' ? m : (m == 'color1' or m == 'primary' ? opts.primarycolor|default('') : (m == 'color2' or m == 'secondary' ? opts.secondarycolor|default('') : (m == 'color3' ? opts.tertiarycolor|default('') : (m == 'defaultbackgroundcolor' ? opts.defaultbackgroundcolor|default('') : (m == 'other' ? custom|default('') : opts.fontcolor|default('')))))) -%}
{#- S1 (SECURITY_REVIEW_20261129.md): validate the RESOLVED value as well — it can
come from another raw option (primarycolor, *custom, legacy stored value).
Hex or plain color keyword only; anything else resolves to '' (the CSS
declaration is then dropped and the stylesheet default applies). Same
validation as sec.color() in security_macros.twig (twig macros cannot call
imported macros, so it is inlined here). -#}
{{- (r matches '/^#[0-9a-fA-F]{3,8}$/' or r matches '/^[a-zA-Z]{3,20}$/') ? r : '' -}}
{%- endmacro %}
{# Row 191 / row 178 plan P6: like cm(), but for BACKGROUND choosers — mode
"default" means "no override" (empty string), so the Bootstrap button default
@ -33,20 +40,25 @@
way as cm(). Used for the per-button background colors of the navigator. #}
{% macro cmbg(mode, custom, opts) -%}
{%- set m = mode|default('') -%}
{{- m matches '/^#[0-9a-fA-F]{3,8}$/' ? m : (m == 'color1' or m == 'primary' ? opts.primarycolor|default('') : (m == 'color2' or m == 'secondary' ? opts.secondarycolor|default('') : (m == 'color3' ? opts.tertiarycolor|default('') : (m == 'defaultbackgroundcolor' ? opts.defaultbackgroundcolor|default('') : (m == 'other' ? custom|default('') : ''))))) -}}
{%- set r = m matches '/^#[0-9a-fA-F]{3,8}$/' ? m : (m == 'color1' or m == 'primary' ? opts.primarycolor|default('') : (m == 'color2' or m == 'secondary' ? opts.secondarycolor|default('') : (m == 'color3' ? opts.tertiarycolor|default('') : (m == 'defaultbackgroundcolor' ? opts.defaultbackgroundcolor|default('') : (m == 'other' ? custom|default('') : ''))))) -%}
{#- S1: same resolved-value validation as cm() above. -#}
{{- (r matches '/^#[0-9a-fA-F]{3,8}$/' or r matches '/^[a-zA-Z]{3,20}$/') ? r : '' -}}
{%- endmacro %}
{% import _self as cmm %}
{# S1 (SECURITY_REVIEW_20261129.md): sanitizers for option values emitted raw below
(autoescape is OFF in LimeSurvey theme twig). #}
{% import './subviews/security_macros.twig' as sec %}
<style>
html, body {
font-size: {{ aSurveyInfo.options.fontsize }} !important;
font-size: {{ sec.length(aSurveyInfo.options.fontsize|default('16px'), '16px') }} !important;
}
{% if (aSurveyInfo.options.backgroundimage == "on" and imageSrc(aSurveyInfo.options.backgroundimagefile )) %}
body {
background-image: url({{ imageSrc(aSurveyInfo.options.backgroundimagefile ) }});
background-attachment: {{ aSurveyInfo.options.backgroundimagefixed }};
background-size: {{ aSurveyInfo.options.backgroundimagedisplay }};
background-attachment: {{ sec.keyword(aSurveyInfo.options.backgroundimagefixed|default('scroll'), 'scroll') }};
background-size: {{ sec.keyword(aSurveyInfo.options.backgroundimagedisplay|default('tile'), 'tile') }};
}
body .top-container {
@ -58,7 +70,7 @@
padding-bottom: 10px;
/*padding-top: 60px;!* now is redefine in JS to fit any title length *!*/
background-color:{{ cmm.cm(aSurveyInfo.options.bodybackgroundcolor|default('defaultbackgroundcolor'), aSurveyInfo.options.bodybackgroundcolorcustom|default(''), aSurveyInfo.options) }} ;
color: {{ aSurveyInfo.options.fontcolor }};
color: {{ sec.color(aSurveyInfo.options.fontcolor|default('#444444'), '#444444') }};
}
{# Row 207: the theme footer "Proudly developed by [logo]" — text in the default
@ -66,12 +78,12 @@
height 25px is static in css/custom.css. The footer itself is rendered by
views/subviews/footer/footer.twig on every page except the welcome page. #}
.respondage-theme-footer {
font-size: {{ aSurveyInfo.options.fontsize|default('16px') }};
color: {{ aSurveyInfo.options.secondarycolor|default('#ef476f') }};
font-size: {{ sec.length(aSurveyInfo.options.fontsize|default('16px'), '16px') }};
color: {{ sec.color(aSurveyInfo.options.secondarycolor|default('#ef476f'), '#ef476f') }};
}
.table {
color: {{ aSurveyInfo.options.fontcolor }};
color: {{ sec.color(aSurveyInfo.options.fontcolor|default('#444444'), '#444444') }};
}
{# Row 180 / plan P2: the question title now uses the color-chooser scheme —
@ -88,7 +100,7 @@
{% set sQTitleBackgroundColor = cmm.cm(aSurveyInfo.options.questiontitlebackgroundcolor|default('color1'), aSurveyInfo.options.questiontitlebackgroundcolorcustom|default(''), aSurveyInfo.options) %}
{% set sQTitleTextColor = aSurveyInfo.options.questiontitletextcolor is defined ? cmm.cm(aSurveyInfo.options.questiontitletextcolor, aSurveyInfo.options.questiontitletextcolorcustom|default(''), aSurveyInfo.options) : aSurveyInfo.options.questiontitlefontcolor|default('') %}
.question-title-container {
font-size: {{ aSurveyInfo.options.questiontitlefontsize }};
font-size: {{ sec.length(aSurveyInfo.options.questiontitlefontsize|default('1.2rem'), '1.2rem') }};
font-weight: {{ sQTitleFontOptions == 'bold' or sQTitleFontOptions == 'bolditalic' ? '700' : '400' }};
font-style: {{ sQTitleFontOptions == 'italic' or sQTitleFontOptions == 'bolditalic' ? 'italic' : 'normal' }};
background: {{ sQTitleBackgroundColor }};
@ -102,19 +114,19 @@
{% set sGroupTitleFontOptions = aSurveyInfo.options.grouptitlefontoptions|default(aSurveyInfo.options.grouptitlefontweight|default('700') == '700' ? 'bold' : 'normal') %}
{% set sGroupDescFontOptions = aSurveyInfo.options.groupdescriptionfontoptions|default(aSurveyInfo.options.groupdescriptionfontweight|default('400') == '700' ? 'bold' : 'normal') %}
.group-title {
font-size: {{ aSurveyInfo.options.grouptitlefontsize }};
font-size: {{ sec.length(aSurveyInfo.options.grouptitlefontsize|default('2.0rem'), '2.0rem') }};
font-weight: {{ sGroupTitleFontOptions == 'bold' or sGroupTitleFontOptions == 'bolditalic' ? '700' : '400' }};
font-style: {{ sGroupTitleFontOptions == 'italic' or sGroupTitleFontOptions == 'bolditalic' ? 'italic' : 'normal' }};
}
.group-description {
font-size: {{ aSurveyInfo.options.groupdescriptionfontsize }};
font-size: {{ sec.length(aSurveyInfo.options.groupdescriptionfontsize|default('1.2rem'), '1.2rem') }};
font-weight: {{ sGroupDescFontOptions == 'bold' or sGroupDescFontOptions == 'bolditalic' ? '700' : '400' }};
font-style: {{ sGroupDescFontOptions == 'italic' or sGroupDescFontOptions == 'bolditalic' ? 'italic' : 'normal' }};
}
.privacy-block, .privacy .form-check {
background-color: {{ aSurveyInfo.options.privacybackgroundcolor }};
background-color: {{ sec.color(aSurveyInfo.options.privacybackgroundcolor|default('lightyellow'), 'lightyellow') }};
}
{% if ( aSurveyInfo.options.brandlogofile|default('off') not in ['off', '', 'inherit'] and aSurveyInfo.options.brandlogo|default('on') != 'off' ) %}
@ -144,7 +156,7 @@
.save-form,
#surveys-list-container,
.privacy {
border: 1px {{ aSurveyInfo.options.questionborderstyle }} {{ cmm.cm(aSurveyInfo.options.questionbordercolor|default('default'), aSurveyInfo.options.questionbordercolorcustom|default('#cccccc'), aSurveyInfo.options) }};
border: 1px {{ sec.keyword(aSurveyInfo.options.questionborderstyle|default('solid'), 'solid') }} {{ cmm.cm(aSurveyInfo.options.questionbordercolor|default('default'), aSurveyInfo.options.questionbordercolorcustom|default('#cccccc'), aSurveyInfo.options) }};
}
{% endif %}
@ -154,7 +166,7 @@
.save-form,
#surveys-list-container,
.privacy {
border-top: 1px {{ aSurveyInfo.options.questionborderstyle }} {{ cmm.cm(aSurveyInfo.options.questionbordercolor|default('default'), aSurveyInfo.options.questionbordercolorcustom|default('#cccccc'), aSurveyInfo.options) }};
border-top: 1px {{ sec.keyword(aSurveyInfo.options.questionborderstyle|default('solid'), 'solid') }} {{ cmm.cm(aSurveyInfo.options.questionbordercolor|default('default'), aSurveyInfo.options.questionbordercolorcustom|default('#cccccc'), aSurveyInfo.options) }};
}
{% endif %}
@ -260,13 +272,13 @@
EXACT distance from the top of the header to the top of the logo —
0 px means the logo touches the top of the page. #}
#survey-nav .logo-left.logo-container {
margin-top: {{ aSurveyInfo.options.brandlogotopmargin|default('30')|replace({'px': ''}) }}px;
margin-top: {{ sec.pxnum(aSurveyInfo.options.brandlogotopmargin|default('30')|replace({'px': ''}), '30') }}px;
}
#survey-nav .logo-center.logo-container {
margin-top: {{ aSurveyInfo.options.centerlogotopmargin|default('30')|replace({'px': ''}) }}px;
margin-top: {{ sec.pxnum(aSurveyInfo.options.centerlogotopmargin|default('30')|replace({'px': ''}), '30') }}px;
}
#survey-nav .logo-right.logo-container {
margin-top: {{ aSurveyInfo.options.rightlogotopmargin|default('30')|replace({'px': ''}) }}px;
margin-top: {{ sec.pxnum(aSurveyInfo.options.rightlogotopmargin|default('30')|replace({'px': ''}), '30') }}px;
}
{# Row 138: header bar height (option headerheight, plain px number; the "px"
@ -274,7 +286,7 @@
as min-height so taller logos can still grow the bar — the sticky body
padding in scripts/custom.js follows the real nav height. #}
#survey-nav {
min-height: {{ aSurveyInfo.options.headerheight|default('90')|replace({'px': ''}) }}px;
min-height: {{ sec.pxnum(aSurveyInfo.options.headerheight|default('90')|replace({'px': ''}), '90') }}px;
}
{# Row 115 working example: navbarfontcolor is now a MODE dropdown
@ -283,7 +295,7 @@
other -> navbarfontcolorcustom. A raw hex value (legacy stored value from
before the dropdown existed, or a hand-set one) is used directly. #}
{% set sNavbarFontColorMode = aSurveyInfo.options.navbarfontcolor|default('default') %}
{% set sNavbarFontColor = sNavbarFontColorMode matches '/^#[0-9a-fA-F]{3,8}$/' ? sNavbarFontColorMode : (sNavbarFontColorMode == 'color1' ? aSurveyInfo.options.primarycolor|default('') : (sNavbarFontColorMode == 'color2' ? aSurveyInfo.options.secondarycolor|default('') : (sNavbarFontColorMode == 'color3' ? aSurveyInfo.options.tertiarycolor|default('') : (sNavbarFontColorMode == 'defaultbackgroundcolor' ? aSurveyInfo.options.defaultbackgroundcolor|default('') : (sNavbarFontColorMode == 'other' ? aSurveyInfo.options.navbarfontcolorcustom|default('') : aSurveyInfo.options.fontcolor|default('')))))) %}
{% set sNavbarFontColor = sec.color(sNavbarFontColorMode matches '/^#[0-9a-fA-F]{3,8}$/' ? sNavbarFontColorMode : (sNavbarFontColorMode == 'color1' ? aSurveyInfo.options.primarycolor|default('') : (sNavbarFontColorMode == 'color2' ? aSurveyInfo.options.secondarycolor|default('') : (sNavbarFontColorMode == 'color3' ? aSurveyInfo.options.tertiarycolor|default('') : (sNavbarFontColorMode == 'defaultbackgroundcolor' ? aSurveyInfo.options.defaultbackgroundcolor|default('') : (sNavbarFontColorMode == 'other' ? aSurveyInfo.options.navbarfontcolorcustom|default('') : aSurveyInfo.options.fontcolor|default('')))))), '#444444') %}
#survey-nav .navbar-nav > li > a,
#survey-nav .navbar-nav .nav-link {
color: {{ sNavbarFontColor }};
@ -344,7 +356,7 @@
}
.btn-primary {
background: {{ aSurveyInfo.options.primarycolor }};
background: {{ sec.color(aSurveyInfo.options.primarycolor|default('#26547c'), '#26547c') }};
}
.bg-primary{
@ -353,7 +365,7 @@
}
.btn-primary:hover, .btn-primary:focus {
background: {{ aSurveyInfo.options.primarycolor }};
background: {{ sec.color(aSurveyInfo.options.primarycolor|default('#26547c'), '#26547c') }};
}
.btn-check:active+.btn-primary,
@ -363,7 +375,7 @@
.show>.btn-primary.dropdown-toggle,
.show > .btn-primary.dropdown-toggle,
.btn-primary:focus {
background: {{ aSurveyInfo.options.primarycolor }};
background: {{ sec.color(aSurveyInfo.options.primarycolor|default('#26547c'), '#26547c') }};
}
{# Row 191 / row 178 plan P6: per-button colors for the navigator buttons
@ -401,7 +413,7 @@
}
{% endif %}
{% set checkicon = '"\\' ~ aSurveyInfo.options.checkicon ~ '"' %}
{% set checkicon = '"\\' ~ sec.iconcode(aSurveyInfo.options.checkicon|default('EB7A'), 'EB7A') ~ '"' %}
.checkbox-item input[type="checkbox"]:checked + label::after, .checkbox-item input[type="radio"]:checked + label::after {
content: {{ checkicon }};
}

View file

@ -36,6 +36,8 @@ For the survey navigator ("Next", "Back", etc.), see: navigation/navigator.twig
{%- endif -%}
{%- endmacro %}
{% import _self as navmacros %}
{# S1: sanitize option values at emission (autoescape is OFF) — see security_macros.twig #}
{% import './subviews/security_macros.twig' as sec %}
{% if not aSurveyInfo.popupPreview %}
{% set navClass = "navbar-light bg-light" %}
{% if( aSurveyInfo.template == "bootswatch") %}
@ -72,6 +74,7 @@ For the survey navigator ("Next", "Back", etc.), see: navigation/navigator.twig
Emitted as an inline style on #survey-nav below. #}
{% set sHeaderTextColorMode = aSurveyInfo.options.headertextcolor|default('default') %}
{% set sHeaderTextColor = sHeaderTextColorMode == 'primary' ? aSurveyInfo.options.primarycolor|default('') : (sHeaderTextColorMode == 'secondary' ? aSurveyInfo.options.secondarycolor|default('') : (sHeaderTextColorMode == 'other' ? aSurveyInfo.options.headertextcolorcustom|default('') : aSurveyInfo.options.fontcolor|default(''))) %}
{% set sHeaderTextColor = sec.color(sHeaderTextColor, '') %}
{# THEME ADDITION: sticky header per screen size (options
headersticky{computer,tablet,mobile}; Prompt L — the master key no longer
counts). When the checkbox options exist (theme was reset after adding
@ -79,7 +82,7 @@ For the survey navigator ("Next", "Back", etc.), see: navigation/navigator.twig
the media queries instead. Before a reset the old behaviour (always
fixed-top) is kept as fallback. #}
{% set bStickyConfigured = aSurveyInfo.options.headerstickycomputer is defined %}
<div id="survey-nav" class="navbar {{ navClass }} navbar-expand-md {% if not bStickyConfigured %}fixed-top{% endif %} border-bottom pt-0 pb-0 {% if sHeaderFont != 'base' %}font-{{ sHeaderFont }}{% endif %}"{% if sHeaderTextColor != '' %} style="color: {{ sHeaderTextColor }};"{% endif %}>
<div id="survey-nav" class="navbar {{ navClass }} navbar-expand-md {% if not bStickyConfigured %}fixed-top{% endif %} border-bottom pt-0 pb-0 {% if sHeaderFont != 'base' %}font-{{ sec.fontslug(sHeaderFont) }}{% endif %}"{% if sHeaderTextColor != '' %} style="color: {{ sHeaderTextColor }};"{% endif %}>
<div class="container-fluid d-flex flex-column">
{# ROW 1 — the logos (layout rules in sass/plain.sass): left logo always
far left, center logo always exactly centered (absolute, independent

View file

@ -20,19 +20,22 @@
{# THEME ADDITION: per-element font override (config.xml option "progressbarfont",
type fontselect). Value "base" = same as the base font: no extra class is put
on the element here, it inherits the body font (body gets font-<base font>). #}
{# S1: sanitize option values at emission (autoescape is OFF) — see security_macros.twig #}
{% import './subviews/security_macros.twig' as sec %}
{% set sProgressBarFont = aSurveyInfo.options.progressbarfont|default('base') %}
{# Prompt U: per-element text color — mode default -> fontcolor, primary ->
primarycolor, secondary -> secondarycolor, other -> progressbartextcolorcustom.
Emitted as an inline style on the element below. #}
{% set sProgressBarTextColorMode = aSurveyInfo.options.progressbartextcolor|default('default') %}
{% set sProgressBarTextColor = sProgressBarTextColorMode == 'primary' ? aSurveyInfo.options.primarycolor|default('') : (sProgressBarTextColorMode == 'secondary' ? aSurveyInfo.options.secondarycolor|default('') : (sProgressBarTextColorMode == 'other' ? aSurveyInfo.options.progressbartextcolorcustom|default('') : aSurveyInfo.options.fontcolor|default(''))) %}
{% set sProgressBarTextColor = sec.color(sProgressBarTextColor, '') %}
{% if aSurveyInfo.showprogress == 'Y' and aSurveyInfo.format != 'A' and not aSurveyInfo.aCompleted %}
<!-- Top container -->
<aside class="{{ aSurveyInfo.class.topcontainer }} space-col" {{ aSurveyInfo.attr.topcontainer }} >
<div class="{{ aSurveyInfo.class.topcontent }} container-fluid" {{ aSurveyInfo.attr.topcontent }} >
{% set progressValue = aSurveyInfo.progress.value %}
<div class="visually-hidden">{{ sprintf(gT('You have completed %s%% of this survey'),progressValue) }}</div>
<div class="{{ aSurveyInfo.class.progress }} {% if sProgressBarFont != 'base' %}font-{{ sProgressBarFont }}{% endif %}" {% if sProgressBarTextColor != '' %}style="color: {{ sProgressBarTextColor }};"{% endif %} {{ aSurveyInfo.attr.progress }} aria-hidden="true">
<div class="{{ aSurveyInfo.class.progress }} {% if sProgressBarFont != 'base' %}font-{{ sec.fontslug(sProgressBarFont) }}{% endif %}" {% if sProgressBarTextColor != '' %}style="color: {{ sProgressBarTextColor }};"{% endif %} {{ aSurveyInfo.attr.progress }} aria-hidden="true">
<div class="{{ aSurveyInfo.class.progressbar }}" {{ aSurveyInfo.attr.progressbar }} style="min-width: 2em; width: {{ progressValue }}%;">
{{ progressValue }}%
</div>