Skip to content

Commit b126a90

Browse files
committed
iss1171 - Add validation of metadata for length and broken JSON.
1 parent c6fe11d commit b126a90

2 files changed

Lines changed: 10 additions & 0 deletions

File tree

lang/en/qtype_stack.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,8 @@
440440
$string['makemecreator'] = 'Make me the creator';
441441
$string['validateandclose'] = 'Validate and close';
442442
$string['JSONmetadata'] = 'JSON metadata';
443+
$string['JSONtoolong'] = 'There is too much metadata. Metadata should be under 32000 bytes.';
444+
$string['JSONbroken'] = 'Metadata is invalid JSON.';
443445
$string['metadataexplanation'] = 'STACK metadata is stored as a JSON object. You can edit metadata in the inputs above or directly edit the JSON below and then click \'Update inputs from JSON\'. On closing this metadata window, the inputs will be validated and, if this is successful you will be returned to the main question edit form. You will still need to save the question as normal to save the metadata.';
444446
$string['scope'] = 'Scope';
445447
$string['property'] = 'Property';

questiontype.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2412,6 +2412,14 @@ public function validate_fromform($fromform, $errors, $question = null) {
24122412
$fromform['questiondescription']['text']
24132413
);
24142414

2415+
$errors['metadata_text'] = [];
2416+
if (mb_strlen($fromform['metadata']) > 32000) {
2417+
$errors['metadata_text'][] = stack_string('JSONtoolong');
2418+
}
2419+
if ($fromform['metadata'] && !json_decode($fromform['metadata'])) {
2420+
$errors['metadata_text'][] = stack_string('JSONbroken');
2421+
}
2422+
24152423
// 2) Validate all inputs.
24162424
$stackinputfactory = new stack_input_factory();
24172425
foreach ($inputs as $inputname => $counts) {

0 commit comments

Comments
 (0)