#!/usr/bin/env python3.14# -*- coding: utf-8 -*-"""Where a sign-in or a sign-out returns to: the page the reader was on.Signing in and out are full-page trips through the auth service; without this, both land on`/` and the reader loses their place. The page they were on travels as `next` — the loginlink's `?next=`, the logout form's field, Caddy's redirect of an expired session — and comesback through :func:`return_path`.A `next` is attacker-controlled (anyone can send a link to `/login?next=…`), so it is an**allowlist**, not a sanitiser: a path on this site, under one of the content routes the leftpane shows, or `/`. Anything else — another host (`//evil.example`, `/\\evil.example`, whichbrowsers treat as one), a scheme, an auth page, the terminal or an API route — returns `/`."""from__future__importannotations__all__=['return_path']fromurllib.parseimporturlsplit,urlunsplit#: The left pane's routes, readable signed in or out (the public site serves them too)._CONTENT:tuple[str,...]=('/solutions','/docs','/blogs','/about','/shell')#: Routes only a session can see: a fine place to return to after signing in, and not after#: signing out (the page would only bounce to the login)._SIGNED_IN:tuple[str,...]=('/account',)#: Longer than any real page path; a `next` beyond it is not one._MAX:int=512def_under(path:str,prefixes:tuple[str,...])->bool:returnany(path==porpath.startswith(f'{p}/')forpinprefixes)defreturn_path(raw:str|None,*,signed_in:bool)->str:"""*raw* if it names a page to return to — signed in, or (*signed_in* False) signed out — else `/`."""ifnotraworlen(raw)>_MAXornotraw.startswith('/')orraw[1:2]in('/','\\') \orany(ord(ch)<0x20orch=='\\'forchinraw):return'/'parts=urlsplit(raw)ifparts.schemeorparts.netloc:return'/'allowed=parts.path=='/'or_under(parts.path,_CONTENT)or(signed_inand_under(parts.path,_SIGNED_IN))returnurlunsplit(('','',parts.path,parts.query,''))ifallowedelse'/'