OLD | NEW |
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 5291 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5302 // the global object for env2 which has the same security token as env1. | 5302 // the global object for env2 which has the same security token as env1. |
5303 result = CompileRun("other.p"); | 5303 result = CompileRun("other.p"); |
5304 CHECK(result->IsInt32()); | 5304 CHECK(result->IsInt32()); |
5305 CHECK_EQ(42, result->Int32Value()); | 5305 CHECK_EQ(42, result->Int32Value()); |
5306 | 5306 |
5307 env2.Dispose(); | 5307 env2.Dispose(); |
5308 env3.Dispose(); | 5308 env3.Dispose(); |
5309 } | 5309 } |
5310 | 5310 |
5311 | 5311 |
| 5312 static bool allowed_access_type[v8::ACCESS_KEYS + 1] = { false }; |
5312 static bool NamedAccessBlocker(Local<v8::Object> global, | 5313 static bool NamedAccessBlocker(Local<v8::Object> global, |
5313 Local<Value> name, | 5314 Local<Value> name, |
5314 v8::AccessType type, | 5315 v8::AccessType type, |
5315 Local<Value> data) { | 5316 Local<Value> data) { |
5316 return Context::GetCurrent()->Global()->Equals(global); | 5317 return Context::GetCurrent()->Global()->Equals(global) || |
| 5318 allowed_access_type[type]; |
5317 } | 5319 } |
5318 | 5320 |
5319 | 5321 |
5320 static bool IndexedAccessBlocker(Local<v8::Object> global, | 5322 static bool IndexedAccessBlocker(Local<v8::Object> global, |
5321 uint32_t key, | 5323 uint32_t key, |
5322 v8::AccessType type, | 5324 v8::AccessType type, |
5323 Local<Value> data) { | 5325 Local<Value> data) { |
5324 return Context::GetCurrent()->Global()->Equals(global); | 5326 return Context::GetCurrent()->Global()->Equals(global) || |
| 5327 allowed_access_type[type]; |
5325 } | 5328 } |
5326 | 5329 |
5327 | 5330 |
5328 static int g_echo_value = -1; | 5331 static int g_echo_value = -1; |
5329 static v8::Handle<Value> EchoGetter(Local<String> name, | 5332 static v8::Handle<Value> EchoGetter(Local<String> name, |
5330 const AccessorInfo& info) { | 5333 const AccessorInfo& info) { |
5331 return v8_num(g_echo_value); | 5334 return v8_num(g_echo_value); |
5332 } | 5335 } |
5333 | 5336 |
5334 | 5337 |
(...skipping 11 matching lines...) Expand all Loading... |
5346 return v8::Undefined(); | 5349 return v8::Undefined(); |
5347 } | 5350 } |
5348 | 5351 |
5349 | 5352 |
5350 static void UnreachableSetter(Local<String>, Local<Value>, | 5353 static void UnreachableSetter(Local<String>, Local<Value>, |
5351 const AccessorInfo&) { | 5354 const AccessorInfo&) { |
5352 CHECK(false); // This function should nto be called. | 5355 CHECK(false); // This function should nto be called. |
5353 } | 5356 } |
5354 | 5357 |
5355 | 5358 |
5356 THREADED_TEST(AccessControl) { | 5359 TEST(AccessControl) { |
5357 v8::HandleScope handle_scope; | 5360 v8::HandleScope handle_scope; |
5358 v8::Handle<v8::ObjectTemplate> global_template = v8::ObjectTemplate::New(); | 5361 v8::Handle<v8::ObjectTemplate> global_template = v8::ObjectTemplate::New(); |
5359 | 5362 |
5360 global_template->SetAccessCheckCallbacks(NamedAccessBlocker, | 5363 global_template->SetAccessCheckCallbacks(NamedAccessBlocker, |
5361 IndexedAccessBlocker); | 5364 IndexedAccessBlocker); |
5362 | 5365 |
5363 // Add an accessor accessible by cross-domain JS code. | 5366 // Add an accessor accessible by cross-domain JS code. |
5364 global_template->SetAccessor( | 5367 global_template->SetAccessor( |
5365 v8_str("accessible_prop"), | 5368 v8_str("accessible_prop"), |
5366 EchoGetter, EchoSetter, | 5369 EchoGetter, EchoSetter, |
5367 v8::Handle<Value>(), | 5370 v8::Handle<Value>(), |
5368 v8::AccessControl(v8::ALL_CAN_READ | v8::ALL_CAN_WRITE)); | 5371 v8::AccessControl(v8::ALL_CAN_READ | v8::ALL_CAN_WRITE)); |
5369 | 5372 |
5370 // Add an accessor that is not accessible by cross-domain JS code. | 5373 // Add an accessor that is not accessible by cross-domain JS code. |
5371 global_template->SetAccessor(v8_str("blocked_prop"), | 5374 global_template->SetAccessor(v8_str("blocked_prop"), |
5372 UnreachableGetter, UnreachableSetter, | 5375 UnreachableGetter, UnreachableSetter, |
5373 v8::Handle<Value>(), | 5376 v8::Handle<Value>(), |
5374 v8::DEFAULT); | 5377 v8::DEFAULT); |
5375 | 5378 |
5376 // Create an environment | 5379 // Create an environment |
5377 v8::Persistent<Context> context0 = Context::New(NULL, global_template); | 5380 v8::Persistent<Context> context0 = Context::New(NULL, global_template); |
5378 context0->Enter(); | 5381 context0->Enter(); |
5379 | 5382 |
5380 v8::Handle<v8::Object> global0 = context0->Global(); | 5383 v8::Handle<v8::Object> global0 = context0->Global(); |
5381 | 5384 |
| 5385 // Define a property with JS getter and setter. |
| 5386 CompileRun( |
| 5387 "function getter() { return 'getter'; };\n" |
| 5388 "function setter() { return 'setter'; }\n" |
| 5389 "Object.defineProperty(this, 'js_accessor_p', {get:getter, set:setter})"); |
| 5390 |
| 5391 Local<Value> getter = global0->Get(v8_str("getter")); |
| 5392 Local<Value> setter = global0->Get(v8_str("setter")); |
| 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 |
5382 v8::HandleScope scope1; | 5406 v8::HandleScope scope1; |
5383 | 5407 |
5384 v8::Persistent<Context> context1 = Context::New(); | 5408 v8::Persistent<Context> context1 = Context::New(); |
5385 context1->Enter(); | 5409 context1->Enter(); |
5386 | 5410 |
5387 v8::Handle<v8::Object> global1 = context1->Global(); | 5411 v8::Handle<v8::Object> global1 = context1->Global(); |
5388 global1->Set(v8_str("other"), global0); | 5412 global1->Set(v8_str("other"), global0); |
5389 | 5413 |
| 5414 // Access blocked property. |
| 5415 CompileRun("other.blocked_prop = 1"); |
| 5416 |
| 5417 ExpectUndefined("other.blocked_prop"); |
| 5418 ExpectUndefined( |
| 5419 "Object.getOwnPropertyDescriptor(other, 'blocked_prop')"); |
| 5420 ExpectFalse("propertyIsEnumerable.call(other, 'blocked_prop')"); |
| 5421 |
| 5422 // Enable ACCESS_HAS |
| 5423 allowed_access_type[v8::ACCESS_HAS] = true; |
| 5424 ExpectUndefined("other.blocked_prop"); |
| 5425 // ... and now we can get the descriptor... |
| 5426 ExpectUndefined( |
| 5427 "Object.getOwnPropertyDescriptor(other, 'blocked_prop').value"); |
| 5428 // ... and enumerate the property. |
| 5429 ExpectTrue("propertyIsEnumerable.call(other, 'blocked_prop')"); |
| 5430 allowed_access_type[v8::ACCESS_HAS] = false; |
| 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. |
| 5449 CompileRun("other.js_accessor_p = 2"); |
| 5450 |
| 5451 ExpectUndefined("other.js_accessor_p"); |
| 5452 ExpectUndefined( |
| 5453 "Object.getOwnPropertyDescriptor(other, 'js_accessor_p')"); |
| 5454 |
| 5455 // Enable ACCESS_HAS. |
| 5456 allowed_access_type[v8::ACCESS_HAS] = true; |
| 5457 ExpectUndefined("other.js_accessor_p"); |
| 5458 ExpectUndefined( |
| 5459 "Object.getOwnPropertyDescriptor(other, 'js_accessor_p').get"); |
| 5460 ExpectUndefined( |
| 5461 "Object.getOwnPropertyDescriptor(other, 'js_accessor_p').set"); |
| 5462 ExpectUndefined( |
| 5463 "Object.getOwnPropertyDescriptor(other, 'js_accessor_p').value"); |
| 5464 allowed_access_type[v8::ACCESS_HAS] = false; |
| 5465 |
| 5466 // Enable both ACCESS_HAS and ACCESS_GET. |
| 5467 allowed_access_type[v8::ACCESS_HAS] = true; |
| 5468 allowed_access_type[v8::ACCESS_GET] = true; |
| 5469 |
| 5470 ExpectString("other.js_accessor_p", "getter"); |
| 5471 ExpectObject( |
| 5472 "Object.getOwnPropertyDescriptor(other, 'js_accessor_p').get", getter); |
| 5473 ExpectUndefined( |
| 5474 "Object.getOwnPropertyDescriptor(other, 'js_accessor_p').set"); |
| 5475 ExpectUndefined( |
| 5476 "Object.getOwnPropertyDescriptor(other, 'js_accessor_p').value"); |
| 5477 |
| 5478 allowed_access_type[v8::ACCESS_GET] = false; |
| 5479 allowed_access_type[v8::ACCESS_HAS] = false; |
| 5480 |
| 5481 // Enable both ACCESS_HAS and ACCESS_SET. |
| 5482 allowed_access_type[v8::ACCESS_HAS] = true; |
| 5483 allowed_access_type[v8::ACCESS_SET] = true; |
| 5484 |
| 5485 ExpectUndefined("other.js_accessor_p"); |
| 5486 ExpectUndefined( |
| 5487 "Object.getOwnPropertyDescriptor(other, 'js_accessor_p').get"); |
| 5488 ExpectObject( |
| 5489 "Object.getOwnPropertyDescriptor(other, 'js_accessor_p').set", setter); |
| 5490 ExpectUndefined( |
| 5491 "Object.getOwnPropertyDescriptor(other, 'js_accessor_p').value"); |
| 5492 |
| 5493 allowed_access_type[v8::ACCESS_SET] = false; |
| 5494 allowed_access_type[v8::ACCESS_HAS] = false; |
| 5495 |
| 5496 // Enable both ACCESS_HAS, ACCESS_GET and ACCESS_SET. |
| 5497 allowed_access_type[v8::ACCESS_HAS] = true; |
| 5498 allowed_access_type[v8::ACCESS_GET] = true; |
| 5499 allowed_access_type[v8::ACCESS_SET] = true; |
| 5500 |
| 5501 ExpectString("other.js_accessor_p", "getter"); |
| 5502 ExpectObject( |
| 5503 "Object.getOwnPropertyDescriptor(other, 'js_accessor_p').get", getter); |
| 5504 ExpectObject( |
| 5505 "Object.getOwnPropertyDescriptor(other, 'js_accessor_p').set", setter); |
| 5506 ExpectUndefined( |
| 5507 "Object.getOwnPropertyDescriptor(other, 'js_accessor_p').value"); |
| 5508 |
| 5509 allowed_access_type[v8::ACCESS_SET] = false; |
| 5510 allowed_access_type[v8::ACCESS_GET] = false; |
| 5511 allowed_access_type[v8::ACCESS_HAS] = false; |
| 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 |
5390 v8::Handle<Value> value; | 5565 v8::Handle<Value> value; |
5391 | 5566 |
5392 // Access blocked property | |
5393 value = CompileRun("other.blocked_prop = 1"); | |
5394 value = CompileRun("other.blocked_prop"); | |
5395 CHECK(value->IsUndefined()); | |
5396 | |
5397 value = CompileRun( | |
5398 "Object.getOwnPropertyDescriptor(other, 'blocked_prop').value"); | |
5399 CHECK(value->IsUndefined()); | |
5400 | |
5401 value = CompileRun("propertyIsEnumerable.call(other, 'blocked_prop')"); | |
5402 CHECK(value->IsFalse()); | |
5403 | |
5404 // Access accessible property | 5567 // Access accessible property |
5405 value = CompileRun("other.accessible_prop = 3"); | 5568 value = CompileRun("other.accessible_prop = 3"); |
5406 CHECK(value->IsNumber()); | 5569 CHECK(value->IsNumber()); |
5407 CHECK_EQ(3, value->Int32Value()); | 5570 CHECK_EQ(3, value->Int32Value()); |
5408 CHECK_EQ(3, g_echo_value); | 5571 CHECK_EQ(3, g_echo_value); |
5409 | 5572 |
5410 value = CompileRun("other.accessible_prop"); | 5573 value = CompileRun("other.accessible_prop"); |
5411 CHECK(value->IsNumber()); | 5574 CHECK(value->IsNumber()); |
5412 CHECK_EQ(3, value->Int32Value()); | 5575 CHECK_EQ(3, value->Int32Value()); |
5413 | 5576 |
(...skipping 6823 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
12237 v8::Context::Scope context_scope(context.local()); | 12400 v8::Context::Scope context_scope(context.local()); |
12238 | 12401 |
12239 v8::Handle<v8::ObjectTemplate> tmpl = v8::ObjectTemplate::New(); | 12402 v8::Handle<v8::ObjectTemplate> tmpl = v8::ObjectTemplate::New(); |
12240 tmpl->SetNamedPropertyHandler(Getter, NULL, NULL, NULL, Enumerator); | 12403 tmpl->SetNamedPropertyHandler(Getter, NULL, NULL, NULL, Enumerator); |
12241 context->Global()->Set(v8_str("o"), tmpl->NewInstance()); | 12404 context->Global()->Set(v8_str("o"), tmpl->NewInstance()); |
12242 v8::Handle<v8::Array> result = v8::Handle<v8::Array>::Cast(CompileRun( | 12405 v8::Handle<v8::Array> result = v8::Handle<v8::Array>::Cast(CompileRun( |
12243 "var result = []; for (var k in o) result.push(k); result")); | 12406 "var result = []; for (var k in o) result.push(k); result")); |
12244 CHECK_EQ(1, result->Length()); | 12407 CHECK_EQ(1, result->Length()); |
12245 CHECK_EQ(v8_str("universalAnswer"), result->Get(0)); | 12408 CHECK_EQ(v8_str("universalAnswer"), result->Get(0)); |
12246 } | 12409 } |
OLD | NEW |