Skip to content
Open
30 changes: 30 additions & 0 deletions lib/Constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,11 @@ class Constants {
public const ANSWER_TYPE_LONG = 'long';
public const ANSWER_TYPE_MULTIPLE = 'multiple';
public const ANSWER_TYPE_MULTIPLEUNIQUE = 'multiple_unique';
public const ANSWER_TYPE_IMAGE = 'image';
public const ANSWER_TYPE_RANKING = 'ranking';
public const ANSWER_TYPE_SHORT = 'short';
public const ANSWER_TYPE_TIME = 'time';
public const ANSWER_TYPE_VIDEO = 'video';

public const ANSWER_GRID_TYPE_CHECKBOX = 'checkbox';
public const ANSWER_GRID_TYPE_NUMBER = 'number';
Expand Down Expand Up @@ -149,9 +151,11 @@ class Constants {
self::ANSWER_TYPE_LONG,
self::ANSWER_TYPE_MULTIPLE,
self::ANSWER_TYPE_MULTIPLEUNIQUE,
self::ANSWER_TYPE_IMAGE,
self::ANSWER_TYPE_RANKING,
self::ANSWER_TYPE_SHORT,
self::ANSWER_TYPE_TIME,
self::ANSWER_TYPE_VIDEO,
];

// AnswerTypes, that need/have predefined Options
Expand Down Expand Up @@ -276,6 +280,32 @@ class Constants {
'next' => ['integer', 'string'],
];

/**
* Display-only blocks carry no answer; they reference something to show and may jump on with `next`.
*/
public const EXTRA_SETTINGS_MEDIA = [
'url' => ['string', 'NULL'],
'alt' => ['string', 'NULL'],
'next' => ['integer', 'string'],
];

/**
* Origins of the video players a video block may load after the respondent agrees.
*/
public const VIDEO_PLAYER_ORIGINS = [
'https://www.youtube-nocookie.com',
'https://player.vimeo.com',
];

/**
* Question types that are shown but never answered, so they are skipped when
* validating a submission and left out of exports.
*/
public const ANSWER_TYPES_DISPLAY_ONLY = [
self::ANSWER_TYPE_IMAGE,
self::ANSWER_TYPE_VIDEO,
];

public const EXTRA_SETTINGS_RANKING = [
'shuffleOptions' => ['boolean'],
'next' => ['integer', 'string'],
Expand Down
24 changes: 21 additions & 3 deletions lib/Controller/PageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,13 @@ public function index(?string $hash = null): TemplateResponse {
}
}

return new TemplateResponse($this->appName, self::TEMPLATE_MAIN, [
$response = new TemplateResponse($this->appName, self::TEMPLATE_MAIN, [
'id-app-content' => '#app-content-vue',
'id-app-navigation' => '#app-navigation-vue',
]);
$response->setContentSecurityPolicy($this->createPolicy());

return $response;
}

/**
Expand Down Expand Up @@ -219,7 +222,10 @@ protected function createPublicSubmitView(Form $form, string $hash): TemplateRes
$this->initialState->provideInitialState('isLoggedIn', $this->userSession->isLoggedIn());
$this->initialState->provideInitialState('shareHash', $hash);
$this->initialState->provideInitialState('maxStringLengths', Constants::MAX_STRING_LENGTHS);
return $this->provideTemplate(self::TEMPLATE_MAIN, $form, ['id-app-navigation' => null]);
$response = $this->provideTemplate(self::TEMPLATE_MAIN, $form, ['id-app-navigation' => null]);
$response->setContentSecurityPolicy($this->createPolicy());

return $response;
}

/**
Expand Down Expand Up @@ -293,13 +299,25 @@ protected function insertHeaderOnIos(): void {
}
}

/**
* Create the CSP for pages showing a form, allowing the players of video blocks as frames
*/
protected function createPolicy(): ContentSecurityPolicy {
$policy = new ContentSecurityPolicy();
foreach (Constants::VIDEO_PLAYER_ORIGINS as $origin) {
$policy->addAllowedFrameDomain($origin);
}

return $policy;
}

/**
* Set CSP options to allow the page be embedded using <iframe>
*
* @return TemplateResponse
*/
protected function setEmbeddedCSP(TemplateResponse $response): TemplateResponse {
$policy = new ContentSecurityPolicy();
$policy = $this->createPolicy();
$policy->addAllowedFrameAncestorDomain('*');

$response->addHeader('X-Frame-Options', 'ALLOW');
Expand Down
2 changes: 2 additions & 0 deletions lib/Service/FormsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -905,6 +905,8 @@ public static function getAllowedExtraSettings(string $questionType): array {
Constants::ANSWER_TYPE_FILE => Constants::EXTRA_SETTINGS_FILE,
Constants::ANSWER_TYPE_DATE => Constants::EXTRA_SETTINGS_DATE,
Constants::ANSWER_TYPE_GRID => Constants::EXTRA_SETTINGS_GRID,
Constants::ANSWER_TYPE_IMAGE => Constants::EXTRA_SETTINGS_MEDIA,
Constants::ANSWER_TYPE_VIDEO => Constants::EXTRA_SETTINGS_MEDIA,
Constants::ANSWER_TYPE_RANKING => Constants::EXTRA_SETTINGS_RANKING,
Constants::ANSWER_TYPE_COLOR => Constants::EXTRA_SETTINGS_COLOR,
Constants::ANSWER_TYPE_TIME => Constants::EXTRA_SETTINGS_TIME,
Expand Down
21 changes: 21 additions & 0 deletions lib/Service/SubmissionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,16 @@ public function getSubmissionsData(Form $form, string $fileFormat, ?File $file =
$submissionEntities = array_reverse($submissionEntities);

$questions = $this->questionMapper->findByForm($form->getId());
// Display-only blocks hold no answers; leaving them in would add an empty column
// per block to every export.
$questions = array_values(array_filter(
$questions,
static fn ($question): bool => !in_array(
$question->getType(),
Constants::ANSWER_TYPES_DISPLAY_ONLY,
true,
),
));

$activeQuestionIds = $this->getActiveQuestionIds($questions);

Expand Down Expand Up @@ -691,6 +701,17 @@ public function validateSubmission(array $questions, array $answers, string $for
throw new \InvalidArgumentException(sprintf('Invalid answer for question "%s".', $question['text']));
}

// Display-only blocks take no answer. An absent answer is therefore expected and
// must not count as an unanswered mandatory question -- but a present one is
// refused outright rather than skipped, since nothing else would stop it being
// stored against a block that has nowhere to show it.
if (in_array($question['type'], Constants::ANSWER_TYPES_DISPLAY_ONLY, true)) {
if ($questionAnswered) {
throw new \InvalidArgumentException(sprintf('Question "%s" does not take an answer.', $question['text']));
}
continue;
}

// Check if all required questions have an answer
if (!empty($question['isRequired'])
&& self::isShownToRespondents($question)
Expand Down
Loading
Loading