@@ -1140,7 +1140,9 @@ def validate_supersession_record(
11401140 validate_environment_approval_evidence (authorization ["environment_approval" ], authorization )
11411141
11421142
1143- def protected_environment_evidence (client : PublicClient ) -> dict [str , Any ]:
1143+ def protected_environment_evidence (
1144+ client : PublicClient ,
1145+ ) -> tuple [dict [str , Any ], set [tuple [int , str ]]]:
11441146 encoded = urllib .parse .quote (SUPERSESSION_ENVIRONMENT , safe = "" )
11451147 environment = client .json (
11461148 f"https://api.github.com/repos/{ CONTROL_REPOSITORY } /environments/{ encoded } " ,
@@ -1164,6 +1166,23 @@ def protected_environment_evidence(client: PublicClient) -> dict[str, Any]:
11641166 and rule ["id" ] > 0
11651167 )
11661168 )
1169+ required_reviewers = {
1170+ (reviewer ["reviewer" ]["id" ], reviewer ["reviewer" ]["login" ])
1171+ for rule in rules
1172+ if isinstance (rule , dict ) and rule .get ("type" ) == "required_reviewers"
1173+ for reviewer in (
1174+ rule .get ("reviewers" ) if isinstance (rule .get ("reviewers" ), list ) else []
1175+ )
1176+ if (
1177+ isinstance (reviewer , dict )
1178+ and reviewer .get ("type" ) == "User"
1179+ and isinstance (reviewer .get ("reviewer" ), dict )
1180+ and type (reviewer ["reviewer" ].get ("id" )) is int
1181+ and reviewer ["reviewer" ]["id" ] > 0
1182+ and isinstance (reviewer ["reviewer" ].get ("login" ), str )
1183+ and re .fullmatch (r"[A-Za-z0-9-]{1,39}" , reviewer ["reviewer" ]["login" ])
1184+ )
1185+ }
11671186 environment_id = environment .get ("id" )
11681187 branch_policy = environment .get ("deployment_branch_policy" )
11691188 if (
@@ -1207,13 +1226,14 @@ def protected_environment_evidence(client: PublicClient) -> dict[str, Any]:
12071226 "required_reviewer_rule_ids" : reviewer_rule_ids ,
12081227 }
12091228 validate_environment_protection_evidence (evidence )
1210- return evidence
1229+ return evidence , required_reviewers
12111230
12121231
12131232def protected_run_approval_evidence (
12141233 client : PublicClient ,
12151234 authorization : dict [str , Any ],
12161235 environment_protection : dict [str , Any ],
1236+ required_reviewers : set [tuple [int , str ]],
12171237) -> dict [str , Any ]:
12181238 run_id = authorization ["run_id" ]
12191239 run_attempt = authorization ["run_attempt" ]
@@ -1245,6 +1265,13 @@ def protected_run_approval_evidence(
12451265 "protected supersession workflow run evidence does not match GitHub" ,
12461266 "plan-discovery" ,
12471267 )
1268+ if run_attempt != 1 :
1269+ # GitHub's approval-history response has no attempt identity. Attempt 1
1270+ # is the only attempt for which an approval cannot be stale.
1271+ raise RecoveryError (
1272+ "GitHub approval history cannot bind protected approval to a rerun attempt" ,
1273+ "plan-discovery" ,
1274+ )
12481275 history = client .json (
12491276 f"https://api.github.com/repos/{ CONTROL_REPOSITORY } /actions/runs/{ run_id } /approvals" ,
12501277 headers = {"X-GitHub-Api-Version" : SUPERSESSION_API_VERSION },
@@ -1301,18 +1328,28 @@ def protected_run_approval_evidence(
13011328 evidence ,
13021329 {** authorization , "environment_protection" : environment_protection },
13031330 )
1331+ if (evidence ["user" ]["id" ], evidence ["user" ]["login" ]) not in required_reviewers :
1332+ raise RecoveryError (
1333+ "protected supersession approving user is not authorized by the current reviewer policy" ,
1334+ "plan-discovery" ,
1335+ )
13041336 return evidence
13051337
13061338
13071339def revalidate_supersession_authority (record : dict [str , Any ], client : PublicClient ) -> None :
13081340 authorization = record ["authorization" ]
1309- protection = protected_environment_evidence (client )
1341+ protection , required_reviewers = protected_environment_evidence (client )
13101342 if protection != authorization ["environment_protection" ]:
13111343 raise RecoveryError (
13121344 "release plan failure protected environment policy no longer matches GitHub" ,
13131345 "plan-discovery" ,
13141346 )
1315- approval = protected_run_approval_evidence (client , authorization , protection )
1347+ approval = protected_run_approval_evidence (
1348+ client ,
1349+ authorization ,
1350+ protection ,
1351+ required_reviewers ,
1352+ )
13161353 if approval != authorization ["environment_approval" ]:
13171354 raise RecoveryError (
13181355 "release plan failure approved deployment evidence no longer matches GitHub" ,
0 commit comments