diff --git a/src/coulobre/hosting/templates/hosting/website_starter.html b/src/coulobre/hosting/templates/hosting/website_starter.html new file mode 100644 index 0000000..e4a8692 --- /dev/null +++ b/src/coulobre/hosting/templates/hosting/website_starter.html @@ -0,0 +1,89 @@ + + + + + + + +This website is coming soon · __COULOBRE_HOSTNAME__ + + + +
+

__COULOBRE_HOSTNAME__

+

This website is coming soon

+

Hosting is set up. The site owner hasn’t published the website yet.

+ +
+ + diff --git a/src/coulobre/test_website_starter.py b/src/coulobre/test_website_starter.py index fe9e2fa..747069e 100644 --- a/src/coulobre/test_website_starter.py +++ b/src/coulobre/test_website_starter.py @@ -3,7 +3,13 @@ from pathlib import Path from tempfile import TemporaryDirectory from unittest import TestCase -from .web_node_apply import STARTER_PAGE, install_starter_page +from .web_node_apply import ( + LEGACY_STARTER_PAGE, + STARTER_PAGE, + install_starter_page, + render_starter_page, +) +from .wordpress_node import check_install_target class WebsiteStarterTests(TestCase): @@ -14,12 +20,16 @@ class WebsiteStarterTests(TestCase): self.index = self.public / "index.html" def install(self): - install_starter_page(self.public, os.getuid(), os.getgid()) + install_starter_page( + self.public, os.getuid(), os.getgid(), "site.example" + ) def test_empty_root_gets_a_neutral_page_only_once(self): self.install() - self.assertEqual(self.index.read_text(), STARTER_PAGE) - self.assertIn('name="robots" content="noindex"', STARTER_PAGE) + page = self.index.read_text() + self.assertEqual(page, render_starter_page("site.example")) + self.assertIn("site.example", page) + self.assertIn('name="robots" content="noindex"', page) self.assertEqual(self.index.stat().st_mode & 0o777, 0o644) self.index.write_text("Published customer site") self.install() @@ -45,3 +55,16 @@ class WebsiteStarterTests(TestCase): with self.assertRaises(OSError): self.install() self.assertFalse((actual / "index.html").exists()) + + def test_wordpress_accepts_only_unchanged_starter_pages(self): + for starter in ( + LEGACY_STARTER_PAGE, + STARTER_PAGE, + render_starter_page("host.example.test"), + ): + with self.subTest(starter=starter[:60]): + self.index.write_text(starter) + check_install_target(self.public) + self.index.write_text(starter + " ") + with self.assertRaisesRegex(ValueError, "original.*starter"): + check_install_target(self.public) diff --git a/src/coulobre/web_node_apply.py b/src/coulobre/web_node_apply.py index d3ed8c9..0f24f3d 100644 --- a/src/coulobre/web_node_apply.py +++ b/src/coulobre/web_node_apply.py @@ -3,14 +3,18 @@ import grp import json import os import pwd +import re import socket import stat import subprocess import time from contextlib import contextmanager from dataclasses import dataclass +from functools import cache +from html import escape from pathlib import Path +from .domain_names import canonical_domain from .node_files import ( check_root_file, read_root_file, @@ -48,6 +52,46 @@ STARTER_PAGE = ( ) +STARTER_TEMPLATE = ( + Path(__file__).parent / "hosting/templates/hosting/website_starter.html" +) +STARTER_HOST = re.compile(rb'

([a-z0-9.-]{1,253})

') + + +@cache +def starter_page_template(): + return STARTER_TEMPLATE.read_text(encoding="utf-8") + + +def render_starter_page(hostname): + return starter_page_template().replace( + "__COULOBRE_HOSTNAME__", escape(canonical_domain(hostname)) + ) + + +def starter_page_matches(page): + if page in (STARTER_PAGE.encode(), LEGACY_STARTER_PAGE.encode()): + return True + if match := STARTER_HOST.search(page): + try: + expected = render_starter_page(match[1].decode("ascii")) + except ValueError: + return False + return page == expected.encode() + return False + + +def starter_page_read_limit(): + return ( + max( + len(STARTER_PAGE.encode()), + len(LEGACY_STARTER_PAGE.encode()), + len(starter_page_template().encode()) + 2 * 253, + ) + + 1 + ) + + @dataclass(frozen=True) class SitePaths: account: Path @@ -182,20 +226,21 @@ def install_site(site, *, php_settings=None, allowed=(), starter_page=False): document_root = paths.account / "public" ensure_public_directory(document_root, uid, web_gid) if starter_page: - install_starter_page(document_root, uid, web_gid) + install_starter_page(document_root, uid, web_gid, site.domain) if create_route: paths.route.symlink_to(paths.socket) applied = install_configurations(site, paths, php_settings=php_settings) return applied, paths.runtime.version -def install_starter_page(document_root, uid, gid): +def install_starter_page(document_root, uid, gid, hostname): directory = os.open( document_root, os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW ) try: if os.listdir(directory): return + content = render_starter_page(hostname) try: file = os.open( "index.html", @@ -206,7 +251,7 @@ def install_starter_page(document_root, uid, gid): except FileExistsError: return with os.fdopen(file, "w", encoding="utf-8") as page: - page.write(STARTER_PAGE) + page.write(content) page.flush() os.fchown(page.fileno(), uid, gid) os.fchmod(page.fileno(), 0o644) diff --git a/src/coulobre/wordpress_node.py b/src/coulobre/wordpress_node.py index ee759d8..28c7e98 100644 --- a/src/coulobre/wordpress_node.py +++ b/src/coulobre/wordpress_node.py @@ -147,12 +147,11 @@ def is_starter_page(index): ) except OSError: return False - expected = (web.STARTER_PAGE.encode(), web.LEGACY_STARTER_PAGE.encode()) with os.fdopen(descriptor, "rb") as page: - return ( - stat.S_ISREG(os.fstat(page.fileno()).st_mode) - and page.read(max(map(len, expected)) + 1) in expected - ) + if not stat.S_ISREG(os.fstat(page.fileno()).st_mode): + return False + contents = page.read(web.starter_page_read_limit()) + return web.starter_page_matches(contents) def check_install_target(public):