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

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

Issue 2796253002: Associated Message Validation (Closed)
Patch Set: Validate payloadInterfaceIds before getting it. Use [0] for dimensions for validateArrayPointer. Ca… Created 3 years, 8 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
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 define("mojo/public/js/validator", [ 5 define("mojo/public/js/validator", [
6 "mojo/public/js/codec", 6 "mojo/public/js/codec",
7 ], function(codec) { 7 "mojo/public/js/interface_types",
8 ], function(codec, types) {
8 9
9 var validationError = { 10 var validationError = {
10 NONE: 'VALIDATION_ERROR_NONE', 11 NONE: 'VALIDATION_ERROR_NONE',
11 MISALIGNED_OBJECT: 'VALIDATION_ERROR_MISALIGNED_OBJECT', 12 MISALIGNED_OBJECT: 'VALIDATION_ERROR_MISALIGNED_OBJECT',
12 ILLEGAL_MEMORY_RANGE: 'VALIDATION_ERROR_ILLEGAL_MEMORY_RANGE', 13 ILLEGAL_MEMORY_RANGE: 'VALIDATION_ERROR_ILLEGAL_MEMORY_RANGE',
13 UNEXPECTED_STRUCT_HEADER: 'VALIDATION_ERROR_UNEXPECTED_STRUCT_HEADER', 14 UNEXPECTED_STRUCT_HEADER: 'VALIDATION_ERROR_UNEXPECTED_STRUCT_HEADER',
14 UNEXPECTED_ARRAY_HEADER: 'VALIDATION_ERROR_UNEXPECTED_ARRAY_HEADER', 15 UNEXPECTED_ARRAY_HEADER: 'VALIDATION_ERROR_UNEXPECTED_ARRAY_HEADER',
15 ILLEGAL_HANDLE: 'VALIDATION_ERROR_ILLEGAL_HANDLE', 16 ILLEGAL_HANDLE: 'VALIDATION_ERROR_ILLEGAL_HANDLE',
16 UNEXPECTED_INVALID_HANDLE: 'VALIDATION_ERROR_UNEXPECTED_INVALID_HANDLE', 17 UNEXPECTED_INVALID_HANDLE: 'VALIDATION_ERROR_UNEXPECTED_INVALID_HANDLE',
17 ILLEGAL_POINTER: 'VALIDATION_ERROR_ILLEGAL_POINTER', 18 ILLEGAL_POINTER: 'VALIDATION_ERROR_ILLEGAL_POINTER',
18 UNEXPECTED_NULL_POINTER: 'VALIDATION_ERROR_UNEXPECTED_NULL_POINTER', 19 UNEXPECTED_NULL_POINTER: 'VALIDATION_ERROR_UNEXPECTED_NULL_POINTER',
20 ILLEGAL_INTERFACE_ID: 'VALIDATION_ERROR_ILLEGAL_INTERFACE_ID',
21 UNEXPECTED_INVALID_INTERFACE_ID:
22 'VALIDATION_ERROR_UNEXPECTED_INVALID_INTERFACE_ID',
19 MESSAGE_HEADER_INVALID_FLAGS: 23 MESSAGE_HEADER_INVALID_FLAGS:
20 'VALIDATION_ERROR_MESSAGE_HEADER_INVALID_FLAGS', 24 'VALIDATION_ERROR_MESSAGE_HEADER_INVALID_FLAGS',
21 MESSAGE_HEADER_MISSING_REQUEST_ID: 25 MESSAGE_HEADER_MISSING_REQUEST_ID:
22 'VALIDATION_ERROR_MESSAGE_HEADER_MISSING_REQUEST_ID', 26 'VALIDATION_ERROR_MESSAGE_HEADER_MISSING_REQUEST_ID',
23 DIFFERENT_SIZED_ARRAYS_IN_MAP: 27 DIFFERENT_SIZED_ARRAYS_IN_MAP:
24 'VALIDATION_ERROR_DIFFERENT_SIZED_ARRAYS_IN_MAP', 28 'VALIDATION_ERROR_DIFFERENT_SIZED_ARRAYS_IN_MAP',
25 INVALID_UNION_SIZE: 'VALIDATION_ERROR_INVALID_UNION_SIZE', 29 INVALID_UNION_SIZE: 'VALIDATION_ERROR_INVALID_UNION_SIZE',
26 UNEXPECTED_NULL_UNION: 'VALIDATION_ERROR_UNEXPECTED_NULL_UNION', 30 UNEXPECTED_NULL_UNION: 'VALIDATION_ERROR_UNEXPECTED_NULL_UNION',
27 UNKNOWN_ENUM_VALUE: 'VALIDATION_ERROR_UNKNOWN_ENUM_VALUE', 31 UNKNOWN_ENUM_VALUE: 'VALIDATION_ERROR_UNKNOWN_ENUM_VALUE',
28 }; 32 };
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
86 90
87 function isInterfaceClass(cls) { 91 function isInterfaceClass(cls) {
88 return cls instanceof codec.Interface; 92 return cls instanceof codec.Interface;
89 } 93 }
90 94
91 function isInterfaceRequestClass(cls) { 95 function isInterfaceRequestClass(cls) {
92 return cls === codec.InterfaceRequest || 96 return cls === codec.InterfaceRequest ||
93 cls === codec.NullableInterfaceRequest; 97 cls === codec.NullableInterfaceRequest;
94 } 98 }
95 99
100 function isAssociatedInterfaceClass(cls) {
101 return cls === codec.AssociatedInterfacePtrInfo ||
102 cls === codec.NullableAssociatedInterfacePtrInfo;
103 }
104
105 function isAssociatedInterfaceRequestClass(cls) {
106 return cls === codec.AssociatedInterfaceRequest ||
107 cls === codec.NullableAssociatedInterfaceRequest;
108 }
109
96 function isNullable(type) { 110 function isNullable(type) {
97 return type === codec.NullableString || type === codec.NullableHandle || 111 return type === codec.NullableString || type === codec.NullableHandle ||
98 type === codec.NullableInterface || 112 type === codec.NullableInterface ||
99 type === codec.NullableInterfaceRequest || 113 type === codec.NullableInterfaceRequest ||
100 type instanceof codec.NullableArrayOf || 114 type instanceof codec.NullableArrayOf ||
101 type instanceof codec.NullablePointerTo; 115 type instanceof codec.NullablePointerTo;
102 } 116 }
103 117
104 function Validator(message) { 118 function Validator(message) {
105 this.message = message; 119 this.message = message;
106 this.offset = 0; 120 this.offset = 0;
107 this.handleIndex = 0; 121 this.handleIndex = 0;
122 this.associatedEndpointHandleIndex = 0;
123 this.payloadInterfaceIds = null;
124 this.offsetLimit = this.message.buffer.byteLength;
108 } 125 }
109 126
110 Object.defineProperty(Validator.prototype, "offsetLimit", {
111 get: function() { return this.message.buffer.byteLength; }
112 });
113
114 Object.defineProperty(Validator.prototype, "handleIndexLimit", { 127 Object.defineProperty(Validator.prototype, "handleIndexLimit", {
115 get: function() { return this.message.handles.length; } 128 get: function() { return this.message.handles.length; }
116 }); 129 });
117 130
131 Object.defineProperty(Validator.prototype, "associatedHandleIndexLimit", {
132 get: function() {
133 return this.payloadInterfaceIds ? this.payloadInterfaceIds.length : 0;
134 }
135 });
136
118 // True if we can safely allocate a block of bytes from start to 137 // True if we can safely allocate a block of bytes from start to
119 // to start + numBytes. 138 // to start + numBytes.
120 Validator.prototype.isValidRange = function(start, numBytes) { 139 Validator.prototype.isValidRange = function(start, numBytes) {
121 // Only positive JavaScript integers that are less than 2^53 140 // Only positive JavaScript integers that are less than 2^53
122 // (Number.MAX_SAFE_INTEGER) can be represented exactly. 141 // (Number.MAX_SAFE_INTEGER) can be represented exactly.
123 if (start < this.offset || numBytes <= 0 || 142 if (start < this.offset || numBytes <= 0 ||
124 !Number.isSafeInteger(start) || 143 !Number.isSafeInteger(start) ||
125 !Number.isSafeInteger(numBytes)) 144 !Number.isSafeInteger(numBytes))
126 return false; 145 return false;
127 146
(...skipping 17 matching lines...) Expand all
145 return true; 164 return true;
146 165
147 if (index < this.handleIndex || index >= this.handleIndexLimit) 166 if (index < this.handleIndex || index >= this.handleIndexLimit)
148 return false; 167 return false;
149 168
150 // This is safe because handle indices are uint32. 169 // This is safe because handle indices are uint32.
151 this.handleIndex = index + 1; 170 this.handleIndex = index + 1;
152 return true; 171 return true;
153 }; 172 };
154 173
174 Validator.prototype.claimAssociatedEndpointHandle = function(index) {
175 if (index === codec.kEncodedInvalidHandleValue) {
176 return true;
177 }
178
179 if (index < this.associatedEndpointHandleIndex ||
180 index >= this.associatedHandleIndexLimit) {
181 return false;
182 }
183
184 // This is safe because handle indices are uint32.
185 this.associatedEndpointHandleIndex = index + 1;
186 return true;
187 };
188
155 Validator.prototype.validateEnum = function(offset, enumClass) { 189 Validator.prototype.validateEnum = function(offset, enumClass) {
156 // Note: Assumes that enums are always 32 bits! But this matches 190 // Note: Assumes that enums are always 32 bits! But this matches
157 // mojom::generate::pack::PackedField::GetSizeForKind, so it should be okay. 191 // mojom::generate::pack::PackedField::GetSizeForKind, so it should be okay.
158 var value = this.message.buffer.getInt32(offset); 192 var value = this.message.buffer.getInt32(offset);
159 return enumClass.validate(value); 193 return enumClass.validate(value);
160 } 194 }
161 195
162 Validator.prototype.validateHandle = function(offset, nullable) { 196 Validator.prototype.validateHandle = function(offset, nullable) {
163 var index = this.message.buffer.getUint32(offset); 197 var index = this.message.buffer.getUint32(offset);
164 198
165 if (index === codec.kEncodedInvalidHandleValue) 199 if (index === codec.kEncodedInvalidHandleValue)
166 return nullable ? 200 return nullable ?
167 validationError.NONE : validationError.UNEXPECTED_INVALID_HANDLE; 201 validationError.NONE : validationError.UNEXPECTED_INVALID_HANDLE;
168 202
169 if (!this.claimHandle(index)) 203 if (!this.claimHandle(index))
170 return validationError.ILLEGAL_HANDLE; 204 return validationError.ILLEGAL_HANDLE;
171 205
172 return validationError.NONE; 206 return validationError.NONE;
173 }; 207 };
174 208
209 Validator.prototype.validateAssociatedEndpointHandle = function(offset,
210 nullable) {
211 var index = this.message.buffer.getUint32(offset);
212
213 if (index === codec.kEncodedInvalidHandleValue) {
214 return nullable ? validationError.NONE :
215 validationError.UNEXPECTED_INVALID_INTERFACE_ID;
216 }
217
218 if (!this.claimAssociatedEndpointHandle(index)) {
219 return validationError.ILLEGAL_INTERFACE_ID;
220 }
221
222 return validationError.NONE;
223 };
224
175 Validator.prototype.validateInterface = function(offset, nullable) { 225 Validator.prototype.validateInterface = function(offset, nullable) {
176 return this.validateHandle(offset, nullable); 226 return this.validateHandle(offset, nullable);
177 }; 227 };
178 228
179 Validator.prototype.validateInterfaceRequest = function(offset, nullable) { 229 Validator.prototype.validateInterfaceRequest = function(offset, nullable) {
180 return this.validateHandle(offset, nullable); 230 return this.validateHandle(offset, nullable);
181 }; 231 };
182 232
233 Validator.prototype.validateAssociatedInterface = function(offset,
234 nullable) {
235 return this.validateAssociatedEndpointHandle(offset, nullable);
236 };
237
238 Validator.prototype.validateAssociatedInterfaceRequest = function(
239 offset, nullable) {
240 return this.validateAssociatedEndpointHandle(offset, nullable);
241 };
242
183 Validator.prototype.validateStructHeader = function(offset, minNumBytes) { 243 Validator.prototype.validateStructHeader = function(offset, minNumBytes) {
184 if (!codec.isAligned(offset)) 244 if (!codec.isAligned(offset))
185 return validationError.MISALIGNED_OBJECT; 245 return validationError.MISALIGNED_OBJECT;
186 246
187 if (!this.isValidRange(offset, codec.kStructHeaderSize)) 247 if (!this.isValidRange(offset, codec.kStructHeaderSize))
188 return validationError.ILLEGAL_MEMORY_RANGE; 248 return validationError.ILLEGAL_MEMORY_RANGE;
189 249
190 var numBytes = this.message.buffer.getUint32(offset); 250 var numBytes = this.message.buffer.getUint32(offset);
191 251
192 if (numBytes < minNumBytes) 252 if (numBytes < minNumBytes)
(...skipping 23 matching lines...) Expand all
216 } 276 }
217 277
218 return validationError.NONE; 278 return validationError.NONE;
219 }; 279 };
220 280
221 Validator.prototype.isFieldInStructVersion = function(offset, fieldVersion) { 281 Validator.prototype.isFieldInStructVersion = function(offset, fieldVersion) {
222 var structVersion = this.message.buffer.getUint32(offset + 4); 282 var structVersion = this.message.buffer.getUint32(offset + 4);
223 return fieldVersion <= structVersion; 283 return fieldVersion <= structVersion;
224 }; 284 };
225 285
226 // TODO(wangjimmy): Add support for v2 messages.
227 Validator.prototype.validateMessageHeader = function() { 286 Validator.prototype.validateMessageHeader = function() {
228 287 var err = this.validateStructHeader(0, codec.kMessageV0HeaderSize);
229 var err = this.validateStructHeader(0, codec.kMessageHeaderSize); 288 if (err != validationError.NONE) {
230 if (err != validationError.NONE)
231 return err; 289 return err;
290 }
232 291
233 var numBytes = this.message.getHeaderNumBytes(); 292 var numBytes = this.message.getHeaderNumBytes();
234 var version = this.message.getHeaderVersion(); 293 var version = this.message.getHeaderVersion();
235 294
236 var validVersionAndNumBytes = 295 var validVersionAndNumBytes =
237 (version == 0 && numBytes == codec.kMessageHeaderSize) || 296 (version == 0 && numBytes == codec.kMessageV0HeaderSize) ||
238 (version == 1 && 297 (version == 1 && numBytes == codec.kMessageV1HeaderSize) ||
239 numBytes == codec.kMessageWithRequestIDHeaderSize) || 298 (version == 2 && numBytes == codec.kMessageV2HeaderSize) ||
240 (version > 1 && 299 (version > 2 && numBytes >= codec.kMessageV2HeaderSize);
241 numBytes >= codec.kMessageWithRequestIDHeaderSize); 300
242 if (!validVersionAndNumBytes) 301 if (!validVersionAndNumBytes) {
243 return validationError.UNEXPECTED_STRUCT_HEADER; 302 return validationError.UNEXPECTED_STRUCT_HEADER;
303 }
244 304
245 var expectsResponse = this.message.expectsResponse(); 305 var expectsResponse = this.message.expectsResponse();
246 var isResponse = this.message.isResponse(); 306 var isResponse = this.message.isResponse();
247 307
248 if (version == 0 && (expectsResponse || isResponse)) 308 if (version == 0 && (expectsResponse || isResponse)) {
249 return validationError.MESSAGE_HEADER_MISSING_REQUEST_ID; 309 return validationError.MESSAGE_HEADER_MISSING_REQUEST_ID;
310 }
250 311
251 if (isResponse && expectsResponse) 312 if (isResponse && expectsResponse) {
252 return validationError.MESSAGE_HEADER_INVALID_FLAGS; 313 return validationError.MESSAGE_HEADER_INVALID_FLAGS;
314 }
315
316 if (version < 2) {
317 return validationError.NONE;
318 }
319
320 var err = this.validateArrayPointer(
321 codec.kMessagePayloadInterfaceIdsPointerOffset,
322 codec.Uint32.encodedSize, codec.Uint32, true, [0], 0);
323
324 if (err != validationError.NONE) {
325 return err;
326 }
327
328 this.payloadInterfaceIds = this.message.getPayloadInterfaceIds();
329 if (this.payloadInterfaceIds) {
330 for (var interfaceId of this.payloadInterfaceIds) {
331 if (!types.isValidInterfaceId(interfaceId) ||
332 types.isMasterInterfaceId(interfaceId)) {
333 return validationError.ILLEGAL_INTERFACE_ID;
334 }
335 }
336 }
337
338 // Set offset to the start of the payload and offsetLimit to the start of
339 // the payload interface Ids so that payload can be validated using the
340 // same messageValidator.
341 this.offset = this.message.getHeaderNumBytes();
342 this.offsetLimit = this.decodePointer(
343 codec.kMessagePayloadInterfaceIdsPointerOffset);
253 344
254 return validationError.NONE; 345 return validationError.NONE;
255 }; 346 };
256 347
257 Validator.prototype.validateMessageIsRequestWithoutResponse = function() { 348 Validator.prototype.validateMessageIsRequestWithoutResponse = function() {
258 if (this.message.isResponse() || this.message.expectsResponse()) { 349 if (this.message.isResponse() || this.message.expectsResponse()) {
259 return validationError.MESSAGE_HEADER_INVALID_FLAGS; 350 return validationError.MESSAGE_HEADER_INVALID_FLAGS;
260 } 351 }
261 return validationError.NONE; 352 return validationError.NONE;
262 }; 353 };
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after
444 var nullable = isNullable(elementType); 535 var nullable = isNullable(elementType);
445 536
446 if (isHandleClass(elementType)) 537 if (isHandleClass(elementType))
447 return this.validateHandleElements(elementsOffset, numElements, nullable); 538 return this.validateHandleElements(elementsOffset, numElements, nullable);
448 if (isInterfaceClass(elementType)) 539 if (isInterfaceClass(elementType))
449 return this.validateInterfaceElements( 540 return this.validateInterfaceElements(
450 elementsOffset, numElements, nullable); 541 elementsOffset, numElements, nullable);
451 if (isInterfaceRequestClass(elementType)) 542 if (isInterfaceRequestClass(elementType))
452 return this.validateInterfaceRequestElements( 543 return this.validateInterfaceRequestElements(
453 elementsOffset, numElements, nullable); 544 elementsOffset, numElements, nullable);
545 if (isAssociatedInterfaceClass(elementType))
546 return this.validateAssociatedInterfaceElements(
547 elementsOffset, numElements, nullable);
548 if (isAssociatedInterfaceRequestClass(elementType))
549 return this.validateAssociatedInterfaceRequestElements(
550 elementsOffset, numElements, nullable);
454 if (isStringClass(elementType)) 551 if (isStringClass(elementType))
455 return this.validateArrayElements( 552 return this.validateArrayElements(
456 elementsOffset, numElements, codec.Uint8, nullable, [0], 0); 553 elementsOffset, numElements, codec.Uint8, nullable, [0], 0);
457 if (elementType instanceof codec.PointerTo) 554 if (elementType instanceof codec.PointerTo)
458 return this.validateStructElements( 555 return this.validateStructElements(
459 elementsOffset, numElements, elementType.cls, nullable); 556 elementsOffset, numElements, elementType.cls, nullable);
460 if (elementType instanceof codec.ArrayOf) 557 if (elementType instanceof codec.ArrayOf)
461 return this.validateArrayElements( 558 return this.validateArrayElements(
462 elementsOffset, numElements, elementType.cls, nullable, 559 elementsOffset, numElements, elementType.cls, nullable,
463 expectedDimensionSizes, currentDimension + 1); 560 expectedDimensionSizes, currentDimension + 1);
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
501 var elementSize = codec.InterfaceRequest.encodedSize; 598 var elementSize = codec.InterfaceRequest.encodedSize;
502 for (var i = 0; i < numElements; i++) { 599 for (var i = 0; i < numElements; i++) {
503 var elementOffset = offset + i * elementSize; 600 var elementOffset = offset + i * elementSize;
504 var err = this.validateInterfaceRequest(elementOffset, nullable); 601 var err = this.validateInterfaceRequest(elementOffset, nullable);
505 if (err != validationError.NONE) 602 if (err != validationError.NONE)
506 return err; 603 return err;
507 } 604 }
508 return validationError.NONE; 605 return validationError.NONE;
509 }; 606 };
510 607
608 Validator.prototype.validateAssociatedInterfaceElements =
609 function(offset, numElements, nullable) {
610 var elementSize = codec.AssociatedInterfacePtrInfo.prototype.encodedSize;
611 for (var i = 0; i < numElements; i++) {
612 var elementOffset = offset + i * elementSize;
613 var err = this.validateAssociatedInterface(elementOffset, nullable);
614 if (err != validationError.NONE) {
615 return err;
616 }
617 }
618 return validationError.NONE;
619 };
620
621 Validator.prototype.validateAssociatedInterfaceRequestElements =
622 function(offset, numElements, nullable) {
623 var elementSize = codec.AssociatedInterfaceRequest.encodedSize;
624 for (var i = 0; i < numElements; i++) {
625 var elementOffset = offset + i * elementSize;
626 var err = this.validateAssociatedInterfaceRequest(elementOffset,
627 nullable);
628 if (err != validationError.NONE) {
629 return err;
630 }
631 }
632 return validationError.NONE;
633 };
634
511 // The elementClass parameter is the element type of the element arrays. 635 // The elementClass parameter is the element type of the element arrays.
512 Validator.prototype.validateArrayElements = 636 Validator.prototype.validateArrayElements =
513 function(offset, numElements, elementClass, nullable, 637 function(offset, numElements, elementClass, nullable,
514 expectedDimensionSizes, currentDimension) { 638 expectedDimensionSizes, currentDimension) {
515 var elementSize = codec.PointerTo.prototype.encodedSize; 639 var elementSize = codec.PointerTo.prototype.encodedSize;
516 for (var i = 0; i < numElements; i++) { 640 for (var i = 0; i < numElements; i++) {
517 var elementOffset = offset + i * elementSize; 641 var elementOffset = offset + i * elementSize;
518 var err = this.validateArrayPointer( 642 var err = this.validateArrayPointer(
519 elementOffset, elementClass.encodedSize, elementClass, nullable, 643 elementOffset, elementClass.encodedSize, elementClass, nullable,
520 expectedDimensionSizes, currentDimension); 644 expectedDimensionSizes, currentDimension);
(...skipping 30 matching lines...) Expand all
551 675
552 var exports = {}; 676 var exports = {};
553 exports.validationError = validationError; 677 exports.validationError = validationError;
554 exports.Validator = Validator; 678 exports.Validator = Validator;
555 exports.ValidationErrorObserverForTesting = ValidationErrorObserverForTesting; 679 exports.ValidationErrorObserverForTesting = ValidationErrorObserverForTesting;
556 exports.reportValidationError = reportValidationError; 680 exports.reportValidationError = reportValidationError;
557 exports.isTestingMode = isTestingMode; 681 exports.isTestingMode = isTestingMode;
558 exports.clearTestingMode = clearTestingMode; 682 exports.clearTestingMode = clearTestingMode;
559 return exports; 683 return exports;
560 }); 684 });
OLDNEW
« no previous file with comments | « mojo/public/js/tests/validation_unittest.js ('k') | mojo/public/tools/bindings/generators/js_templates/module.amd.tmpl » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698