2020
2121import os
2222from collections .abc import Mapping
23+ from typing import NoReturn
2324
2425from mcp_server .errors import ValidationError
2526from mcp_server .shared .domain_mapping import (
@@ -45,9 +46,16 @@ def _allowed(environ: Mapping[str, str]) -> frozenset[str]:
4546 return frozenset (d .strip ().lower () for d in raw .split ("," ) if d .strip ())
4647
4748
48- def _refused_domain (domain : str , environ : Mapping [str , str ]) -> tuple [str , str ] | None :
49- """(target, session) when `domain` is another project's than the session's."""
50- target = resolve_explicit_domain (domain or "" )
49+ def _refused_domain (target : str , environ : Mapping [str , str ]) -> tuple [str , str ] | None :
50+ """(target, session) when the canonical `target` is another project's domain.
51+
52+ `target` is a domain identity already: resolved from a path, read from a stored
53+ row, or resolved once from a name the caller gave. It is never resolved again
54+ here. A directory name can be an alias of one project and the canonical of
55+ another repository family; resolving a canonical a second time would turn that
56+ family's domain into the aliased project, refuse the family its own writes and
57+ let the aliased project change the family's rows.
58+ """
5159 if not target or target not in project_domains ():
5260 return None
5361 session = session_domain (environ )
@@ -68,11 +76,29 @@ def require_writable_domain(
6876 under the raw name would reach that family's hot memories. A name that resolves
6977 to no known project is a shared domain and is returned as given.
7078 """
71- refused = _refused_domain (domain , environ )
72- if refused is None :
73- target = resolve_explicit_domain (domain or "" )
74- return target if target in project_domains () else (domain or "" )
75- target , session = refused
79+ target = resolve_explicit_domain (domain or "" )
80+ if _refused_domain (target , environ ) is not None :
81+ _refuse (target , tool , environ )
82+ return target if target in project_domains () else (domain or "" )
83+
84+
85+ def require_writable_canonical (
86+ domain : str , tool : str , environ : Mapping [str , str ] = os .environ
87+ ) -> None :
88+ """Raise ValidationError when the already resolved `domain` is another project's.
89+
90+ For a domain that is an identity already (resolved from a path by `remember` or
91+ `seed_project`, or stored on a row): it is compared as it is, never resolved
92+ through the name aliases a second time (see `_refused_domain`).
93+ """
94+ target = (domain or "" ).strip ().lower ()
95+ if _refused_domain (target , environ ) is not None :
96+ _refuse (target , tool , environ )
97+
98+
99+ def _refuse (target : str , tool : str , environ : Mapping [str , str ]) -> NoReturn :
100+ """Raise the refusal of a write into `target`, another project's domain."""
101+ session = session_domain (environ )
76102 where = f"this session's project ({ session !r} )" if session else "no known project"
77103 raise ValidationError (
78104 f"{ tool } : domain { target !r} belongs to another project than { where } ; "
@@ -127,11 +153,12 @@ def require_writable_memory(
127153 f"project; set { ALLOW_ENV } ={ EVERY_PROJECT } in the environment to allow it" ,
128154 {"tool" : tool , "field" : "memory_id" , "memory_id" : row .get ("id" )},
129155 )
130- require_writable_domain (row .get ("domain" ) or "" , tool , environ )
156+ require_writable_canonical (row .get ("domain" ) or "" , tool , environ )
131157
132158
133159def memory_writable (row : Mapping , environ : Mapping [str , str ] = os .environ ) -> bool :
134160 """Whether a write may pick ``row`` as its target (merge, replica refresh)."""
135161 if row .get ("is_global" ) and not _every_project (environ ):
136162 return False
137- return _refused_domain (row .get ("domain" ) or "" , environ ) is None
163+ target = (row .get ("domain" ) or "" ).strip ().lower ()
164+ return _refused_domain (target , environ ) is None
0 commit comments