1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_dom_ReferrerInfo_h
8 #define mozilla_dom_ReferrerInfo_h
11 #include "nsIReferrerInfo.h"
12 #include "nsReadableUtils.h"
13 #include "mozilla/Maybe.h"
14 #include "mozilla/HashFunctions.h"
15 #include "mozilla/dom/ReferrerPolicyBinding.h"
17 #define REFERRERINFO_CONTRACTID "@mozilla.org/referrer-info;1"
18 // 041a129f-10ce-4bda-a60d-e027a26d5ed0
19 #define REFERRERINFO_CID \
21 0x041a129f, 0x10ce, 0x4bda, { \
22 0xa6, 0x0d, 0xe0, 0x27, 0xa2, 0x6d, 0x5e, 0xd0 \
35 class URLAndReferrerInfo
;
38 class HttpBaseChannel
;
41 } // namespace mozilla
43 namespace mozilla::dom
{
46 * The ReferrerInfo class holds the raw referrer and potentially a referrer
47 * policy which allows to query the computed referrer which should be applied to
48 * a channel as the actual referrer value.
50 * The ReferrerInfo class solely contains readonly fields and represents a 1:1
51 * sync to the referrer header of the corresponding channel. In turn that means
52 * the class is immutable - so any modifications require to clone the current
55 * For example if a request undergoes a redirect, the new channel
56 * will need a new ReferrerInfo clone with members being updated accordingly.
59 class ReferrerInfo
: public nsIReferrerInfo
{
61 typedef enum ReferrerPolicy ReferrerPolicyEnum
;
64 explicit ReferrerInfo(
65 nsIURI
* aOriginalReferrer
,
66 ReferrerPolicyEnum aPolicy
= ReferrerPolicy::_empty
,
67 bool aSendReferrer
= true,
68 const Maybe
<nsCString
>& aComputedReferrer
= Maybe
<nsCString
>());
70 // Creates already initialized ReferrerInfo from an element or a document.
71 explicit ReferrerInfo(const Element
&);
72 explicit ReferrerInfo(const Document
&, const bool = true);
74 // Creates already initialized ReferrerInfo from an element or a document with
75 // a specific referrer policy.
76 ReferrerInfo(const Element
&, ReferrerPolicyEnum
);
78 // create an exact copy of the ReferrerInfo
79 already_AddRefed
<ReferrerInfo
> Clone() const;
81 // create an copy of the ReferrerInfo with new referrer policy
82 already_AddRefed
<ReferrerInfo
> CloneWithNewPolicy(
83 ReferrerPolicyEnum aPolicy
) const;
85 // create an copy of the ReferrerInfo with new original referrer
86 already_AddRefed
<ReferrerInfo
> CloneWithNewOriginalReferrer(
87 nsIURI
* aOriginalReferrer
) const;
89 // Record the telemetry for the referrer policy.
90 void RecordTelemetry(nsIHttpChannel
* aChannel
);
93 * Helper function to create a new ReferrerInfo object from a given document
94 * and override referrer policy if needed (for example, when parsing link
95 * header or speculative loading).
97 * @param aDocument the document to init referrerInfo object.
98 * @param aPolicyOverride referrer policy to override if necessary.
100 static already_AddRefed
<nsIReferrerInfo
> CreateFromDocumentAndPolicyOverride(
101 Document
* aDoc
, ReferrerPolicyEnum aPolicyOverride
);
104 * Implements step 3.1 and 3.3 of the Determine request's Referrer algorithm
105 * from the Referrer Policy specification.
107 * https://w3c.github.io/webappsec/specs/referrer-policy/#determine-requests-referrer
109 static already_AddRefed
<nsIReferrerInfo
> CreateForFetch(
110 nsIPrincipal
* aPrincipal
, Document
* aDoc
);
113 * Helper function to create new ReferrerInfo object from a given external
114 * stylesheet. The returned nsIReferrerInfo object will be used for any
115 * requests or resources referenced by the sheet.
117 * @param aSheet the stylesheet to init referrerInfo.
118 * @param aPolicy referrer policy from header if there's any.
120 static already_AddRefed
<nsIReferrerInfo
> CreateForExternalCSSResources(
121 StyleSheet
* aExternalSheet
,
122 ReferrerPolicyEnum aPolicy
= ReferrerPolicy::_empty
);
125 * Helper function to create new ReferrerInfo object from a given document.
126 * The returned nsIReferrerInfo object will be used for any requests or
127 * resources referenced by internal stylesheet (for example style="" or
128 * wrapped by <style> tag), as well as SVG resources.
130 * @param aDocument the document to init referrerInfo object.
132 static already_AddRefed
<nsIReferrerInfo
> CreateForInternalCSSAndSVGResources(
133 Document
* aDocument
);
136 * Check whether the given referrer's scheme is allowed to be computed and
137 * sent. The allowlist schemes are: http, https.
139 static bool IsReferrerSchemeAllowed(nsIURI
* aReferrer
);
142 * The Referrer Policy should be inherited for nested browsing contexts that
143 * are not created from responses. Such as: srcdoc, data, blob.
145 static bool ShouldResponseInheritReferrerInfo(nsIChannel
* aChannel
);
148 * Check whether referrer is allowed to send in secure to insecure scenario.
150 static nsresult
HandleSecureToInsecureReferral(nsIURI
* aOriginalURI
,
152 ReferrerPolicyEnum aPolicy
,
156 * Returns true if the given channel is cross-origin request
158 * Computing whether the request is cross-origin may be expensive, so please
159 * do that in cases where we're going to use this information later on.
161 static bool IsCrossOriginRequest(nsIHttpChannel
* aChannel
);
164 * Returns true if aReferrer's origin and aChannel's URI are cross-origin.
166 static bool IsReferrerCrossOrigin(nsIHttpChannel
* aChannel
,
170 * Returns true if the given channel is cross-site request.
172 static bool IsCrossSiteRequest(nsIHttpChannel
* aChannel
);
175 * Returns true if the given channel is suppressed by Referrer-Policy header
176 * and should set "null" to Origin header.
178 static bool ShouldSetNullOriginHeader(net::HttpBaseChannel
* aChannel
,
182 * Getter for network.http.sendRefererHeader.
184 static uint32_t GetUserReferrerSendingPolicy();
187 * Getter for network.http.referer.XOriginPolicy.
189 static uint32_t GetUserXOriginSendingPolicy();
192 * Getter for network.http.referer.trimmingPolicy.
194 static uint32_t GetUserTrimmingPolicy();
197 * Getter for network.http.referer.XOriginTrimmingPolicy.
199 static uint32_t GetUserXOriginTrimmingPolicy();
202 * Return default referrer policy which is controlled by user
204 * network.http.referer.defaultPolicy for regular mode
205 * network.http.referer.defaultPolicy.trackers for third-party trackers
207 * network.http.referer.defaultPolicy.pbmode for private mode
208 * network.http.referer.defaultPolicy.trackers.pbmode for third-party trackers
211 static ReferrerPolicyEnum
GetDefaultReferrerPolicy(
212 nsIHttpChannel
* aChannel
= nullptr, nsIURI
* aURI
= nullptr,
213 bool aPrivateBrowsing
= false);
216 * Return default referrer policy for third party which is controlled by user
218 * network.http.referer.defaultPolicy.trackers for regular mode
219 * network.http.referer.defaultPolicy.trackers.pbmode for private mode
221 static ReferrerPolicyEnum
GetDefaultThirdPartyReferrerPolicy(
222 bool aPrivateBrowsing
= false);
225 * Helper function to parse ReferrerPolicy from meta tag referrer content.
226 * For example: <meta name="referrer" content="origin">
228 * @param aContent content string to be transformed into ReferrerPolicyEnum,
231 static ReferrerPolicyEnum
ReferrerPolicyFromMetaString(
232 const nsAString
& aContent
);
235 * Helper function to parse ReferrerPolicy from string content of
236 * referrerpolicy attribute.
237 * For example: <a href="http://example.com" referrerpolicy="no-referrer">
239 * @param aContent content string to be transformed into ReferrerPolicyEnum,
240 * e.g. "no-referrer".
242 static ReferrerPolicyEnum
ReferrerPolicyAttributeFromString(
243 const nsAString
& aContent
);
246 * Helper function to parse ReferrerPolicy from string content of
247 * Referrer-Policy header.
248 * For example: Referrer-Policy: origin no-referrer
249 * https://www.w3.org/tr/referrer-policy/#parse-referrer-policy-from-header
251 * @param aContent content string to be transformed into ReferrerPolicyEnum.
252 * e.g. "origin no-referrer"
254 static ReferrerPolicyEnum
ReferrerPolicyFromHeaderString(
255 const nsAString
& aContent
);
258 * Hash function for this object
260 HashNumber
Hash() const;
262 NS_DECL_THREADSAFE_ISUPPORTS
263 NS_DECL_NSIREFERRERINFO
264 NS_DECL_NSISERIALIZABLE
267 virtual ~ReferrerInfo() = default;
269 ReferrerInfo(const ReferrerInfo
& rhs
);
272 * Trimming policy when compute referrer, indicate how much information in the
273 * referrer will be sent. Order matters here.
275 enum TrimmingPolicy
: uint32_t {
277 ePolicySchemeHostPortPath
= 1,
278 ePolicySchemeHostPort
= 2,
282 * Referrer sending policy, indicates type of action could trigger to send
283 * referrer header, not send at all, send only with user's action (click on a
284 * link) or send even with inline content request (image request).
285 * Order matters here.
287 enum ReferrerSendingPolicy
: uint32_t {
289 ePolicySendWhenUserTrigger
= 1,
290 ePolicySendInlineContent
= 2,
294 * Sending referrer when cross origin policy, indicates when referrer should
295 * be send when compare 2 origins. Order matters here.
297 enum XOriginSendingPolicy
: uint32_t {
298 ePolicyAlwaysSend
= 0,
299 ePolicySendWhenSameDomain
= 1,
300 ePolicySendWhenSameHost
= 2,
304 * Handle user controlled pref network.http.referer.XOriginPolicy
306 nsresult
HandleUserXOriginSendingPolicy(nsIURI
* aURI
, nsIURI
* aReferrer
,
307 bool& aAllowed
) const;
310 * Handle user controlled pref network.http.sendRefererHeader
312 nsresult
HandleUserReferrerSendingPolicy(nsIHttpChannel
* aChannel
,
313 bool& aAllowed
) const;
316 * Compute trimming policy from user controlled prefs.
317 * This function is called when we already made sure a nonempty referrer is
320 TrimmingPolicy
ComputeTrimmingPolicy(nsIHttpChannel
* aChannel
,
321 nsIURI
* aReferrer
) const;
323 // HttpBaseChannel could access IsInitialized() and ComputeReferrer();
324 friend class mozilla::net::HttpBaseChannel
;
327 * Compute referrer for a given channel. The computation result then will be
328 * stored in this class and then used to set the actual referrer header of
329 * the channel. The computation could be controlled by several user prefs
330 * which are defined in StaticPrefList.yaml (see StaticPrefList.yaml for more
332 * network.http.sendRefererHeader
333 * network.http.referer.spoofSource
334 * network.http.referer.hideOnionSource
335 * network.http.referer.XOriginPolicy
336 * network.http.referer.trimmingPolicy
337 * network.http.referer.XOriginTrimmingPolicy
339 nsresult
ComputeReferrer(nsIHttpChannel
* aChannel
);
342 * Check whether the ReferrerInfo has been initialized or not.
344 bool IsInitialized() { return mInitialized
; }
346 // nsHttpChannel, Document could access IsPolicyOverrided();
347 friend class mozilla::net::nsHttpChannel
;
348 friend class mozilla::dom::Document
;
350 * Check whether if unset referrer policy is overrided by default or not
352 bool IsPolicyOverrided() { return mOverridePolicyByDefault
; }
355 * Get origin string from a given valid referrer URI (http, https)
357 * @aReferrer - the full referrer URI
358 * @aResult - the resulting aReferrer in string format.
360 nsresult
GetOriginFromReferrerURI(nsIURI
* aReferrer
,
361 nsACString
& aResult
) const;
364 * Trim a given referrer with a given a trimming policy,
366 nsresult
TrimReferrerWithPolicy(nsIURI
* aReferrer
,
367 TrimmingPolicy aTrimmingPolicy
,
368 nsACString
& aResult
) const;
371 * Returns true if we should ignore less restricted referrer policies,
372 * including 'unsafe_url', 'no_referrer_when_downgrade' and
373 * 'origin_when_cross_origin', for the given channel. We only apply this
374 * restriction for cross-site requests. For the same-site request, we will
375 * still allow overriding the default referrer policy with less restricted
378 * Note that the channel triggered by the system and the extension will be
379 * exempt from this restriction.
381 bool ShouldIgnoreLessRestrictedPolicies(
382 nsIHttpChannel
* aChannel
, const ReferrerPolicyEnum aPolicy
) const;
385 * Limit referrer length using the following ruleset:
386 * - If the length of referrer URL is over max length, strip down to origin.
387 * - If the origin is still over max length, remove the referrer entirely.
389 * This function comlements TrimReferrerPolicy and needs to be called right
390 * after TrimReferrerPolicy.
392 * @aChannel - used to query information needed for logging to the console.
393 * @aReferrer - the full referrer URI; needs to be identical to aReferrer
394 * passed to TrimReferrerPolicy.
395 * @aTrimmingPolicy - represents the trimming policy which was applied to the
396 * referrer; needs to be identical to aTrimmingPolicy
397 * passed to TrimReferrerPolicy.
398 * @aInAndOutTrimmedReferrer - an in and outgoing argument representing the
399 * referrer value. Please pass the result of
400 * TrimReferrerWithPolicy as
401 * aInAndOutTrimmedReferrer which will then be
402 * reduced to the origin or completely truncated
403 * in case the referrer value exceeds the length
406 nsresult
LimitReferrerLength(nsIHttpChannel
* aChannel
, nsIURI
* aReferrer
,
407 TrimmingPolicy aTrimmingPolicy
,
408 nsACString
& aInAndOutTrimmedReferrer
) const;
411 * The helper function to read the old data format before gecko 100 for
414 nsresult
ReadTailDataBeforeGecko100(const uint32_t& aData
,
415 nsIObjectInputStream
* aInputStream
);
418 * Write message to the error console
420 void LogMessageToConsole(nsIHttpChannel
* aChannel
, const char* aMsg
,
421 const nsTArray
<nsString
>& aParams
) const;
423 friend class mozilla::URLAndReferrerInfo
;
425 nsCOMPtr
<nsIURI
> mOriginalReferrer
;
427 ReferrerPolicyEnum mPolicy
;
429 // The referrer policy that has been set originally for the channel. Note that
430 // the policy may have been overridden by the default referrer policy, so we
431 // need to keep track of this if we need to recover the original referrer
433 ReferrerPolicyEnum mOriginalPolicy
;
435 // Indicates if the referrer should be sent or not even when it's available
436 // (default is true).
439 // Since the ReferrerInfo is immutable, we use this member as a helper to
440 // ensure no one can call e.g. init() twice to modify state of the
444 // Indicates if unset referrer policy is overrided by default
445 bool mOverridePolicyByDefault
;
447 // Store a computed referrer for a given channel
448 Maybe
<nsCString
> mComputedReferrer
;
451 // Indicates if the telemetry has been recorded. This is used to make sure the
452 // telemetry will be only recored once.
453 bool mTelemetryRecorded
= false;
457 } // namespace mozilla::dom
459 #endif // mozilla_dom_ReferrerInfo_h