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

Side by Side Diff: mojo/public/js/bindings/validator.js

Issue 424463003: Basic Mojo message header validation for JavaScript. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: One last change for JS purists Created 6 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « mojo/public/js/bindings/router.js ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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.
4
5 define("mojo/public/js/bindings/validator", [
6 "mojo/public/js/bindings/codec",
7 ], function(codec) {
8
9 var validationError = {
10 NONE: 'VALIDATION_ERROR_NONE',
11 MISALIGNED_OBJECT: 'VALIDATION_ERROR_MISALIGNED_OBJECT',
12 ILLEGAL_MEMORY_RANGE: 'VALIDATION_ERROR_ILLEGAL_MEMORY_RANGE',
13 UNEXPECTED_STRUCT_HEADER: 'VALIDATION_ERROR_UNEXPECTED_STRUCT_HEADER',
14 UNEXPECTED_ARRAY_HEADER: 'VALIDATION_ERROR_UNEXPECTED_ARRAY_HEADER',
15 ILLEGAL_HANDLE: 'VALIDATION_ERROR_ILLEGAL_HANDLE',
16 ILLEGAL_POINTER: 'VALIDATION_ERROR_ILLEGAL_POINTER',
17 MESSAGE_HEADER_INVALID_FLAG_COMBINATION:
18 'VALIDATION_ERROR_MESSAGE_HEADER_INVALID_FLAG_COMBINATION',
19 MESSAGE_HEADER_MISSING_REQUEST_ID:
20 'VALIDATION_ERROR_MESSAGE_HEADER_MISSING_REQUEST_ID'
21 };
22
23 function Validator(message) {
24 this.message = message;
25 this.offset = 0;
26 }
27
28 Object.defineProperty(Validator.prototype, "offsetLimit", {
29 get: function() { return this.message.buffer.byteLength; }
30 });
31
32 // True if we can safely allocate a block of bytes from offset + start
yzshen1 2014/07/30 17:14:56 Do you mean |start| is relative to the current |of
hansmuller 2014/07/30 18:32:30 I agree, it should be relative to the beginning of
33 // to offset + start + numBytes.
34 Validator.prototype.isValidRange = function(start, numBytes) {
35 if (start < this.offset || numBytes < 0 ||
yzshen1 2014/07/30 17:14:56 start < this.offset: it indicates start and offset
hansmuller 2014/07/30 18:32:30 Right, that's my mistake (again). The check just n
36 !Number.isSafeInteger(start) ||
37 !Number.isSafeInteger(numBytes))
38 return false;
39
40 var newOffset = this.offset + start + numBytes;
41 if (!Number.isSafeInteger(newOffset) || newOffset > this.offsetLimit)
42 return false;
43
44 return true;
45 }
46
47 Validator.prototype.claimRange = function(start, numBytes) {
48 if (this.isValidRange(start, numBytes)) {
49 this.offset += start + numBytes;
50 return true;
51 }
52 return false;
53 }
54
55 Validator.prototype.validateStructHeader =
56 function(offset, minNumBytes, minNumFields) {
57 if (!codec.isAligned(offset))
58 return validationError.MISALIGNED_OBJECT;
59
60 if (!this.isValidRange(offset, codec.kStructHeaderSize))
61 return validationError.ILLEGAL_MEMORY_RANGE;
62
63 var numBytes = this.message.buffer.getUint32(offset);
64 var numFields = this.message.buffer.getUint32(offset + 4);
65
66 if (numBytes < minNumBytes || numFields < minNumFields)
67 return validationError.UNEXPECTED_STRUCT_HEADER;
68
69 if (!this.claimRange(offset, numBytes))
70 return validationError.ILLEGAL_MEMORY_RANGE;
71
72 return validationError.NONE;
73 }
74
75 Validator.prototype.validateMessageHeader = function() {
76 var numBytes = this.message.getHeaderNumBytes();
77 var numFields = this.message.getHeaderNumFields();
78
79 var validNumFieldsAndNumBytes =
80 (numFields == 2 && numBytes == codec.kMessageHeaderSize) ||
81 (numFields == 3 &&
82 numBytes == codec.kMessageWithRequestIDHeaderSize) ||
83 (numFields > 3 &&
84 numBytes >= codec.kMessageWithRequestIDHeaderSize);
85 if (!validNumFieldsAndNumBytes)
86 return validationError.UNEXPECTED_STRUCT_HEADER;
87
88 var expectsResponse = this.message.expectsResponse();
89 var isResponse = this.message.isResponse();
90
91 if (numFields == 2 && (expectsResponse || isResponse))
92 return validationError.MESSAGE_HEADER_MISSING_REQUEST_ID;
93
94 if (isResponse && expectsResponse)
95 return validationError.MESSAGE_HEADER_INVALID_FLAG_COMBINATION;
96
97 return validationError.NONE;
98 }
99
100 Validator.prototype.validateMessage = function() {
101 var err = this.validateStructHeader(0, codec.kStructHeaderSize, 2);
102 if (err != validationError.NONE)
103 return err;
104
105 return this.validateMessageHeader();
106 }
107
108 var exports = {};
109 exports.validationError = validationError;
110 exports.Validator = Validator;
111 return exports;
112 });
OLDNEW
« no previous file with comments | « mojo/public/js/bindings/router.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698