Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix "Parse Error" when performing HTTP requests #5988

Merged
merged 27 commits into from
Mar 18, 2020

Conversation

flotwig
Copy link
Contributor

@flotwig flotwig commented Dec 17, 2019

User facing changelog

  • Fixed a regression in 3.5.0 where certain HTTP requests could fail with "Parse Error: Invalid header value char" or "Parse Error: Header overflow".

Additional details


Notes:

  • "header overflow" will be fixed by There is no way to set --max-http-header-size in Electron 5.x electron/electron#20831 and adding a bigger max http header size
  • able to reproduce by adding test to visit header with null char:
    ## https://github.com/cypress-io/cypress/issues/5602
    app.get "/invalid-header-char", (req, res) ->
    ## express/node may interfere if we just use res.setHeader
    res.connection.write(
    """
    HTTP/1.1 200 OK
    Content-Type: text/html
    Set-Cookie: foo=bar-#{String.fromCharCode(1)}-baz
    foo
    """
    )
    res.connection.end()
  • passing --http-parser=legacy in NODE_OPTIONS gets rid of the error
  • even with --http-parser=legacy, still fails, but in a different place:
    TypeError [ERR_INVALID_CHAR] [ERR_INVALID_CHAR]: Invalid character in header content ["x-foo"]     
      at ServerResponse.setHeader (_http_outgoing.js:467:3)
    
    • fixed this by catching ERR_INVALID_CHAR errors and manually doing the setHeader steps in that case:
      // run the original function - if an "invalid header char" error is raised,
      // set the header manually. this way we can retain Node's original error behavior
      try {
      return originalSetHeader.call(this, k, v)
      } catch (err) {
      if (err.code !== 'ERR_INVALID_CHAR') {
      throw err
      }
      debug('setHeader error ignored %o', { code: err.code, err })
      if (!kOutHeaders) {
      kOutHeaders = getKOutHeadersSymbol()
      }
      // https://github.com/nodejs/node/blob/42cce5a9d0fd905bf4ad7a2528c36572dfb8b5ad/lib/_http_outgoing.js#L483-L495
      let headers = this[kOutHeaders]
      if (!headers) {
      this[kOutHeaders] = headers = Object.create(null)
      }
      headers[k.toLowerCase()] = [k, v]
      }
    • gnarly code to find the kOutHeaders symbol, can't find a better way

How has the user experience changed?

PR Tasks

  • Have tests been added/updated?
  • Has the original issue been tagged with a release in ZenHub?
  • [na] Has a PR for user-facing changes been opened in cypress-documentation?
  • [na] Have API changes been updated in the type definitions?
  • [na] Have new configuration options been added to the cypress.schema.json?

@cypress-bot
Copy link
Contributor

cypress-bot bot commented Dec 17, 2019

Thanks for the contribution! Below are some guidelines Cypress uses when doing PR reviews.

  • Please write [WIP] in the title of your Pull Request if your PR is not ready for review - someone will review your PR as soon as the [WIP] is removed.
  • Please familiarize yourself with the PR Review Checklist and feel free to make updates on your PR based on these guidelines.

PR Review Checklist

If any of the following requirements can't be met, leave a comment in the review selecting 'Request changes', otherwise 'Approve'.

User Experience

  • The feature/bugfix is self-documenting from within the product.
  • The change provides the end user with a way to fix their problem (no dead ends).

Functionality

  • The code works and performs its intended function with the correct logic.
  • Performance has been factored in (for example, the code cleans up after itself to not cause memory leaks).
  • The code guards against edge cases and invalid input and has tests to cover it.

Maintainability

  • The code is readable (too many nested 'if's are a bad sign).
  • Names used for variables, methods, etc, clearly describe their function.
  • The code is easy to understood and there are relevant comments explaining.
  • New algorithms are documented in the code with link(s) to external docs (flowcharts, w3c, chrome, firefox).
  • There are comments containing link(s) to the addressed issue (in tests and code).

Quality

  • The change does not reimplement code.
  • There's not a module from the ecosystem that should be used instead.
  • There is no redundant or duplicate code.
  • There are no irrelevant comments left in the code.
  • Tests are testing the code’s intended functionality in the best way possible.

Internal

  • The original issue has been tagged with a release in ZenHub.

@cypress
Copy link

cypress bot commented Dec 17, 2019



Test summary

3270 0 49 0


Run details

Project cypress
Status Passed
Commit 4060beb
Started Mar 17, 2020 8:29 PM
Ended Mar 17, 2020 8:36 PM
Duration 06:21 💡
OS Linux Debian - 10.1
Browser Firefox 74

View run in Cypress Dashboard ➡️


This comment has been generated by cypress-bot as a result of this project's GitHub integration settings. You can manage this integration in this project's settings in the Cypress Dashboard

@flotwig flotwig force-pushed the issue-5602-cookie-parse-error branch from b0b6fd0 to c656bfa Compare January 7, 2020 18:05
* cli: set NODE_OPTIONS=--max-http-header-size=1024*1024 on spawn

* electron: remove redundant max-http-header-size

* server: add useCli option to make e2e tests go thru cli

* server: add test for XHR with body > 100kb via CLI

* clean up conditional

* cli: don't pass --max-http-header-size in dev w node < 11.10

* add original_node_options to restore o.g. user node_options

* force no color
…cookie-parse-error

See #5492 for original review and discussion
@flotwig flotwig changed the title [WIP] Fix "Parse Error" [WIP] Fix "Parse Error" when performing HTTP requests Mar 17, 2020
@flotwig flotwig changed the title [WIP] Fix "Parse Error" when performing HTTP requests Fix "Parse Error" when performing HTTP requests Mar 17, 2020
@flotwig flotwig requested review from a team, brian-mann and CypressJoseph and removed request for a team March 17, 2020 20:17
Copy link
Contributor

@CypressJoseph CypressJoseph left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm! some pretty arcane things here, but looks really solid 👍

just out of curiosity -- why do we use the legacy electron http parser? (is the idea just compliance with previous cypress behavior?)

@flotwig
Copy link
Contributor Author

flotwig commented Mar 17, 2020

why do we use the legacy electron http parser? (is the idea just compliance with previous cypress behavior?)

@CypressJoseph yeah, it fixes the regression in Cypress, but imo it's not just about backwards-compatibility - Cypress should be a 100% transparent HTTP proxy, even if the HTTP traffic it's proxying is a little out-of-spec. Cypress should load pages the same as a normal browser does, and browsers are very tolerant. So when Node introduced a new, stricter HTTP parser that's on by default, it made the proxy start rejecting some previously-acceptable traffic. See this comment for technical info on the changes that made this necessary.

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.

Cypress is unable to load site in v3.5.0 due to "Parse Error"
2 participants