Skip to content

Support GitHub Enterprise Server URLs and creating apps from manifests - #237

Open
cjwagner wants to merge 2 commits into
google:masterfrom
cjwagner:rfc0008-manifest-base-url
Open

cjwagner wants to merge 2 commits into
google:masterfrom
cjwagner:rfc0008-manifest-base-url

Conversation

@cjwagner

@cjwagner cjwagner commented Nov 26, 2026 •

Copy link
Copy Markdown
Member

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: Api and create_login_session take the web root.

    • The default is https://github.com. A GHES URL such as
      https://github.example.com is also accepted.
    • Every page URL template is built from it, including the
      dormant-users export link. That link was hard-coded to one
      github.com enterprise; it is now built from base_url and the
      requested enterprise.
    • Only sign-in and manifest creation were verified on GHES.
    • A value that isn't the absolute http(s) URL of a web root raises
      ValueError. That includes a URL with a path, query or fragment.
  • Login:

    • tfa_callback is optional. Without it, a two-factor, sign-in or
      device-verification page after the password step raises
      LoginChallengeError (a FormError). It no longer returns a
      signed-out session.
    • With a callback, a page on the GitHub host other than the two-factor
      prompt still raises, as it did upstream.
    • A new check: if the session is still on a sign-in page after the code
      is submitted, the code was rejected, and that raises
      LoginChallengeError too.
    • Error messages name the likely causes (wrong credentials, captcha,
      device verification, rejected code).
    • create_login_session mounts transport retries on the session.
  • Api.create_app_from_manifest runs GitHub's documented manifest flow
    the way a browser does, using the forms module:

    1. POST the manifest. This only renders a confirmation page, so it may be
      retried.
    2. Find the single POST form that has an integration_manifest[owner_login]
      field.
    3. Check that the form offers to create the app for the requested
      organization, or for the signed-in user named by the page's
      user-login meta tag.
    4. Submit the form exactly once, with redirects disabled.
    5. Return the absolute callback URL, which carries the single-use code.

    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"> or a#redirect link. It is used
    only 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:

    • A manifest posted for an organization is redirected to
      /settings/apps/manifest, and that form posts to /settings/apps, the
      same as the user's own form.
    • A manifest posted for an organization the user did not own was answered
      with the user's own confirmation form. Submitting it would have made the
      app user-owned. This case now raises PageShapeError, which asks whether
      the user is an owner of the organization.

    Errors:

    Error Raised when App state
    LoginChallengeError (a FormError) The manifest or the confirmation was answered with GitHub's sign-in or sudo confirmation page, or a redirect to it. No app.
    FormRejectedError GitHub showed visible errors instead of the confirmation page, or answered the confirmation with visible errors and no URL with a code. No app (it needs a code exchange).
    PageShapeError (with a redacted body_excerpt) Before the confirmation is sent: an unexpected page, no single confirmation form with a submit button, or a form for the wrong owner. After it: a 4xx answer with no URL with a code. No app (it needs a code exchange).
    OutcomeUnknownError (with a redacted body_excerpt for pages) The confirmation may have been accepted but no usable code came back: no definitive answer, any other redirect (even one to a URL with a code; the message redacts the code), or a page that has no visible errors and doesn't have exactly one URL with a code. We don't know if GitHub accepted the confirmation. No app was registered, because registering needs a code exchange and the caller got no code.
    requests.RequestException Posting the manifest failed, or the confirmation was never sent or was rate limited. Posting the manifest is retried on 429/5xx. Read errors are not retried. A session passed to Api(session=...) gets no transport retries unless the caller mounted them. No app.
  • Tests: Tests use the responses library, which is added to
    requirements.txt along with its PyYAML dependency (both test-only). They
    also use two sanitized captures from a GitHub Enterprise Server:

    • the organization-owned confirmation page, reached through the same
      redirect;
    • the full continue-setup page that answered the confirmation, including
      its real head. Both its meta refresh and its a#redirect link carry the
      code.

    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 single
    mocked 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, app
    registered and owned by the organization).

BUG=http://b/571180795

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
cjwagner force-pushed the rfc0008-manifest-base-url branch from 0889ebb to ca63a71 Compare November 26, 2026 01:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant