Firefly III: a personal finances manager
PHP accountingbudgetbudgetingbudgetscash-flowcashflowcredit-carddockerexpensesfinancefinancesfinanciallinuxmoneypaycheckpersonal-financephpphp7
Better JS escape
Sander Dorigo0adaef8b994c
diff --git a/app/Helpers/Functions/helpers.php b/app/Helpers/Functions/helpers.phpindex 744c429f2d..f3b88407bc 100644--- a/app/Helpers/Functions/helpers.php+++ b/app/Helpers/Functions/helpers.php@@ -43,11 +43,62 @@ use Illuminate\Support\Facades\Log;use Illuminate\Support\Facades\Route;use League\CommonMark\GithubFlavoredMarkdownConverter;+use Twig\Error\RuntimeError;use function Safe\json_decode;use function Safe\mb_ord;use function Safe\preg_match;use function Safe\preg_replace_callback;+if(!function_exists('escape_for_js')) {+ function escape_for_js(string $string):string {+ // escape all non-alphanumeric characters+ // into their \x or \uHHHH representations++ if (!preg_match('//u', $string)) {+ throw new FireflyException('The string to escape is not a valid UTF-8 string.');+ }++ $string = preg_replace_callback('#[^a-zA-Z0-9,\._]#Su', static function ($matches) {+ $char = $matches[0];++ /*+ * A few characters have short escape sequences in JSON and JavaScript.+ * Escape sequences supported only by JavaScript, not JSON, are omitted.+ * \" is also supported but omitted, because the resulting string is not HTML safe.+ */+ $short = match ($char) {+ '\\' => '\\\\',+ '/' => '\\/',+ "\x08" => '\b',+ "\x0C" => '\f',+ "\x0A" => '\n',+ "\x0D" => '\r',+ "\x09" => '\t',+ default => false,+ };++ if ($short) {+ return $short;+ }++ $codepoint = mb_ord($char, 'UTF-8');+ if (0x10000 > $codepoint) {+ return \sprintf('\u%04X', $codepoint);+ }++ // Split characters outside the BMP into surrogate pairs+ // https://tools.ietf.org/html/rfc2781.html#section-2.1+ $u = $codepoint - 0x10000;+ $high = 0xD800 | ($u >> 10);+ $low = 0xDC00 | ($u & 0x3FF);++ return \sprintf('\u%04X\u%04X', $high, $low);+ }, $string);++ return $string;+ }+}+if (!function_exists('env_default_when_empty')) {/*** @return null|mixeddiff --git a/resources/views/javascript/variables.blade.php b/resources/views/javascript/variables.blade.phpindex 94423427a4..560db750cb 100644--- a/resources/views/javascript/variables.blade.php+++ b/resources/views/javascript/variables.blade.php@@ -9,11 +9,11 @@ var dateRangeMeta = {title: "{{ $dateRangeTitle }}",url: "{{ route('daterange') }}",labels: {-apply: "{{ __('firefly.apply') }}",-cancel: "{{ __('firefly.cancel') }}",-from: "{{ __('firefly.from') }}",-to: "{{ __('firefly.to') }}",-customRange: "{{ __('firefly.customRange') }}"+apply: "{{ escape_for_js(__('firefly.apply')) }}",+cancel: "{{ escape_for_js(__('firefly.cancel')) }}",+from: "{{ escape_for_js(__('firefly.from')) }}",+to: "{{ escape_for_js(__('firefly.to')) }}",+customRange: "{{ escape_for_js(__('firefly.customRange')) }}"}};@@ -44,27 +44,27 @@ var sessionEnd = '{{ session('end')->format('Y-m-d') }}';var todayText = ' {{ trans('firefly.today') }}';// some formatting stuff:-var month_and_day_js = "{{ trans('config.month_and_day_js') }}";-var date_time_js = "{{ trans('config.date_time_js') }}";+var month_and_day_js = "{{ escape_for_js(trans('config.month_and_day_js')) }}";+var date_time_js = "{{ escape_for_js(trans('config.date_time_js')) }}";var acc_config_new = {format: accountingConfig};// strings and translations used often:-var helpPageTitle = "{{ trans('firefly.help_for_this_page') }}";-var helpPageBody = "{{ trans('firefly.help_for_this_page_body') }}";+var helpPageTitle = "{{ escape_for_js(trans('firefly.help_for_this_page')) }}";+var helpPageBody = "{{ escape_for_js(trans('firefly.help_for_this_page_body')) }}";-var anonymous_warning_on_txt = "{{ trans('firefly.anonymous_warning_on') }}";-var anonymous_warning_off_txt= "{{ trans('firefly.anonymous_warning_off') }}";+var anonymous_warning_on_txt = "{{ escape_for_js(trans('firefly.anonymous_warning_on')) }}";+var anonymous_warning_off_txt= "{{ escape_for_js(trans('firefly.anonymous_warning_off')) }}";-var edit_selected_txt = "{{ trans('firefly.mass_edit') }}";-var edit_bulk_selected_txt = "{{ trans('firefly.bulk_edit') }}";-var delete_selected_txt = "{{ trans('firefly.mass_delete') }}";+var edit_selected_txt = "{{ escape_for_js(trans('firefly.mass_edit')) }}";+var edit_bulk_selected_txt = "{{ escape_for_js(trans('firefly.bulk_edit')) }}";+var delete_selected_txt = "{{ escape_for_js(trans('firefly.mass_delete')) }}";var mass_edit_url = '{{ route('transactions.mass.edit', ['']) }}';var bulk_edit_url = '{{ route('transactions.bulk.edit', ['']) }}';var mass_delete_url = '{{ route('transactions.mass.delete', ['']) }}';// for demo:-var nextLabel = "{{ trans('firefly.intro_next_label') }}";-var prevLabel = "{{ trans('firefly.intro_prev_label') }}";-var skipLabel = "{{ trans('firefly.intro_skip_label') }}";-var doneLabel = "{{ trans('firefly.intro_done_label') }}";+var nextLabel = "{{ escape_for_js(trans('firefly.intro_next_label')) }}";+var prevLabel = "{{ escape_for_js(trans('firefly.intro_prev_label')) }}";+var skipLabel = "{{ escape_for_js(trans('firefly.intro_skip_label')) }}";+var doneLabel = "{{ escape_for_js(trans('firefly.intro_done_label')) }}";diff --git a/resources/views/reports/index.blade.php b/resources/views/reports/index.blade.phpindex 360bde18c7..66a7eb34bd 100644--- a/resources/views/reports/index.blade.php+++ b/resources/views/reports/index.blade.php@@ -182,13 +182,12 @@<script type="text/javascript" nonce="{{ $JS_NONCE }}">var minDate = "{{ $start->format('Y-m-d') }}";var picker;- var selectAllText = "{{ trans('firefly.multi_select_select_all') }}";- var nonSelectedText = "{{ trans('firefly.multi_select_no_selection') }}";- var nSelectedText = "{{ trans('firefly.multi_select_n_selected') }}";- var allSelectedText = "{{ trans('firefly.multi_select_all_selected') }}";- var filterPlaceholder = "{{ trans('firefly.multi_select_filter_placeholder') }}";+ var selectAllText = "{{ escape_for_js(trans('firefly.multi_select_select_all')) }}";+ var nonSelectedText = "{{ escape_for_js(trans('firefly.multi_select_no_selection')) }}";+ var nSelectedText = "{{ escape_for_js(trans('firefly.multi_select_n_selected')) }}";+ var allSelectedText = "{{ escape_for_js(trans('firefly.multi_select_all_selected')) }}";+ var filterPlaceholder = "{{ escape_for_js(trans('firefly.multi_select_filter_placeholder')) }}";</script>- <script type="text/javascript" src="v1/js/lib/jquery-4.0.0.min.js?v={{ $FF_BUILD_TIME }}" nonce="{{ $JS_NONCE }}"></script><script type="text/javascript" src="v1/js/lib/daterangepicker.js?v={{ $FF_BUILD_TIME }}" nonce="{{ $JS_NONCE }}"></script><script type="text/javascript" src="v1/js/lib/bootstrap-multiselect.js?v={{ $FF_BUILD_TIME }}" nonce="{{ $JS_NONCE }}"></script><script type="text/javascript" src="v1/js/ff/reports/all.js?v={{ $FF_BUILD_TIME }}" nonce="{{ $JS_NONCE }}"></script>diff --git a/resources/views/search/index.blade.php b/resources/views/search/index.blade.phpindex f8c5807f85..c9c386e36d 100644--- a/resources/views/search/index.blade.php+++ b/resources/views/search/index.blade.php@@ -133,11 +133,8 @@@section('scripts')@vite(['js/pages/generic.js'])<script type="text/javascript" nonce="{{ $JS_NONCE }}">- var edit_selected_txt = "{{ trans('firefly.edit') }}";- var delete_selected_txt = "{{ trans('firefly.delete') }}";- var edit_bulk_selected_txt = "{{ trans('firefly.bulk_edit') }}";- var searchQuery = "{!! addslashes($fullQuery) !!}";+ var searchQuery = "{!! escape_for_js($fullQuery) !!}";var searchUrl = "{{ route('search.search') }}?page={{ $page }}";var searchPage = {{ $page }};var cloneGroupUrl = '{{ route('transactions.clone') }}';