As you can see from this Bugzilla thread (and also), Firefox does not always send an Origin header in POST requests. The RFC states that it should not be sent in certain undefined "privacy-sensitive" contexts. Mozilla defines those contexts here.
I'd like to know whether these are the only situations in which Firefox will not send the Origin header. As far as I can tell, it also will not send it in cross-origin POST requests (though Chrome and Internet Explorer will), but I can't confirm that in the documentation. Is it enumerated somewhere that I'm missing?
As far as what the specs require, the question above needs to be divided into a couple of answers:
null
I doubt that what Firefox requires around this (where it’s different from the spec) is enumerated. But as far as enumerating the spec requirements, here they are, in exhaustive detail, in two parts:
When browsers must send the Origin header
The answer to the question When must browsers must send the Origin header? is: The
Origin
header is sent only for any request which the Fetch spec defines as a CORS request:The actual statement in the Fetch spec that requires browsers to send the
Origin
header for all requests whose method is neitherGET
norHEAD
is this:So that requires browsers to send
Origin
for allPOST
requests, including same-originPOST
s (which by definition in Fetch are actually “CORS requests”—even though they’re same-origin).Note: The above describes how the Fetch spec currently defines the requirements, due to a change that was made to the spec on 2016-12-09. Up until then the requirements were different:
Origin
was sent for a same-origin POSTOrigin
was sent for cross-origin POST from a<form>
(without CORS)So I think the Firefox behavior described in the question conforms to what the spec previously required, but not what the spec currently requires.
The other cases when browsers must send the
Origin
header are any cases where a request is made with the “CORS flag” set—which, as far as HTTP(S) requests is except when the request mode isnavigate
,websocket
,same-origin
, orno-cors
.XHR always sets the mode to
cors
. But with the Fetch API, those request modes are the ones you can set with themode
field of the init-object argument to thefetch(…)
method:Along with that, for any element with a
crossorigin
attribute (aka “CORS setting attribute), the HTML spec requires browsers to set the request mode tocors
(and to send theOrigin
header).Otherwise, for any elements that initiate requests (scripts, stylesheets, images, media elements), the mode for the requests defaults to
no-cors
, which means noOrigin
header is sent for them.So that above are the details about the conditions under which browsers send the
Origin
header.The next part of the answer is about when the origin value will be set to
null
.When browsers must set origin to a value that’ll get serialized as
null
Separate from requirements for when browsers must send the
Origin
header are requirements for when browsers must set an origin tonull
, which are defined in the following specs:The HTML spec uses the term opaque origin and says this:
In other words everywhere the HTML spec says opaque origin, you can translate that to
null
.The HTML spec requires browsers to set an opaque origin or unique origin in the following cases:
img
elements)video
andaudio
elements)data:
URLiframe
with asandbox
attribute that doesn’t contain the valueallow-same-origin
createDocument()
, etc.The Fetch spec requires browsers to set the origin to a “globally unique identifier” (which basically means the same thing as “opaque origin” which basically means
null
…) in one case:The URL spec requires browsers to set an opaque origin in the following cases:
blob:
URLsfile:
URLshttp
,https
,ftp
,ws
,wss
, orgopher
.But it’s important to understand that just because the browser has internally set an opaque origin—essentially
null
—that doesn’t necessarily mean the browser will send anOrigin
header. So see the first part of this answer for details about when browsers must send theOrigin
header.