Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check server addresses against common mistakes #62

Merged
merged 2 commits into from
Mar 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion config-example.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

# Enables detailed tracebacks and an interactive Python console on errors.
# Never use in production!
DEBUG = False
Expand Down Expand Up @@ -26,3 +25,6 @@
# This WILL cause problems such as mapgen, mods and privilege information missing from the list
ALLOW_UPDATE_WITHOUT_OLD = False

# Reject servers with private addresses and domain names.
# Enable this if you are running a list on the public internet.
REJECT_PRIVATE_ADDRESSES = False
83 changes: 80 additions & 3 deletions server.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,28 @@

# Helpers

# checkRequestAddress() error codes
ADDR_IS_PRIVATE = 1
ADDR_IS_INVALID = 2
ADDR_IS_INVALID_PORT = 3
ADDR_IS_UNICODE = 4
ADDR_IS_EXAMPLE = 5

ADDR_ERROR_HELP_TEXTS = {
ADDR_IS_PRIVATE: "The server_address you provided is private or local. "
"It is only reachable in your local network.\n"
"If you meant to host a public server, adjust the setting and make sure your "
"firewall is permitting connections (e.g. port forwarding).",
ADDR_IS_INVALID: "The server_address you provided is invalid.\n"
"If you do not have a domain name or need to configure the external IP, "
"try removing the setting from your configuration.",
ADDR_IS_INVALID_PORT: "The server_address you provided is invalid.\n"
"Note that the value must not include a port number.",
ADDR_IS_UNICODE: "The server_address you provided includes Unicode characters.\n"
"If you have a domain name please enter the punycode notation.",
ADDR_IS_EXAMPLE: "The server_address you provided is an example value.",
}

def geoip_lookup_continent(ip):
if ip.startswith("::ffff:"):
ip = ip[7:]
Expand Down Expand Up @@ -133,6 +155,12 @@ def announce():
else:
return "Server to update not found."

# Since 'address' isn't the primary key it can change
if action == "start" or old.get("address") != server.get("address"):
err = checkRequestAddress(server)
if err:
return ADDR_ERROR_HELP_TEXTS[err], 400

server["update_time"] = int(time.time())

if action == "start":
Expand Down Expand Up @@ -177,6 +205,7 @@ def announce():

# Returns ping time in seconds (up), False (down), or None (error).
def serverUp(info):
sock = None
try:
sock = socket.socket(info[0], info[1], info[2])
sock.settimeout(3)
Expand Down Expand Up @@ -214,12 +243,60 @@ def serverUp(info):
# [8] u8 controltype (CONTROLTYPE_DISCO)
buf = b"\x4f\x45\x74\x03" + peer_id + b"\x00\x00\x03"
sock.send(buf)
sock.close()
return end - start
except socket.timeout:
except (socket.timeout, socket.error):
return False
except:
except Exception as e:
app.logger.warning("Unexpected exception during serverUp: %r", e)
return None
finally:
if sock:
sock.close()


def checkRequestAddress(server):
# will fall back to IP of requester, can't possibly be wrong
if "address" not in server or not server["address"]:
return

name = server["address"].lower()

# example value from minetest.conf
if name == "game.minetest.net":
return ADDR_IS_EXAMPLE

# length limit for good measure
if len(name) > 255:
return ADDR_IS_INVALID
# characters invalid in DNS names and IPs
if any(c in name for c in " @#/*\"'\t\v\r\n\x00") or name.startswith("-"):
return ADDR_IS_INVALID
# if not ipv6, there must be at least one dot (two components)
# Note: This is not actually true ('com' is valid domain), but we'll assume
# nobody who owns a TLD will ever host a Minetest server on the root domain.
# getaddrinfo also allows specifying IPs as integers, we don't want people
# to do that either.
if ":" not in name and "." not in name:
return ADDR_IS_INVALID

if app.config["REJECT_PRIVATE_ADDRESSES"]:
# private IPs (there are more but in practice these are 99% of cases)
PRIVATE_NETS = ("10.", "192.168.", "127.", "0.")
if any(name.startswith(s) for s in PRIVATE_NETS):
return ADDR_IS_PRIVATE
# reserved TLDs
RESERVED_TLDS = (".localhost", ".local", ".internal")
if name == "localhost" or any(name.endswith(s) for s in RESERVED_TLDS):
return ADDR_IS_PRIVATE

# ipv4/domain with port -or- ipv6 bracket notation
if ("." in name and ":" in name) or (":" in name and "[" in name):
return ADDR_IS_INVALID_PORT

# unicode in hostname
# Not sure about Python but the Minetest client definitely doesn't support it.
if any(ord(c) > 127 for c in name):
return ADDR_IS_UNICODE


# fieldName: (Required, Type, SubType)
Expand Down