Line data Source code
1 : /**
2 : * Copyright Soramitsu Co., Ltd. All Rights Reserved.
3 : * SPDX-License-Identifier: Apache-2.0
4 : */
5 :
6 : #ifndef IROHA_SHARED_MODEL_FIELD_VALIDATOR_HPP
7 : #define IROHA_SHARED_MODEL_FIELD_VALIDATOR_HPP
8 :
9 : #include <regex>
10 :
11 : #include "datetime/time.hpp"
12 : #include "interfaces/base/signable.hpp"
13 : #include "interfaces/permissions.hpp"
14 : #include "interfaces/queries/query_payload_meta.hpp"
15 : #include "validators/answer.hpp"
16 :
17 : namespace shared_model {
18 :
19 : namespace interface {
20 : class Amount;
21 : class BatchMeta;
22 : class Peer;
23 : } // namespace interface
24 :
25 : namespace validation {
26 :
27 : /**
28 : * Class that validates fields of commands, concrete queries, transaction,
29 : * and query
30 : */
31 : class FieldValidator {
32 : private:
33 : using TimeFunction = std::function<iroha::ts64_t()>;
34 :
35 : public:
36 : FieldValidator(time_t future_gap = kDefaultFutureGap,
37 : TimeFunction time_provider = [] {
38 2207 : return iroha::time::now();
39 : });
40 :
41 : void validateAccountId(
42 : ReasonsGroupType &reason,
43 : const interface::types::AccountIdType &account_id) const;
44 :
45 : void validateAssetId(ReasonsGroupType &reason,
46 : const interface::types::AssetIdType &asset_id) const;
47 :
48 : void validatePeer(ReasonsGroupType &reason,
49 : const interface::Peer &peer) const;
50 :
51 : void validateAmount(ReasonsGroupType &reason,
52 : const interface::Amount &amount) const;
53 :
54 : void validatePubkey(ReasonsGroupType &reason,
55 : const interface::types::PubkeyType &pubkey) const;
56 :
57 : void validatePeerAddress(
58 : ReasonsGroupType &reason,
59 : const interface::types::AddressType &address) const;
60 :
61 : void validateRoleId(ReasonsGroupType &reason,
62 : const interface::types::RoleIdType &role_id) const;
63 :
64 : void validateAccountName(
65 : ReasonsGroupType &reason,
66 : const interface::types::AccountNameType &account_name) const;
67 :
68 : // clang-format off
69 : /**
70 : * Check if the given string `domain_id` is in valid domain syntax defined in
71 : * the RFC 1035 and 1123. Return the result of the validation.
72 : *
73 : * The domain syntax in RFC 1035 is given below:
74 : *
75 : * <domain> ::= <subdomain> | ” ”
76 : * <subdomain> ::= <label> | <subdomain> “.” <label>
77 : * <label> ::= <letter> [ [ <ldh-str> ] <let-dig> ]
78 : * <ldh-str> ::= <let-dig-hyp> | <let-dig-hyp> <ldh-str>
79 : * <let-dig-hyp> ::= <let-dig> | “-”
80 : * <let-dig> ::= <letter> | <digit>
81 : * <letter> ::= any one of the 52 alphabetic characters A through Z in
82 : * upper case and a through z in lower case
83 : * <digit> ::= any one of the ten digits 0 through 9
84 : *
85 : * And the subsequent RFC 1123 disallows the root white space.
86 : *
87 : * If the validation is not successful reason is updated with corresponding message
88 : */
89 : // clang-format on
90 : void validateDomainId(
91 : ReasonsGroupType &reason,
92 : const interface::types::DomainIdType &domain_id) const;
93 :
94 : void validateAssetName(
95 : ReasonsGroupType &reason,
96 : const interface::types::AssetNameType &asset_name) const;
97 :
98 : void validateAccountDetailKey(
99 : ReasonsGroupType &reason,
100 : const interface::types::AccountDetailKeyType &key) const;
101 :
102 : void validateAccountDetailValue(
103 : ReasonsGroupType &reason,
104 : const interface::types::AccountDetailValueType &value) const;
105 :
106 : void validatePrecision(
107 : ReasonsGroupType &reason,
108 : const interface::types::PrecisionType &precision) const;
109 :
110 : void validateRolePermission(
111 : ReasonsGroupType &reason,
112 : const interface::permissions::Role &permission) const;
113 :
114 : void validateGrantablePermission(
115 : ReasonsGroupType &reason,
116 : const interface::permissions::Grantable &permission) const;
117 :
118 : void validateQuorum(ReasonsGroupType &reason,
119 : const interface::types::QuorumType &quorum) const;
120 :
121 : void validateCreatorAccountId(
122 : ReasonsGroupType &reason,
123 : const interface::types::AccountIdType &account_id) const;
124 :
125 : /**
126 : * Validate timestamp against now
127 : */
128 : void validateCreatedTime(ReasonsGroupType &reason,
129 : interface::types::TimestampType timestamp,
130 : interface::types::TimestampType now) const;
131 :
132 : /**
133 : * Validate timestamp against time_provider_
134 : */
135 : void validateCreatedTime(ReasonsGroupType &reason,
136 : interface::types::TimestampType timestamp) const;
137 :
138 : void validateCounter(ReasonsGroupType &reason,
139 : const interface::types::CounterType &counter) const;
140 :
141 : void validateSignatures(
142 : ReasonsGroupType &reason,
143 : const interface::types::SignatureRangeType &signatures,
144 : const crypto::Blob &source) const;
145 :
146 : void validateQueryPayloadMeta(
147 : ReasonsGroupType &reason,
148 : const interface::QueryPayloadMeta &meta) const;
149 :
150 : void validateDescription(
151 : ReasonsGroupType &reason,
152 : const interface::types::DescriptionType &description) const;
153 :
154 : void validateBatchMeta(ReasonsGroupType &reason,
155 : const interface::BatchMeta &description) const;
156 :
157 : void validateHeight(ReasonsGroupType &reason,
158 : const interface::types::HeightType &height) const;
159 :
160 : void validateHash(ReasonsGroupType &reason,
161 : const crypto::Hash &hash) const;
162 :
163 : private:
164 : const static std::string account_name_pattern_;
165 : const static std::string asset_name_pattern_;
166 : const static std::string domain_pattern_;
167 : const static std::string ip_v4_pattern_;
168 : const static std::string peer_address_pattern_;
169 : const static std::string account_id_pattern_;
170 : const static std::string asset_id_pattern_;
171 : const static std::string detail_key_pattern_;
172 : const static std::string role_id_pattern_;
173 :
174 : const static std::regex account_name_regex_;
175 : const static std::regex asset_name_regex_;
176 : const static std::regex domain_regex_;
177 : const static std::regex ip_v4_regex_;
178 : const static std::regex peer_address_regex_;
179 : const static std::regex account_id_regex_;
180 : const static std::regex asset_id_regex_;
181 : const static std::regex detail_key_regex_;
182 : const static std::regex role_id_regex_;
183 :
184 : // gap for future transactions
185 : time_t future_gap_;
186 : // time provider callback
187 : TimeFunction time_provider_;
188 :
189 : public:
190 : // max-delay between tx creation and validation
191 : static constexpr auto kMaxDelay =
192 : std::chrono::hours(24) / std::chrono::milliseconds(1);
193 : // default value for future_gap field of FieldValidator
194 : static constexpr auto kDefaultFutureGap =
195 : std::chrono::minutes(5) / std::chrono::milliseconds(1);
196 :
197 : // size of key
198 : static const size_t public_key_size;
199 : static const size_t signature_size;
200 : static const size_t hash_size;
201 : static const size_t value_size;
202 : static const size_t description_size;
203 : };
204 :
205 : boost::optional<ConcreteReasonType> validatePubkey(
206 : const interface::types::PubkeyType &pubkey);
207 :
208 : } // namespace validation
209 : } // namespace shared_model
210 :
211 : #endif // IROHA_SHARED_MODEL_FIELD_VALIDATOR_HPP
|