"""How CompMaps locks your maps: the code that does it, copied unchanged from the running service. Published so you can check the privacy page, not take it on trust. 1. The job API (below, first part) makes a random key for each job. It keeps only a hash of a token made from that key, so the job ID alone opens nothing. 2. The key reaches you after "#k=" in the page and email links. Browsers never send that part of an address to a server. 3. The map maker (below, second part) locks every map, the summary, the notes and the diagnostics file with AES-256-GCM, using a key made from your key, before storing them. 4. The status page (https://comp-maps.ca/p.js) unlocks them in your browser. """ # ---- Job API: making the key, and checking the token (from api_handler.py) ---- # CM-24 (2026-09-26): each job gets a secret. It goes back to the page and, through # the worker, into the email link after "#k=", which browsers never send to a # server. The row keeps only the hash of the access token derived from it, so the # job ID printed on a map opens nothing. The row expires with the maps. key = secrets.token_urlsafe(24) now = int(time.time()) _ddb.Table(TABLE).put_item(Item={ "job_id": job_id, "tool": "compmap", "status": "queued", "address": f"Comp Map ({len(rows)} rows)", "email": email, "created_at": now, "expires_at": now + COMP_MAP_KEEP_DAYS * 86400, "access_hash": _sha256(_compmap_access(key)), }) message = {"job_id": job_id, "rows": rows, "options": options, "email": email, "key": key} if firm_id: # CM-44: in the queue message only, never in the job row message["firm_id"] = firm_id ... def _sha256(s): return hashlib.sha256(s.encode("utf-8")).hexdigest() def _compmap_access(key): """The access token the page derives from the job's secret (same in p.html).""" return _sha256("compmaps-access:" + key) def _get_compmap_job(job_id, access): if not re.fullmatch(r"[0-9a-f]{12}", job_id or ""): return _resp(400, {"error": "bad job id"}) item = _ddb.Table(TABLE).get_item(Key={"job_id": job_id}).get("Item") # CM-24: a wrong or missing token answers exactly like a missing job, and a job # from before the key existed opens for no one. if (not item or item.get("tool") != "compmap" or not item.get("access_hash") or not hmac.compare_digest(item["access_hash"], _sha256(access or ""))): return _resp(404, {"error": "not found"}) ... # ---- Map maker: locking what it stores (from handler.py) ---- class _Sealer: def __init__(self, secret): self.key = ( hashlib.sha256(("compmaps-enc:" + secret).encode("utf-8")).digest() if secret else None ) def seal(self, data): from cryptography.hazmat.primitives.ciphers.aead import AESGCM # noqa: PLC0415 nonce = os.urandom(12) return nonce + AESGCM(self.key).encrypt(nonce, data, None) def text(self, s): return base64.b64encode(self.seal(s.encode("utf-8"))).decode("ascii") def fields(self, **plain): """Status fields to store: sealed as enc_, or plain on a LOCAL test run with no secret.""" if not self.key: return plain return {f"enc_{k}": self.text(v) for k, v in plain.items()}