Skip to content

fix(a11y): add type="button" to native buttons to prevent host form submission#10863

Open
yogeshwaran-c wants to merge 1 commit into
swagger-api:masterfrom
yogeshwaran-c:fix/a11y-button-type-default-submit
Open

fix(a11y): add type="button" to native buttons to prevent host form submission#10863
yogeshwaran-c wants to merge 1 commit into
swagger-api:masterfrom
yogeshwaran-c:fix/a11y-button-type-default-submit

Conversation

@yogeshwaran-c
Copy link
Copy Markdown

Description

Adds type="button" to every interactive native <button> rendered by Swagger UI that previously omitted the attribute. Without it, every such button defaults to type="submit" and triggers form submission whenever Swagger UI is rendered inside a host application's <form>.

Affected components:

  • AuthorizeBtn, AuthorizeOperationBtn (auth)
  • Clear, Execute, TryItOutButton (Try it out / Cancel / Reset)
  • OperationSummary (opblock summary control + opblock arrow)
  • OperationTag (expand-operation)
  • ModelCollapse, ModelExample tablinks, Models and OAS 3.1 Models (Schemas/Models toggle)
  • RequestSnippets snippet expand button, HighlightCode Download button
  • Standalone DarkModeToggle

Buttons already covered by other open a11y/security PRs are intentionally left untouched to avoid merge conflicts.

Motivation and Context

Fixes #10234.

Issue #10234 reports that Swagger UI React, when wrapped inside a host React Hook Form / <form>, submits the form whenever the user clicks the operation expand arrow. The reproduction confirms that the rendered DOM is <button aria-label="post /pet/{petId}/uploadImage" class="opblock-control-arrow" aria-expanded="false" tabindex="-1"></button> — no type attribute, so the browser defaults it to submit. The same defect applies to every other interactive button Swagger UI renders.

This is a 1.3.1 / 4.1.2 a11y concern as well: assistive tech and form integrations both rely on the type attribute being explicit for non-submitting controls.

How Has This Been Tested?

  • Jest unit tests (test/unit/components/a11y/button-type.jsx) — 8 cases asserting wrapper.find("button").prop("type") === "button" on AuthorizeBtn, AuthorizeOperationBtn, Clear, Execute, OperationTag, TryItOutButton (Try it out / Cancel + Reset) and ModelCollapse. Verified that the suite fails on master and passes after the fix.
  • Cypress e2e (test/e2e-cypress/e2e/bugs/10234.cy.js, with fixture page test/e2e-cypress/static/pages/10234/index.html) — mounts Swagger UI inside a host <form action="/should-not-submit" onsubmit="...">. Five scenarios exercise the tag expand button, opblock summary, opblock arrow, "Try it out", and the Schemas/Models toggle, asserting after each click that the host form's onsubmit sentinel stays false and the URL pathname does not change. All five fail on master and pass after the fix (verified locally with npx cypress run --spec test/e2e-cypress/e2e/bugs/10234.cy.js).
  • Full unit suite (npm run test:unit) — no new failures introduced; the two pre-existing "Test suite must contain at least one test" failures on jest-shim.js/setup.js are unrelated and present on master.

Screenshots (if appropriate):

N/A — no visual changes.

Checklist

My PR contains...

  • Bug fixes (non-breaking change which fixes an issue)

My changes...

  • are breaking changes to a public API (config options, System API, major UI change, etc).
  • are not breaking changes to a public API.

Documentation

  • My changes do not require a change to the project documentation.

…ubmission

Closes swagger-api#10234

When Swagger UI is mounted inside a host application's <form> element, every
native <button> without an explicit `type` attribute defaults to
`type="submit"`. Clicking the operation expand arrow, the Authorize button,
Execute, Try it out, the Schemas/Models toggle, etc. therefore navigates the
host page or fires the host form's onSubmit handler.

Adds `type="button"` to every interactive native <button> rendered by Swagger
UI that previously omitted the attribute.

Includes:

- Jest unit tests asserting wrapper.find("button").prop("type") === "button"
  on AuthorizeBtn, AuthorizeOperationBtn, Clear, Execute, OperationTag,
  TryItOutButton (Try it out / Cancel / Reset) and ModelCollapse.

- Cypress e2e regression test (test/e2e-cypress/e2e/bugs/10234.cy.js) that
  mounts Swagger UI inside a host <form action="/should-not-submit"> and
  asserts that clicking the tag expand button, opblock summary, opblock
  arrow, "Try it out", and the Schemas/Models toggle all leave the form
  un-submitted (the onsubmit sentinel stays false and the URL pathname
  does not change).
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.

add type to button elements

1 participant