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
8 changes: 6 additions & 2 deletions .ci/sai/publish.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
BRANCHES = {"main", "develop"}
REVIEW_ENV = "sai-libddla"
RESULT_LABEL = "sai-libddla-results"
MAX_TOKEN_BYTES = 8192


def api(path):
Expand Down Expand Up @@ -128,8 +129,11 @@ def main():
# This mailbox is writable by jobs. Never follow links or read special files.
descriptor = os.open(request, os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK)
require(stat.S_ISREG(os.fstat(descriptor).st_mode), "Token is not a regular file")
token = os.read(descriptor, 257).decode("ascii")
require(re.fullmatch(r"[A-Za-z0-9_]{20,256}", token), "Invalid token format")
token = os.read(descriptor, MAX_TOKEN_BYTES + 1).decode("ascii")
# GitHub tokens are opaque and can use a longer, dotted format.
# Bound the input and reject header control characters; GitHub authenticates it.
require(len(token) <= MAX_TOKEN_BYTES and re.fullmatch(r"[!-~]+", token),
"Invalid token format or size")
os.environ["GH_TOKEN"] = token
run = api(f"actions/runs/{match[1]}")
if run["run_attempt"] != int(match[2]) or run["status"] == "completed":
Expand Down
29 changes: 29 additions & 0 deletions .ci/sai/test_publish.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from contextlib import redirect_stderr
import io
import json
import os
from pathlib import Path
Expand Down Expand Up @@ -83,6 +85,33 @@ def test_request_tokens_are_scoped_and_links_rejected(self):
site.main()
publish.assert_not_called()

def test_opaque_tokens_are_preserved_and_unsafe_input_is_rejected(self):
dotted = b"a" * 100 + b"." + b"b" * 200 + b"." + b"c" * 75
cases = [(b"ghs_" + b"a" * 36, True), (dotted, True),
(b"a.b-c_d~e+f/g==", True), (b"a" * 8192, True),
(b"a" * 8193, False), (b"", False),
(dotted + b"\n", False), (b"a\r\nheader", False),
(b"a b", False), (b"a\x00b", False), (b"\xff", False)]
for token, allowed in cases:
with self.subTest(size=len(token), allowed=allowed), tempfile.TemporaryDirectory() as tmp:
root = Path(tmp)
for folder in ("work", "results", "requests"):
(root / folder).mkdir()
(root / "requests/gh-17-1.token").write_bytes(token)
env = {"SAI_WORK": str(root / "work"), "SAI_RESULTS": str(root / "results"),
"SAI_REQUESTS": str(root / "requests"), "SAI_REVIEWERS": "owner"}
self.api.reset_mock()
with patch.dict(os.environ, env), patch.object(site, "publish") as publish, \
redirect_stderr(io.StringIO()):
publish.side_effect = lambda *args: self.assertEqual(os.environ["GH_TOKEN"], token.decode())
site.main()
if allowed:
publish.assert_called_once_with(self.run, {"owner"})
else:
self.api.assert_not_called()
publish.assert_not_called()
self.assertNotIn("GH_TOKEN", os.environ)

def test_receipt_and_cancellation(self):
for exit_status in (0, 1, "cancel"):
with self.subTest(exit_status=exit_status), tempfile.TemporaryDirectory() as tmp:
Expand Down
Loading