Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / mojo / public / cpp / bindings / lib / message_header_validator.cc
bloba55917afde567cfdf51f73bf4cc9b3a9737e6b76
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "mojo/public/cpp/bindings/lib/message_header_validator.h"
7 #include "mojo/public/cpp/bindings/lib/bindings_serialization.h"
8 #include "mojo/public/cpp/bindings/lib/bounds_checker.h"
9 #include "mojo/public/cpp/bindings/lib/validation_errors.h"
11 namespace mojo {
12 namespace internal {
13 namespace {
15 bool IsValidMessageHeader(const MessageHeader* header) {
16 // NOTE: Our goal is to preserve support for future extension of the message
17 // header. If we encounter fields we do not understand, we must ignore them.
19 // Extra validation of the struct header:
20 if (header->num_fields == 2) {
21 if (header->num_bytes != sizeof(MessageHeader)) {
22 ReportValidationError(VALIDATION_ERROR_UNEXPECTED_STRUCT_HEADER);
23 return false;
25 } else if (header->num_fields == 3) {
26 if (header->num_bytes != sizeof(MessageHeaderWithRequestID)) {
27 ReportValidationError(VALIDATION_ERROR_UNEXPECTED_STRUCT_HEADER);
28 return false;
30 } else if (header->num_fields > 3) {
31 if (header->num_bytes < sizeof(MessageHeaderWithRequestID)) {
32 ReportValidationError(VALIDATION_ERROR_UNEXPECTED_STRUCT_HEADER);
33 return false;
37 // Validate flags (allow unknown bits):
39 // These flags require a RequestID.
40 if (header->num_fields < 3 &&
41 ((header->flags & kMessageExpectsResponse) ||
42 (header->flags & kMessageIsResponse))) {
43 ReportValidationError(VALIDATION_ERROR_MESSAGE_HEADER_MISSING_REQUEST_ID);
44 return false;
47 // These flags are mutually exclusive.
48 if ((header->flags & kMessageExpectsResponse) &&
49 (header->flags & kMessageIsResponse)) {
50 ReportValidationError(
51 VALIDATION_ERROR_MESSAGE_HEADER_INVALID_FLAG_COMBINATION);
52 return false;
55 return true;
58 } // namespace
60 MessageHeaderValidator::MessageHeaderValidator(MessageReceiver* sink)
61 : MessageFilter(sink) {
64 bool MessageHeaderValidator::Accept(Message* message) {
65 // Pass 0 as number of handles because we don't expect any in the header, even
66 // if |message| contains handles.
67 BoundsChecker bounds_checker(message->data(), message->data_num_bytes(), 0);
69 if (!ValidateStructHeader(message->data(), sizeof(MessageHeader), 2,
70 &bounds_checker)) {
71 return false;
74 if (!IsValidMessageHeader(message->header()))
75 return false;
77 return sink_->Accept(message);
80 } // namespace internal
81 } // namespace mojo