Beautiful Open-Source Form Builder
Harden submission formatter URL and email links (#1263)
Julien Nahum2a01e558ea07
diff --git a/api/app/Service/Forms/FormSubmissionFormatter.php b/api/app/Service/Forms/FormSubmissionFormatter.phpindex 1ebbe4fe5..8875c13d8 100644--- a/api/app/Service/Forms/FormSubmissionFormatter.php+++ b/api/app/Service/Forms/FormSubmissionFormatter.php@@ -153,9 +153,9 @@ class FormSubmissionFormatter}if ($this->createLinks && $field['type'] == 'url') {- $returnArray[$field['name']] = '<a href="' . $data[$field['id']] . '">' . $data[$field['id']] . '</a>';+ $returnArray[$field['name']] = $this->formatUrlLink($data[$field['id']]);} elseif ($this->createLinks && $field['type'] == 'email') {- $returnArray[$field['name']] = '<a href="mailto:' . $data[$field['id']] . '">' . $data[$field['id']] . '</a>';+ $returnArray[$field['name']] = $this->formatEmailLink($data[$field['id']]);} elseif ($field['type'] == 'multi_select') {$val = $data[$field['id']];if ($this->outputStringsOnly && is_array($val)) {@@ -315,13 +315,31 @@ class FormSubmissionFormatterprivate function formatUrlLink(mixed $value): string{- $url = is_scalar($value) ? (string) $value : '';-- if (!filter_var($url, FILTER_VALIDATE_URL)) {+ if (!is_scalar($value)) {return $this->escapeHtmlValue($value);}- return $this->buildSafeHtmlLink($url, $url);+ $label = trim((string) $value);+ $scheme = parse_url($label, PHP_URL_SCHEME);++ if ($scheme !== null && (!is_string($scheme) || !in_array(strtolower($scheme), ['http', 'https'], true))) {+ return $this->escapeHtmlValue($label);+ }++ $url = $scheme === null ? 'https://' . $label : $label;+ $urlParts = parse_url($url);++ if (+ !filter_var($url, FILTER_VALIDATE_URL)+ || !is_array($urlParts)+ || empty($urlParts['host'])+ || isset($urlParts['user'])+ || isset($urlParts['pass'])+ ) {+ return $this->escapeHtmlValue($label);+ }++ return $this->buildSafeHtmlLink($url, $label);}private function formatEmailLink(mixed $value): stringdiff --git a/api/tests/Unit/Service/Forms/FormSubmissionFormatterTest.php b/api/tests/Unit/Service/Forms/FormSubmissionFormatterTest.phpnew file mode 100644index 000000000..1a25b91cf--- /dev/null+++ b/api/tests/Unit/Service/Forms/FormSubmissionFormatterTest.php@@ -0,0 +1,116 @@+<?php++use App\Models\Forms\Form;+use App\Service\Forms\FormSubmissionFormatter;++function formatterForField(string $type, mixed $value): FormSubmissionFormatter+{+ $fieldId = 'field-id';+ $form = new Form([+ 'properties' => [[+ 'id' => $fieldId,+ 'name' => 'Field',+ 'type' => $type,+ ]],+ 'removed_properties' => [],+ ]);++ return (new FormSubmissionFormatter($form, [$fieldId => $value]))+ ->createLinks()+ ->showHiddenFields();+}++it('formats safe submission links consistently', function (string $type, mixed $value, string $expected) {+ $formatter = formatterForField($type, $value);++ expect($formatter->getCleanKeyValue())->toBe([+ 'Field (field-id)' => $expected,+ ]);++ expect($formatter->getFieldsWithValue()[0])+ ->toMatchArray([+ 'value' => $expected,+ 'value_is_html' => true,+ ]);+})->with([+ 'https url' => [+ 'url',+ 'https://example.com/path?a=1&b=2',+ '<a href="https://example.com/path?a=1&b=2">https://example.com/path?a=1&b=2</a>',+ ],+ 'http url' => [+ 'url',+ 'http://example.com/path',+ '<a href="http://example.com/path">http://example.com/path</a>',+ ],+ 'url without scheme' => [+ 'url',+ 'example.com/path',+ '<a href="https://example.com/path">example.com/path</a>',+ ],+ 'email' => [+ 'email',+ '[email protected]',+ '<a href="mailto:[email protected]">[email protected]</a>',+ ],+]);++it('renders unsafe or invalid links as escaped text', function (string $type, mixed $value, string $expected) {+ $formatter = formatterForField($type, $value);++ expect($formatter->getCleanKeyValue())->toBe([+ 'Field (field-id)' => $expected,+ ]);++ expect($formatter->getFieldsWithValue()[0])+ ->toMatchArray([+ 'value' => $expected,+ 'value_is_html' => true,+ ]);+})->with([+ 'javascript url' => [+ 'url',+ 'javascript://example.com/%0Aalert(1)',+ 'javascript://example.com/%0Aalert(1)',+ ],+ 'data url' => [+ 'url',+ 'data://text/html,<script>alert(1)</script>',+ 'data://text/html,<script>alert(1)</script>',+ ],+ 'file url' => [+ 'url',+ 'file:///etc/passwd',+ 'file:///etc/passwd',+ ],+ 'ftp url' => [+ 'url',+ 'ftp://example.com/file',+ 'ftp://example.com/file',+ ],+ 'attribute injection' => [+ 'url',+ 'https://example.com/" onclick="alert(1)',+ 'https://example.com/" onclick="alert(1)',+ ],+ 'url with explicit user info' => [+ 'url',+ 'https://[email protected]/path',+ 'https://[email protected]/path',+ ],+ 'url without scheme containing user info' => [+ 'url',+ '[email protected]/path',+ '[email protected]/path',+ ],+ 'invalid email' => [+ 'email',+ '[email protected]" onclick="alert(1)',+ '[email protected]" onclick="alert(1)',+ ],+ 'non-scalar url' => [+ 'url',+ ['https://example.com', '<script>alert(1)</script>'],+ 'https://example.com, <script>alert(1)</script>',+ ],+]);