Repository navigation
Conversation
GitHub's web UI posts HTML forms, and automating a page reliably requires sending the same fields a browser sends. The existing _get_and_submit_form helper sends only <input> elements that have a value, submits the first form on the page by default, and parses pages with html5lib 1.1, which lets a <form> inside a <template> close the outer form and drop its submit button. The new github_nonpublic_api.forms module provides: * parse_page: html5lib parsing with <template> contents emptied first, so forms come out the way a browser builds them. * find_form_by_action: picks a form by action path and Rails _method. * browser_form_data: the HTML entry-list algorithm (what FormData returns) for urlencoded forms: selects, textareas, checkbox/radio state, form= owners, disabled fieldsets, and the default submitter. Simulated edits to controls the form doesn't have raise PageShapeError. * page_errors and response_summary: the visible flash/inline errors and a status/redirect/title summary of a rejected submission, ignoring the hidden error markup every GitHub page ships. * redacted_body_excerpt: up to 4 KB of a page, starting at <main> (else <body>), with CSRF/token and session-ID values, Rails-signed values (such as the live-update session and notification channel), script contents and manifest codes redacted before truncation. FormError.body_excerpt carries it outside the exception message, so str(error) and logs never contain page HTML. * describe_forms / describe_document_forms: a redacted description of a page's forms, for verifying that a page still has the expected shape. * request_with_retry and configure_session_adapters: retries on rate limits, 5xx and transport errors that never resend a non-idempotent request unless the caller opts in. The mounted urllib3 Retry sets other=0, because urllib3 otherwise resends any request, POST included, after an error it cannot classify, such as an SSL error while reading the response. * submit_once: sends a state-changing POST once and reports OutcomeUnknownError when it may have been applied. Failures are FormError subclasses (PageShapeError, FormRejectedError, OutcomeUnknownError) carrying the visible errors and response summary. Existing Api methods and _get_and_submit_form are unchanged; moving them to the new engine is left for later changes. No new runtime dependencies. BUG=http://b/571180795
Api and create_login_session only worked against github.com, and the library had no way to register a GitHub App from a manifest, which GitHub only offers as a browser flow. * base_url: Api and create_login_session take the web root, defaulting to https://github.com, and every page URL is built from it. Only sign-in and manifest creation were verified on a GitHub Enterprise Server. A value that is not the absolute http(s) URL of a web root (with no path, query or fragment) raises ValueError. download_dormant_users_report looks for the export link under base_url and the requested enterprise instead of a hard-coded github.com enterprise. * Login: tfa_callback is optional. Without it, a two-factor, sign-in or device-verification page after the password step raises LoginChallengeError (a FormError) instead of returning a signed-out session. With it, a page on the GitHub host other than the two-factor prompt still raises, and so does a sign-in page after the code is submitted, which means the code was rejected. create_login_session mounts transport retries on the session. * Api.create_app_from_manifest follows GitHub's documented manifest flow the way a browser does, using the forms module: POST the manifest (which only renders a confirmation page and may be retried), find the single POST form with an integration_manifest[owner_login] field, check that it offers to create the app for the requested organization (or for the signed-in user named by the page's user-login meta tag), submit it exactly once with redirects disabled, and return the absolute callback URL that carries the single-use code. The caller exchanges the code with POST /app-manifests/{code}/conversions; that exchange, not the confirmation, is what registers the app, so a code lost after the confirmation leaves no app. GitHub answers the confirmation with a 200 "You are being redirected to continue setup of your GitHub App" page, so the URL is taken from that page's meta refresh or a#redirect link, and only if exactly one URL with a non-empty code is found. The form is found by its owner field rather than its action because GitHub redirects a manifest posted for an organization to /settings/apps/manifest, whose form posts to /settings/apps just like the user's own, and answered a manifest posted for an organization the user did not own with the user's own confirmation form, which would have made the app user-owned; that now raises PageShapeError asking whether the user is an owner of the organization. Failures are typed: FormRejectedError for visible errors, PageShapeError (with a redacted body_excerpt) for an unexpected page or owner before the confirmation is sent, or a 4xx answer to it, LoginChallengeError for GitHub's sign-in or sudo confirmation page (or a redirect to it), and OutcomeUnknownError when the confirmation may have been accepted without delivering a usable code: no definitive answer, any other redirect (even one to a URL with a code, which the message redacts), or a page with neither visible errors nor exactly one URL with a code (with a redacted body_excerpt). Tests use the responses library (added to requirements.txt with its PyYAML dependency) and two sanitized captures from a GitHub Enterprise Server: the organization-owned confirmation page, reached through the same redirect, and the full continue-setup page that answered the confirmation, whose meta refresh and a#redirect link both carry the code. The upstream test_create_login_session_success now gives the code submission its own landing page: its single mocked response put the session back on the two-factor page, which is now a rejected code. BUG=http://b/571180795
cjwagner
force-pushed
the
rfc0008-manifest-base-url
branch
from
November 26, 2026 01:24
0889ebb to
ca63a71
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Important
This PR depends on #236 (forms module). Please review #236 first.
Review only the second commit here: ca63a71 ("Support GitHub Enterprise Server URLs and creating apps from manifests").
The first commit (e996cdc) is #236. It appears in this diff only because a pull request from a fork can't target another PR's branch. Once #236 merges, I'll rebase this PR and only the second commit will remain.
base_url:Apiandcreate_login_sessiontake the web root.https://github.com. A GHES URL such ashttps://github.example.comis also accepted.dormant-users export link. That link was hard-coded to one
github.com enterprise; it is now built from
base_urland therequested enterprise.
ValueError. That includes a URL with a path, query or fragment.Login:
tfa_callbackis optional. Without it, a two-factor, sign-in ordevice-verification page after the password step raises
LoginChallengeError(aFormError). It no longer returns asigned-out session.
prompt still raises, as it did upstream.
is submitted, the code was rejected, and that raises
LoginChallengeErrortoo.device verification, rejected code).
create_login_sessionmounts transport retries on the session.Api.create_app_from_manifestruns GitHub's documented manifest flowthe way a browser does, using the forms module:
retried.
integration_manifest[owner_login]field.
organization, or for the signed-in user named by the page's
user-loginmeta tag.The caller exchanges the code with
POST /app-manifests/{code}/conversions.The exchange registers the app; the confirmation does not. So if the code
is lost after the confirmation, no app is left.
GitHub answers the confirmation with a 200 "You are being redirected to
continue setup of your GitHub App" page, so the callback URL is read from
that page's
<meta http-equiv="refresh">ora#redirectlink. It is usedonly if the page has exactly one such URL with a non-empty code.
The form is found by its owner field, not its action, because of two GitHub
behaviours:
/settings/apps/manifest, and that form posts to/settings/apps, thesame as the user's own form.
with the user's own confirmation form. Submitting it would have made the
app user-owned. This case now raises
PageShapeError, which asks whetherthe user is an owner of the organization.
Errors:
LoginChallengeError(aFormError)FormRejectedErrorPageShapeError(with a redactedbody_excerpt)OutcomeUnknownError(with a redactedbody_excerptfor pages)requests.RequestExceptionApi(session=...)gets no transport retries unless the caller mounted them.Tests: Tests use the
responseslibrary, which is added torequirements.txtalong with its PyYAML dependency (both test-only). Theyalso use two sanitized captures from a GitHub Enterprise Server:
redirect;
its real head. Both its meta refresh and its
a#redirectlink carry thecode.
The tests cover these continue-setup cases: the link alone or the meta
refresh alone is enough; a code changed in only one of the two makes the
outcome unknown; a link inside a
<template>is ignored.One upstream test changed:
test_create_login_session_success. Its singlemocked response sent the code submission back to the two-factor page,
which now counts as a rejected code. The test now gives the code
submission its own landing page (the dashboard). Its assertions are
unchanged.
Verification: run end to end against a GitHub Enterprise Server 3.21
instance: sign-in with TOTP, an organization-owned manifest, the returned
code exchanged with
POST /app-manifests/{code}/conversions(201, appregistered and owned by the organization).
BUG=http://b/571180795