Protect managed tool hostnames from website claims

This commit is contained in:
parkel committed 2026-12-01 15:10:23 +01:00
1 parent c29874fa60
commit 471b76fd98
4 files changed
+60 -7

No files matched your search

@@ -10,6 +10,7 @@ from coulobre.web_node import WebSite
from coulobre.web_node_apply import ( from coulobre.web_node_apply import (
SitePaths, SitePaths,
install_configurations, install_configurations,
reserve_account,
site_status, site_status,
) )
@@ -34,6 +35,18 @@ class PhpRuntimeTests(SimpleTestCase):
class WebNodeConfigurationTests(SimpleTestCase): class WebNodeConfigurationTests(SimpleTestCase):
def test_primary_website_cannot_claim_a_managed_tool_hostname(self):
website = WebSite(7, "tools.example.test")
with (
patch(
"coulobre.web_node_apply.read_root_file",
return_value="<VirtualHost *:443>\n"
" ServerName tools.example.test\n</VirtualHost>\n",
),
self.assertRaisesRegex(ValueError, "reserved for a panel tool"),
):
reserve_account(website, "fixture", ())
def setUp(self): def setUp(self):
directory = Path(self.enterContext(TemporaryDirectory())) directory = Path(self.enterContext(TemporaryDirectory()))
apache_available = directory / "sites-available" apache_available = directory / "sites-available"
+26 -7
View File
@@ -1,5 +1,6 @@
"""Install hosting tools on dedicated HTTPS hosts.""" """Install hosting tools on dedicated HTTPS hosts."""
import fcntl
import hashlib import hashlib
import json import json
import os import os
@@ -15,6 +16,7 @@ from types import SimpleNamespace
from urllib.parse import urlsplit from urllib.parse import urlsplit
from . import local_update from . import local_update
from . import web_node_apply as web
from .domain_names import canonical_domain from .domain_names import canonical_domain
from .linked_updates import REMOTE_MODES, receiver_status from .linked_updates import REMOTE_MODES, receiver_status
from .local_feature_setup import ( from .local_feature_setup import (
@@ -473,13 +475,8 @@ def apply_tool(request, config, origin, mail_host, command):
raise ValueError("Review the existing tool integration before setup.") raise ValueError("Review the existing tool integration before setup.")
if not keys and original.rstrip("\n").endswith("\\"): if not keys and original.rstrip("\n").endswith("\\"):
raise ValueError("Review the end of the panel configuration.") raise ValueError("Review the end of the panel configuration.")
existing = command("/usr/sbin/apache2ctl", "-S")
site = WEB_CONFIG / "sites-available" / f"{base}.conf" site = WEB_CONFIG / "sites-available" / f"{base}.conf"
if ( check_tool_hostname(hostname, site, command)
re.search(rf"\b{re.escape(hostname)}\b", existing)
and f"/{site.name}:" not in existing
):
raise ValueError("Choose a hostname Apache does not already serve.")
chain, key = tool_certificate(hostname, command) chain, key = tool_certificate(hostname, command)
php = install_packages(target, command) php = install_packages(target, command)
create_service_identity(base, base, command) create_service_identity(base, base, command)
@@ -508,8 +505,30 @@ def apply_tool(request, config, origin, mail_host, command):
updated.rstrip("\n") updated.rstrip("\n")
+ f"\n{marker}\n{prefix}_BRIDGE_KEY={bridge}\n" + f"\n{marker}\n{prefix}_BRIDGE_KEY={bridge}\n"
) )
require_root_directory(web.STATE_DIRECTORY.parent, create_mode=0o750)
require_root_directory(web.STATE_DIRECTORY, create_mode=0o700)
with web.locked_web_node(fcntl.LOCK_EX):
check_tool_hostname(hostname, site, command)
configure_apache(target, hostname, chain, key, site, php, command) configure_apache(target, hostname, chain, key, site, php, command)
activate_tool(site, php, config, original, updated, url, origin, command) activate_tool(
site, php, config, original, updated, url, origin, command
)
def check_tool_hostname(hostname, site, command):
from .web_node_aliases import check_alias_domain_available
web.reject_tool_hostname(hostname, except_site=site)
check_alias_domain_available(hostname)
for marker in web.STATE_DIRECTORY.glob("*.json"):
if json.loads(read_root_file(marker))["domain"] == hostname:
raise ValueError("Choose a hostname away from hosted websites.")
existing = command("/usr/sbin/apache2ctl", "-S")
if (
re.search(rf"\b{re.escape(hostname)}\b", existing)
and f"/{site.name}:" not in existing
):
raise ValueError("Choose a hostname Apache does not already serve.")
def configure_apache(target, hostname, chain, key, site, php, command): def configure_apache(target, hostname, chain, key, site, php, command):
+1
View File
@@ -64,6 +64,7 @@ def check_apache_hostname_available(domain):
def reserve_alias(alias): def reserve_alias(alias):
web.reject_tool_hostname(alias.domain)
for marker in web.STATE_DIRECTORY.glob("*.json"): for marker in web.STATE_DIRECTORY.glob("*.json"):
if json.loads(read_root_file(marker))["domain"] == alias.domain: if json.loads(read_root_file(marker))["domain"] == alias.domain:
raise ValueError( raise ValueError(
+20
View File
@@ -294,6 +294,7 @@ def check_services(runtime):
def reserve_account(site, user, paths): def reserve_account(site, user, paths):
reject_tool_hostname(site.domain)
marker = STATE_DIRECTORY / f"{site.node_key}.json" marker = STATE_DIRECTORY / f"{site.node_key}.json"
if os.path.lexists(marker.with_suffix(".retired")): if os.path.lexists(marker.with_suffix(".retired")):
raise ValueError("This website belongs to a retired hosting account.") raise ValueError("This website belongs to a retired hosting account.")
@@ -333,6 +334,25 @@ def reserve_account(site, user, paths):
write_new_file(marker, json.dumps(manifest) + "\n", mode=0o600) write_new_file(marker, json.dumps(manifest) + "\n", mode=0o600)
def reject_tool_hostname(hostname, *, except_site=None):
for tool in ("phpmyadmin", "roundcube"):
site = APACHE_AVAILABLE / f"coulobre-{tool}.conf"
if site == except_site:
continue
try:
configuration = read_root_file(site)
except FileNotFoundError:
continue
for line in configuration.splitlines():
directive = line.split()
if (
len(directive) == 2
and directive[0].lower() == "servername"
and directive[1].lower().rstrip(".") == hostname
):
raise ValueError("This hostname is reserved for a panel tool.")
def current_user(name): def current_user(name):
try: try:
return pwd.getpwnam(name) return pwd.getpwnam(name)