Skip to content

Commit

Permalink
Fix fetchHttpClient issue with redirects (#21051)
Browse files Browse the repository at this point in the history
* Fix fetchHttpClient issue with redirects

Browsers expect to handle redirects automatically when using fetch, much like XMLHttpRequest's default behavior.

The fact that fetch() lets you set a "manual" mode, isn't intended for common use, but rather for a specific Service Worker scenario. The response sent back is heavily redacted and contains neither the true status or any HTTP headers from the response.
  • Loading branch information
xirzec authored Mar 28, 2022
1 parent fe4bd93 commit ff6f0ac
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 2 deletions.
1 change: 1 addition & 0 deletions sdk/core/core-rest-pipeline/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
### Bugs Fixed

- Updated `redirectPolicy` to remove the `Authorization` header from redirected requests. [#21026](https://github.com/Azure/azure-sdk-for-js/pull/21026)
- Fixed an issue introduced in 1.6.0 where redirects were not properly followed in the browser. [#21051](https://github.com/Azure/azure-sdk-for-js/pull/21051)

### Other Changes

Expand Down
6 changes: 5 additions & 1 deletion sdk/core/core-rest-pipeline/src/createPipelineFromOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,11 @@ export function createPipelineFromOptions(options: InternalPipelineOptions): Pip
pipeline.addPolicy(setClientRequestIdPolicy());
pipeline.addPolicy(defaultRetryPolicy(options.retryOptions), { phase: "Retry" });
pipeline.addPolicy(tracingPolicy(options.userAgentOptions), { afterPhase: "Retry" });
pipeline.addPolicy(redirectPolicy(options.redirectOptions), { afterPhase: "Retry" });
if (isNode) {
// Both XHR and Fetch expect to handle redirects automatically,
// so only include this policy when we're in Node.
pipeline.addPolicy(redirectPolicy(options.redirectOptions), { afterPhase: "Retry" });
}
pipeline.addPolicy(logPolicy(options.loggingOptions), { afterPhase: "Retry" });

return pipeline;
Expand Down
7 changes: 6 additions & 1 deletion sdk/core/core-rest-pipeline/src/fetchHttpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,18 @@ async function makeRequest(request: PipelineRequest): Promise<PipelineResponse>
const headers = buildFetchHeaders(request.headers);
const requestBody = buildRequestBody(request);

/**
* Developers of the future:
* Do not set redirect: "manual" as part
* of request options.
* It will not work as you expect.
*/
const response = await fetch(request.url, {
body: requestBody,
method: request.method,
headers: headers,
signal: abortController.signal,
credentials: request.withCredentials ? "include" : "same-origin",
redirect: "manual",
cache: "no-store",
});
return buildPipelineResponse(response, request);
Expand Down
1 change: 1 addition & 0 deletions sdk/core/core-rest-pipeline/src/policies/redirectPolicy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export interface RedirectPolicyOptions {
/**
* A policy to follow Location headers from the server in order
* to support server-side redirection.
* In the browser, this policy is not used.
* @param options - Options to control policy behavior.
*/
export function redirectPolicy(options: RedirectPolicyOptions = {}): PipelinePolicy {
Expand Down

0 comments on commit ff6f0ac

Please sign in to comment.