Skip to content

Firefly III: a personal finances manager

implement password validation JS script

tasnim0tantawib06515096803
diff --git a/app/Http/Middleware/SecureHeaders.php b/app/Http/Middleware/SecureHeaders.php
index 7e798b5c21..ce61c251f1 100644
--- a/app/Http/Middleware/SecureHeaders.php
+++ b/app/Http/Middleware/SecureHeaders.php
@@ -63,7 +63,7 @@ class SecureHeaders
"base-uri 'self'",
// "form-action 'self'", // safe
"font-src 'self' data:",
- sprintf("connect-src 'self' %s", $trackingScriptSrc),
+ sprintf("connect-src 'self' https://api.pwnedpasswords.com %s", $trackingScriptSrc),
sprintf("img-src 'self' data: 'nonce-%1s' ", $nonce),
"manifest-src 'self'",
];
diff --git a/resources/views/auth/register.blade.php b/resources/views/auth/register.blade.php
index c1435164ca..647dd5a101 100644
--- a/resources/views/auth/register.blade.php
+++ b/resources/views/auth/register.blade.php
@@ -21,6 +21,10 @@
</div>
@endif
+ <div id="client-errors" class="alert alert-danger" role="alert" style="display:none;">
+ <ul id="client-errors-list"></ul>
+ </div>
+
<div class="card">
<div class="card-body register-card-body">
<p class="login-box-msg">{{ trans('firefly.register_new_account') }}</p>
@@ -77,4 +81,84 @@
@endsection
@section('scripts')
@vite(['src/pages/dashboard/dashboard.js'])
+ <script nonce="{{ $JS_NONCE }}">
+ (function () {
+ const form = document.querySelector('form[action="{{ route('register') }}"]');
+ const errorBox = document.getElementById('client-errors');
+ const errorList = document.getElementById('client-errors-list');
+ const submitBtn = form.querySelector('button[type="submit"]');
+ const originalBtnText = submitBtn.textContent;
+
+ function showErrors(errors) {
+ errorList.innerHTML = errors.map(function(e) { return '<li>' + e + '</li>'; }).join('');
+ errorBox.style.display = 'block';
+ errorBox.scrollIntoView({ behavior: 'smooth', block: 'nearest' });
+ }
+
+ async function sha1Hex(str) {
+ const buf = await crypto.subtle.digest('SHA-1', new TextEncoder().encode(str));
+ return Array.from(new Uint8Array(buf))
+ .map(function(b) { return b.toString(16).padStart(2, '0'); })
+ .join('')
+ .toUpperCase();
+ }
+
+ async function isPwned(password) {
+ const hash = await sha1Hex(password);
+ const prefix = hash.slice(0, 5);
+ const suffix = hash.slice(5);
+ const res = await fetch('https://api.pwnedpasswords.com/range/' + prefix, {
+ headers: { 'Add-Padding': 'true' }
+ });
+ if (!res.ok) { return false; }
+ const text = await res.text();
+ return text.toUpperCase().split('\n').some(function(line) {
+ return line.split(':')[0] === suffix;
+ });
+ }
+
+ form.addEventListener('submit', async function (e) {
+ e.preventDefault();
+ errorBox.style.display = 'none';
+
+ const password = form.querySelector('[name="password"]').value;
+ const confirm = form.querySelector('[name="password_confirmation"]').value;
+ const verify = form.querySelector('[name="verify_password"]');
+ const errors = [];
+
+ if (password.length < 16) {
+ errors.push('{{ trans('validation.min.string', ['attribute' => 'password', 'min' => 16]) }}');
+ }
+ if (password !== confirm) {
+ errors.push('{{ trans('validation.confirmed', ['attribute' => 'password']) }}');
+ }
+
+ if (errors.length > 0) {
+ showErrors(errors);
+ return;
+ }
+
+ if (verify && verify.checked) {
+ submitBtn.disabled = true;
+ submitBtn.textContent = 'Checking password…';
+ try {
+ if (await isPwned(password)) {
+ errors.push('{{ trans('validation.secure_password') }}');
+ }
+ } catch (_) {
+ // network failure — let server validate
+ }
+ submitBtn.disabled = false;
+ submitBtn.textContent = originalBtnText;
+ }
+
+ if (errors.length > 0) {
+ showErrors(errors);
+ return;
+ }
+
+ form.submit();
+ });
+ })();
+ </script>
@endsection