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
privacy.py · 36.7 KB · 897 lines
| 1 | """site_privacy -- a privacy policy that is a FOLD over this server's |
| 2 | configuration, not a template somebody filled in once. |
| 3 | |
| 4 | GET /privacy the policy |
| 5 | GET /cookies the cookie table on its own |
| 6 | GET /privacy.json the same fold, machine-readable, for anyone who wants |
| 7 | to diff it between deploys |
| 8 | |
| 9 | Almost every privacy policy on the web is a lie of omission, and not |
| 10 | because anyone set out to lie: the policy is written once, by a template, |
| 11 | and the software changes weekly. Nothing connects the two, so nothing |
| 12 | notices when they part company. This one is generated from the live |
| 13 | settings, so it cannot claim a 30-day retention while the box keeps 180, |
| 14 | cannot mention a payment processor that is not configured, and cannot |
| 15 | stay silent about a cookie the server sets. |
| 16 | |
| 17 | The load-bearing property is that it is a fold, in the same family as the |
| 18 | nav registry (`action_nav_entries`) and the attention counts: install the |
| 19 | shop and the policy grows a payments paragraph with nobody remembering to |
| 20 | write one; switch analytics off and the traffic section disappears |
| 21 | entirely. A policy that CAN drift from the software is the normal case; |
| 22 | one that cannot is worth having. |
| 23 | |
| 24 | ## What this object refuses to do, and why it is a refusal rather than a gap |
| 25 | |
| 26 | **It does not write legal text.** No lawful bases, no legitimate-interest |
| 27 | balancing, no Standard Contractual Clauses, no adequacy decisions, no |
| 28 | transfer mechanisms, no retention justifications, no claims about which |
| 29 | regimes apply to the operator's business. It states FACTS about this |
| 30 | system -- what is stored, where, for how long, which third parties are |
| 31 | configured, what a cookie does -- and stops. |
| 32 | |
| 33 | That boundary is the same one this system holds everywhere else. A |
| 34 | generated paragraph asserting "we rely on legitimate interests under |
| 35 | Article 6(1)(f)" would be a confident sentence about somebody else's legal |
| 36 | position, produced by a program that cannot know their business, their |
| 37 | customers or their regulator, and pasted into a document a regulator |
| 38 | reads. Wrong legal text is worse than no legal text, because it is |
| 39 | evidence of a claim the operator never made and cannot support. |
| 40 | |
| 41 | `privacy.extra_markdown` is where the operator's own reviewed wording |
| 42 | goes. It is rendered verbatim, under its own heading, so a reader can see |
| 43 | which half of the page is machine-generated fact and which half is a |
| 44 | human's legal statement. (`app_settings` caps a value at 500 characters, |
| 45 | so long text belongs in a document this links to rather than inline.) |
| 46 | |
| 47 | **It refuses to render at all until it is signed.** A policy with no named |
| 48 | controller and no contact address is not a policy; it is a page shaped |
| 49 | like one, and it is worse than nothing because it looks like compliance |
| 50 | while giving a reader nobody to write to. Absent |
| 51 | `privacy.controller_name`, `privacy.contact_email` or |
| 52 | `privacy.jurisdiction`, this page says exactly which settings are missing |
| 53 | and renders no claims whatsoever about the system. The cookie table is |
| 54 | still served at /cookies, because the browser-facing facts are true |
| 55 | regardless of who signed them. |
| 56 | |
| 57 | ## The settings it folds |
| 58 | |
| 59 | privacy.controller_name who is responsible for the data (required) |
| 60 | privacy.contact_email where a subject writes (required) |
| 61 | privacy.jurisdiction where the controller is established (required) |
| 62 | privacy.extra_markdown the operator's own reviewed legal text |
| 63 | |
| 64 | Everything else is read from the running configuration: the analytics |
| 65 | env, the cookie switches, which packages are installed, and which third |
| 66 | parties actually have credentials. |
| 67 | """ |
| 68 | |
| 69 | import html |
| 70 | import json |
| 71 | import os |
| 72 | from pathlib import Path |
| 73 | |
| 74 | import object_ai |
| 75 | import object_analytics |
| 76 | import object_email |
| 77 | import object_identity |
| 78 | import object_package_baselines |
| 79 | import object_packages |
| 80 | import object_records |
| 81 | import object_service_keys |
| 82 | import object_stripe |
| 83 | |
| 84 | DATA_DIR_ENV = "DBBASIC_DATA_DIR" |
| 85 | SETTINGS_COLLECTION = "app_settings" |
| 86 | |
| 87 | CONTROLLER_NAME_KEY = "privacy.controller_name" |
| 88 | CONTACT_EMAIL_KEY = "privacy.contact_email" |
| 89 | JURISDICTION_KEY = "privacy.jurisdiction" |
| 90 | EXTRA_MARKDOWN_KEY = "privacy.extra_markdown" |
| 91 | CARRIER_PROVIDER_KEY = "carrier.provider" |
| 92 | |
| 93 | REQUIRED_SETTINGS = ( |
| 94 | (CONTROLLER_NAME_KEY, "the name of the person or company responsible for this data"), |
| 95 | (CONTACT_EMAIL_KEY, "an address a visitor can actually write to"), |
| 96 | (JURISDICTION_KEY, "where that controller is established, e.g. 'England and Wales'"), |
| 97 | ) |
| 98 | |
| 99 | # The shop's basket cookie lives in app-shop (objects/site/shop.py, COOKIE) |
| 100 | # and its lifetime is a literal in the header that object builds. Restated |
| 101 | # here because a package may not import another package's objects -- and |
| 102 | # pinned by tests/test_privacy.py, which reads the literal back out of |
| 103 | # app-shop's source and fails if the two ever disagree. A disclosure that |
| 104 | # can drift from the cookie it describes is the drift this whole page |
| 105 | # exists to prevent. |
| 106 | CART_COOKIE_NAME = "cart" |
| 107 | CART_COOKIE_MAX_AGE = 1209600 # 14 days |
| 108 | CART_COOKIE_PACKAGE = "app-shop" |
| 109 | |
| 110 | # The apps whose presence changes what this policy says, and the |
| 111 | # collection each one owns. Nothing else is folded, and nothing else is |
| 112 | # reported: /privacy is public, and the full inventory of what an |
| 113 | # operator installed is not a visitor's business. |
| 114 | FOLDED_PACKAGES = { |
| 115 | "app-shop": ("carts",), |
| 116 | "app-orders": ("orders",), |
| 117 | "app-invoices": ("invoices",), |
| 118 | "app-payments": ("payments",), |
| 119 | "app-shipping": ("shipments",), |
| 120 | } |
| 121 | |
| 122 | |
| 123 | # === the registry of cookies this server can set ============================ |
| 124 | # |
| 125 | # THE list. tests/test_privacy.py sweeps every source file on the box for |
| 126 | # anything that sets a cookie and fails unless each name it finds appears |
| 127 | # here, naming the undisclosed cookie in the failure message. A cookie |
| 128 | # added anywhere in this repo therefore breaks the build until somebody |
| 129 | # writes down what it is for and whether it is strictly necessary. |
| 130 | # |
| 131 | # `strictly_necessary` is the ePrivacy question and it is the only one on |
| 132 | # this page with a consent consequence: a cookie needed for a service the |
| 133 | # user actively asked for (the session they signed in to, the basket they |
| 134 | # filled) needs no consent; anything else does. Two of three here are |
| 135 | # necessary, which is why this server's default posture needs no banner. |
| 136 | COOKIE_DISCLOSURES = ( |
| 137 | { |
| 138 | "name": "dbbasic_session", |
| 139 | "purpose": "Keeps you signed in after you enter your password.", |
| 140 | "strictly_necessary": True, |
| 141 | "necessary_because": "you asked to sign in, and there is no way to stay " |
| 142 | "signed in without it", |
| 143 | "set_when": "You sign in.", |
| 144 | "requires": "", |
| 145 | }, |
| 146 | { |
| 147 | "name": CART_COOKIE_NAME, |
| 148 | "purpose": "Remembers which basket is yours, so what you put in it is " |
| 149 | "still there on the next page. It holds an opaque token, " |
| 150 | "not the contents.", |
| 151 | "strictly_necessary": True, |
| 152 | "necessary_because": "you asked for a basket; a shop cannot keep one " |
| 153 | "without a way to recognise it", |
| 154 | "set_when": "You put something in the basket.", |
| 155 | "requires": "package:" + CART_COOKIE_PACKAGE, |
| 156 | }, |
| 157 | { |
| 158 | "name": object_analytics.VISITOR_COOKIE_NAME, |
| 159 | "purpose": "An opaque random token used to tell a returning visit from " |
| 160 | "a new one in this site's own traffic figures. It carries no " |
| 161 | "name, no email and no account, and it is never joined to a " |
| 162 | "signed-in identity.", |
| 163 | "strictly_necessary": False, |
| 164 | "necessary_because": "", |
| 165 | "set_when": "You open a page — unless your browser sends Do Not Track " |
| 166 | "or Global Privacy Control, in which case it is never set.", |
| 167 | "requires": "setting:visitor_cookie", |
| 168 | }, |
| 169 | ) |
| 170 | |
| 171 | # What a page_views row actually holds. Named field by field rather than |
| 172 | # summarised, because "usage data" is the phrasing that lets a policy mean |
| 173 | # anything. |
| 174 | PAGE_VIEW_FIELDS = ( |
| 175 | ("IP address", "the address the request came from"), |
| 176 | ("Host", "which of this server's sites was asked"), |
| 177 | ("Path and method", "the URL requested and how"), |
| 178 | ("Status", "what the server answered"), |
| 179 | ("User agent", "the browser or client string your software sent"), |
| 180 | ("Referrer", "the page that linked you here, if your browser sent one"), |
| 181 | ("Visitor token", "the dbbasic_visitor cookie value, when that cookie is on"), |
| 182 | ("Timestamp", "when"), |
| 183 | ) |
| 184 | |
| 185 | _STYLE = """ |
| 186 | .pp { max-width: 46rem; } |
| 187 | .pp h2 { font-size: 1.05rem; margin: 1.8rem 0 .4rem; } |
| 188 | .pp h3 { font-size: .95rem; margin: 1.2rem 0 .3rem; } |
| 189 | .pp p, .pp li { line-height: 1.55; } |
| 190 | .pp table { width: 100%; border-collapse: collapse; margin: .6rem 0 1.2rem; } |
| 191 | .pp th, .pp td { text-align: left; padding: .35rem .5rem; vertical-align: top; |
| 192 | border-bottom: 1px solid var(--line, #38384a); font-size: .9rem; } |
| 193 | .pp .yes { color: var(--accent, #b5713a); font-weight: 600; } |
| 194 | .pp .note { border-left: 3px solid var(--line, #55556a); padding: .1rem 0 .1rem .7rem; |
| 195 | margin: .6rem 0 1.2rem; font-size: .88rem; opacity: .85; } |
| 196 | .pp .refusal { border: 1px solid var(--accent, #b5713a); border-radius: 8px; |
| 197 | padding: .8rem 1rem; margin: 1rem 0; } |
| 198 | .pp code { font-size: .85em; } |
| 199 | """ |
| 200 | |
| 201 | |
| 202 | def _esc(value): |
| 203 | return html.escape(str(value if value is not None else ""), quote=True) |
| 204 | |
| 205 | |
| 206 | def _data_dir(): |
| 207 | return os.environ.get(DATA_DIR_ENV, object_records.DEFAULT_DATA_DIR) |
| 208 | |
| 209 | |
| 210 | # --- reading the box --------------------------------------------------------- |
| 211 | |
| 212 | def _settings(base): |
| 213 | """Every privacy.* setting as a plain dict, missing collection and all. |
| 214 | |
| 215 | Duplicated rather than shared, like every other package that reads |
| 216 | app_settings -- see docs/logic-decisions.md #4.""" |
| 217 | values = {} |
| 218 | try: |
| 219 | rows = object_records.read_collection_records( |
| 220 | SETTINGS_COLLECTION, base_dir=base) |
| 221 | except Exception: |
| 222 | return values |
| 223 | for row in rows: |
| 224 | key = str(row.get("key") or "").strip() |
| 225 | value = str(row.get("value") or "").strip() |
| 226 | if key and value: |
| 227 | values[key] = value |
| 228 | return values |
| 229 | |
| 230 | |
| 231 | def _schema_present(base, collection): |
| 232 | """Whether this box knows a collection at all. |
| 233 | |
| 234 | Cheaper and more honest than reading it: the question is "does this |
| 235 | server store shipments", not "has it stored one yet". An empty |
| 236 | shipments collection is still a server that ships things. |
| 237 | """ |
| 238 | return (Path(base) / "schemas" / f"{collection}.json").is_file() |
| 239 | |
| 240 | |
| 241 | def installed_packages(base, *, root=None): |
| 242 | """Which of the apps this policy folds over are installed on this box. |
| 243 | |
| 244 | Only those apps, deliberately. The full inventory of what an operator |
| 245 | has installed is not a visitor's business and /privacy is public; what |
| 246 | a visitor is owed is the apps that touch their data, which is exactly |
| 247 | the list below. |
| 248 | |
| 249 | Two signals, either sufficient, because either one alone is wrong |
| 250 | somewhere real: |
| 251 | |
| 252 | * an install baseline (object_package_baselines) -- the crisp record |
| 253 | that `install_package` ran here. Absent on a box whose packages |
| 254 | predate baselines, which would drop a payments section from a |
| 255 | server that plainly takes payments. |
| 256 | * the package's own collection present in the data directory -- the |
| 257 | schema exists, so this server stores that kind of record whether or |
| 258 | not anybody stamped a baseline. Emptiness proves nothing either |
| 259 | way: a shipments collection with no rows is still a server that |
| 260 | ships things. |
| 261 | |
| 262 | The manifest merely being present under packages/ is NOT sufficient. |
| 263 | This repo ships every app it has, and a policy that grew a fulfilment |
| 264 | section on a box that never installed the shop would be inventing |
| 265 | processing rather than reporting it. `list_packages` is still what |
| 266 | decides the candidate set, so an app removed from the box drops out |
| 267 | of the policy with it. |
| 268 | """ |
| 269 | installed = set() |
| 270 | try: |
| 271 | summaries = (object_packages.list_packages(root=root) if root |
| 272 | else object_packages.list_packages()) |
| 273 | except Exception: |
| 274 | return installed |
| 275 | shipped = {summary.get("id") or "" for summary in summaries} |
| 276 | |
| 277 | for package_id, collections in FOLDED_PACKAGES.items(): |
| 278 | if package_id not in shipped: |
| 279 | continue |
| 280 | try: |
| 281 | baseline = object_package_baselines.load_baseline( |
| 282 | package_id, base_dir=base) |
| 283 | except Exception: |
| 284 | baseline = None |
| 285 | if baseline is not None or any(_schema_present(base, name) |
| 286 | for name in collections): |
| 287 | installed.add(package_id) |
| 288 | return installed |
| 289 | |
| 290 | |
| 291 | def _ai_services(base): |
| 292 | """AI providers this box has a key for, by name. |
| 293 | |
| 294 | Read from the stored-service-key metadata, never the keys themselves. |
| 295 | Restricted to the services object_ai can actually call, so a key |
| 296 | somebody stored for something else is not reported as an AI |
| 297 | sub-processor. |
| 298 | """ |
| 299 | services = set() |
| 300 | try: |
| 301 | path = object_service_keys.service_keys_path(base) |
| 302 | except Exception: |
| 303 | return [] |
| 304 | try: |
| 305 | import csv |
| 306 | with open(path, newline="") as handle: |
| 307 | for row in csv.DictReader(handle, delimiter="\t"): |
| 308 | service = (row.get("service") or "").strip() |
| 309 | if service in object_ai.SUPPORTED_SERVICES: |
| 310 | services.add(service) |
| 311 | except (OSError, ValueError): |
| 312 | return [] |
| 313 | return sorted(services) |
| 314 | |
| 315 | |
| 316 | # === the fold ================================================================ |
| 317 | |
| 318 | def policy(*, base=None, env=None, packages_root=None): |
| 319 | """Everything the page and the JSON are rendered from. One fold, three |
| 320 | surfaces -- /privacy, /cookies and /privacy.json cannot disagree |
| 321 | because there is nothing for them to disagree about. |
| 322 | |
| 323 | No timestamps in here, deliberately: /privacy.json is meant to be |
| 324 | diffed between deploys, and a generated-at stamp would make every |
| 325 | fetch differ from the last one and hide the changes that matter. |
| 326 | """ |
| 327 | base = _data_dir() if base is None else base |
| 328 | env = os.environ if env is None else env |
| 329 | settings = _settings(base) |
| 330 | installed = installed_packages(base, root=packages_root) |
| 331 | |
| 332 | missing = [key for key, _why in REQUIRED_SETTINGS if not settings.get(key)] |
| 333 | controller = { |
| 334 | "name": settings.get(CONTROLLER_NAME_KEY, ""), |
| 335 | "contact_email": settings.get(CONTACT_EMAIL_KEY, ""), |
| 336 | "jurisdiction": settings.get(JURISDICTION_KEY, ""), |
| 337 | "missing_settings": missing, |
| 338 | "signed": not missing, |
| 339 | } |
| 340 | |
| 341 | analytics_on = object_analytics.analytics_enabled(env) |
| 342 | cookie_on = object_analytics.visitor_cookie_enabled(env) |
| 343 | analytics = { |
| 344 | "enabled": analytics_on, |
| 345 | "visitor_cookie": cookie_on, |
| 346 | "visitor_cookie_days": object_analytics.visitor_days(env) if cookie_on else 0, |
| 347 | "retention_days": object_analytics.retention_days(env) if analytics_on else 0, |
| 348 | "max_rows": object_analytics.max_rows(env) if analytics_on else 0, |
| 349 | "fields": [{"field": name, "meaning": meaning} |
| 350 | for name, meaning in PAGE_VIEW_FIELDS |
| 351 | if analytics_on and (cookie_on or name != "Visitor token")], |
| 352 | "respects_dnt": True, |
| 353 | } |
| 354 | |
| 355 | return { |
| 356 | "controller": controller, |
| 357 | "analytics": analytics, |
| 358 | "cookies": cookie_table(base=base, env=env, installed=installed), |
| 359 | "processing": _processing(base, env, installed), |
| 360 | "subprocessors": subprocessors(base=base, env=env, installed=installed), |
| 361 | "rights": _rights(controller, analytics), |
| 362 | "extra_markdown": settings.get(EXTRA_MARKDOWN_KEY, ""), |
| 363 | "installed_packages": sorted(installed), |
| 364 | } |
| 365 | |
| 366 | |
| 367 | def disclosed_cookie_names(): |
| 368 | """Every cookie name this server is disclosed as being able to set. |
| 369 | |
| 370 | The set the codebase sweep in tests/test_privacy.py checks itself |
| 371 | against. Deliberately NOT filtered by configuration: the sweep is |
| 372 | asking "is this cookie written down anywhere", and a box with no shop |
| 373 | installed must still fail if somebody adds an undisclosed cookie to |
| 374 | the shop. |
| 375 | """ |
| 376 | return {entry["name"] for entry in COOKIE_DISCLOSURES} |
| 377 | |
| 378 | |
| 379 | def cookie_max_age_seconds(name, *, env=None): |
| 380 | """The real Max-Age this server puts on one cookie, in seconds. |
| 381 | |
| 382 | Read from the same places the cookie itself is built from, so the |
| 383 | published number cannot be a number somebody typed. |
| 384 | """ |
| 385 | env = os.environ if env is None else env |
| 386 | if name == object_analytics.VISITOR_COOKIE_NAME: |
| 387 | return object_analytics.visitor_days(env) * 86400 |
| 388 | if name == CART_COOKIE_NAME: |
| 389 | return CART_COOKIE_MAX_AGE |
| 390 | if name == "dbbasic_session": |
| 391 | return object_identity.DEFAULT_SESSION_TTL_SECONDS |
| 392 | return 0 |
| 393 | |
| 394 | |
| 395 | def cookie_table(*, base=None, env=None, installed=None): |
| 396 | """The cookies THIS server, as configured, can put in a browser. |
| 397 | |
| 398 | Filtered by configuration rather than listing the catalogue: telling a |
| 399 | visitor about a basket cookie on a box with no shop is noise, and |
| 400 | listing an analytics cookie that is switched off would be the page |
| 401 | lying in the direction that flatters nobody. |
| 402 | """ |
| 403 | base = _data_dir() if base is None else base |
| 404 | env = os.environ if env is None else env |
| 405 | if installed is None: |
| 406 | installed = installed_packages(base) |
| 407 | |
| 408 | rows = [] |
| 409 | for entry in COOKIE_DISCLOSURES: |
| 410 | requires = entry["requires"] |
| 411 | if requires.startswith("package:"): |
| 412 | if requires.split(":", 1)[1] not in installed: |
| 413 | continue |
| 414 | elif requires == "setting:visitor_cookie": |
| 415 | if not (object_analytics.visitor_cookie_enabled(env) |
| 416 | and object_analytics.analytics_enabled(env)): |
| 417 | continue |
| 418 | seconds = cookie_max_age_seconds(entry["name"], env=env) |
| 419 | rows.append({ |
| 420 | "name": entry["name"], |
| 421 | "purpose": entry["purpose"], |
| 422 | "strictly_necessary": entry["strictly_necessary"], |
| 423 | "necessary_because": entry["necessary_because"], |
| 424 | "set_when": entry["set_when"], |
| 425 | "max_age_seconds": seconds, |
| 426 | "max_age_days": round(seconds / 86400, 2) if seconds else 0, |
| 427 | "first_party": True, |
| 428 | "http_only": True, |
| 429 | "same_site": "Lax", |
| 430 | }) |
| 431 | return rows |
| 432 | |
| 433 | |
| 434 | def _processing(base, env, installed): |
| 435 | """The sections that exist only because the thing they describe does. |
| 436 | |
| 437 | Each entry is a fact about a collection this box actually has, so a |
| 438 | server that does not sell anything has no payments paragraph to be |
| 439 | wrong about. |
| 440 | """ |
| 441 | sections = [] |
| 442 | |
| 443 | if object_analytics.analytics_enabled(env): |
| 444 | sections.append({ |
| 445 | "id": "traffic", |
| 446 | "title": "Traffic to this site", |
| 447 | "why": "To see how many people visit, which pages they read, and " |
| 448 | "to spot attacks and broken links.", |
| 449 | "kept": f"{object_analytics.retention_days(env)} days", |
| 450 | "shared_with": [], |
| 451 | }) |
| 452 | else: |
| 453 | sections.append({ |
| 454 | "id": "traffic", |
| 455 | "title": "Traffic to this site", |
| 456 | "why": "Not collected. This server keeps no record of visits: no " |
| 457 | "page log, no visitor counter, no third-party analytics " |
| 458 | "script of any kind.", |
| 459 | "kept": "nothing is stored", |
| 460 | "shared_with": [], |
| 461 | }) |
| 462 | |
| 463 | if "app-orders" in installed or "app-invoices" in installed: |
| 464 | sections.append({ |
| 465 | "id": "orders", |
| 466 | "title": "Orders and invoices", |
| 467 | "why": "To take, fulfil and account for what you bought. These are " |
| 468 | "business records.", |
| 469 | "kept": "for as long as the law requires business records to be " |
| 470 | "kept, which is longer than you might expect", |
| 471 | "shared_with": [], |
| 472 | }) |
| 473 | |
| 474 | if "app-payments" in installed: |
| 475 | stripe_configured = object_stripe.stripe_config_from_env(env).configured |
| 476 | sections.append({ |
| 477 | "id": "payments", |
| 478 | "title": "Payments", |
| 479 | "why": "To take payment and to reconcile it against your invoice.", |
| 480 | "kept": "with the invoice it pays", |
| 481 | "shared_with": (["Stripe"] if stripe_configured else []), |
| 482 | "note": ("Card details are entered on Stripe's own pages and never " |
| 483 | "reach this server; what it stores is the amount, the " |
| 484 | "date and Stripe's reference." |
| 485 | if stripe_configured else |
| 486 | "No payment provider is configured on this server; " |
| 487 | "payments are recorded by hand."), |
| 488 | }) |
| 489 | |
| 490 | if "app-shipping" in installed: |
| 491 | carrier = _settings(base).get(CARRIER_PROVIDER_KEY, "").strip().lower() |
| 492 | integrated = carrier not in ("", "none", "manual") |
| 493 | sections.append({ |
| 494 | "id": "fulfilment", |
| 495 | "title": "Delivery", |
| 496 | "why": "To get the parcel to the address you gave.", |
| 497 | "kept": "with the order it belongs to", |
| 498 | "shared_with": ([carrier] if integrated else []), |
| 499 | "note": ("Your delivery address and name are sent to this carrier " |
| 500 | "so a label can be produced." |
| 501 | if integrated else |
| 502 | "No carrier account is connected to this server, so no " |
| 503 | "address is sent to one from here."), |
| 504 | }) |
| 505 | |
| 506 | smtp = object_email.smtp_config_from_env(env) |
| 507 | if smtp.mode != "disabled": |
| 508 | sections.append({ |
| 509 | "id": "email", |
| 510 | "title": "Email we send you", |
| 511 | "why": "Order confirmations, invoices and replies to things you " |
| 512 | "asked about.", |
| 513 | "kept": "with the record that caused it", |
| 514 | "shared_with": ([smtp.host] if smtp.mode == "live" and smtp.host |
| 515 | else []), |
| 516 | "note": ("Outgoing mail is handed to this mail server for " |
| 517 | "delivery." if smtp.mode == "live" and smtp.host else |
| 518 | "Mail is written to a local log on this server rather " |
| 519 | "than sent."), |
| 520 | }) |
| 521 | |
| 522 | ai_services = _ai_services(base) |
| 523 | if ai_services: |
| 524 | sections.append({ |
| 525 | "id": "ai", |
| 526 | "title": "AI features", |
| 527 | "why": "Text you type into an AI feature on this server is sent to " |
| 528 | "the provider below to answer it.", |
| 529 | "kept": "the request is not stored by this server beyond its usage " |
| 530 | "record (which service, how many tokens, when)", |
| 531 | "shared_with": list(ai_services), |
| 532 | "note": "AI features are only reachable by a signed-in operator of " |
| 533 | "this server, not by visitors.", |
| 534 | }) |
| 535 | |
| 536 | return sections |
| 537 | |
| 538 | |
| 539 | def subprocessors(*, base=None, env=None, installed=None): |
| 540 | """Third parties that actually have credentials on this box. |
| 541 | |
| 542 | Derived, so it is a list nobody has to remember to update: a key |
| 543 | removed from the environment removes the row, and a provider nobody |
| 544 | configured never appears. It is a list of who this server CAN send |
| 545 | data to, which is the question a reader is asking. |
| 546 | """ |
| 547 | base = _data_dir() if base is None else base |
| 548 | env = os.environ if env is None else env |
| 549 | if installed is None: |
| 550 | installed = installed_packages(base) |
| 551 | |
| 552 | rows = [] |
| 553 | if "app-payments" in installed and object_stripe.stripe_config_from_env(env).configured: |
| 554 | rows.append({ |
| 555 | "name": "Stripe", |
| 556 | "role": "Payment processing", |
| 557 | "data": "your name, email address, and the amount and reference of " |
| 558 | "the payment", |
| 559 | "because": "DBBASIC_STRIPE_SECRET_KEY and " |
| 560 | "DBBASIC_STRIPE_WEBHOOK_SECRET are configured", |
| 561 | }) |
| 562 | |
| 563 | carrier = _settings(base).get(CARRIER_PROVIDER_KEY, "").strip().lower() |
| 564 | if "app-shipping" in installed and carrier not in ("", "none", "manual"): |
| 565 | rows.append({ |
| 566 | "name": carrier, |
| 567 | "role": "Delivery", |
| 568 | "data": "the delivery name and address on your order", |
| 569 | "because": f"app_settings {CARRIER_PROVIDER_KEY} is set to " |
| 570 | f"{carrier!r}", |
| 571 | }) |
| 572 | |
| 573 | smtp = object_email.smtp_config_from_env(env) |
| 574 | if smtp.mode == "live" and smtp.host: |
| 575 | rows.append({ |
| 576 | "name": smtp.host, |
| 577 | "role": "Email delivery", |
| 578 | "data": "your email address and the contents of mail sent to you", |
| 579 | "because": "DBBASIC_SMTP_MODE is live and DBBASIC_SMTP_HOST is set", |
| 580 | }) |
| 581 | |
| 582 | for service in _ai_services(base): |
| 583 | rows.append({ |
| 584 | "name": service, |
| 585 | "role": "AI features", |
| 586 | "data": "the text an operator of this server types into an AI " |
| 587 | "feature, and whatever server records that feature reads " |
| 588 | "to answer it", |
| 589 | "because": f"an API key for {service} is stored on this server", |
| 590 | }) |
| 591 | |
| 592 | return rows |
| 593 | |
| 594 | |
| 595 | def _rights(controller, analytics): |
| 596 | """What a subject can actually be given here, stated as capability |
| 597 | rather than as law. `export` is real -- action_export_subject_data |
| 598 | folds every collection keyed to an email. Erasure is deliberately |
| 599 | absent rather than promised: see that object's docstring.""" |
| 600 | return { |
| 601 | "contact_email": controller["contact_email"], |
| 602 | "export": "Ask, and everything this server holds against your email " |
| 603 | "address can be exported and sent to you.", |
| 604 | "correction": "Ask, and a wrong name, address or email will be " |
| 605 | "corrected.", |
| 606 | "erasure": "Ask. Orders, invoices and payments are business records " |
| 607 | "with a statutory retention period and usually cannot be " |
| 608 | "deleted; contact details that are not part of such a " |
| 609 | "record can be.", |
| 610 | "traffic": ("Traffic records are keyed to an address and a token with " |
| 611 | "no link to a person, so they cannot be searched for you " |
| 612 | "in either direction; they age out on the retention above." |
| 613 | if analytics["enabled"] else |
| 614 | "No traffic records exist, so there are none to ask about."), |
| 615 | } |
| 616 | |
| 617 | |
| 618 | # === rendering =============================================================== |
| 619 | |
| 620 | def _markdown_ish(text): |
| 621 | """Paragraphs and line breaks, escaped. Deliberately not a Markdown |
| 622 | engine: this is an operator's legal text and the failure mode of a |
| 623 | half-implemented renderer is a mangled legal sentence.""" |
| 624 | paragraphs = [block.strip() for block in str(text or "").split("\n\n") |
| 625 | if block.strip()] |
| 626 | return "".join(f"<p>{_esc(block).replace(chr(10), '<br>')}</p>" |
| 627 | for block in paragraphs) |
| 628 | |
| 629 | |
| 630 | def _days(value): |
| 631 | """14, not 14.0. A lifetime rendered with a spurious decimal reads as |
| 632 | a computed estimate rather than the exact number of seconds this |
| 633 | server writes.""" |
| 634 | return int(value) if float(value) == int(value) else value |
| 635 | |
| 636 | |
| 637 | def _cookie_row_html(row): |
| 638 | necessary = ('<span class="yes">Yes</span>' if row["strictly_necessary"] |
| 639 | else "No") |
| 640 | because = (f'<br><span class="muted">{_esc(row["necessary_because"])}</span>' |
| 641 | if row["necessary_because"] else "") |
| 642 | return (f'<tr><td><code>{_esc(row["name"])}</code></td>' |
| 643 | f'<td>{_esc(row["purpose"])}' |
| 644 | f'<br><span class="muted">{_esc(row["set_when"])}</span></td>' |
| 645 | f'<td>{_days(row["max_age_days"])} days' |
| 646 | f'<br><span class="muted">Max-Age={row["max_age_seconds"]}</span>' |
| 647 | f'</td>' |
| 648 | f'<td>{necessary}{because}</td></tr>') |
| 649 | |
| 650 | |
| 651 | def _cookie_table_html(rows): |
| 652 | if not rows: |
| 653 | return ('<p>This server sets <strong>no cookies at all</strong> in the ' |
| 654 | 'configuration it is running.</p>') |
| 655 | body = "".join(_cookie_row_html(row) for row in rows) |
| 656 | return f""" |
| 657 | <table> |
| 658 | <thead><tr><th>Cookie</th><th>What it is for</th><th>How long</th> |
| 659 | <th>Strictly necessary?</th></tr></thead> |
| 660 | <tbody>{body}</tbody></table> |
| 661 | <p class="note">Every cookie above is <strong>first-party</strong> (set by |
| 662 | this server, sent only back to it), <strong>HttpOnly</strong> (no script on |
| 663 | the page can read it) and <strong>SameSite=Lax</strong>. None of them is an |
| 664 | advertising or cross-site identifier, and there is no third-party script on |
| 665 | any page of this site to set one.</p> |
| 666 | """ |
| 667 | |
| 668 | |
| 669 | def _analytics_html(analytics): |
| 670 | if not analytics["enabled"]: |
| 671 | return """ |
| 672 | <h2>What is recorded about your visit</h2> |
| 673 | <p><strong>Nothing.</strong> This server keeps no log of page views: no |
| 674 | record of which pages were read, no visitor counter, and no third-party |
| 675 | analytics script. Requests may appear briefly in ordinary web-server |
| 676 | operational logs, which is not something this application stores or |
| 677 | reads.</p> |
| 678 | """ |
| 679 | fields = "".join( |
| 680 | f"<tr><td>{_esc(row['field'])}</td><td>{_esc(row['meaning'])}</td></tr>" |
| 681 | for row in analytics["fields"]) |
| 682 | cookie_line = ( |
| 683 | f"A <code>{_esc(object_analytics.VISITOR_COOKIE_NAME)}</code> cookie is " |
| 684 | f"set so a returning visit can be told from a new one; it is described " |
| 685 | f"in the table below." |
| 686 | if analytics["visitor_cookie"] else |
| 687 | "<strong>No cookie is involved.</strong> Visits are counted by network " |
| 688 | "address, which means a returning visitor is indistinguishable from a " |
| 689 | "new one — that is the trade this server makes rather than storing an " |
| 690 | "identifier on your device.") |
| 691 | return f""" |
| 692 | <h2>What is recorded about your visit</h2> |
| 693 | <p>Every request this server answers appends one row to its own log, on its |
| 694 | own disk. No third party is involved and no script is served to your browser |
| 695 | to do it. {cookie_line}</p> |
| 696 | <table> |
| 697 | <thead><tr><th>Field</th><th>What it is</th></tr></thead> |
| 698 | <tbody>{fields}</tbody></table> |
| 699 | <p>Those rows are kept for <strong>{analytics["retention_days"]} days</strong> |
| 700 | and then deleted, and the log is additionally capped at |
| 701 | {analytics["max_rows"]:,} rows so the oldest are dropped if it fills sooner. |
| 702 | Both figures are read from this server's own configuration by the page you |
| 703 | are reading, so they are the numbers actually in force.</p> |
| 704 | <p class="note"><strong>Do Not Track and Global Privacy Control are |
| 705 | honoured.</strong> If your browser sends either signal, no cookie is set for |
| 706 | you at all. You are still counted by address, because asking not to be |
| 707 | remembered is not the same as asking not to be counted.</p> |
| 708 | """ |
| 709 | |
| 710 | |
| 711 | def _processing_html(sections): |
| 712 | parts = [] |
| 713 | for section in sections: |
| 714 | shared = section.get("shared_with") or [] |
| 715 | shared_line = ( |
| 716 | f"<p>Shared with: {_esc(', '.join(shared))}.</p>" if shared else "") |
| 717 | note = (f"<p>{_esc(section['note'])}</p>" if section.get("note") else "") |
| 718 | parts.append( |
| 719 | f"<h3>{_esc(section['title'])}</h3>" |
| 720 | f"<p>{_esc(section['why'])}</p>{note}{shared_line}" |
| 721 | f"<p class=\"muted\">Kept: {_esc(section['kept'])}.</p>") |
| 722 | return "".join(parts) |
| 723 | |
| 724 | |
| 725 | def _subprocessors_html(rows): |
| 726 | if not rows: |
| 727 | return ("<p>None. No third party is configured on this server, so " |
| 728 | "nothing you give it is sent anywhere else.</p>") |
| 729 | body = "".join( |
| 730 | f"<tr><td>{_esc(row['name'])}</td><td>{_esc(row['role'])}</td>" |
| 731 | f"<td>{_esc(row['data'])}</td></tr>" for row in rows) |
| 732 | return f""" |
| 733 | <table> |
| 734 | <thead><tr><th>Who</th><th>What for</th><th>What they receive</th></tr></thead> |
| 735 | <tbody>{body}</tbody></table> |
| 736 | <p class="note">This list is generated from the credentials actually |
| 737 | configured on this server, not maintained by hand. A provider that is not |
| 738 | set up cannot appear on it, and one that is set up cannot be left off.</p> |
| 739 | """ |
| 740 | |
| 741 | |
| 742 | def _unsigned_html(fold): |
| 743 | """The refusal. It names the missing settings and states no policy. |
| 744 | |
| 745 | Rendering a policy body under a heading with nobody's name on it would |
| 746 | be the worst of both: a reader would take it for a commitment and have |
| 747 | no one to hold to it. |
| 748 | """ |
| 749 | missing = set(fold["controller"]["missing_settings"]) |
| 750 | rows = "".join( |
| 751 | f"<tr><td><code>{_esc(key)}</code></td><td>{_esc(why)}</td>" |
| 752 | f"<td>{'<strong>missing</strong>' if key in missing else 'set'}</td></tr>" |
| 753 | for key, why in REQUIRED_SETTINGS) |
| 754 | return f""" |
| 755 | <div class="breadcrumb"><a href="/">Home</a> / Privacy</div> |
| 756 | <h1>No privacy policy has been published for this server</h1> |
| 757 | <div class="refusal"> |
| 758 | <p>This page generates a privacy policy from the server's own |
| 759 | configuration, and it will not publish one that nobody has signed. A policy |
| 760 | with no named controller and no address to write to looks like compliance |
| 761 | while giving a reader nobody to contact, which is worse than saying |
| 762 | nothing.</p> |
| 763 | <p>The operator of this server needs to set the following in |
| 764 | <code>app_settings</code>:</p> |
| 765 | <table> |
| 766 | <thead><tr><th>Setting</th><th>What it is</th><th></th></tr></thead> |
| 767 | <tbody>{rows}</tbody></table> |
| 768 | <p>Optionally also <code>{_esc(EXTRA_MARKDOWN_KEY)}</code>, for reviewed |
| 769 | legal wording of your own. This page never writes legal text itself — it |
| 770 | states facts about the software and stops.</p> |
| 771 | </div> |
| 772 | <p>The cookie facts are true regardless of who signed them, so they are |
| 773 | published anyway: <a href="/cookies">what this server stores in your |
| 774 | browser</a>.</p> |
| 775 | """ |
| 776 | |
| 777 | |
| 778 | def _policy_html(fold): |
| 779 | controller = fold["controller"] |
| 780 | extra = fold["extra_markdown"] |
| 781 | extra_html = (f"<h2>Additional terms from {_esc(controller['name'])}</h2>" |
| 782 | f"{_markdown_ish(extra)}" |
| 783 | f"<p class=\"note\">The section above is the operator's own " |
| 784 | f"text. Everything else on this page is generated from this " |
| 785 | f"server's configuration.</p>") if extra else "" |
| 786 | rights = fold["rights"] |
| 787 | return f""" |
| 788 | <div class="breadcrumb"><a href="/">Home</a> / Privacy</div> |
| 789 | <h1>Privacy</h1> |
| 790 | <p class="note">This page is <strong>generated from this server's live |
| 791 | configuration</strong> every time it is loaded — the retention periods, the |
| 792 | cookie lifetimes and the list of third parties below are read from the |
| 793 | settings actually in force, not from a template. It describes what the |
| 794 | software does. It contains no legal interpretation, because the software |
| 795 | is not in a position to offer any.</p> |
| 796 | |
| 797 | <h2>Who is responsible</h2> |
| 798 | <p><strong>{_esc(controller['name'])}</strong>, established in |
| 799 | {_esc(controller['jurisdiction'])}, is responsible for the information |
| 800 | described here. Write to |
| 801 | <a href="mailto:{_esc(controller['contact_email'])}"> |
| 802 | {_esc(controller['contact_email'])}</a> about anything on this page.</p> |
| 803 | |
| 804 | {_analytics_html(fold["analytics"])} |
| 805 | |
| 806 | <h2>Cookies</h2> |
| 807 | {_cookie_table_html(fold["cookies"])} |
| 808 | |
| 809 | <h2>What else this server holds, and why</h2> |
| 810 | {_processing_html(fold["processing"])} |
| 811 | |
| 812 | <h2>Who else receives it</h2> |
| 813 | {_subprocessors_html(fold["subprocessors"])} |
| 814 | |
| 815 | <h2>What you can ask for</h2> |
| 816 | <ul> |
| 817 | <li><strong>A copy of your data.</strong> {_esc(rights['export'])}</li> |
| 818 | <li><strong>A correction.</strong> {_esc(rights['correction'])}</li> |
| 819 | <li><strong>Deletion.</strong> {_esc(rights['erasure'])}</li> |
| 820 | </ul> |
| 821 | <p>{_esc(rights['traffic'])}</p> |
| 822 | <p>Write to <a href="mailto:{_esc(controller['contact_email'])}"> |
| 823 | {_esc(controller['contact_email'])}</a>.</p> |
| 824 | |
| 825 | {extra_html} |
| 826 | |
| 827 | <p class="note">A machine-readable version of everything above is at |
| 828 | <a href="/privacy.json">/privacy.json</a>, and the cookie table on its own is |
| 829 | at <a href="/cookies">/cookies</a>. They are the same fold over the same |
| 830 | settings, so they cannot disagree with this page.</p> |
| 831 | """ |
| 832 | |
| 833 | |
| 834 | def _cookies_page_html(fold): |
| 835 | return f""" |
| 836 | <div class="breadcrumb"><a href="/">Home</a> / <a href="/privacy">Privacy</a> |
| 837 | / Cookies</div> |
| 838 | <h1>Cookies</h1> |
| 839 | <p>Everything this server can store in your browser, with the lifetime it |
| 840 | actually sets. Read from the running configuration, so it is the current |
| 841 | list rather than a list somebody maintained.</p> |
| 842 | {_cookie_table_html(fold["cookies"])} |
| 843 | <p><a href="/privacy">The full privacy page</a> · |
| 844 | <a href="/privacy.json">machine-readable</a></p> |
| 845 | """ |
| 846 | |
| 847 | |
| 848 | def _page(title, body): |
| 849 | return { |
| 850 | "status": 200, |
| 851 | "content_type": "text/html; charset=utf-8", |
| 852 | "body": f"""<!doctype html> |
| 853 | <html lang="en"> |
| 854 | <head> |
| 855 | <meta charset="utf-8"> |
| 856 | <meta name="viewport" content="width=device-width, initial-scale=1"> |
| 857 | <title>{_esc(title)}</title> |
| 858 | <link rel="stylesheet" href="/style"> |
| 859 | <style>{_STYLE}</style> |
| 860 | </head> |
| 861 | <body> |
| 862 | <div class="wrap pp"> |
| 863 | <header class="app"><h1><a href="/">DBBASIC</a></h1></header> |
| 864 | {body} |
| 865 | </div> |
| 866 | <script src="/nav"></script> |
| 867 | </body> |
| 868 | </html>""", |
| 869 | } |
| 870 | |
| 871 | |
| 872 | def GET(request): |
| 873 | path = str((request or {}).get("_path") or "/privacy").strip().lower() |
| 874 | fold = policy() |
| 875 | |
| 876 | if path.endswith(".json"): |
| 877 | # The JSON surface is served whether or not the policy is signed: |
| 878 | # an unsigned fold is still an honest answer, and it says so in |
| 879 | # `controller.signed` and `controller.missing_settings` rather |
| 880 | # than by 404ing and leaving a monitor guessing. |
| 881 | return { |
| 882 | "status": 200, |
| 883 | "content_type": "application/json; charset=utf-8", |
| 884 | "body": json.dumps(fold, indent=2, sort_keys=True), |
| 885 | } |
| 886 | |
| 887 | if path.rstrip("/").endswith("/cookies"): |
| 888 | return _page("Cookies", _cookies_page_html(fold)) |
| 889 | |
| 890 | if not fold["controller"]["signed"]: |
| 891 | return _page("Privacy", _unsigned_html(fold)) |
| 892 | return _page("Privacy", _policy_html(fold)) |
| 893 | |
| 894 | |
| 895 | def POST(request): |
| 896 | # Reading a policy is a read either way. |
| 897 | return GET(request) |