Skip to content

Web: Fixes handling native fetch response that can result in unexpected errors#52154

Merged
kimlisa merged 2 commits intomasterfrom
lisa/fix-reading-json
Feb 18, 2025
Merged

Web: Fixes handling native fetch response that can result in unexpected errors#52154
kimlisa merged 2 commits intomasterfrom
lisa/fix-reading-json

Conversation

@kimlisa
Copy link
Copy Markdown
Contributor

@kimlisa kimlisa commented Feb 13, 2025

fixes regression introduced by #51134

some of the callsite to api.fetch expects and handles native fetch response, explained more here

also some of our api responses does not return a body like here, and if we try to response.json() it throws an error, even though the request itself was successful

previously, the api.fetch definition was just returning native fetch call but ^ PR changed it to also read JSON

we should ideally be returning OK json response, but some of it has slipped and i don't think there is too many

example of error on a success fetch:
image

changelog: Fixed broken Download Metadata File button from the SAML enrolling resource flow in the web UI.
changelog: Fixed broken Refresh button in the Access Monitoring reports page in the web UI.
changelog: Fixed broken Download app.zip menu item in the Integrations list dropdown menu for Microsoft Teams in the web UI.
changelog: Fixed Unexpected end of JSON input error in an otherwise successful web API call.

Comment thread lib/web/git_servers.go
@kimlisa kimlisa requested review from Joerger and avatus February 13, 2025 22:09
@kimlisa kimlisa force-pushed the lisa/fix-reading-json branch 2 times, most recently from a4286a4 to bfcd7e8 Compare February 13, 2025 22:15
@kimlisa kimlisa force-pushed the lisa/fix-reading-json branch from bfcd7e8 to db852fc Compare February 14, 2025 00:06
@zmb3
Copy link
Copy Markdown
Collaborator

zmb3 commented Feb 14, 2025

Can we add a test case which would have caught the regression?

Copy link
Copy Markdown
Contributor

@bl-nero bl-nero left a comment

Choose a reason for hiding this comment

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

Approving with a small caveat.

// to read.
const contentType = response.headers?.get('content-type');
if (!contentType || !contentType.includes('application/json')) {
return response;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm wondering if using https://developer.mozilla.org/en-US/docs/Web/API/Response/text wouldn't be a more "symmetrical" solution to returning JSON if the body is a JSON — that is, unless in our case, an empty body also throws instead of resolving to an empty string.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

your comment saved us. because it led to a discussion about typing the return value... where the linter caught more regressions 🙏

what happened was, previous api.fetch had a auto derived type Promise<Response>, after the change, the type changed to Promise<any>, which is why the linter didn't catch bugs

what i ended up doing was revert the api.fetch to its previous implementation, which is to return the native fetch response object (and explicitly typed it), and moved the json processing to another function call.

the reason for the revert was that in some areas of our code, the caller expects and handles the native fetch response (eg: here, here and here)

@kimlisa kimlisa force-pushed the lisa/fix-reading-json branch 2 times, most recently from a42b23b to 8181876 Compare February 18, 2025 20:14
@kimlisa kimlisa changed the title Web: don't try to read JSON if content-type is not application/json Web: Fixes handling native fetch response that can result in unexpected errors Feb 18, 2025
@kimlisa kimlisa requested review from avatus and bl-nero February 18, 2025 20:23
@kimlisa kimlisa force-pushed the lisa/fix-reading-json branch from 8181876 to 57cec68 Compare February 18, 2025 20:45
@kimlisa kimlisa enabled auto-merge February 18, 2025 20:55
@kimlisa kimlisa added this pull request to the merge queue Feb 18, 2025
Merged via the queue into master with commit 221c316 Feb 18, 2025
@kimlisa kimlisa deleted the lisa/fix-reading-json branch February 18, 2025 21:26
@public-teleport-github-review-bot
Copy link
Copy Markdown

@kimlisa See the table below for backport results.

Branch Result
branch/v17 Create PR

carloscastrojumo pushed a commit to carloscastrojumo/teleport that referenced this pull request Feb 19, 2025
…ed errors (gravitational#52154)

* Web: fix reading JSON from an empty response

* Revert api.fetch to native fetch return type
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants