forked from Pissandshittium/pissandshittium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathipp_validator.cc
377 lines (327 loc) · 11.8 KB
/
ipp_validator.cc
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/services/cups_proxy/ipp_validator.h"
#include <cups/cups.h>
#include <algorithm>
#include <map>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "base/containers/cxx20_erase.h"
#include "base/containers/span.h"
#include "base/strings/strcat.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_piece.h"
#include "base/strings/string_util.h"
#include "chrome/services/cups_proxy/ipp_attribute_validator.h"
#include "chrome/services/cups_proxy/public/cpp/cups_util.h"
#include "net/http/http_util.h"
#include "printing/backend/cups_ipp_helper.h"
namespace cups_proxy {
namespace {
using ValueType = ipp_parser::mojom::IppAttributeValue::Tag;
// Initial version only supports english lcoales.
// TODO(crbug.com/945409): Extending to supporting arbitrary locales.
const char kLocaleEnglish[] = "en";
// Converting to vector<char> for libCUPS API:
// ippAddBooleans(..., int num_values, const char *values)
std::vector<char> ConvertBooleans(const std::vector<bool>& bools) {
std::vector<char> ret;
for (bool value : bools) {
ret.push_back(value ? 1 : 0);
}
return ret;
}
// Converting to vector<const char*> for libCUPS API:
// ippAddStrings(..., int num_values, const char *const *values)
// Note: The values in the returned vector refer to |strings|; so |strings|
// must outlive them.
std::vector<const char*> ConvertStrings(
const std::vector<std::string>& strings) {
std::vector<const char*> ret;
for (auto& value : strings) {
ret.push_back(value.c_str());
}
return ret;
}
std::array<std::vector<int>, 2> ConvertResolutions(
const std::vector<ipp_parser::mojom::ResolutionPtr>& resolutions) {
std::array<std::vector<int>, 2> ret;
for (auto& res : resolutions) {
if (res->xres <= 0 || res->yres <= 0)
continue;
ret[0].push_back(res->xres);
ret[1].push_back(res->yres);
}
return ret;
}
// Depending on |type|, returns the number of values associated with |attr|.
size_t GetAttributeValuesSize(const ipp_parser::mojom::IppAttributePtr& attr) {
const auto& attr_value = attr->value;
switch (attr->value->which()) {
case ValueType::kDate:
return 1;
case ValueType::kBools:
return attr_value->get_bools().size();
case ValueType::kInts:
return attr_value->get_ints().size();
case ValueType::kStrings:
return attr_value->get_strings().size();
case ValueType::kOctets:
return attr_value->get_octets().size();
case ValueType::kResolutions:
return attr_value->get_resolutions().size();
}
NOTREACHED();
return 0;
}
// Returns true if |data| starts with the full |prefix|, false otherwise.
bool StartsWith(base::span<uint8_t const> data,
base::span<uint8_t const> prefix) {
if (data.size() < prefix.size())
return false;
return std::equal(data.begin(), data.begin() + prefix.size(), prefix.begin());
}
} // namespace
// Verifies that |method|, |endpoint|, and |http_version| form a valid HTTP
// request-line. On success, returns a wrapper obj containing the verified
// request-line.
absl::optional<HttpRequestLine> IppValidator::ValidateHttpRequestLine(
base::StringPiece method,
base::StringPiece endpoint,
base::StringPiece http_version) {
if (method != "POST") {
return absl::nullopt;
}
if (http_version != "HTTP/1.1") {
return absl::nullopt;
}
// Empty endpoint is allowed.
if (endpoint == "/") {
return HttpRequestLine{std::string(method), std::string(endpoint),
std::string(http_version)};
}
// Ensure endpoint is a known printer.
auto printer_id = ParseEndpointForPrinterId(std::string(endpoint));
if (!printer_id.has_value()) {
return absl::nullopt;
}
auto printer = delegate_->GetPrinter(*printer_id);
if (!printer.has_value()) {
return absl::nullopt;
}
return HttpRequestLine{std::string(method), std::string(endpoint),
std::string(http_version)};
}
absl::optional<std::vector<ipp_converter::HttpHeader>>
IppValidator::ValidateHttpHeaders(
const size_t http_content_length,
const base::flat_map<std::string, std::string>& headers) {
// Sane, character-set checks.
for (const auto& header : headers) {
if (!net::HttpUtil::IsValidHeaderName(header.first) ||
!net::HttpUtil::IsValidHeaderValue(header.second)) {
return absl::nullopt;
}
}
std::vector<ipp_converter::HttpHeader> ret(headers.begin(), headers.end());
// Update the ContentLength.
base::EraseIf(ret, [](const ipp_converter::HttpHeader& header) {
return header.first == "Content-Length";
});
ret.push_back({"Content-Length", base::NumberToString(http_content_length)});
return ret;
}
// Note: Since its possible to have valid IPP attributes that our
// ipp_attribute_validator.cc is unaware of, we drop unknown attributes, rather
// than fail the request.
ipp_t* IppValidator::ValidateIppMessage(
ipp_parser::mojom::IppMessagePtr ipp_message) {
printing::ScopedIppPtr ipp = printing::WrapIpp(ippNew());
// Fill ids.
if (!ippSetVersion(ipp.get(), ipp_message->major_version,
ipp_message->minor_version)) {
return nullptr;
}
const ipp_op_t ipp_oper_id = static_cast<ipp_op_t>(ipp_message->operation_id);
if (!ippSetOperation(ipp.get(), ipp_oper_id))
return nullptr;
if (!ippSetRequestId(ipp.get(), ipp_message->request_id)) {
return nullptr;
}
// Fill attributes.
for (size_t i = 0; i < ipp_message->attributes.size(); ++i) {
ipp_parser::mojom::IppAttributePtr attribute =
std::move(ipp_message->attributes[i]);
size_t num_values = GetAttributeValuesSize(attribute);
if (!num_values) {
return nullptr;
}
auto ret = ValidateAttribute(ipp_oper_id, attribute->name,
attribute->value->which(), num_values);
if (ret == ValidateAttributeResult::kFatalError) {
return nullptr;
}
if (ret == ValidateAttributeResult::kUnknownAttribute) {
// We drop unknown attributes.
DVLOG(1) << "CupsProxy validation: dropping unknown attribute "
<< attribute->name;
continue;
}
switch (attribute->value->which()) {
case ValueType::kBools: {
std::vector<char> values =
ConvertBooleans(attribute->value->get_bools());
auto* attr = ippAddBooleans(
ipp.get(), static_cast<ipp_tag_t>(attribute->group_tag),
attribute->name.c_str(), values.size(), values.data());
if (!attr) {
return nullptr;
}
break;
}
case ValueType::kDate: {
std::vector<uint8_t> date = attribute->value->get_date();
// Per RFC2910, ipp_uchar_t is defined as an OCTET, so below
// reinterpret_cast is safe.
auto* attr =
ippAddDate(ipp.get(), static_cast<ipp_tag_t>(attribute->group_tag),
attribute->name.c_str(),
reinterpret_cast<const ipp_uchar_t*>(date.data()));
if (!attr) {
return nullptr;
}
break;
}
case ValueType::kInts: {
std::vector<int> values = attribute->value->get_ints();
auto* attr = ippAddIntegers(
ipp.get(), static_cast<ipp_tag_t>(attribute->group_tag),
static_cast<ipp_tag_t>(attribute->value_tag),
attribute->name.c_str(), values.size(), values.data());
if (!attr) {
return nullptr;
}
break;
}
case ValueType::kStrings: {
// Note: cstrings_values references attribute->value's strings, i.e.
// attribute->value MUST outlive cstrings_values.
std::vector<const char*> cstrings_values =
ConvertStrings(attribute->value->get_strings());
auto* attr = ippAddStrings(
ipp.get(), static_cast<ipp_tag_t>(attribute->group_tag),
static_cast<ipp_tag_t>(attribute->value_tag),
attribute->name.c_str(), cstrings_values.size(), kLocaleEnglish,
cstrings_values.data());
if (!attr) {
return nullptr;
}
break;
}
case ValueType::kOctets: {
size_t num = attribute->value->get_octets().size();
if (num != 1) {
LOG(ERROR) << "CUPS API only supports adding a single octet string "
"- cannot add "
<< num << " octet strings.";
return nullptr;
}
int size = attribute->value->get_octets()[0].size();
if (size < 1) {
LOG(ERROR) << "Invalid octet string size=" << size;
return nullptr;
}
auto* attr = ippAddOctetString(
ipp.get(), static_cast<ipp_tag_t>(attribute->group_tag),
attribute->name.c_str(), attribute->value->get_octets()[0].data(),
size);
if (!attr) {
return nullptr;
}
break;
}
case ValueType::kResolutions: {
std::array<std::vector<int>, 2> res =
ConvertResolutions(attribute->value->get_resolutions());
auto* attr = ippAddResolutions(
ipp.get(), static_cast<ipp_tag_t>(attribute->group_tag),
attribute->name.c_str(), res[0].size(), IPP_RES_PER_INCH,
res[0].data(), res[1].data());
if (!attr) {
return nullptr;
}
break;
}
default:
NOTREACHED() << "Unknown IPP attribute type found.";
}
}
// Validate Attributes.
if (!ippValidateAttributes(ipp.get())) {
return nullptr;
}
// Return built ipp object.
return ipp.release();
}
// Requires |ipp_data| to be empty or look like a PDF or PostScript document.
bool IppValidator::ValidateIppData(const std::vector<uint8_t>& ipp_data) {
// Empty IPP data portion.
if (ipp_data.empty()) {
return true;
}
// Check if |ipp_data| looks like a PDF or PostScript document.
return StartsWith(ipp_data, pdf_magic_bytes) ||
StartsWith(ipp_data, ps_magic_bytes);
}
IppValidator::IppValidator(CupsProxyServiceDelegate* const delegate)
: delegate_(delegate) {}
IppValidator::~IppValidator() = default;
absl::optional<IppRequest> IppValidator::ValidateIppRequest(
ipp_parser::mojom::IppRequestPtr to_validate) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
// Build ipp message.
// Note: Moving ipp here, to_validate->ipp no longer valid below.
printing::ScopedIppPtr ipp =
printing::WrapIpp(ValidateIppMessage(std::move(to_validate->ipp)));
if (ipp == nullptr) {
return absl::nullopt;
}
// Validate ipp data.
// TODO(crbug/894607): Validate ippData (pdf).
if (!ValidateIppData(to_validate->data)) {
return absl::nullopt;
}
// Build request line.
auto request_line = ValidateHttpRequestLine(
to_validate->method, to_validate->endpoint, to_validate->http_version);
if (!request_line.has_value()) {
return absl::nullopt;
}
// Build headers; must happen after ipp message/data since it requires the
// ContentLength.
const size_t http_content_length =
ippLength(ipp.get()) + to_validate->data.size();
auto headers = ValidateHttpHeaders(http_content_length, to_validate->headers);
if (!headers.has_value()) {
return absl::nullopt;
}
// Marshall request
IppRequest ret;
ret.request_line = std::move(*request_line);
ret.headers = std::move(*headers);
ret.ipp = std::move(ipp);
ret.ipp_data = std::move(to_validate->data);
// Build parsed request buffer.
auto request_buffer = ipp_converter::BuildIppRequest(
ret.request_line.method, ret.request_line.endpoint,
ret.request_line.http_version, ret.headers, ret.ipp.get(), ret.ipp_data);
if (!request_buffer.has_value()) {
return absl::nullopt;
}
ret.buffer = std::move(*request_buffer);
return ret;
}
} // namespace cups_proxy