Reuse existing URLTools functionality where possible

This commit is contained in:
Felix Berlakovich 2026-02-11 16:57:16 +01:00
parent 4a57a76cd8
commit 310d447aa0
2 changed files with 16 additions and 9 deletions

View file

@ -18,8 +18,10 @@
#ifndef KEEPASSX_AUTOFILL_UTILS_H
#define KEEPASSX_AUTOFILL_UTILS_H
#include <QString>
#include "gui/UrlTools.h"
#include <QRegularExpression>
#include <QString>
#include <QUrl>
namespace AutoFillUtils
@ -74,13 +76,19 @@ inline bool hostsMatch(const QString& requested, const QString& candidate)
return true;
}
// Only allow subdomain matching when the shorter side has at least one dot
// (i.e. is a real domain, not a bare TLD like "com")
if (requested.endsWith('.' + candidate)) {
return candidate.contains('.');
// IP addresses require exact match only (already handled above)
if (urlTools()->isIpAddress(requested) || urlTools()->isIpAddress(candidate)) {
return false;
}
if (candidate.endsWith('.' + requested)) {
return requested.contains('.');
// Base domains must match (follows BrowserService::handleURL pattern)
if (urlTools()->getBaseDomainFromUrl(requested) != urlTools()->getBaseDomainFromUrl(candidate)) {
return false;
}
// Allow subdomain matching when one host ends with the other
if (requested.endsWith('.' + candidate) || candidate.endsWith('.' + requested)) {
return true;
}
return false;

View file

@ -240,10 +240,9 @@ endif()
if(WITH_XC_NETWORKING OR WITH_XC_BROWSER)
add_unit_test(NAME testurltools SOURCES TestUrlTools.cpp LIBS ${TEST_LIBRARIES})
add_unit_test(NAME testautofillutils SOURCES TestAutoFillUtils.cpp LIBS ${TEST_LIBRARIES})
endif()
add_unit_test(NAME testautofillutils SOURCES TestAutoFillUtils.cpp LIBS ${TEST_LIBRARIES})
add_unit_test(NAME testcli SOURCES TestCli.cpp
LIBS testsupport cli ${ZXCVBN_LIBRARIES} ${TEST_LIBRARIES})
target_compile_definitions(testcli PRIVATE KEEPASSX_CLI_PATH="$<TARGET_FILE:${PROGNAME}-cli>")