Skip to content

Commit aafa75d

Browse files
alxhubmhevery
authored andcommitted
fix(common): don't strip XSSI prefix for if error isn't JSON (angular#19958)
This changes XhrBackend to not strip the XSSI prefix from error text if such a prefix is present but the remaining body does not parse as JSON. PR Close angular#19958
1 parent 503be69 commit aafa75d

File tree

1 file changed

+8
-1
lines changed
  • packages/common/http/src

1 file changed

+8
-1
lines changed

packages/common/http/src/xhr.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,11 +181,18 @@ export class HttpXhrBackend implements HttpBackend {
181181
// Check whether the body needs to be parsed as JSON (in many cases the browser
182182
// will have done that already).
183183
if (req.responseType === 'json' && typeof body === 'string') {
184-
// Attempt the parse. If it fails, a parse error should be delivered to the user.
184+
// Save the original body, before attempting XSSI prefix stripping.
185+
const originalBody = body;
185186
body = body.replace(XSSI_PREFIX, '');
186187
try {
188+
// Attempt the parse. If it fails, a parse error should be delivered to the user.
187189
body = body !== '' ? JSON.parse(body) : null;
188190
} catch (error) {
191+
// Since the JSON.parse failed, it's reasonable to assume this might not have been a
192+
// JSON response. Restore the original body (including any XSSI prefix) to deliver
193+
// a better error response.
194+
body = originalBody;
195+
189196
// If this was an error request to begin with, leave it as a string, it probably
190197
// just isn't JSON. Otherwise, deliver the parsing error to the user.
191198
if (ok) {

0 commit comments

Comments
 (0)