Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(744)

Unified Diff: mojo/public/rust/src/bindings/message.rs

Issue 2220183003: Rust: Add validation to decode (Closed) Base URL: git@github.com:domokit/mojo.git@coder-testing
Patch Set: Update from upstream branches (fixed arrays, validation code, etc.) Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « mojo/public/rust/src/bindings/macros.rs ('k') | mojo/public/rust/src/bindings/mojom.rs » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: mojo/public/rust/src/bindings/message.rs
diff --git a/mojo/public/rust/src/bindings/message.rs b/mojo/public/rust/src/bindings/message.rs
index de40873836b2fa1348f07ec61470add12bb78692..0d9834db31a9126b2d6bb568a4b641fc8b1d7d66 100644
--- a/mojo/public/rust/src/bindings/message.rs
+++ b/mojo/public/rust/src/bindings/message.rs
@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-use bindings::decoding::Decoder;
+use bindings::decoding::{Decoder, ValidationError};
use bindings::encoding;
use bindings::encoding::{Context, DATA_HEADER_SIZE, DataHeaderValue, Encoder};
use bindings::mojom::{MojomEncodable, MojomPointer, MojomStruct};
@@ -18,6 +18,8 @@ pub const MESSAGE_HEADER_EXPECT_RESPONSE: u32 = 1;
/// a response.
pub const MESSAGE_HEADER_IS_RESPONSE: u32 = 2;
+const MESSAGE_HEADER_VERSIONS: [(u32, u32); 2] = [(0, 16), (1, 24)];
+
/// A message header object implemented as a Mojom struct.
pub struct MessageHeader {
pub version: u32,
@@ -68,30 +70,36 @@ impl MojomPointer for MessageHeader {
}
}
- fn decode_value(decoder: &mut Decoder, context: Context) -> Self {
+ fn decode_value(decoder: &mut Decoder, context: Context) -> Result<Self, ValidationError> {
let mut state = decoder.get_mut(&context);
- // TODO(mknyszek): Verify bytes and T::embed_size match
- let bytes = state.decode::<u32>();
- let version = state.decode::<u32>();
- // TODO(mknyszek): Don't trust bytes blindly
+ let version = match state.decode_struct_header(&MESSAGE_HEADER_VERSIONS) {
+ Ok(header) => header.data(),
+ Err(err) => return Err(err),
+ };
let name = state.decode::<u32>();
let flags = state.decode::<u32>();
- if (bytes as usize) == DATA_HEADER_SIZE + 8 {
- MessageHeader {
+ if flags > MESSAGE_HEADER_IS_RESPONSE {
+ return Err(ValidationError::MessageHeaderInvalidFlags);
+ }
+ if version == 0 {
+ if flags == MESSAGE_HEADER_IS_RESPONSE || flags == MESSAGE_HEADER_EXPECT_RESPONSE {
+ return Err(ValidationError::MessageHeaderMissingRequestId);
+ }
+ Ok(MessageHeader {
version: version,
name: name,
flags: flags,
request_id: 0,
- }
- } else if (bytes as usize) == DATA_HEADER_SIZE + 16 {
- MessageHeader {
+ })
+ } else if version == 1 {
+ Ok(MessageHeader {
version: version,
name: name,
flags: flags,
request_id: state.decode::<u64>(),
- }
+ })
} else {
- panic!("Invalid message header size found: `{}`", bytes);
+ return Err(ValidationError::UnexpectedStructHeader);
}
}
}
@@ -104,4 +112,3 @@ impl MojomEncodable for MessageHeader {
}
impl MojomStruct for MessageHeader {}
-
« no previous file with comments | « mojo/public/rust/src/bindings/macros.rs ('k') | mojo/public/rust/src/bindings/mojom.rs » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698