cypress icon indicating copy to clipboard operation
cypress copied to clipboard

fix: HTTP response with invalid headers doesn't throw error #28865

Open BernardoSousa03 opened this issue 10 months ago • 7 comments

When receiving the described HTTP response Cypress resets the headers. This would cause the validateHeaderName method from node to be called which would cause an error, since the headers where invalid. Now Crypress verifies all the headers before reseting them, discards invalid ones and sends a warning in the console when debug module is on.

  • Closes https://github.com/cypress-io/cypress/issues/28865

Additional details

The error lead to Cypress throwing errors when it wasn't supposed to

Steps to test

Create a server that responds with invalid headers to a HTTP request

How has the user experience changed?

The tests don't fail anymore and with the debug module on there is a warning that shows the invalid headers

PR Tasks

BernardoSousa03 avatar Apr 26 '24 23:04 BernardoSousa03

CLA assistant check
All committers have signed the CLA.

CLAassistant avatar Apr 26 '24 23:04 CLAassistant

cypress-app-bot avatar Apr 26 '24 23:04 cypress-app-bot

@BernardoSousa03 Thanks for the contribution and for adding a test case around the issue! I’ll check in the upcoming sprint to see who is available to review.

jennifer-shehane avatar Apr 27 '24 04:04 jennifer-shehane

Thank you for your review @cacieprins. I'll get to it as soon as i can!

BernardoSousa03 avatar May 12 '24 17:05 BernardoSousa03

Great, thank you @BernardoSousa03 !

Additionally, it may be useful to include validation of the header value, as well, with validateHeaderValue from the http package.

I recommend taking a look at /packages/errors/.

  • a new entry can be added to errors.ts for templating how the error message is displayed in stdout
  • including the error thrown, original http header (if not included in the error object's message), as well as the the URL and method being proxied for the warning display would be great
  • a new entry in packages/errors/test/unit/errors_spec.ts for CI to accept the new error template - make sure to run yarn test in this package to generate a snapshot of the formatted error message. You can review the error message visually by opening the corresponding HTML file in ./packages/errors/__snapshot-html__/
  • errors.warning('PROXY_ENCOUNTERED_INVALID_HEADER_NAME', error, headerName) and errors.warning('PROXY_ENCOUNTERED_INVALID_HEADER_VALUE', error, headerValue) would be the expected usage in the proxy package.

This message could read similar to:

Warning: While proxying a ${fmt.highlight(method)} request to ${fmt.url(urll)}, an HTTP header did not pass validation, and was removed. This header will not be present in the response received by the application under test.

Invalid header name: ${fmt.code(JSON.stringify(header, undefined, 2))}

${fmt.highlightSecondary(error)

cacieprins avatar May 14 '24 15:05 cacieprins

@cacieprins while trying out the /packages/errors/ i found out that there is no visual representation for the warning while using errors.warning in the cypress GUI app. Is this what you had in mind? I was thinking, since there is no plausible reason for having badly formed headers, it might be of interest to show the user it's mistake. I tried it with errors.throwErr but with no success. Should i just stick to using the first solution?

BernardoSousa03 avatar May 17 '24 21:05 BernardoSousa03

The errors package is for displaying errors to the commandline - they won't show in the GUI, but they will be helpful for folks who may run into issues with this in CI. Please use errors.warning, instead of errors.throwErr, because we don't want to fully throw an error when this is encountered :)

cacieprins avatar May 20 '24 14:05 cacieprins

@cacieprins i have added the requested changes, anything else let me know!

BernardoSousa03 avatar May 23 '24 22:05 BernardoSousa03

Released in 13.12.0.

This comment thread has been locked. If you are still experiencing this issue after upgrading to Cypress v13.12.0, please open a new issue.

cypress-bot[bot] avatar Jun 18 '24 22:06 cypress-bot[bot]