Skip to content

Add a forms module that submits GitHub web forms like a browser - #236

Open
cjwagner wants to merge 1 commit into
google:masterfrom
cjwagner:rfc0008-forms-engine
Open

cjwagner wants to merge 1 commit into
google:masterfrom
cjwagner:rfc0008-forms-engine

Conversation

@cjwagner

Copy link
Copy Markdown
Member

GitHub's web UI posts HTML forms. To automate a page reliably, a client has to
send the same fields a browser sends. The current _get_and_submit_form falls
short in three ways:

  • it sends only <input> elements that have a value;
  • by default it submits the first form on the page;
  • html5lib 1.1 lets a <form> inside a <template> close the outer form.
    On GitHub's New GitHub App page this dropped the submit button.

This PR adds github_nonpublic_api/forms.py:

  • parse_page: empties <template> contents before parsing, 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 "constructing the entry list" algorithm
    (what new FormData(form, submitter) returns) for urlencoded forms. It
    handles selects, textareas, checkbox/radio state, form= owners,
    disabled fieldsets (except inside the first legend) and the default
    submitter. An edit to a control the form doesn't have raises
    PageShapeError.

  • page_errors / response_summary: the visible flash or inline
    errors, plus the status, redirect chain and title. Hidden error markup
    that every GitHub page ships (the ajax-error banner, js-flash-template,
    hidden/d-none notes) is skipped.

  • FormError.body_excerpt / redacted_body_excerpt(html, limit=4096):
    a redacted excerpt of the page, at most 4 KB of UTF-8 (never splits a
    character). It starts at <main>, else <body>, else byte 0. The
    excerpt is never part of the exception message, so str(e), repr(e)
    and logs never contain page HTML. Redaction runs before truncation, so a
    token cut off at the limit can't leak a fragment. These are replaced with
    [REDACTED]:

    • every occurrence of the value of an authenticity_token, CSRF,
      nonce, session-ID or other token-named control, <meta> tag or
      attribute, whether written literally or as character references.
      This includes class/id-marked CSRF inputs and data-csrf inputs.
    • Rails-signed values (base64--hexdigest), such as live-update
      session URLs and notification channels, including inside
      percent-encoded URLs.
    • the contents of every <script>, including embedded csrf_tokens
      JSON and unclosed scripts.
    • values of token-named JSON/JS keys, query parameters and attributes,
      and the code query parameter of manifest redirect URLs.

    If the page can't be scanned, the function returns "" rather than risk
    an unredacted excerpt.

  • describe_forms / describe_document_forms: a description of a
    page's forms with token-like values redacted, for checking that a page
    still has the expected shape.

  • Retries:

    • request_with_retry retries 429 and 5xx responses, honoring
      Retry-After and X-RateLimit-Reset. By default it only resends
      GET/HEAD/OPTIONS. Other methods need retry_unsafe_methods=True.
    • configure_session_adapters mounts transport retries:
      • connection failures are retried for every method;
      • read errors are retried only for idempotent methods, not POST;
      • other errors are never retried (other=0). Without that
        setting, urllib3 resends any request, POST included, after an
        error it can't classify, such as an SSL error while reading the
        response.
    • submit_once POSTs exactly once. It raises OutcomeUnknownError
      when the request may have been applied (5xx, read timeout, dropped
      connection, SSL error). It re-raises when the request was never sent.

Failures are FormError subclasses: PageShapeError, FormRejectedError and
OutcomeUnknownError. Each carries summary and errors.

Compatibility: existing Api methods and _get_and_submit_form are
unchanged. No new runtime dependencies (html5lib, requests and urllib3
are already required).

Tests: tests/test_forms.py with fixture
tests/browser_form_edge_cases.html.

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
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