File: bad_message.h

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (93 lines) | stat: -rw-r--r-- 3,129 bytes parent folder | download | duplicates (6)
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
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_AUTOFILL_CONTENT_BROWSER_BAD_MESSAGE_H_
#define COMPONENTS_AUTOFILL_CONTENT_BROWSER_BAD_MESSAGE_H_

#include "components/autofill/core/common/aliases.h"
#include "components/autofill/core/common/unique_ids.h"

namespace content {
class RenderFrameHost;
}  // namespace content

namespace autofill {

class FormData;

namespace bad_message {

namespace internal {

// Returns `true` if the predicate `pred` is `true` for all arguments that it
// can be applied to.
template <typename Pred, typename... Args>
bool ValidateArguments(Pred&& pred, const Args&... args) {
  auto recursion_helper = [&pred](const auto& arg) {
    if constexpr (requires { std::invoke(pred, arg); }) {
      return std::invoke(pred, arg);
    }
    return true;
  };
  return (recursion_helper(args) && ...);
}

// Returns true if `trigger_source` is a trigger source that may be used in
// renderer -> browser communication. Kills the renderer and returns false
// otherwise.
bool CheckSingleValidTriggerSource(
    AutofillSuggestionTriggerSource trigger_source);

template <typename... Args>
bool CheckValidTriggerSource(const Args&... args) {
  return ValidateArguments(&CheckSingleValidTriggerSource, args...);
}

// Returns true if `form.fields` contains a field identified by `field_id`.
// Kills the renderer otherwise.
bool CheckFieldInForm(const FormData& form, FieldRendererId field_id);

// The default case for the overload below where there is no FormData parameter.
template <typename... Args>
  requires(!std::same_as<std::remove_cvref_t<Args>, FormData> && ...)
bool CheckFieldInForm(const Args&... args) {
  return true;
}

// Returns true if all `FieldRendererId`s among `args...` are elements of
// `form`. Kills the renderer otherwise.
//
// The intended use is to call `CheckFieldInForm(args...)` in Mojo receiver
// implementations, where `args...` are the arguments of the Mojo message.
template <typename... Args>
bool CheckFieldInForm(const FormData& form, const Args&... args) {
  return ValidateArguments(
      [&form](FieldRendererId field_id) {
        return CheckFieldInForm(form, field_id);
      },
      args...);
}

}  // namespace internal

// Returns true if `frame` is not prerendering (when autofill updates are
// disallowed). Kills the renderer if we are prerendering.
bool CheckFrameNotPrerendering(content::RenderFrameHost* frame);

// Returns true if the following checks pass:
// - The trigger source is allowed to be sent by the renderer.
// - If the first argument is a `FormData` and it is succeeded by
// `FieldRendererId` arguments, then they must all correspond to entries in
// `FormData::fields()`.
// Returns false and kills the renderer otherwise.
template <typename... Args>
bool CheckArgs(const Args&... args) {
  return internal::CheckValidTriggerSource(args...) &&
         internal::CheckFieldInForm(args...);
}

}  // namespace bad_message
}  // namespace autofill

#endif  // COMPONENTS_AUTOFILL_CONTENT_BROWSER_BAD_MESSAGE_H_