···124124 @classmethod
125125 def _validate_did(cls, v: str) -> None:
126126 """Validate DID format"""
127127- if not v.startswith("did:"):
128128- raise ValueError("DID must start with 'did:'")
129127 if len(v) > 2048:
130128 raise ValueError("DID too long, max 2048 chars")
129129+ if not re.match(r"^did:[a-z]+:[a-zA-Z0-9._:%-]*[a-zA-Z0-9._-]$", v):
130130+ raise ValueError("Invalid URI format")
131131132132 @classmethod
133133 def _validate_handle(cls, v: str) -> None:
134134 """Validate handle format"""
135135- if not re.match(r"^[a-zA-Z0-9._-]+$", v):
135135+ if not re.match(r"^([a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?$", v):
136136 raise ValueError("Handle contains invalid characters")
137137 if len(v) > 253:
138138 raise ValueError("Handle too long, max 253 chars")
···150150151151 @classmethod
152152 def _validate_at_uri(cls, v: str) -> None:
153153- """Validate AT-URI format"""
153153+ """
154154+ Validate AT-URI format according to AT Protocol specification.
155155+156156+ Args:
157157+ v: AT-URI string to validate
158158+159159+ Raises:
160160+ ValueError: If URI violates any of these rules:
161161+ - Must start with 'at://'
162162+ - Max length 8KB
163163+ - No trailing slash
164164+ - Authority must be valid DID or handle
165165+ - Path segments must follow NSID/RKEY rules if present
166166+ """
154167 if not v.startswith("at://"):
155168 raise ValueError("AT-URI must start with 'at://'")
156156- if len(v) > 8000:
157157- raise ValueError("AT-URI too long, max 8000 chars")
169169+ if len(v) > 8192: # 8KB
170170+ raise ValueError("AT-URI too long, max 8KB")
171171+ if v.endswith('/'):
172172+ raise ValueError("AT-URI cannot have trailing slash")
173173+174174+ # Split into parts
175175+ parts = v[5:].split('/') # Skip 'at://'
176176+ authority = parts[0]
177177+178178+ # Validate authority (DID or handle)
179179+ if not authority:
180180+ raise ValueError("AT-URI must have authority")
181181+182182+ if authority.startswith('did:'):
183183+ # Basic DID format check - actual DID validation is done elsewhere
184184+ if len(authority) > 2048:
185185+ raise ValueError("DID too long")
186186+ if ':' not in authority[4:]:
187187+ raise ValueError("Invalid DID format")
188188+ else:
189189+ # Handle validation
190190+ if not re.match(r'^[a-z0-9.-]+$', authority):
191191+ raise ValueError("Invalid handle characters")
192192+ if len(authority) > 253:
193193+ raise ValueError("Handle too long")
194194+195195+ # Validate path segments if present
196196+ if len(parts) > 1:
197197+ if len(parts) > 3:
198198+ raise ValueError("AT-URI path too deep")
199199+200200+ collection = parts[1]
201201+ if not re.match(r'^[a-zA-Z0-9.-]+$', collection):
202202+ raise ValueError("Invalid collection NSID")
203203+204204+ if len(parts) > 2:
205205+ rkey = parts[2]
206206+ if not rkey:
207207+ raise ValueError("Record key cannot be empty")
208208+ if not re.match(r'^[a-zA-Z0-9._:%-~]+$', rkey):
209209+ raise ValueError("Invalid record key characters")
158210159211 @classmethod
160212 def _validate_cid(cls, v: str) -> None:
···169221 """Validate NSID format"""
170222 if len(v) > 317:
171223 raise ValueError("NSID too long, max 317 chars")
172172- if not re.match(r"^[a-zA-Z0-9.-]+$", v):
224224+ if not re.match(r"^[a-zA-Z]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(\.[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)+(\.[a-zA-Z]([a-zA-Z0-9]{0,62})?)$", v):
173225 raise ValueError("NSID contains invalid characters")
174226175227 @classmethod
···177229 """Validate TID format"""
178230 if len(v) > 13:
179231 raise ValueError("TID too long, max 13 chars")
180180- if not re.match(r"^[234567abcdefghijklmnopqrstuvwxyz]+$", v):
232232+ if not re.match(r"^[234567abcdefghij][234567abcdefghijklmnopqrstuvwxyz]{12}$", v):
181233 raise ValueError("TID contains invalid characters")
182234183235 @classmethod
···185237 """Validate record-key format"""
186238 if len(v) > 512:
187239 raise ValueError("Record key too long, max 512 chars")
240240+ if v == "." or v == "..":
241241+ raise ValueError(f"Record key is {v}, which is not allowed")
188242 if not re.match(r"^[a-zA-Z0-9._:%-~]+$", v):
189243 raise ValueError("Record key contains invalid characters")
190244