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

webRequest.onBeforeSendHeaders callback response requestHeaders not optional in 7.0.0 #20751

Closed
3 tasks done
binux opened this issue Oct 25, 2019 · 10 comments · Fixed by #29798
Closed
3 tasks done

webRequest.onBeforeSendHeaders callback response requestHeaders not optional in 7.0.0 #20751

binux opened this issue Oct 25, 2019 · 10 comments · Fixed by #29798

Comments

@binux
Copy link

binux commented Oct 25, 2019

Preflight Checklist

  • I have read the Contributing Guidelines for this project.
  • I agree to follow the Code of Conduct that this project adheres to.
  • I have searched the issue tracker for an issue that matches the one I want to file, without success.

Issue Details

  • Electron Version:
    • v7.0.0
  • Operating System:
    • macOS
  • Last Known Working Electron version:
    • v6.1.2

Expected Behavior

Additional headers sent without changes as documented
https://github.com/electron/electron/blob/v7.0.0/docs/api/web-request.md

https://github.com/electron/electron/blob/v7.0.0/docs/api/web-request.md

<head></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">{
  "headers": {
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3",
    "Accept-Encoding": "gzip, deflate, br",
    "Accept-Language": "en-GB",
    "Addition-Header": "hello world",
    "Host": "httpbin.org",
    "Sec-Fetch-Mode": "navigate",
    "Sec-Fetch-Site": "none",
    "Sec-Fetch-User": "?1",
    "Upgrade-Insecure-Requests": "1",
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.146 Electron/6.1.2 Safari/537.36"
  }
}
</pre></body>

Actual Behavior

Additional headers removed and not sent to server.

<head></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">{
  "headers": {
    "Accept-Encoding": "gzip, deflate, br",
    "Accept-Language": "en-GB",
    "Host": "httpbin.org",
    "Sec-Fetch-Mode": "navigate",
    "Sec-Fetch-Site": "none",
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3905.1 Electron/7.0.0 Safari/537.36"
  }
}
</pre></body>

To Reproduce

run following snippet:
https://gist.github.com/binux/3c2023793e1bf1ff03a8fcb24b026162

electron electron_bug.webRequest.onBeforeSendHeaders.js

Additional Information

Workaround will be change callback as callback({ requestHeaders: details.requestHeaders }).

@sandeep1995
Copy link

I am trying to connect to a local WebSocket server.
For connecting to the WebSocket server, I need to set the Origin header to be null

I was on electron 4.2.x, it was working fine then. I tried upgrading to electron 7.1.1, it's not working now.

I am using the following code

session.defaultSession.webRequest.onBeforeSendHeaders(
  {
    urls: [
      '*://*/*',
    ],
  },
  (details, callback) => {
    let reqHeaders = Object.assign({}, details.requestHeaders, {
      Origin: null,
    });
    callback({
      cancel: false,
      requestHeaders: reqHeaders,
    });
  }
);

@vladimiry
Copy link

@sandeep1995 I'd suggest trying 7.1.2+, see #20923 for details.

@jalanga
Copy link

jalanga commented Nov 28, 2019

@vladimiry on 7.1.2, onBeforeSendHeaders is not called, at leas I can't find an output in console after a console.log. The header CCC is not sent, neither the origin. Any suggestions please?

  session.defaultSession.webRequest.onBeforeSendHeaders(filter, (details, callback) => {
    details.requestHeaders.Origin = constants.originHeader;
    details.requestHeaders.CCC = '1';
    callback({
      cancel: false,
      requestHeaders: details.requestHeaders
    });
  });

@sandeep1995
Copy link

I am still not able to connect to a local WebSocket server from the renderer as I could not modify the Origin

I tried 7.1.2 and even electron 8.0.0-beta.3. Still no luck.

@vladimiry
Copy link

@sandeep1995
Copy link

sandeep1995 commented Nov 28, 2019

@vladimiry Maybe that is because you are trying to modify for an XHR request.
I am trying for a WebSocket (wss://) request.

According to my investigation, it seems like it's a bug on the Chromium side.
https://bugs.chromium.org/p/chromium/issues/detail?id=1011108

@vladimiry
Copy link

Got it, so I overlooked the websockets use.

@danielweck
Copy link

I filed a similar issue for Electron v8 and WebRequest.onBeforeSendHeaders (WebRequest.onHeadersReceived seems to "do the right thing" as per the documentation and previous Electron versions' behaviour).
I provided a Gist to reproduce the issue in the console, and compare onBeforeSendHeaders vs. onHeadersReceived:
#23988

@danielweck
Copy link

danielweck commented Jun 5, 2020

According to my tests with Electron Fiddle, version 6.1.2 is not affected, but versions 10.0.0-beta.2, 9.0.2, 8.3.1 and 7.3.1 are affected, so this seems like a regression from version 6 to 7+.
See: #23988

@binux
Copy link
Author

binux commented Sep 6, 2020

NOTE: the workaround callback({ requestHeaders: details.requestHeaders }) will cause the request header reordered when sent to target website, which might be used as a fingerprint to detect electron.

There is an other workaround callback(undefined) wouldn't have this issue if you just want to peek on the headers sent. (you have to specifically send undefined here, otherwise will get a TypeError: Insufficient number of arguments. error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
No open projects
7.2.x
Unsorted Issues
Development

Successfully merging a pull request may close this issue.

7 participants