Skip to content

Commit b1104ba

Browse files
gauponclaude
andcommitted
Use the Markdown API workaround for GitHub App tokens and repo-scoped image URLs
GitHub App installation tokens get the same 404 as fine-grained PATs when downloading attachments from private repositories (#477), but the workaround was only used with -f. Older github.com/{owner}/{repo}/assets/* image URLs also 404 with both token types and render to the same JWT-signed URLs. Move the check into needs_jwt_workaround() so it can be tested directly, and pass the current token from get_auth() so App tokens are refreshed. The README now also notes that the workaround needs read access to contents: without it, POST /markdown returns 200 but leaves the image URLs unsigned. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01JefeUY9THfz2WzRtBGEP3U
1 parent 14b733c commit b1104ba

3 files changed

Lines changed: 95 additions & 46 deletions

File tree

‎README.rst‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ The tool automatically extracts file extensions from HTTP headers to ensure file
314314

315315
**Repository filtering** for repo files/assets handles renamed and transferred repositories gracefully. URLs are included if they either match the current repository name directly, or redirect to it (e.g., ``willmcgugan/rich`` redirects to ``Textualize/rich`` after transfer).
316316

317-
**Fine-grained token limitation:** Due to a GitHub platform limitation, fine-grained personal access tokens (``github_pat_...``) cannot download attachments from private repositories directly. This affects both ``/assets/`` (images) and ``/files/`` (documents) URLs. The tool implements a workaround for image attachments using GitHub's Markdown API, which converts URLs to temporary JWT-signed URLs that can be downloaded. However, this workaround only works for images - document attachments (PDFs, text files, etc.) will fail with 404 errors when using fine-grained tokens on private repos. For full attachment support on private repositories, use a classic token (``-t``) instead of a fine-grained token (``-f``). See `#477 <https://github.com/josegonzalez/python-github-backup/issues/477>`_ for details.
317+
**Fine-grained and GitHub App token limitation:** Due to a GitHub platform limitation, fine-grained personal access tokens (``github_pat_...``) and GitHub App installation tokens (``--as-app`` or ``--app-id``) cannot download attachments from private repositories directly. This affects ``/assets/`` (images) and ``/files/`` (documents) URLs, in both the ``github.com/user-attachments/`` and the older ``github.com/{owner}/{repo}/`` format. The tool implements a workaround for image attachments using GitHub's Markdown API, which converts URLs to temporary JWT-signed URLs that can be downloaded. The token needs read access to contents for this (included in the permissions above); without it, the Markdown API still responds but does not sign the image URLs. However, this workaround only works for images - document attachments (PDFs, text files, etc.) will fail with 404 errors when using these tokens on private repos. For full attachment support on private repositories, use a classic token (``-t``) instead. See `#477 <https://github.com/josegonzalez/python-github-backup/issues/477>`_ for details.
318318

319319

320320
About Discussions

‎github_backup/github_backup.py‎

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1420,24 +1420,44 @@ def download_attachment_file(url, path, auth, as_app=False, fine=False):
14201420
return metadata
14211421

14221422

1423+
def needs_jwt_workaround(args, repository, url):
1424+
"""Whether an attachment must be fetched through a JWT-signed URL.
1425+
1426+
Fine-grained PATs (#477) and GitHub App installation tokens get a 404 when
1427+
downloading attachments from private repositories directly, while classic
1428+
tokens do not. Image attachments, in both the modern
1429+
github.com/user-attachments/assets/* and the older
1430+
github.com/{owner}/{repo}/assets/* format, can be fetched through the
1431+
JWT-signed URL that the Markdown API renders for them. /files/ URLs cannot.
1432+
"""
1433+
if args.token_fine is None and not args.as_app:
1434+
return False
1435+
if not repository.get("private", False):
1436+
return False
1437+
return "github.com/user-attachments/assets/" in url or bool(
1438+
re.match(r"https://github\.com/[^/]+/[^/]+/assets/\d+/", url)
1439+
)
1440+
1441+
14231442
def get_jwt_signed_url_via_markdown_api(url, token, repo_context):
14241443
"""Convert a user-attachments/assets URL to a JWT-signed URL via Markdown API.
14251444
14261445
GitHub's Markdown API renders image URLs and returns HTML containing
14271446
JWT-signed private-user-images.githubusercontent.com URLs that work
14281447
without token authentication.
14291448
1430-
This is a workaround for issue #477 where fine-grained PATs cannot
1431-
download user-attachments URLs from private repos directly.
1449+
This is a workaround for issue #477 where fine-grained PATs (and GitHub
1450+
App installation tokens) cannot download attachments from private repos
1451+
directly.
14321452
14331453
Limitations:
14341454
- Only works for /assets/ URLs (images)
14351455
- Does NOT work for /files/ URLs (PDFs, text files, etc.)
14361456
- JWT URLs expire after ~5 minutes
14371457
14381458
Args:
1439-
url: The github.com/user-attachments/assets/UUID URL
1440-
token: Raw fine-grained PAT (github_pat_...)
1459+
url: The image attachment URL (user-attachments/assets or repo assets)
1460+
token: Raw fine-grained PAT (github_pat_...) or App installation token
14411461
repo_context: Repository context as "owner/repo"
14421462
14431463
Returns:
@@ -1832,16 +1852,7 @@ def download_attachments(
18321852
filename = get_attachment_filename(url)
18331853
filepath = os.path.join(attachments_dir, filename)
18341854

1835-
# Issue #477: Fine-grained PATs cannot download user-attachments/assets
1836-
# from private repos directly (404). Use Markdown API workaround to get
1837-
# a JWT-signed URL. Only works for /assets/ (images), not /files/.
1838-
needs_jwt = (
1839-
args.token_fine is not None
1840-
and repository.get("private", False)
1841-
and "github.com/user-attachments/assets/" in url
1842-
)
1843-
1844-
if not needs_jwt:
1855+
if not needs_jwt_workaround(args, repository, url):
18451856
# NORMAL download path
18461857
metadata = download_attachment_file(
18471858
url,
@@ -1851,7 +1862,7 @@ def download_attachments(
18511862
fine=args.token_fine is not None,
18521863
)
18531864
elif jwt_url := get_jwt_signed_url_via_markdown_api(
1854-
url, args.token_fine, repository["full_name"]
1865+
url, get_auth(args, encode=False), repository["full_name"]
18551866
):
18561867
# JWT needed and extracted, download via JWT
18571868
metadata = download_attachment_file(
@@ -1865,8 +1876,8 @@ def download_attachments(
18651876
"url": url,
18661877
"success": False,
18671878
"skipped_at": datetime.now(timezone.utc).isoformat(),
1868-
"error": "Fine-grained token cannot download private repo attachments. "
1869-
"Markdown API workaround failed. Use --token-classic instead.",
1879+
"error": "Fine-grained and GitHub App tokens cannot download private repo "
1880+
"attachments. Markdown API workaround failed. Use --token-classic instead.",
18701881
}
18711882
logger.warning(
18721883
"Skipping attachment {0}: {1}".format(url, metadata["error"])

‎tests/test_attachments.py‎

Lines changed: 66 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -398,37 +398,75 @@ def test_markdown_api_returns_none_when_no_jwt_url(self):
398398

399399
assert result is None
400400

401-
def test_needs_jwt_only_for_fine_grained_private_assets(self):
402-
"""needs_jwt is True only for fine-grained + private + /assets/ URL."""
403-
assets_url = "https://github.com/user-attachments/assets/abc123"
404-
files_url = "https://github.com/user-attachments/files/123/doc.pdf"
405-
token_fine = "github_pat_test"
406-
private = True
407-
public = False
408-
409-
# Fine-grained + private + assets = True
410-
needs_jwt = (
411-
token_fine is not None
412-
and private
413-
and "github.com/user-attachments/assets/" in assets_url
414-
)
415-
assert needs_jwt is True
401+
@pytest.mark.parametrize(
402+
"token, private, url, expected",
403+
[
404+
# Fine-grained PAT (#477)
405+
("fine", True, "https://github.com/user-attachments/assets/abc123", True),
406+
("fine", True, "https://github.com/user-attachments/files/123/doc.pdf", False),
407+
("fine", False, "https://github.com/user-attachments/assets/abc123", False),
408+
# GitHub App installation token gets the same 404 as a fine-grained PAT
409+
("app", True, "https://github.com/user-attachments/assets/abc123", True),
410+
("app", True, "https://github.com/user-attachments/files/123/doc.pdf", False),
411+
("app", False, "https://github.com/user-attachments/assets/abc123", False),
412+
# Older repo-scoped image URLs render to the same JWT-signed URLs
413+
("fine", True, "https://github.com/testuser/testrepo/assets/123/abc-def", True),
414+
("app", True, "https://github.com/testuser/testrepo/assets/123/abc-def", True),
415+
("app", True, "https://github.com/testuser/testrepo/files/123/doc.pdf", False),
416+
# Classic tokens download private attachments directly
417+
("classic", True, "https://github.com/user-attachments/assets/abc123", False),
418+
("classic", True, "https://github.com/testuser/testrepo/assets/123/abc-def", False),
419+
],
420+
)
421+
def test_needs_jwt_workaround(self, create_args, token, private, url, expected):
422+
"""Only fine-grained and App tokens on private repos use the workaround, and only for images."""
423+
args = create_args()
424+
if token == "fine":
425+
args.token_fine = "github_pat_test"
426+
elif token == "app":
427+
args.as_app = True
428+
args.token_classic = "ghs_test"
429+
else:
430+
args.token_classic = "ghp_test"
431+
repository = {"full_name": "testuser/testrepo", "private": private}
432+
433+
assert github_backup.needs_jwt_workaround(args, repository, url) is expected
434+
435+
def test_jwt_workaround_uses_app_installation_token(self, attachment_test_setup):
436+
"""With --as-app, the Markdown API is called with the installation token."""
437+
setup = attachment_test_setup
438+
setup["args"].as_app = True
439+
setup["args"].token_classic = "ghs_test"
440+
setup["repository"]["private"] = True
416441

417-
# Fine-grained + private + files = False
418-
needs_jwt = (
419-
token_fine is not None
420-
and private
421-
and "github.com/user-attachments/assets/" in files_url
422-
)
423-
assert needs_jwt is False
442+
issue_data = {"body": "https://github.com/user-attachments/assets/abc123"}
443+
jwt_url = "https://private-user-images.githubusercontent.com/123/abc.png?jwt=token"
444+
445+
with patch(
446+
"github_backup.github_backup.get_jwt_signed_url_via_markdown_api",
447+
return_value=jwt_url,
448+
) as markdown_api, patch(
449+
"github_backup.github_backup.download_attachment_file",
450+
return_value={"success": True, "http_status": 200, "url": jwt_url},
451+
) as download:
452+
github_backup.download_attachments(
453+
setup["args"], setup["issue_cwd"], issue_data, 123, setup["repository"]
454+
)
424455

425-
# Fine-grained + public + assets = False
426-
needs_jwt = (
427-
token_fine is not None
428-
and public
429-
and "github.com/user-attachments/assets/" in assets_url
456+
markdown_api.assert_called_once_with(
457+
"https://github.com/user-attachments/assets/abc123",
458+
"ghs_test",
459+
"testuser/testrepo",
430460
)
431-
assert needs_jwt is False
461+
# The JWT-signed URL is downloaded without the installation token
462+
download.assert_called_once()
463+
assert download.call_args.args[0] == jwt_url
464+
assert download.call_args.kwargs["auth"] is None
465+
466+
manifest_path = os.path.join(setup["issue_cwd"], "attachments", "123", "manifest.json")
467+
with open(manifest_path) as f:
468+
manifest = json.load(f)
469+
assert manifest["attachments"][0]["jwt_workaround"] is True
432470

433471
def test_jwt_workaround_sets_manifest_flag(self, attachment_test_setup):
434472
"""Successful JWT workaround sets jwt_workaround flag in manifest."""

0 commit comments

Comments
 (0)