mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-11-05 09:33:25 -08:00
[Servatrice] Match trusted_sources by address, not substring (#7582)
trusted_sources was checked with QString::contains. A dual-stack listener reports a local proxy as ::ffff:127.0.0.1, which never matches the default "127.0.0.1,::1". Behind nginx the forwarded client IP header was then ignored, every websocket user shared the proxy's address, and logins failed with "too many concurrent connections" once max_users_per_address was reached. Add isTrustedSource(), which parses each entry as a QHostAddress and compares exact addresses with IPv4-mapped addresses normalized, and use it at all four call sites. The ban command also read server/trusted_sources instead of the documented security/trusted_sources; it now uses the same getter. Entries that are not IP addresses are logged at startup since they no longer match anything. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
4343544588
commit
46cdbac830
9 changed files with 136 additions and 9 deletions
|
|
@ -33,6 +33,7 @@
|
|||
#include "serversocketinterface.h"
|
||||
#include "settingscache.h"
|
||||
#include "smtpclient.h"
|
||||
#include "trusted_sources.h"
|
||||
|
||||
#include <QDateTime>
|
||||
#include <QDebug>
|
||||
|
|
@ -324,6 +325,14 @@ bool Servatrice::initServer()
|
|||
qDebug() << "Maximum websocket user limit:" << getMaxWebSocketUserLimit();
|
||||
}
|
||||
|
||||
// trusted_sources entries are matched as exact addresses; older configs may hold hostnames,
|
||||
// prefixes or subnets that substring matching used to accept and that now never match.
|
||||
for (const QString &entry : getTrustedSources().split(',', Qt::SkipEmptyParts)) {
|
||||
if (QHostAddress(entry.trimmed()).isNull()) {
|
||||
qWarning() << "security/trusted_sources entry is not an IP address and will be ignored:" << entry.trimmed();
|
||||
}
|
||||
}
|
||||
|
||||
qDebug() << "Accept registered users only:" << getRegOnlyServerEnabled();
|
||||
qDebug() << "Registration enabled:" << getRegistrationEnabled();
|
||||
if (getRegistrationEnabled()) {
|
||||
|
|
@ -1324,6 +1333,11 @@ int Servatrice::getMaxUsersPerAddress() const
|
|||
return settingsCache->value("security/max_users_per_address", 4).toInt();
|
||||
}
|
||||
|
||||
QString Servatrice::getTrustedSources() const
|
||||
{
|
||||
return settingsCache->value("security/trusted_sources", "127.0.0.1,::1").toString();
|
||||
}
|
||||
|
||||
int Servatrice::getMessageCountingInterval() const
|
||||
{
|
||||
return settingsCache->value("security/message_counting_interval", 10).toInt();
|
||||
|
|
@ -1538,8 +1552,7 @@ bool Servatrice::isRateLimitExempt(const QString &ipAddress) const
|
|||
// Mirrors the max_users_per_address bypass in initSession: trusted sources
|
||||
// (e.g. 127.0.0.1 for Webatrice behind local nginx, where every websocket
|
||||
// client shares one address) are not subject to per-address limits.
|
||||
const QString trustedSources = settingsCache->value("security/trusted_sources", "127.0.0.1,::1").toString();
|
||||
return trustedSources.contains(ipAddress, Qt::CaseInsensitive);
|
||||
return isTrustedSource(getTrustedSources(), ipAddress);
|
||||
}
|
||||
|
||||
void Servatrice::clearFailedLogins(const QString &ipAddress, const QString &userName)
|
||||
|
|
|
|||
|
|
@ -321,6 +321,8 @@ public:
|
|||
int getDeckShareMaxDecksPerShare() const;
|
||||
int getDeckShareMaxSharesPerDay() const;
|
||||
int getMaxUsersPerAddress() const;
|
||||
/// Raw security/trusted_sources list; match addresses against it with isTrustedSource().
|
||||
QString getTrustedSources() const;
|
||||
int getMessageCountingInterval() const override;
|
||||
int getMaxMessageCountPerInterval() const override;
|
||||
int getMaxMessageSizePerInterval() const override;
|
||||
|
|
|
|||
|
|
@ -42,6 +42,7 @@
|
|||
#include "server_logger.h"
|
||||
#include "server_protocolhandler.h"
|
||||
#include "settingscache.h"
|
||||
#include "trusted_sources.h"
|
||||
#include "version_string.h"
|
||||
|
||||
#include <QChar>
|
||||
|
|
@ -220,8 +221,7 @@ bool AbstractServerSocketInterface::initSession()
|
|||
delete identSe;
|
||||
|
||||
// allow unlimited number of connections from the trusted sources
|
||||
QString trustedSources = settingsCache->value("security/trusted_sources", "127.0.0.1,::1").toString();
|
||||
if (trustedSources.contains(getAddress(), Qt::CaseInsensitive)) {
|
||||
if (isTrustedSource(servatrice->getTrustedSources(), getPeerAddress())) {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
@ -1908,9 +1908,8 @@ Response::ResponseCode AbstractServerSocketInterface::cmdBanFromServer(const Com
|
|||
if (amountRemove != 0) {
|
||||
removeSaidMessages(userName, amountRemove);
|
||||
}
|
||||
QString trustedSources = settingsCache->value("server/trusted_sources", "127.0.0.1,::1").toString();
|
||||
int minutes = cmd.minutes();
|
||||
if (trustedSources.contains(address, Qt::CaseInsensitive)) {
|
||||
if (isTrustedSource(servatrice->getTrustedSources(), address)) {
|
||||
address = "";
|
||||
}
|
||||
|
||||
|
|
@ -4560,8 +4559,7 @@ void WebsocketServerSocketInterface::initConnection(void *_socket)
|
|||
if (websocketIPHeader.length() > 0 && socket->request().hasRawHeader(websocketIPHeader)) {
|
||||
// Only trust the forwarded header if the connection itself comes from a trusted proxy,
|
||||
// otherwise a client could spoof its address to bypass bans and rate limits.
|
||||
QString trustedSources = settingsCache->value("security/trusted_sources", "127.0.0.1,::1").toString();
|
||||
if (trustedSources.contains(socket->peerAddress().toString(), Qt::CaseInsensitive)) {
|
||||
if (isTrustedSource(servatrice->getTrustedSources(), socket->peerAddress())) {
|
||||
QString header(socket->request().rawHeader(websocketIPHeader));
|
||||
QHostAddress parsed(header);
|
||||
if (!parsed.isNull()) {
|
||||
|
|
|
|||
23
servatrice/src/trusted_sources.cpp
Normal file
23
servatrice/src/trusted_sources.cpp
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
#include "trusted_sources.h"
|
||||
|
||||
#include <QStringList>
|
||||
|
||||
bool isTrustedSource(const QString &trustedSources, const QHostAddress &address)
|
||||
{
|
||||
if (address.isNull()) {
|
||||
return false;
|
||||
}
|
||||
const QStringList entries = trustedSources.split(',', Qt::SkipEmptyParts);
|
||||
for (const QString &entry : entries) {
|
||||
const QHostAddress trusted(entry.trimmed());
|
||||
if (!trusted.isNull() && trusted.isEqual(address, QHostAddress::ConvertV4MappedToIPv4)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool isTrustedSource(const QString &trustedSources, const QString &address)
|
||||
{
|
||||
return isTrustedSource(trustedSources, QHostAddress(address.trimmed()));
|
||||
}
|
||||
20
servatrice/src/trusted_sources.h
Normal file
20
servatrice/src/trusted_sources.h
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
#ifndef TRUSTED_SOURCES_H
|
||||
#define TRUSTED_SOURCES_H
|
||||
|
||||
#include <QHostAddress>
|
||||
#include <QString>
|
||||
|
||||
/**
|
||||
* @brief True if @p address is listed in the comma-separated @p trustedSources (security/trusted_sources).
|
||||
*
|
||||
* Entries are parsed as addresses and compared exactly, never as substrings, so
|
||||
* 27.0.0.1 does not match 127.0.0.1. IPv4-mapped IPv6 addresses compare equal to
|
||||
* their IPv4 form: a dual-stack listener reports a local proxy as ::ffff:127.0.0.1,
|
||||
* which matches a 127.0.0.1 entry. Malformed entries and null addresses never match.
|
||||
*/
|
||||
bool isTrustedSource(const QString &trustedSources, const QHostAddress &address);
|
||||
|
||||
/** @brief Overload for an address in string form; an unparsable string never matches. */
|
||||
bool isTrustedSource(const QString &trustedSources, const QString &address);
|
||||
|
||||
#endif
|
||||
Loading…
Add table
Add a link
Reference in a new issue