Skip to content

Firefly III: a personal finances manager

Add helpers and fix missing string.

James Cole30cce327e87b
diff --git a/app/Helpers/Functions/helpers.php b/app/Helpers/Functions/helpers.php
new file mode 100644
index 0000000000..45d4b1235a
--- /dev/null
+++ b/app/Helpers/Functions/helpers.php
@@ -0,0 +1,96 @@
+<?php
+/*
+ * helpers.php
+ * Copyright (c) 2026 [email protected]
+ *
+ * This file is part of Firefly III (https://github.com/firefly-iii).
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+
+use FireflyIII\Exceptions\FireflyException;
+use function Safe\preg_match;
+
+if (!function_exists('envDefaultWhenEmpty')) {
+ /**
+ *
+ * @return mixed|null
+ */
+ function envDefaultWhenEmpty(mixed $value, string | int | bool | null $default = null): mixed
+ {
+ if (null === $value) {
+ return $default;
+ }
+ if ('' === $value) {
+ return $default;
+ }
+ return $value;
+ }
+}
+
+if (!function_exists('stringIsEqual')) {
+ function stringIsEqual(string $left, string $right): bool
+ {
+ return $left === $right;
+ }
+}
+
+if (!function_exists('blade_escape_js')) {
+ function blade_escape_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.');
+ }
+
+ return 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);
+ }
+}
diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php
index da8f692a67..8c18e39396 100644
--- a/app/Providers/AppServiceProvider.php
+++ b/app/Providers/AppServiceProvider.php
@@ -23,6 +23,7 @@ declare(strict_types=1);
namespace FireflyIII\Providers;
+use FireflyIII\Exceptions\FireflyException;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Facades\Response;
use Illuminate\Support\Facades\Route;
@@ -30,7 +31,6 @@ use Illuminate\Support\Facades\Schema;
use Illuminate\Support\ServiceProvider;
use Laravel\Passport\Passport;
use Override;
-
use function Safe\preg_match;
/**
@@ -49,7 +49,7 @@ class AppServiceProvider extends ServiceProvider
Schema::defaultStringLength(191);
Response::macro('api', function (array $value) {
$headers = ['Cache-Control' => 'no-store'];
- $uuid = (string) request()->header('X-Trace-Id');
+ $uuid = (string)request()->header('X-Trace-Id');
if ('' !== trim($uuid) && 1 === preg_match('/^[a-f\d]{8}(-[a-f\d]{4}){4}[a-f\d]{8}$/i', trim($uuid))) {
$headers['X-Trace-Id'] = $uuid;
}
@@ -67,18 +67,20 @@ class AppServiceProvider extends ServiceProvider
return '';
});
Blade::if('partialroute', function (string $route, string $firstParam = ''): bool {
- $name = Route::getCurrentRoute()->getName() ?? '';
+ $name = Route::getCurrentRoute()->getName() ?? '';
if ('' === $firstParam && str_contains($name, $route)) {
return true;
}
/** @var null|array $params */
$params = Route::getCurrentRoute()->parameters();
- $params ??= [];
+ $params ??= [];
$objectType = $params['objectType'] ?? '';
return $objectType === $firstParam && str_contains($name, $route);
});
+
+
}
/**
diff --git a/bootstrap/app.php b/bootstrap/app.php
index 40b6fb48ef..08b06bf3b3 100644
--- a/bootstrap/app.php
+++ b/bootstrap/app.php
@@ -63,29 +63,7 @@ use PragmaRX\Google2FALaravel\Middleware as MFAMiddleware;
bcscale(12);
-if (!function_exists('envDefaultWhenEmpty')) {
- /**
- *
- * @return mixed|null
- */
- function envDefaultWhenEmpty(mixed $value, string | int | bool | null $default = null): mixed
- {
- if(null === $value) {
- return $default;
- }
- if('' === $value) {
- return $default;
- }
- return $value;
- }
-}
-
-if (!function_exists('stringIsEqual')) {
- function stringIsEqual(string $left, string $right): bool
- {
- return $left === $right;
- }
-}
+
$app = Application::configure(basePath: dirname(__DIR__))
->withRouting(
diff --git a/composer.json b/composer.json
index 33e48e62ce..718097603d 100644
--- a/composer.json
+++ b/composer.json
@@ -135,6 +135,9 @@
"suggest": {},
"autoload": {
+ "files": [
+ "app/Helpers/Functions/helpers.php"
+ ],
"psr-4": {
"FireflyIII\\": "app/",
"Domain\\": "domain/",
diff --git a/resources/lang/en_US/validation.php b/resources/lang/en_US/validation.php
index dbe2d3d327..5ba532f5f4 100644
--- a/resources/lang/en_US/validation.php
+++ b/resources/lang/en_US/validation.php
@@ -23,6 +23,7 @@
declare(strict_types=1);
return [
+ 'verifying_password' => 'Verifying password...',
'invalid_account_list' => 'Invalid account type list entry ":value"',
'invalid_transaction_type_list' => 'Invalid transaction type list',
'limit_exists' => 'There is already a budget limit (amount) for this budget and currency in the given period.',
diff --git a/resources/views/auth/register.blade.php b/resources/views/auth/register.blade.php
index 647dd5a101..c39fc0a06d 100644
--- a/resources/views/auth/register.blade.php
+++ b/resources/views/auth/register.blade.php
@@ -39,11 +39,11 @@
</div>
<div class="input-group mb-3">
<input type="password" autocomplete="new-password" required class="form-control"
- placeholder="{{ trans('form.password') }}" name="password"/>
+ placeholder="{{ trans('form.password') }}" minlength="16" name="password"/>
<div class="input-group-text"> <em class="fa-solid fa-lock"></em> </div>
</div>
<div class="input-group mb-3">
- <input type="password" autocomplete="new-password" required class="form-control"
+ <input type="password" autocomplete="new-password" minlength="16" required class="form-control"
placeholder="{{ trans('form.password_confirmation') }}" name="password_confirmation"/>
<div class="input-group-text"> <em class="fa-solid fa-lock"></em> </div>
</div>
@@ -80,7 +80,6 @@
@endsection
@section('scripts')
- @vite(['src/pages/dashboard/dashboard.js'])
<script nonce="{{ $JS_NONCE }}">
(function () {
const form = document.querySelector('form[action="{{ route('register') }}"]');
@@ -127,10 +126,10 @@
const errors = [];
if (password.length < 16) {
- errors.push('{{ trans('validation.min.string', ['attribute' => 'password', 'min' => 16]) }}');
+ errors.push('{{ blade_escape_js((string)trans('validation.min.string', ['attribute' => 'password', 'min' => 16])) }}');
}
if (password !== confirm) {
- errors.push('{{ trans('validation.confirmed', ['attribute' => 'password']) }}');
+ errors.push('{{ blade_escape_js(trans('validation.confirmed', ['attribute' => 'password'])) }}');
}
if (errors.length > 0) {
@@ -140,10 +139,10 @@
if (verify && verify.checked) {
submitBtn.disabled = true;
- submitBtn.textContent = 'Checking password…';
+ submitBtn.textContent = '{{ blade_escape_js(trans('validation.verifying_password')) }}';
try {
if (await isPwned(password)) {
- errors.push('{{ trans('validation.secure_password') }}');
+ errors.push('{{ blade_escape_js(trans('validation.secure_password')) }}');
}
} catch (_) {
// network failure — let server validate