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

All POST fetch requests in browser window have Origin: 'null' in headers #7931

Closed
karelbilek opened this issue Nov 10, 2016 · 12 comments
Closed

Comments

@karelbilek
Copy link

karelbilek commented Nov 10, 2016

  • Electron version: 1.4.5
  • Operating system: Ubuntu + OS X

Expected behavior

I use fetch API with method: 'POST' in the browser window and it has something sensible in 'origin' header.

Actual behavior

I use fetch API with method: 'POST' in the browser window and it has the string 'null' in 'origin' header.

How to reproduce

I cloned electron-quick-start for a simple repro here (diff)

I do a simple request to https://www.example.org 1 second after start (so that the dev tools see it).

When you click on the example.org request and scroll down, you can see the 'origin: null'

hello

I also added logging of headers in the main thread, so you can see that the 'null' is indeed a string (and not "javascript null").

{ Accept: '*/*',
  'Accept-Encoding': 'gzip, deflate',
  'Accept-Language': 'en-US',
  Origin: 'null',
  'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) electron-quick-start/1.0.0 Chrome/53.0.2785.113 Electron/1.4.5 Safari/537.36',
  'X-DevTools-Emulate-Network-Conditions-Client-Id': '5934e521-44cd-4ca2-bccc-023c044c93a8' }

Removing {method: 'POST'} causes the Origin: 'null' to go away

Notes

This might actually be a good behavior; however, I want to know why exactly that happens. What is the spec, what is the reasoning behind it. Is it a bug or a feature?

I found some references to string 'null' here

Particularly in the second link

The ascii-serialization of an origin is the value returned by the following algorithm:

  1. If the origin is not a scheme/host/port triple, then return the string null

(i.e., the code point sequence U+006E, U+0075, U+006C, U+006C) and abort these steps.

@MarshallOfSound
Copy link
Member

I would also like to know why it is inconsistent with a GET request. If you send a GET request with the same code it will simply not have an origin header at all (somewhat expected it's a file:// uri). It's the inconsistency that throws me more than anything else 😕

@deepak1556
Copy link
Member

I would also like to know why it is inconsistent with a GET request

This issue was raised sometime back, you can find the reason why no origin header is sent for cors GET request here from file scheme.

What is the spec, what is the reasoning behind it. Is it a bug or a feature?

The fetch spec allows origin header when sending POST request, which you can find here .

Closing this out, since its the expected behavior in Electron. You can use custom protocols for this scenario.

@karelbilek
Copy link
Author

I am sorry, I still don't understand this.

  • Why is GET and POST treated differently? I cannot find it in the thread.
  • Where does the null string come from? I don't know how to interpret it. Why is it null specifically and not some "actual" origin?

Thanks!

@karelbilek
Copy link
Author

(I am hoping to understand this so I know why Electron behaves the way it behaves, and also to help other people who will maybe find this issue by google :))

karelbilek added a commit to trezor/trezor-common that referenced this issue Nov 10, 2016
Electron discussion there

electron/electron#7931 

It seems `Origin: 'null'` from electron apps is a feature, not a bug
karelbilek added a commit to trezor/trezor-common that referenced this issue Nov 10, 2016
Electron discussion there

electron/electron#7931 

It seems `Origin: 'null'` from electron apps is a feature, not a bug
@deepak1556
Copy link
Member

Why is GET and POST treated differently? I cannot find it in the thread.

For both the requests origin will not be set initially, as they are treated as same origin requests because of the reason specified in the above linked thread. But just before the request is sent blink sets the origin for non-get and non-head requests here. Hence you see the different behavior.

Where does the null string come from? I don't know how to interpret it. Why is it null specifically and not some "actual" origin?

The value is set by blink, the reason for null is related to the securityorigin value.

@karelbilek
Copy link
Author

Thanks! That's what I needed to know.

@intellix
Copy link

I've got around this by doing:

electron.session.defaultSession.webRequest.onBeforeSendHeaders((details, callback) => {
  details.requestHeaders['Origin'] = 'electron://graphiql-app';
  callback({ cancel: false, requestHeaders: details.requestHeaders });
});

@andreiglingeanu
Copy link

Managed to get around this (in the context of a web extension though) by conditionally removing Origin header in onBeforeSendHeaders handler.

@SmadusankaB
Copy link

Run your app on a (local) server (like wamp, xampp, mamp)

@ghost
Copy link

ghost commented Aug 7, 2019

@deepak1556

But just before the request is sent blink sets the origin for non-get and non-head requests here.

why is the origin header not set for head request ?
any workaround for this ??

@davidsth
Copy link

This still isn't fixed yet. We had to follow instructions from @intellix 's instructions.

@kimseonguk197
Copy link

kimseonguk197 commented Jan 12, 2023

I am sorry, I still don't understand this.

  • Why is GET and POST treated differently? I cannot find it in the thread.
  • Where does the null string come from? I don't know how to interpret it. Why is it null specifically and not some "actual" origin?

Thanks!

this issue is long time ago issue, but I recently found some resources heer. in 414 line you can find that chrome send origin header when it is blank without 'get' or 'head' method

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants