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

Bypassed request's referrerPolicy is reset to default #1380

Closed
4 tasks done
ne2030 opened this issue Aug 30, 2022 · 2 comments · Fixed by #1436
Closed
4 tasks done

Bypassed request's referrerPolicy is reset to default #1380

ne2030 opened this issue Aug 30, 2022 · 2 comments · Fixed by #1436
Labels
bug Something isn't working help wanted Extra attention is needed needs:triage Issues that have not been investigated yet. scope:browser Related to MSW running in a browser

Comments

@ne2030
Copy link

ne2030 commented Aug 30, 2022

Prerequisites

Environment check

  • I'm using the latest msw version
  • I'm using Node.js version 14 or higher

Browsers

Chromium (Chrome, Brave, etc.)

Reproduction repository

https://github.com/ne2030/msw-issue

Reproduction steps

  1. Start a local server at root directory. For example, if you have Python on your machine you could start a server running on port 8001 using python -m SimpleHTTPServer 8001 for Python 2.x, or python3 -m http.server 8001 for Python 3.x.
  2. Navigate to the local server, e.g. http://localhost:8001/
  3. Open the chrome dev tool - network tab and check whether the problem is reproduced.

Reproduction repository does not use MSW directly, instead, focuses on Fetch issue.

Current behavior

Referrer policy is reset to default 'strict-origin-when-cross-origin' when the request is fetched with cloned request and custom options.

const headers = Object.fromEntries(clonedRequest.headers.entries())
delete headers['x-msw-bypass']

return fetch(clonedRequest, { headers })

Here, if any options are passed as a second argument to fetch API, then the referrer policy is reset! I don't know the exact cause of this problem, but it can be solved below.

const headers = Object.fromEntries(clonedRequest.headers.entries())

delete headers['x-msw-bypass']

return fetch(clonedRequest, { headers, referrerPolicy: request.referrerPolicy, referrer: request.referrer })

Or, I saw msw condesandbox and there, this problem is not reproduced and found out mockserviceworker.js file is different from deployed version (0.45.0). I think the version on condsandbox that creates a new Request instance and uses this directly as argument with no options at Fetch can also handle this problem.

function serializeHeaders(headers) {
  const reqHeaders = {};
  headers.forEach((value, name) => {
    reqHeaders[name] = reqHeaders[name]
      ? [].concat(reqHeaders[name]).concat(value)
      : value;
  });
  return reqHeaders;
}

const cleanRequestHeaders = serializeHeaders(clonedRequest.headers);

// Remove the bypass header to comply with the CORS preflight check.

const originalRequest = new Request(clonedRequest, {
  headers: new Headers(cleanRequestHeaders)
});

fetch(originalRequest);

The below screenshots are for reference.

스크린샷 2022-08-30 오후 12 21 42

스크린샷 2022-08-30 오후 12 21 38

스크린샷 2022-08-30 오후 12 21 33

Expected behavior

Preserve original option of request (especially referrerPolicy and referrer)

@ne2030 ne2030 added bug Something isn't working needs:triage Issues that have not been investigated yet. scope:browser Related to MSW running in a browser labels Aug 30, 2022
@ne2030 ne2030 changed the title Bypassed request replace referrerPolicy to default Bypassed request's referrerPolicy is reset to default Aug 30, 2022
@kettanaito kettanaito added the help wanted Extra attention is needed label Aug 30, 2022
@ne2030
Copy link
Author

ne2030 commented Oct 18, 2022

Is there any updates on this?

@kettanaito
Copy link
Member

Released: v2.0.0 🎉

This has been released in v2.0.0!

Make sure to always update to the latest version (npm i msw@latest) to get the newest features and bug fixes.


Predictable release automation by @ossjs/release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed needs:triage Issues that have not been investigated yet. scope:browser Related to MSW running in a browser
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants