From 4ed14db4f7b1792fb206fb3b4cdb73ad58a58e21 Mon Sep 17 00:00:00 2001 From: pacnpal <183241239+pacnpal@users.noreply.github.com> Date: Wed, 29 Jan 2025 13:01:26 -0500 Subject: [PATCH] feat(index.html): add domain preprocessing function for improved validation and error handling --- src/simpleguardhome/templates/index.html | 32 ++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/src/simpleguardhome/templates/index.html b/src/simpleguardhome/templates/index.html index 7276c8b..af5c544 100644 --- a/src/simpleguardhome/templates/index.html +++ b/src/simpleguardhome/templates/index.html @@ -46,9 +46,37 @@ } } + function preprocessDomain(input) { + // Strip http:// or https:// from the beginning + let domain = input.replace(/^https?:\/\//i, ''); + + // Strip any paths or query parameters + domain = domain.split('/')[0]; + + // Basic domain validation + const domainRegex = /^[a-zA-Z0-9][a-zA-Z0-9.-]*[a-zA-Z0-9]$/; + if (!domainRegex.test(domain)) { + throw new Error('Invalid domain format. Please enter a valid domain name (e.g., example.com)'); + } + + return domain; + } + async function checkDomain(event) { event.preventDefault(); - const domain = DOMPurify.sanitize(document.getElementById('domain').value); + const rawInput = DOMPurify.sanitize(document.getElementById('domain').value); + let domain; + try { + domain = preprocessDomain(rawInput); + } catch (error) { + resultDiv.innerHTML = ` +
+

Invalid Input

+

${escapeHtml(error.message)}

+
`; + unblockDiv.innerHTML = ''; + return; + } const resultDiv = document.getElementById('result'); const unblockDiv = document.getElementById('unblock-action'); const submitBtn = document.getElementById('submit-btn'); @@ -156,7 +184,7 @@ -