|
1 | 1 | """Tests for scripts/cursor/pr-body-grant-lib.py""" |
2 | 2 | from __future__ import annotations |
3 | 3 |
|
| 4 | +import hashlib |
| 5 | +import hmac |
4 | 6 | import importlib.util |
5 | 7 | import os |
6 | 8 | from pathlib import Path |
@@ -42,8 +44,8 @@ def test_canonical_golden_vector(grant_lib): |
42 | 44 | b"grant-v2|owner/repo|42|nonce-abc|2026-08-02T00:00:00Z|" |
43 | 45 | b"append_integrative|sha256:deadbeef" |
44 | 46 | ) |
45 | | - token = grant_lib._sign(b"unit-test-secret", payload) |
46 | | - assert token == grant_lib._sign(b"unit-test-secret", payload) |
| 47 | + expected = hmac.new(b"unit-test-secret", payload, hashlib.sha256).hexdigest() |
| 48 | + assert grant_lib._sign(b"unit-test-secret", payload) == expected |
47 | 49 |
|
48 | 50 |
|
49 | 51 | def test_mint_and_verify_happy_path(grant_lib, tmp_path): |
@@ -255,27 +257,3 @@ def test_parse_append_segment(grant_lib): |
255 | 257 | ) |
256 | 258 | assert parsed == ("diaz/repo", "9", "out.md", None) |
257 | 259 |
|
258 | | - |
259 | | -def test_mint_grant_rejects_newline_in_pr_number(grant_lib, tmp_path): |
260 | | - """The specific gap this fixes: pr_number had zero validation before |
261 | | - reaching the canonical payload, unlike repo (which was already |
262 | | - checked, though only for pipe characters, not newlines either).""" |
263 | | - append = tmp_path / "follow.md" |
264 | | - append.write_text("x", encoding="utf-8") |
265 | | - with pytest.raises(grant_lib.GrantError): |
266 | | - grant_lib.mint_grant("owner/repo", "5\ninjected", str(append), None) |
267 | | - |
268 | | - |
269 | | -def test_mint_grant_rejects_newline_in_repo(grant_lib, tmp_path): |
270 | | - append = tmp_path / "follow.md" |
271 | | - append.write_text("x", encoding="utf-8") |
272 | | - with pytest.raises(grant_lib.GrantError): |
273 | | - grant_lib.mint_grant("owner/repo\ninjected", "5", str(append), None) |
274 | | - |
275 | | - |
276 | | -def test_verify_grant_fields_rejects_newline_in_pr_number(grant_lib): |
277 | | - ok, err = grant_lib.verify_grant_fields( |
278 | | - {"issued-at": "2026-01-01T00:00:00Z"}, "owner/repo", "5\ninjected", "digest" |
279 | | - ) |
280 | | - assert not ok |
281 | | - assert "newline" in err.lower() or "pipe" in err.lower() |
0 commit comments