Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.

0 downloads

Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-object-server.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-object-server.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-object-server.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

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
3Webmaster (or any site builder) publishes each page as a site_pages record;
4this serves it. The HTML is whatever the author exported, scripts included,
5so 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
19Links between pages: a builder exports "about.html"; published under
20/s/{site}/ the page slug is "about", and /s/{site}/about.html is accepted
21too (the ".html" is dropped), so relative links keep working. The site's
22bare 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
24resolve it to /s/about.html (tried: the home page's menu led nowhere).
25"""
26
27import html as html_lib
28import os
29import re
30
31import object_records
32
33DATA_DIR_ENV = "DBBASIC_DATA_DIR"
34COLLECTION = "site_pages"
35_NAME_RE = re.compile(r"^[a-z0-9][a-z0-9-]{0,79}$")
36
37SANDBOX = "sandbox allow-scripts allow-forms allow-popups allow-popups-to-escape-sandbox"
38
39
40def _data_dir():
41 return os.environ.get(DATA_DIR_ENV, object_records.DEFAULT_DATA_DIR)
42
43
44def _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
54def _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
59def 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
72def 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)