askrobots / dbbasic-object-server
Public
Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.
danq9
signup: the person chooses the password and name on the confirm page
83c7aa6
2 weeks, 5 days ago
History
sites_page.py · 3.7 KB · 94 lines
| 1 | """A published page, GET /s/{site} and /s/{site}/{page} (app-sites). |
| 2 | |
| 3 | Webmaster (or any site builder) publishes each page as a site_pages record; |
| 4 | this serves it. The HTML is whatever the author exported, scripts included, |
| 5 | so it is never trusted: |
| 6 | |
| 7 | - Served with `Content-Security-Policy: sandbox allow-scripts allow-forms |
| 8 | allow-popups allow-popups-to-escape-sandbox` -- deliberately WITHOUT |
| 9 | allow-same-origin. The page runs in an opaque origin: its scripts cannot |
| 10 | read this server's cookies (the visitor's session) or call its API as the |
| 11 | visitor, which is the whole danger of serving user HTML from the app's own |
| 12 | origin. A dedicated hostname per site (site_hosts) is the stronger shape |
| 13 | later; the sandbox is what makes the shared origin safe now. |
| 14 | - `X-Content-Type-Options: nosniff` and `Referrer-Policy: no-referrer`. |
| 15 | - Shown only when the record is public or the visitor owns it. Anything |
| 16 | else, including a page that does not exist, is the same plain 404: a |
| 17 | private page's existence is not revealed. |
| 18 | |
| 19 | Links between pages: a builder exports "about.html"; published under |
| 20 | /s/{site}/ the page slug is "about", and /s/{site}/about.html is accepted |
| 21 | too (the ".html" is dropped), so relative links keep working. The site's |
| 22 | bare address /s/{site} redirects to /s/{site}/index: from there a relative |
| 23 | "about.html" is /s/{site}/about.html, where from /s/{site} a browser would |
| 24 | resolve it to /s/about.html (tried: the home page's menu led nowhere). |
| 25 | """ |
| 26 | |
| 27 | import html as html_lib |
| 28 | import os |
| 29 | import re |
| 30 | |
| 31 | import object_records |
| 32 | |
| 33 | DATA_DIR_ENV = "DBBASIC_DATA_DIR" |
| 34 | COLLECTION = "site_pages" |
| 35 | _NAME_RE = re.compile(r"^[a-z0-9][a-z0-9-]{0,79}$") |
| 36 | |
| 37 | SANDBOX = "sandbox allow-scripts allow-forms allow-popups allow-popups-to-escape-sandbox" |
| 38 | |
| 39 | |
| 40 | def _data_dir(): |
| 41 | return os.environ.get(DATA_DIR_ENV, object_records.DEFAULT_DATA_DIR) |
| 42 | |
| 43 | |
| 44 | def _headers(content_type="text/html; charset=utf-8"): |
| 45 | return { |
| 46 | "content-type": content_type, |
| 47 | "content-security-policy": SANDBOX, |
| 48 | "x-content-type-options": "nosniff", |
| 49 | "referrer-policy": "no-referrer", |
| 50 | "cache-control": "no-cache", |
| 51 | } |
| 52 | |
| 53 | |
| 54 | def _not_found(): |
| 55 | body = "<!doctype html><meta charset=utf-8><title>Not found</title><h1>Not found</h1>" |
| 56 | return (404, _headers(), body) |
| 57 | |
| 58 | |
| 59 | def find_page(site, slug, user_id, rows): |
| 60 | """The record for site/slug if the visitor may see it, else None.""" |
| 61 | for row in rows: |
| 62 | if row.get("site") != site or row.get("slug") != slug: |
| 63 | continue |
| 64 | if str(row.get("is_public") or "").lower() == "true": |
| 65 | return row |
| 66 | if user_id and row.get("owner_id") == user_id: |
| 67 | return row |
| 68 | return None |
| 69 | return None |
| 70 | |
| 71 | |
| 72 | def GET(request): |
| 73 | site = str(request.get("site") or "").strip().lower() |
| 74 | if "page" not in request and _NAME_RE.fullmatch(site): |
| 75 | # the site's home: send the browser into the site's own folder |
| 76 | return (302, {"location": f"/s/{site}/index", "cache-control": "no-cache"}, "") |
| 77 | page = str(request.get("page") or "index").strip().lower() |
| 78 | if page.endswith(".html"): |
| 79 | page = page[: -len(".html")] |
| 80 | if not _NAME_RE.fullmatch(site) or not _NAME_RE.fullmatch(page): |
| 81 | return _not_found() |
| 82 | identity = request.get("_identity") or {} |
| 83 | try: |
| 84 | rows = object_records.read_collection_records(COLLECTION, base_dir=_data_dir()) |
| 85 | except Exception: # the collection is not installed yet: nothing is published |
| 86 | return _not_found() |
| 87 | row = find_page(site, page, identity.get("user_id"), rows) |
| 88 | if row is None: |
| 89 | return _not_found() |
| 90 | body = row.get("html") or "" |
| 91 | if not body.strip(): |
| 92 | title = html_lib.escape(row.get("title") or page) |
| 93 | body = f"<!doctype html><meta charset=utf-8><title>{title}</title><h1>{title}</h1>" |
| 94 | return (200, _headers(), body) |