1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101

content / browser / interest_group / ad_auction_headers_util.h [blame]

// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CONTENT_BROWSER_INTEREST_GROUP_AD_AUCTION_HEADERS_UTIL_H_
#define CONTENT_BROWSER_INTEREST_GROUP_AD_AUCTION_HEADERS_UTIL_H_

#include <functional>

#include "base/memory/scoped_refptr.h"
#include "content/browser/renderer_host/render_frame_host_impl.h"
#include "content/common/content_export.h"
#include "content/public/browser/weak_document_ptr.h"
#include "net/http/http_response_headers.h"
#include "services/network/public/cpp/resource_request.h"
#include "url/origin.h"

namespace content {

// These values are persisted to logs. Entries should not be renumbered and
// numeric values should never be reused.
enum class AdAuctionHeadersIsEligibleOutcomeForMetrics {
  kNoInitiatorFrame = 0,
  kInFencedFrame = 1,
  kNotPrimaryPage = 2,
  // kNotOutermostMainFrame = 3, DEPRECATED
  kOpaqueRequestOrigin = 4,
  kNotPotentiallyTrustworthy = 5,
  kDisabledByPermissionsPolicy = 6,
  kApiNotAllowed = 7,
  kSuccess = 8,
  kMaxValue = kSuccess,
};

// The request header key that triggers interception of the auction result,
// signals, and additional bids from their associated response headers.
extern const char kAdAuctionRequestHeaderKey[];

// Response header keys associated with auction result, signals, and
// additional bids, respectively.
extern const char CONTENT_EXPORT kAdAuctionResultResponseHeaderKey[];
extern const char CONTENT_EXPORT kAdAuctionSignalsResponseHeaderKey[];
extern const char CONTENT_EXPORT kAdAuctionAdditionalBidResponseHeaderKey[];

// Returns whether or not this request is eligible for ad auction headers
// requested for fetch requests. The `initiator_rfh` should be the
// frame from which the fetch request is being issued.`initiator_rfh`
// is not modified by this function, and needs to be passed in non-const only
// because downstream functions use it for console logging.
CONTENT_EXPORT bool IsAdAuctionHeadersEligible(
    RenderFrameHostImpl& initiator_rfh,
    const network::ResourceRequest& resource_request);

// Returns whether or not this request is eligible for ad auction headers
// requested for iframe navigations. The `frame` argument provided should be
// that of the iframe. This uses the parent frame's permissions policy to
// provide greater consistency with fetch requests by treating the iframe
// navigation as a subresource request.
CONTENT_EXPORT bool IsAdAuctionHeadersEligibleForNavigation(
    const FrameTreeNode& frame,
    const url::Origin& navigation_request_origin);

// NOTE: Exposed only for fuzz testing. This is used by
// `ProcessAdAuctionResponseHeaders`, declared below.
//
// Splits and base64 decodes the `Ad-Auction-Result` response header,
// and returns the results. This function processes untrusted content, in an
// unsafe language, from an unsandboxed process, hence the fuzz test coverage.
CONTENT_EXPORT std::vector<std::string> ParseAdAuctionResultResponseHeader(
    const std::string& ad_auction_results);

// NOTE: Exposed only for fuzz testing. This is used by
// `ProcessAdAuctionResponseHeaders`, declared below.
//
// Splits and validates the `Ad-Auction-Additional-Bid` response header,
// and inserts the resulting additional bids into the provided map. This
// function processes untrusted content, in an unsafe language, from an
// unsandboxed process, hence the fuzz test coverage.
CONTENT_EXPORT void ParseAdAuctionAdditionalBidResponseHeader(
    const std::string& header_line,
    std::map<std::string, std::vector<std::string>>& nonce_additional_bids_map);

// Parses and sets the values of the `Ad-Auction-Result`, `Ad-Auction-Signals`,
// and `Ad-Auction-Additional-Bid` headers on the `AdAuctionPageData`
// associated with the `render_frame_host` for later use in the auction.
// Clears the `Ad-Auction-Signals` and `Ad-Auction-Additional-Bid` headers from
// `headers`.
CONTENT_EXPORT void ProcessAdAuctionResponseHeaders(
    const url::Origin& request_origin,
    Page& page,
    scoped_refptr<net::HttpResponseHeaders> headers);

// Removes the `Ad-Auction-Signals` and `Ad-Auction-Additional-Bid` response
// headers from `headers`. Called when the request encountered a redirect or
// error page.
CONTENT_EXPORT void RemoveAdAuctionResponseHeaders(
    scoped_refptr<net::HttpResponseHeaders> headers);

}  // namespace content

#endif  // CONTENT_BROWSER_INTEREST_GROUP_AD_AUCTION_HEADERS_UTIL_H_