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
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.
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_formfallsshort in three ways:
<input>elements that have avalue;<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 formscome 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. Ithandles 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 inlineerrors, plus the status, redirect chain and title. Hidden error markup
that every GitHub page ships (the ajax-error banner,
js-flash-template,hidden/d-nonenotes) 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. Theexcerpt 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]:authenticity_token, CSRF,nonce, session-ID or other token-named control,
<meta>tag orattribute, whether written literally or as character references.
This includes class/id-marked CSRF inputs and
data-csrfinputs.base64--hexdigest), such as live-updatesession URLs and notification channels, including inside
percent-encoded URLs.
<script>, including embeddedcsrf_tokensJSON and unclosed scripts.
and the
codequery parameter of manifest redirect URLs.If the page can't be scanned, the function returns
""rather than riskan unredacted excerpt.
describe_forms/describe_document_forms: a description of apage's forms with token-like values redacted, for checking that a page
still has the expected shape.
Retries:
request_with_retryretries 429 and 5xx responses, honoringRetry-AfterandX-RateLimit-Reset. By default it only resendsGET/HEAD/OPTIONS. Other methods need
retry_unsafe_methods=True.configure_session_adaptersmounts transport retries:other=0). Without thatsetting, urllib3 resends any request, POST included, after an
error it can't classify, such as an SSL error while reading the
response.
submit_oncePOSTs exactly once. It raisesOutcomeUnknownErrorwhen the request may have been applied (5xx, read timeout, dropped
connection, SSL error). It re-raises when the request was never sent.
Failures are
FormErrorsubclasses:PageShapeError,FormRejectedErrorandOutcomeUnknownError. Each carriessummaryanderrors.Compatibility: existing
Apimethods and_get_and_submit_formareunchanged. No new runtime dependencies (
html5lib,requestsandurllib3are already required).
Tests:
tests/test_forms.pywith fixturetests/browser_form_edge_cases.html.BUG=http://b/571180795