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 5286 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5297 // the global object for env2 which has the same security token as env1. | 5297 // the global object for env2 which has the same security token as env1. |
5298 result = CompileRun("other.p"); | 5298 result = CompileRun("other.p"); |
5299 CHECK(result->IsInt32()); | 5299 CHECK(result->IsInt32()); |
5300 CHECK_EQ(42, result->Int32Value()); | 5300 CHECK_EQ(42, result->Int32Value()); |
5301 | 5301 |
5302 env2.Dispose(); | 5302 env2.Dispose(); |
5303 env3.Dispose(); | 5303 env3.Dispose(); |
5304 } | 5304 } |
5305 | 5305 |
5306 | 5306 |
| 5307 static bool allowed_access_type[v8::ACCESS_KEYS] = { false }; |
5307 static bool NamedAccessBlocker(Local<v8::Object> global, | 5308 static bool NamedAccessBlocker(Local<v8::Object> global, |
5308 Local<Value> name, | 5309 Local<Value> name, |
5309 v8::AccessType type, | 5310 v8::AccessType type, |
5310 Local<Value> data) { | 5311 Local<Value> data) { |
5311 return Context::GetCurrent()->Global()->Equals(global); | 5312 return Context::GetCurrent()->Global()->Equals(global) || |
| 5313 allowed_access_type[type]; |
5312 } | 5314 } |
5313 | 5315 |
5314 | 5316 |
5315 static bool IndexedAccessBlocker(Local<v8::Object> global, | 5317 static bool IndexedAccessBlocker(Local<v8::Object> global, |
5316 uint32_t key, | 5318 uint32_t key, |
5317 v8::AccessType type, | 5319 v8::AccessType type, |
5318 Local<Value> data) { | 5320 Local<Value> data) { |
5319 return Context::GetCurrent()->Global()->Equals(global); | 5321 return Context::GetCurrent()->Global()->Equals(global); |
5320 } | 5322 } |
5321 | 5323 |
(...skipping 19 matching lines...) Expand all Loading... |
5341 return v8::Undefined(); | 5343 return v8::Undefined(); |
5342 } | 5344 } |
5343 | 5345 |
5344 | 5346 |
5345 static void UnreachableSetter(Local<String>, Local<Value>, | 5347 static void UnreachableSetter(Local<String>, Local<Value>, |
5346 const AccessorInfo&) { | 5348 const AccessorInfo&) { |
5347 CHECK(false); // This function should nto be called. | 5349 CHECK(false); // This function should nto be called. |
5348 } | 5350 } |
5349 | 5351 |
5350 | 5352 |
5351 THREADED_TEST(AccessControl) { | 5353 TEST(AccessControl) { |
5352 v8::HandleScope handle_scope; | 5354 v8::HandleScope handle_scope; |
5353 v8::Handle<v8::ObjectTemplate> global_template = v8::ObjectTemplate::New(); | 5355 v8::Handle<v8::ObjectTemplate> global_template = v8::ObjectTemplate::New(); |
5354 | 5356 |
5355 global_template->SetAccessCheckCallbacks(NamedAccessBlocker, | 5357 global_template->SetAccessCheckCallbacks(NamedAccessBlocker, |
5356 IndexedAccessBlocker); | 5358 IndexedAccessBlocker); |
5357 | 5359 |
5358 // Add an accessor accessible by cross-domain JS code. | 5360 // Add an accessor accessible by cross-domain JS code. |
5359 global_template->SetAccessor( | 5361 global_template->SetAccessor( |
5360 v8_str("accessible_prop"), | 5362 v8_str("accessible_prop"), |
5361 EchoGetter, EchoSetter, | 5363 EchoGetter, EchoSetter, |
5362 v8::Handle<Value>(), | 5364 v8::Handle<Value>(), |
5363 v8::AccessControl(v8::ALL_CAN_READ | v8::ALL_CAN_WRITE)); | 5365 v8::AccessControl(v8::ALL_CAN_READ | v8::ALL_CAN_WRITE)); |
5364 | 5366 |
5365 // Add an accessor that is not accessible by cross-domain JS code. | 5367 // Add an accessor that is not accessible by cross-domain JS code. |
5366 global_template->SetAccessor(v8_str("blocked_prop"), | 5368 global_template->SetAccessor(v8_str("blocked_prop"), |
5367 UnreachableGetter, UnreachableSetter, | 5369 UnreachableGetter, UnreachableSetter, |
5368 v8::Handle<Value>(), | 5370 v8::Handle<Value>(), |
5369 v8::DEFAULT); | 5371 v8::DEFAULT); |
5370 | 5372 |
5371 // Create an environment | 5373 // Create an environment |
5372 v8::Persistent<Context> context0 = Context::New(NULL, global_template); | 5374 v8::Persistent<Context> context0 = Context::New(NULL, global_template); |
5373 context0->Enter(); | 5375 context0->Enter(); |
5374 | 5376 |
5375 v8::Handle<v8::Object> global0 = context0->Global(); | 5377 v8::Handle<v8::Object> global0 = context0->Global(); |
5376 | 5378 |
| 5379 // Define a property with JS getter and setter. |
| 5380 CompileRun( |
| 5381 "function getter() { return 'getter'; };\n" |
| 5382 "function setter() { return 'setter'; }\n" |
| 5383 "Object.defineProperty(this, 'js_accessor_p', {get:getter, set:setter})"); |
| 5384 |
| 5385 Local<Value> getter = global0->Get(v8_str("getter")); |
| 5386 Local<Value> setter = global0->Get(v8_str("setter")); |
| 5387 |
5377 v8::HandleScope scope1; | 5388 v8::HandleScope scope1; |
5378 | 5389 |
5379 v8::Persistent<Context> context1 = Context::New(); | 5390 v8::Persistent<Context> context1 = Context::New(); |
5380 context1->Enter(); | 5391 context1->Enter(); |
5381 | 5392 |
5382 v8::Handle<v8::Object> global1 = context1->Global(); | 5393 v8::Handle<v8::Object> global1 = context1->Global(); |
5383 global1->Set(v8_str("other"), global0); | 5394 global1->Set(v8_str("other"), global0); |
5384 | 5395 |
| 5396 // Access blocked property |
| 5397 CompileRun("other.blocked_prop = 1"); |
| 5398 |
| 5399 ExpectUndefined("other.blocked_prop"); |
| 5400 ExpectUndefined( |
| 5401 "Object.getOwnPropertyDescriptor(other, 'blocked_prop')"); |
| 5402 ExpectFalse("propertyIsEnumerable.call(other, 'blocked_prop')"); |
| 5403 |
| 5404 // Enable ACCESS_HAS |
| 5405 allowed_access_type[v8::ACCESS_HAS] = true; |
| 5406 ExpectUndefined("other.blocked_prop"); |
| 5407 // ... and now we can get the descriptor... |
| 5408 ExpectUndefined( |
| 5409 "Object.getOwnPropertyDescriptor(other, 'blocked_prop').value"); |
| 5410 // ... and enumerate the property. |
| 5411 ExpectTrue("propertyIsEnumerable.call(other, 'blocked_prop')"); |
| 5412 allowed_access_type[v8::ACCESS_HAS] = false; |
| 5413 |
| 5414 CompileRun("other.js_accessor_p = 2"); |
| 5415 |
| 5416 ExpectUndefined("other.js_accessor_p"); |
| 5417 ExpectUndefined( |
| 5418 "Object.getOwnPropertyDescriptor(other, 'js_accessor_p')"); |
| 5419 |
| 5420 // Enable ACCESS_HAS. |
| 5421 allowed_access_type[v8::ACCESS_HAS] = true; |
| 5422 ExpectUndefined("other.js_accessor_p"); |
| 5423 ExpectUndefined( |
| 5424 "Object.getOwnPropertyDescriptor(other, 'js_accessor_p').get"); |
| 5425 ExpectUndefined( |
| 5426 "Object.getOwnPropertyDescriptor(other, 'js_accessor_p').set"); |
| 5427 ExpectUndefined( |
| 5428 "Object.getOwnPropertyDescriptor(other, 'js_accessor_p').value"); |
| 5429 allowed_access_type[v8::ACCESS_HAS] = false; |
| 5430 |
| 5431 // Enable both ACCESS_HAS and ACCESS_GET. |
| 5432 allowed_access_type[v8::ACCESS_HAS] = true; |
| 5433 allowed_access_type[v8::ACCESS_GET] = true; |
| 5434 |
| 5435 ExpectString("other.js_accessor_p", "getter"); |
| 5436 ExpectObject( |
| 5437 "Object.getOwnPropertyDescriptor(other, 'js_accessor_p').get", getter); |
| 5438 ExpectUndefined( |
| 5439 "Object.getOwnPropertyDescriptor(other, 'js_accessor_p').set"); |
| 5440 ExpectUndefined( |
| 5441 "Object.getOwnPropertyDescriptor(other, 'js_accessor_p').value"); |
| 5442 |
| 5443 allowed_access_type[v8::ACCESS_GET] = false; |
| 5444 allowed_access_type[v8::ACCESS_HAS] = false; |
| 5445 |
| 5446 // Enable both ACCESS_HAS and ACCESS_SET. |
| 5447 allowed_access_type[v8::ACCESS_HAS] = true; |
| 5448 allowed_access_type[v8::ACCESS_SET] = true; |
| 5449 |
| 5450 ExpectUndefined("other.js_accessor_p"); |
| 5451 ExpectUndefined( |
| 5452 "Object.getOwnPropertyDescriptor(other, 'js_accessor_p').get"); |
| 5453 ExpectObject( |
| 5454 "Object.getOwnPropertyDescriptor(other, 'js_accessor_p').set", setter); |
| 5455 ExpectUndefined( |
| 5456 "Object.getOwnPropertyDescriptor(other, 'js_accessor_p').value"); |
| 5457 |
| 5458 allowed_access_type[v8::ACCESS_SET] = false; |
| 5459 allowed_access_type[v8::ACCESS_HAS] = false; |
| 5460 |
| 5461 // Enable both ACCESS_HAS, ACCESS_GET and ACCESS_SET. |
| 5462 allowed_access_type[v8::ACCESS_HAS] = true; |
| 5463 allowed_access_type[v8::ACCESS_GET] = true; |
| 5464 allowed_access_type[v8::ACCESS_SET] = true; |
| 5465 |
| 5466 ExpectString("other.js_accessor_p", "getter"); |
| 5467 ExpectObject( |
| 5468 "Object.getOwnPropertyDescriptor(other, 'js_accessor_p').get", getter); |
| 5469 ExpectObject( |
| 5470 "Object.getOwnPropertyDescriptor(other, 'js_accessor_p').set", setter); |
| 5471 ExpectUndefined( |
| 5472 "Object.getOwnPropertyDescriptor(other, 'js_accessor_p').value"); |
| 5473 |
| 5474 allowed_access_type[v8::ACCESS_SET] = false; |
| 5475 allowed_access_type[v8::ACCESS_GET] = false; |
| 5476 allowed_access_type[v8::ACCESS_HAS] = false; |
| 5477 |
5385 v8::Handle<Value> value; | 5478 v8::Handle<Value> value; |
5386 | 5479 |
5387 // Access blocked property | |
5388 value = CompileRun("other.blocked_prop = 1"); | |
5389 value = CompileRun("other.blocked_prop"); | |
5390 CHECK(value->IsUndefined()); | |
5391 | |
5392 value = CompileRun( | |
5393 "Object.getOwnPropertyDescriptor(other, 'blocked_prop').value"); | |
5394 CHECK(value->IsUndefined()); | |
5395 | |
5396 value = CompileRun("propertyIsEnumerable.call(other, 'blocked_prop')"); | |
5397 CHECK(value->IsFalse()); | |
5398 | |
5399 // Access accessible property | 5480 // Access accessible property |
5400 value = CompileRun("other.accessible_prop = 3"); | 5481 value = CompileRun("other.accessible_prop = 3"); |
5401 CHECK(value->IsNumber()); | 5482 CHECK(value->IsNumber()); |
5402 CHECK_EQ(3, value->Int32Value()); | 5483 CHECK_EQ(3, value->Int32Value()); |
5403 CHECK_EQ(3, g_echo_value); | 5484 CHECK_EQ(3, g_echo_value); |
5404 | 5485 |
5405 value = CompileRun("other.accessible_prop"); | 5486 value = CompileRun("other.accessible_prop"); |
5406 CHECK(value->IsNumber()); | 5487 CHECK(value->IsNumber()); |
5407 CHECK_EQ(3, value->Int32Value()); | 5488 CHECK_EQ(3, value->Int32Value()); |
5408 | 5489 |
(...skipping 6842 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
12251 v8::Context::Scope context_scope(context.local()); | 12332 v8::Context::Scope context_scope(context.local()); |
12252 | 12333 |
12253 v8::Handle<v8::ObjectTemplate> tmpl = v8::ObjectTemplate::New(); | 12334 v8::Handle<v8::ObjectTemplate> tmpl = v8::ObjectTemplate::New(); |
12254 tmpl->SetNamedPropertyHandler(Getter, NULL, NULL, NULL, Enumerator); | 12335 tmpl->SetNamedPropertyHandler(Getter, NULL, NULL, NULL, Enumerator); |
12255 context->Global()->Set(v8_str("o"), tmpl->NewInstance()); | 12336 context->Global()->Set(v8_str("o"), tmpl->NewInstance()); |
12256 v8::Handle<v8::Array> result = v8::Handle<v8::Array>::Cast(CompileRun( | 12337 v8::Handle<v8::Array> result = v8::Handle<v8::Array>::Cast(CompileRun( |
12257 "var result = []; for (var k in o) result.push(k); result")); | 12338 "var result = []; for (var k in o) result.push(k); result")); |
12258 CHECK_EQ(1, result->Length()); | 12339 CHECK_EQ(1, result->Length()); |
12259 CHECK_EQ(v8_str("universalAnswer"), result->Get(0)); | 12340 CHECK_EQ(v8_str("universalAnswer"), result->Get(0)); |
12260 } | 12341 } |
OLD | NEW |