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

Side by Side Diff: test/cctest/test-api.cc

Issue 6246055: Do proper security checks when accessing elements with getOwnPropertyDescriptor. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Addressing Mads' comment Created 9 years, 10 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 | « src/runtime.cc ('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
1 // Copyright 2007-2009 the V8 project authors. All rights reserved. 1 // Copyright 2007-2009 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 5305 matching lines...) Expand 10 before | Expand all | Expand 10 after
5316 Local<Value> data) { 5316 Local<Value> data) {
5317 return Context::GetCurrent()->Global()->Equals(global) || 5317 return Context::GetCurrent()->Global()->Equals(global) ||
5318 allowed_access_type[type]; 5318 allowed_access_type[type];
5319 } 5319 }
5320 5320
5321 5321
5322 static bool IndexedAccessBlocker(Local<v8::Object> global, 5322 static bool IndexedAccessBlocker(Local<v8::Object> global,
5323 uint32_t key, 5323 uint32_t key,
5324 v8::AccessType type, 5324 v8::AccessType type,
5325 Local<Value> data) { 5325 Local<Value> data) {
5326 return Context::GetCurrent()->Global()->Equals(global); 5326 return Context::GetCurrent()->Global()->Equals(global) ||
5327 allowed_access_type[type];
5327 } 5328 }
5328 5329
5329 5330
5330 static int g_echo_value = -1; 5331 static int g_echo_value = -1;
5331 static v8::Handle<Value> EchoGetter(Local<String> name, 5332 static v8::Handle<Value> EchoGetter(Local<String> name,
5332 const AccessorInfo& info) { 5333 const AccessorInfo& info) {
5333 return v8_num(g_echo_value); 5334 return v8_num(g_echo_value);
5334 } 5335 }
5335 5336
5336 5337
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
5383 5384
5384 // Define a property with JS getter and setter. 5385 // Define a property with JS getter and setter.
5385 CompileRun( 5386 CompileRun(
5386 "function getter() { return 'getter'; };\n" 5387 "function getter() { return 'getter'; };\n"
5387 "function setter() { return 'setter'; }\n" 5388 "function setter() { return 'setter'; }\n"
5388 "Object.defineProperty(this, 'js_accessor_p', {get:getter, set:setter})"); 5389 "Object.defineProperty(this, 'js_accessor_p', {get:getter, set:setter})");
5389 5390
5390 Local<Value> getter = global0->Get(v8_str("getter")); 5391 Local<Value> getter = global0->Get(v8_str("getter"));
5391 Local<Value> setter = global0->Get(v8_str("setter")); 5392 Local<Value> setter = global0->Get(v8_str("setter"));
5392 5393
5394 // And define normal element.
5395 global0->Set(239, v8_str("239"));
5396
5397 // Define an element with JS getter and setter.
5398 CompileRun(
5399 "function el_getter() { return 'el_getter'; };\n"
5400 "function el_setter() { return 'el_setter'; };\n"
5401 "Object.defineProperty(this, '42', {get: el_getter, set: el_setter});");
5402
5403 Local<Value> el_getter = global0->Get(v8_str("el_getter"));
5404 Local<Value> el_setter = global0->Get(v8_str("el_setter"));
5405
5393 v8::HandleScope scope1; 5406 v8::HandleScope scope1;
5394 5407
5395 v8::Persistent<Context> context1 = Context::New(); 5408 v8::Persistent<Context> context1 = Context::New();
5396 context1->Enter(); 5409 context1->Enter();
5397 5410
5398 v8::Handle<v8::Object> global1 = context1->Global(); 5411 v8::Handle<v8::Object> global1 = context1->Global();
5399 global1->Set(v8_str("other"), global0); 5412 global1->Set(v8_str("other"), global0);
5400 5413
5401 // Access blocked property 5414 // Access blocked property.
5402 CompileRun("other.blocked_prop = 1"); 5415 CompileRun("other.blocked_prop = 1");
5403 5416
5404 ExpectUndefined("other.blocked_prop"); 5417 ExpectUndefined("other.blocked_prop");
5405 ExpectUndefined( 5418 ExpectUndefined(
5406 "Object.getOwnPropertyDescriptor(other, 'blocked_prop')"); 5419 "Object.getOwnPropertyDescriptor(other, 'blocked_prop')");
5407 ExpectFalse("propertyIsEnumerable.call(other, 'blocked_prop')"); 5420 ExpectFalse("propertyIsEnumerable.call(other, 'blocked_prop')");
5408 5421
5409 // Enable ACCESS_HAS 5422 // Enable ACCESS_HAS
5410 allowed_access_type[v8::ACCESS_HAS] = true; 5423 allowed_access_type[v8::ACCESS_HAS] = true;
5411 ExpectUndefined("other.blocked_prop"); 5424 ExpectUndefined("other.blocked_prop");
5412 // ... and now we can get the descriptor... 5425 // ... and now we can get the descriptor...
5413 ExpectUndefined( 5426 ExpectUndefined(
5414 "Object.getOwnPropertyDescriptor(other, 'blocked_prop').value"); 5427 "Object.getOwnPropertyDescriptor(other, 'blocked_prop').value");
5415 // ... and enumerate the property. 5428 // ... and enumerate the property.
5416 ExpectTrue("propertyIsEnumerable.call(other, 'blocked_prop')"); 5429 ExpectTrue("propertyIsEnumerable.call(other, 'blocked_prop')");
5417 allowed_access_type[v8::ACCESS_HAS] = false; 5430 allowed_access_type[v8::ACCESS_HAS] = false;
5418 5431
5432 // Access blocked element.
5433 CompileRun("other[239] = 1");
5434
5435 ExpectUndefined("other[239]");
5436 ExpectUndefined("Object.getOwnPropertyDescriptor(other, '239')");
5437 ExpectFalse("propertyIsEnumerable.call(other, '239')");
5438
5439 // Enable ACCESS_HAS
5440 allowed_access_type[v8::ACCESS_HAS] = true;
5441 ExpectUndefined("other[239]");
5442 // ... and now we can get the descriptor...
5443 ExpectUndefined("Object.getOwnPropertyDescriptor(other, '239').value");
5444 // ... and enumerate the property.
5445 ExpectTrue("propertyIsEnumerable.call(other, '239')");
5446 allowed_access_type[v8::ACCESS_HAS] = false;
5447
5448 // Access a property with JS accessor.
5419 CompileRun("other.js_accessor_p = 2"); 5449 CompileRun("other.js_accessor_p = 2");
5420 5450
5421 ExpectUndefined("other.js_accessor_p"); 5451 ExpectUndefined("other.js_accessor_p");
5422 ExpectUndefined( 5452 ExpectUndefined(
5423 "Object.getOwnPropertyDescriptor(other, 'js_accessor_p')"); 5453 "Object.getOwnPropertyDescriptor(other, 'js_accessor_p')");
5424 5454
5425 // Enable ACCESS_HAS. 5455 // Enable ACCESS_HAS.
5426 allowed_access_type[v8::ACCESS_HAS] = true; 5456 allowed_access_type[v8::ACCESS_HAS] = true;
5427 ExpectUndefined("other.js_accessor_p"); 5457 ExpectUndefined("other.js_accessor_p");
5428 ExpectUndefined( 5458 ExpectUndefined(
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
5473 "Object.getOwnPropertyDescriptor(other, 'js_accessor_p').get", getter); 5503 "Object.getOwnPropertyDescriptor(other, 'js_accessor_p').get", getter);
5474 ExpectObject( 5504 ExpectObject(
5475 "Object.getOwnPropertyDescriptor(other, 'js_accessor_p').set", setter); 5505 "Object.getOwnPropertyDescriptor(other, 'js_accessor_p').set", setter);
5476 ExpectUndefined( 5506 ExpectUndefined(
5477 "Object.getOwnPropertyDescriptor(other, 'js_accessor_p').value"); 5507 "Object.getOwnPropertyDescriptor(other, 'js_accessor_p').value");
5478 5508
5479 allowed_access_type[v8::ACCESS_SET] = false; 5509 allowed_access_type[v8::ACCESS_SET] = false;
5480 allowed_access_type[v8::ACCESS_GET] = false; 5510 allowed_access_type[v8::ACCESS_GET] = false;
5481 allowed_access_type[v8::ACCESS_HAS] = false; 5511 allowed_access_type[v8::ACCESS_HAS] = false;
5482 5512
5513 // Access an element with JS accessor.
5514 CompileRun("other[42] = 2");
5515
5516 ExpectUndefined("other[42]");
5517 ExpectUndefined("Object.getOwnPropertyDescriptor(other, '42')");
5518
5519 // Enable ACCESS_HAS.
5520 allowed_access_type[v8::ACCESS_HAS] = true;
5521 ExpectUndefined("other[42]");
5522 ExpectUndefined("Object.getOwnPropertyDescriptor(other, '42').get");
5523 ExpectUndefined("Object.getOwnPropertyDescriptor(other, '42').set");
5524 ExpectUndefined("Object.getOwnPropertyDescriptor(other, '42').value");
5525 allowed_access_type[v8::ACCESS_HAS] = false;
5526
5527 // Enable both ACCESS_HAS and ACCESS_GET.
5528 allowed_access_type[v8::ACCESS_HAS] = true;
5529 allowed_access_type[v8::ACCESS_GET] = true;
5530
5531 ExpectString("other[42]", "el_getter");
5532 ExpectObject("Object.getOwnPropertyDescriptor(other, '42').get", el_getter);
5533 ExpectUndefined("Object.getOwnPropertyDescriptor(other, '42').set");
5534 ExpectUndefined("Object.getOwnPropertyDescriptor(other, '42').value");
5535
5536 allowed_access_type[v8::ACCESS_GET] = false;
5537 allowed_access_type[v8::ACCESS_HAS] = false;
5538
5539 // Enable both ACCESS_HAS and ACCESS_SET.
5540 allowed_access_type[v8::ACCESS_HAS] = true;
5541 allowed_access_type[v8::ACCESS_SET] = true;
5542
5543 ExpectUndefined("other[42]");
5544 ExpectUndefined("Object.getOwnPropertyDescriptor(other, '42').get");
5545 ExpectObject("Object.getOwnPropertyDescriptor(other, '42').set", el_setter);
5546 ExpectUndefined("Object.getOwnPropertyDescriptor(other, '42').value");
5547
5548 allowed_access_type[v8::ACCESS_SET] = false;
5549 allowed_access_type[v8::ACCESS_HAS] = false;
5550
5551 // Enable both ACCESS_HAS, ACCESS_GET and ACCESS_SET.
5552 allowed_access_type[v8::ACCESS_HAS] = true;
5553 allowed_access_type[v8::ACCESS_GET] = true;
5554 allowed_access_type[v8::ACCESS_SET] = true;
5555
5556 ExpectString("other[42]", "el_getter");
5557 ExpectObject("Object.getOwnPropertyDescriptor(other, '42').get", el_getter);
5558 ExpectObject("Object.getOwnPropertyDescriptor(other, '42').set", el_setter);
5559 ExpectUndefined("Object.getOwnPropertyDescriptor(other, '42').value");
5560
5561 allowed_access_type[v8::ACCESS_SET] = false;
5562 allowed_access_type[v8::ACCESS_GET] = false;
5563 allowed_access_type[v8::ACCESS_HAS] = false;
5564
5483 v8::Handle<Value> value; 5565 v8::Handle<Value> value;
5484 5566
5485 // Access accessible property 5567 // Access accessible property
5486 value = CompileRun("other.accessible_prop = 3"); 5568 value = CompileRun("other.accessible_prop = 3");
5487 CHECK(value->IsNumber()); 5569 CHECK(value->IsNumber());
5488 CHECK_EQ(3, value->Int32Value()); 5570 CHECK_EQ(3, value->Int32Value());
5489 CHECK_EQ(3, g_echo_value); 5571 CHECK_EQ(3, g_echo_value);
5490 5572
5491 value = CompileRun("other.accessible_prop"); 5573 value = CompileRun("other.accessible_prop");
5492 CHECK(value->IsNumber()); 5574 CHECK(value->IsNumber());
(...skipping 6912 matching lines...) Expand 10 before | Expand all | Expand 10 after
12405 v8::Context::Scope context_scope(context.local()); 12487 v8::Context::Scope context_scope(context.local());
12406 12488
12407 v8::Handle<v8::ObjectTemplate> tmpl = v8::ObjectTemplate::New(); 12489 v8::Handle<v8::ObjectTemplate> tmpl = v8::ObjectTemplate::New();
12408 tmpl->SetNamedPropertyHandler(Getter, NULL, NULL, NULL, Enumerator); 12490 tmpl->SetNamedPropertyHandler(Getter, NULL, NULL, NULL, Enumerator);
12409 context->Global()->Set(v8_str("o"), tmpl->NewInstance()); 12491 context->Global()->Set(v8_str("o"), tmpl->NewInstance());
12410 v8::Handle<v8::Array> result = v8::Handle<v8::Array>::Cast(CompileRun( 12492 v8::Handle<v8::Array> result = v8::Handle<v8::Array>::Cast(CompileRun(
12411 "var result = []; for (var k in o) result.push(k); result")); 12493 "var result = []; for (var k in o) result.push(k); result"));
12412 CHECK_EQ(1, result->Length()); 12494 CHECK_EQ(1, result->Length());
12413 CHECK_EQ(v8_str("universalAnswer"), result->Get(0)); 12495 CHECK_EQ(v8_str("universalAnswer"), result->Get(0));
12414 } 12496 }
OLDNEW
« no previous file with comments | « src/runtime.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698