Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 57 additions & 4 deletions tests/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -860,6 +860,11 @@ def test_an_owner_owns_what_the_authority_table_names_and_each_category_has_one(
self.assertTrue(caught(self.team({"pr-review": self.review(), "pr-review-two": twin}),
"security is owned by ['pr-review', 'pr-review-two']"))

def test_the_review_role_permits_its_check_run_now_its_app_is_granted_it(self): # decision 0059, Muse on #364
manifest = json.loads((ROOT / "roles" / "pr-review.json").read_text())
review = next(i for i in manifest["intents"] if i["name"] == "review-change")
self.assertEqual(review["permitted_effects"], ["submit_review", "create_check_run"])

def test_each_permitted_effect_is_within_the_ceiling_of_its_roles_own_app(self): # section 5.4, decision 0005
wide = self.review(intents=[dict(self.review()["intents"][0], permitted_effects=["submit_review",
"create_check_run"])])
Expand Down Expand Up @@ -4931,12 +4936,18 @@ def answer(status, body):
answer(200, {"full_name": repo, "default_branch": "main"})
if path.endswith("/git/ref/heads/main"):
answer(200, {"object": {"sha": "a" * 40}})
if path.endswith("/git/refs") or "/contents/" in path:
if path.endswith("/git/refs"):
answer(201, {"ok": True})
if "/contents/" in path:
# The commit the push makes; GitHub then reads the pull request's head as the old one once, as it did in window 2.
state["pushed"] = True
answer(201, {"commit": {"sha": "b" * 40}})
if path.endswith("/pulls") and method == "POST":
answer(201, {"number": 7})
if path.endswith("/pulls/7") and method == "GET":
answer(200, {"number": 7, "state": "open", "head": {"sha": "b" * 40, "ref": "trial/p-threads", "repo": {"full_name": repo}}})
stale = os.environ.get("STANDIN_STALE") == "always" or (state.get("pushed") and not state.get("moved"))
state["moved"] = bool(state.get("pushed"))
answer(200, {"number": 7, "state": "open", "head": {"sha": ("c" if stale else "b") * 40, "ref": "trial/p-threads", "repo": {"full_name": repo}}})
if path.endswith("/pulls/7/reviews"):
state["reviews"] += 1
answer(200, {"id": state["reviews"]})
Expand Down Expand Up @@ -4984,9 +4995,36 @@ def run_threads(self, **extra) -> tuple:
capture_output=True, text=True, env=env, timeout=120, stdin=subprocess.DEVNULL)
return bin_, ran

def test_a_head_github_never_moves_is_read_five_times_and_said_so(self):
bin_, ran = self.run_threads(STANDIN_STALE="always")
calls = [json.loads(line) for line in (bin_ / "calls").read_text().splitlines()]
reads = [c for c in calls if any(a.endswith("/pulls/7") for a in c["argv"]) and "GET" in c["argv"]]
self.assertEqual(len(reads), 5)
self.assertIn("the push made " + "b" * 40 + ", and the pull request still read " + "c" * 40, ran.stderr)

def test_the_token_is_read_after_the_last_status_lines_headers(self):
# The mint's answer as curl -i prints it: through a proxy a block of its own comes first, and GitHub
# pretty-prints an empty list across a blank line, which once hid the window's token, unread and unrevoked.
token = "ghs_" + "Q" * 36
body = json.dumps({"token": token, "repositories": [], "permissions": {"contents": "write"}}, indent=2)
script = self.SCRIPT.read_text()
parse = re.search(r"/access_tokens\" \|\n python3 -I -c '\n(.*?)' \"\$raw\"\)", script, re.S).group(1)
for answer in ("HTTP/2 201 \r\ncontent-type: application/json\r\n\r\n" + body.replace("[]", "[\n\n ]"),
"HTTP/1.1 200 Connection established\r\n\r\nHTTP/2 201 \r\nx: y\r\n\r\n" + body):
kept = pathlib.Path(tempfile.mkdtemp(dir=SCRATCH.name)) / "02.raw"
ran = subprocess.run([sys.executable, "-I", "-c", parse, str(kept)], input=answer, capture_output=True,
text=True, timeout=60)
self.assertEqual((ran.returncode, ran.stdout), (0, token), answer[:40])
self.assertNotIn(token, kept.read_text())
self.assertTrue(kept.read_text().startswith("HTTP/2 201"), kept.read_text()[:40])

def test_the_app_opens_resolves_and_deletes_and_revokes_its_token(self):
bin_, ran = self.run_threads()
self.assertEqual(ran.returncode, 0, ran.stderr)
# The head it says is the commit its push made, read again until GitHub moved the pull request to it (window 2
# printed the head before the push).
self.assertIn("head " + "b" * 40, ran.stdout)
self.assertNotIn("still read", ran.stderr)
self.assertNotIn(self.TOKEN, ran.stdout + ran.stderr)
calls = [json.loads(line) for line in (bin_ / "calls").read_text().splitlines()]
curl_calls = [json.loads(line) for line in (bin_ / "curl-calls").read_text().splitlines()]
Expand Down Expand Up @@ -5822,6 +5860,8 @@ def status(name, default):
code = status("mint", 201)
body = {"token": "''' + "ghs_" + "P" * 36 + r'''", "expires_at": "2026-10-22T19:00:00Z",
"permissions": {"actions_variables": "read", "issues": "write", "metadata": "read"}, "repository_selection": "selected"}
if (here / "status-notoken").exists():
body.pop("token")
print(json.dumps(body) + "\n" + str(code), end="")
sys.exit(0)
if method == "DELETE":
Expand Down Expand Up @@ -5896,9 +5936,22 @@ def test_a_mint_refused_reads_nothing(self):
folder = self.stand_in(mint=403)
out, ran = self.run_script(folder)
self.assertEqual(ran.returncode, 1)
self.assertIn("no token was minted", ran.stderr)
self.assertEqual(len(self.calls(folder)), 1)
self.assertIn("the mint was refused", ran.stderr)
# The stand-in's refusal carries a token all the same, and the trap revokes it before the script ends; nothing
# else is asked, and the token is said nowhere.
said = self.calls(folder)
self.assertEqual(len(said), 2, said)
self.assertIn("installation/token", json.dumps(said[1]))
self.assertIn("stopped with a token live", ran.stderr)
self.assertFalse((out / "token.json").exists())
self.assertNotIn("ghs_", ran.stdout + ran.stderr)
for kept in ("token.json", "requests.json", "ran_at"):
self.assertFalse((out / kept).exists() and "ghs_" in (out / kept).read_text(), kept)
# A refusal that carries no token revokes nothing: one call, the mint.
folder = self.stand_in(mint=403, notoken=1)
out, ran = self.run_script(folder)
self.assertEqual(ran.returncode, 1)
self.assertEqual(len(self.calls(folder)), 1)

def test_a_revocation_refused_stops_it_and_says_so(self):
folder = self.stand_in(revoke=500)
Expand Down
16 changes: 16 additions & 0 deletions tools/mutants.py
Original file line number Diff line number Diff line change
Expand Up @@ -5197,6 +5197,22 @@ def moved_before(rel: str, block: str, anchor: str) -> tuple:
("the redaction writes in the locale's encoding", "tools/redact_trial.py",
' target = getattr(stdout, "buffer", None)\n', ' target = None\n',
("tests.test_tools.Redaction.test_it_opens_no_path_it_is_given",)),
("the App reads the token after the last blank line", "tools/trial_as_app.sh",
'cut = text.find("\\n\\n", starts[-1]) if starts else -1', 'cut = text.rfind("\\n\\n")',
("tests.test_tools.AppThreads.test_the_token_is_read_after_the_last_status_lines_headers",)),
("the App says the head before its push moved it", "tools/trial_as_app.sh",
' [[ "$read_head" = "$pushed" ]] && break\n', ' break\n',
("tests.test_tools.AppThreads.test_the_app_opens_resolves_and_deletes_and_revokes_its_token",)),
("P-52 forgets a token its refused mint carried", "tools/trial_p52_pause.sh",
' # A token the refusal carried all the same is left for the trap, which revokes it and says so (Muse on #364).\n',
' token=""\n',
("tests.test_tools.PauseReadTrial.test_a_mint_refused_reads_nothing",)),
("the review Role does not permit its check run", "roles/pr-review.json",
'"submit_review",\n "create_check_run"\n', '"submit_review"\n',
("tests.test_tools.Authority.test_the_review_role_permits_its_check_run_now_its_app_is_granted_it",)),
("the App says nothing of a head that never moved", "tools/trial_as_app.sh",
'[[ "$read_head" = "$pushed" ]] || echo "trial_as_app: the push made', '[[ "$read_head" = "$pushed" ]] || true "trial_as_app: the push made',
("tests.test_tools.AppThreads.test_a_head_github_never_moves_is_read_five_times_and_said_so",)),
("the App's failed redaction leaves its partial clean file", "tools/trial_as_app.sh",
' rm -f "${1%.raw}.clean"\n', '', ("tests.test_tools.Redaction.test_it_opens_no_path_it_is_given",)),
# Issue 282: the App's review threads, run against a stand-in gh, curl and openssl.
Expand Down
27 changes: 20 additions & 7 deletions tools/trial_as_app.sh
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ root=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd -P)
out=$(cd "$(mktemp -d)" && pwd -P)
case "$out/" in "$root"/*) echo "the scratch directory is inside the repo" >&2; exit 2 ;; esac

jwt="" token="" n=0 id="" raw=""
jwt="" token="" n=0 id="" raw="" pushed="" read_head=""

# The value at a dotted path of the JSON body of a redacted capture.
value() {
Expand Down Expand Up @@ -237,11 +237,14 @@ token=$(printf 'Authorization: Bearer %s\n' "$jwt" |
curl -q -sS -i --fail-with-body -X POST -H @- -H "Accept: $ACCEPT" -H "X-GitHub-Api-Version: $VERSION" \
"$API/app/installations/$installation/access_tokens" |
python3 -I -c '
import json, sys
import json, re, sys
text = sys.stdin.read().replace("\r\n", "\n")
# The last blank line ends the headers: a proxy prints its own "Connection established" block before the one
# from GitHub, and a JSON body holds no blank line.
head, gap, body = text.rpartition("\n\n")
# The headers are the block of the last status line: a proxy prints its own "Connection established" block before
# the one from GitHub, and GitHub prints an empty list in its JSON across a blank line, so neither the first blank
# line nor the last ends the headers.
starts = [m.start(1) for m in re.finditer(r"(?:\A|\n\n)(HTTP/[0-9.]+ [0-9]{3})", text)]
cut = text.find("\n\n", starts[-1]) if starts else -1
head, body = (text[starts[-1]:cut], text[cut + 2:]) if cut >= 0 else (text, "")
try:
data = json.loads(body)
except ValueError:
Expand All @@ -266,6 +269,7 @@ if [ "$action" = open ] || [ "$action" = threads ]; then
as_installation POST "repos/$repo/git/refs" "$(object "ref=refs/heads/$branch" "sha=$sha")"
as_installation PUT "repos/$repo/contents/$file" \
"$(object "message=Trial: $file, added by the trial-author App" "content=$content" "branch=$branch")"
pushed=$(value "$out/$id.clean" commit.sha)
as_installation POST "repos/$repo/pulls" "$(object "title=Trial: $branch, opened by the trial-author App" \
"head=$branch" "base=$base" "body=A platform trial. It is approved by the machine user alone, and read.")"
pr=$(value "$out/$id.clean" number)
Expand All @@ -277,9 +281,18 @@ else
branch=$(value "$out/$id.clean" head.ref)
as_installation PUT "repos/$repo/contents/$file" \
"$(object "message=Trial: $file, pushed by the trial-author App" "content=$content" "branch=$branch")"
pushed=$(value "$out/$id.clean" commit.sha)
fi
as_installation GET "repos/$repo/pulls/$pr"
echo "Pull request $pr, branch $branch, head $(value "$out/$id.clean" head.sha)"
# GitHub moves a pull request's head after the push that moves it, so the head is read until it is the commit the
# push made, a few times, and said as it was last read.
for attempt in 1 2 3 4 5; do
as_installation GET "repos/$repo/pulls/$pr"
read_head=$(value "$out/$id.clean" head.sha)
[[ "$read_head" = "$pushed" ]] && break
if [[ "$attempt" -lt 5 ]]; then sleep 2; fi
done
echo "Pull request $pr, branch $branch, head $read_head"
[[ "$read_head" = "$pushed" ]] || echo "trial_as_app: the push made $pushed, and the pull request still read $read_head" >&2

if [ "$action" = threads ]; then
# The first thread: the App opens it, resolves it, and reads it as itself.
Expand Down
4 changes: 2 additions & 2 deletions tools/trial_p52_pause.sh
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@ import json, sys
answer = json.load(sys.stdin)
print(answer.get("token") or "" if isinstance(answer, dict) else "")' 2>/dev/null || true)
if [ "$minted_status" != 201 ] || [ -z "$token" ]; then
token=""
# A token the refusal carried all the same is left for the trap, which revokes it and says so (Muse on #364).
minted=""
echo "trial_p52_pause: $slug: no token was minted for installation $installation (status ${minted_status:-none});" \
echo "trial_p52_pause: $slug: the mint was refused for installation $installation (status ${minted_status:-none});" \
"nothing read" >&2
exit 1
fi
Expand Down
Loading